blob: 697efa30cfac9dde282dd49da1b1111698979011 [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);
glepnirbcd59952025-04-24 21:48:35 +02002497 clear_string_option(&buf->b_p_ise);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002498#ifdef FEAT_COMPL_FUNC
2499 clear_string_option(&buf->b_p_cfu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002500 free_callback(&buf->b_cfu_cb);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002501 clear_string_option(&buf->b_p_ofu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002502 free_callback(&buf->b_ofu_cb);
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +01002503 clear_string_option(&buf->b_p_tsrfu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002504 free_callback(&buf->b_tsrfu_cb);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002505#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506#ifdef FEAT_QUICKFIX
2507 clear_string_option(&buf->b_p_gp);
2508 clear_string_option(&buf->b_p_mp);
2509 clear_string_option(&buf->b_p_efm);
2510#endif
2511 clear_string_option(&buf->b_p_ep);
2512 clear_string_option(&buf->b_p_path);
2513 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002514 clear_string_option(&buf->b_p_tc);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002515#ifdef FEAT_EVAL
2516 clear_string_option(&buf->b_p_tfu);
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +00002517 free_callback(&buf->b_tfu_cb);
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002518 clear_string_option(&buf->b_p_ffu);
2519 free_callback(&buf->b_ffu_cb);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002520#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 clear_string_option(&buf->b_p_dict);
2522 clear_string_option(&buf->b_p_tsr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002523 clear_string_option(&buf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002525 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002526 clear_string_option(&buf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002527 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002528 clear_string_option(&buf->b_p_menc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529}
2530
2531/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002532 * Get alternate file "n".
2533 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2534 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 * if (options & GETF_SETMARK) call setpcmark()
2536 * if (options & GETF_ALT) we are jumping to an alternate file.
2537 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2538 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002539 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 */
2541 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002542buflist_getfile(
2543 int n,
2544 linenr_T lnum,
2545 int options,
2546 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547{
2548 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 pos_T *fpos;
2551 colnr_T col;
2552
2553 buf = buflist_findnr(n);
2554 if (buf == NULL)
2555 {
2556 if ((options & GETF_ALT) && n == 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02002557 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 else
Bram Moolenaare1242042021-12-16 20:56:57 +00002559 semsg(_(e_buffer_nr_not_found), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 return FAIL;
2561 }
2562
Bram Moolenaarc667da52019-11-30 20:52:27 +01002563 // if alternate file is the current buffer, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 if (buf == curbuf)
2565 return OK;
2566
Bram Moolenaar71223e22022-05-30 15:23:09 +01002567 if (text_or_buf_locked())
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002568 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569
Bram Moolenaarc667da52019-11-30 20:52:27 +01002570 // altfpos may be changed by getfile(), get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 if (lnum == 0)
2572 {
2573 fpos = buflist_findfpos(buf);
2574 lnum = fpos->lnum;
2575 col = fpos->col;
2576 }
2577 else
2578 col = 0;
2579
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 if (options & GETF_SWITCH)
2581 {
Yegappan Lakshmanane42c27d2023-05-14 17:24:22 +01002582 // If 'switchbuf' is set jump to the window containing "buf".
2583 wp = swbuf_goto_win_with_buf(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002584
Bram Moolenaarc667da52019-11-30 20:52:27 +01002585 // If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2586 // current buffer isn't empty: open new tab or window
Bram Moolenaara594d772015-06-19 14:41:49 +02002587 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002588 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002590 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002591 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002592 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2593 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002595 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 }
2597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598
2599 ++RedrawingDisabled;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002600 int retval = FAIL;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002601 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2602 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002604 // cursor is at to BOL and w_cursor.lnum is checked due to getfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 if (!p_sol && col != 0)
2606 {
2607 curwin->w_cursor.col = col;
2608 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 curwin->w_set_curswant = TRUE;
2611 }
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002612 retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 }
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002614
2615 if (RedrawingDisabled > 0)
2616 --RedrawingDisabled;
2617 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618}
2619
2620/*
2621 * go to the last know line number for the current buffer
2622 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002624buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625{
2626 pos_T *fpos;
2627
2628 fpos = buflist_findfpos(curbuf);
2629
2630 curwin->w_cursor.lnum = fpos->lnum;
2631 check_cursor_lnum();
2632
2633 if (p_sol)
2634 curwin->w_cursor.col = 0;
2635 else
2636 {
2637 curwin->w_cursor.col = fpos->col;
2638 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 curwin->w_set_curswant = TRUE;
2641 }
2642}
2643
Bram Moolenaar81695252004-12-29 20:58:21 +00002644/*
2645 * Find file in buffer list by name (it has to be for the current window).
2646 * Returns NULL if not found.
2647 */
2648 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002649buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002650{
2651 char_u *ffname;
2652 buf_T *buf = NULL;
2653
Bram Moolenaarc667da52019-11-30 20:52:27 +01002654 // First make the name into a full path name
Bram Moolenaar81695252004-12-29 20:58:21 +00002655 ffname = FullName_save(fname,
2656#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002657 TRUE // force expansion, get rid of symbolic links
Bram Moolenaar81695252004-12-29 20:58:21 +00002658#else
2659 FALSE
2660#endif
2661 );
2662 if (ffname != NULL)
2663 {
2664 buf = buflist_findname(ffname);
2665 vim_free(ffname);
2666 }
2667 return buf;
2668}
Bram Moolenaar81695252004-12-29 20:58:21 +00002669
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670/*
2671 * Find file in buffer list by name (it has to be for the current window).
2672 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002673 * Skips dummy buffers.
2674 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 */
2676 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002677buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678{
2679#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002680 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681
2682 if (mch_stat((char *)ffname, &st) < 0)
2683 st.st_dev = (dev_T)-1;
2684 return buflist_findname_stat(ffname, &st);
2685}
2686
2687/*
2688 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2689 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002690 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 */
2692 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002693buflist_findname_stat(
2694 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002695 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696{
2697#endif
2698 buf_T *buf;
2699
Bram Moolenaarc667da52019-11-30 20:52:27 +01002700 // Start at the last buffer, expect to find a match sooner.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002701 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaar81695252004-12-29 20:58:21 +00002702 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703#ifdef UNIX
2704 , stp
2705#endif
2706 ))
2707 return buf;
2708 return NULL;
2709}
2710
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711/*
2712 * Find file in buffer list by a regexp pattern.
2713 * Return fnum of the found buffer.
2714 * Return < 0 for error.
2715 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002717buflist_findpat(
2718 char_u *pattern,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002719 char_u *pattern_end, // pointer to first char after pattern
2720 int unlisted, // find unlisted buffers
2721 int diffmode UNUSED, // find diff-mode buffers only
2722 int curtab_only) // find buffers in current tab only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723{
2724 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 int match = -1;
2726 int find_listed;
2727 char_u *pat;
2728 char_u *patend;
2729 int attempt;
2730 char_u *p;
2731 int toggledollar;
2732
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002733 // "%" is current file, "%%" or "#" is alternate file
2734 if ((pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2735 || (in_vim9script() && pattern_end == pattern + 2
2736 && pattern[0] == '%' && pattern[1] == '%'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 {
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002738 if (*pattern == '#' || pattern_end == pattern + 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 match = curwin->w_alt_fnum;
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002740 else
2741 match = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742#ifdef FEAT_DIFF
2743 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2744 match = -1;
2745#endif
2746 }
2747
2748 /*
2749 * Try four ways of matching a listed buffer:
2750 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002751 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 * attempt == 2: with '$' at end (only match at end)
2753 * attempt == 3: with '^' at start and '$' at end (only full match)
2754 * Repeat this for finding an unlisted buffer if there was no matching
2755 * listed buffer.
2756 */
2757 else
2758 {
2759 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2760 if (pat == NULL)
2761 return -1;
2762 patend = pat + STRLEN(pat) - 1;
2763 toggledollar = (patend > pat && *patend == '$');
2764
Bram Moolenaarc667da52019-11-30 20:52:27 +01002765 // First try finding a listed buffer. If not found and "unlisted"
2766 // is TRUE, try finding an unlisted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 find_listed = TRUE;
2768 for (;;)
2769 {
2770 for (attempt = 0; attempt <= 3; ++attempt)
2771 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002772 regmatch_T regmatch;
2773
Bram Moolenaarc667da52019-11-30 20:52:27 +01002774 // may add '^' and '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 if (toggledollar)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002776 *patend = (attempt < 2) ? NUL : '$'; // add/remove '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 p = pat;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002778 if (*p == '^' && !(attempt & 1)) // add/remove '^'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 ++p;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01002780 regmatch.regprog = vim_regcomp(p, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002782 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002783 {
2784 if (regmatch.regprog == NULL)
2785 {
2786 // invalid pattern, possibly after switching engine
2787 vim_free(pat);
2788 return -1;
2789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 if (buf->b_p_bl == find_listed
2791#ifdef FEAT_DIFF
2792 && (!diffmode || diff_mode_buf(buf))
2793#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002794 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002796 if (curtab_only)
2797 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002798 // Ignore the match if the buffer is not open in
2799 // the current tab.
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002800 win_T *wp;
2801
Bram Moolenaar29323592016-07-24 22:04:11 +02002802 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002803 if (wp->w_buffer == buf)
2804 break;
2805 if (wp == NULL)
2806 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002807 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002808 if (match >= 0) // already found a match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 {
2810 match = -2;
2811 break;
2812 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002813 match = buf->b_fnum; // remember first match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 }
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002817 vim_regfree(regmatch.regprog);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002818 if (match >= 0) // found one match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 break;
2820 }
2821
Bram Moolenaarc667da52019-11-30 20:52:27 +01002822 // Only search for unlisted buffers if there was no match with
2823 // a listed buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 if (!unlisted || !find_listed || match != -1)
2825 break;
2826 find_listed = FALSE;
2827 }
2828
2829 vim_free(pat);
2830 }
2831
2832 if (match == -2)
Bram Moolenaare1242042021-12-16 20:56:57 +00002833 semsg(_(e_more_than_one_match_for_str), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 else if (match < 0)
Bram Moolenaare1242042021-12-16 20:56:57 +00002835 semsg(_(e_no_matching_buffer_for_str), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 return match;
2837}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838
Bram Moolenaar52410572019-10-27 05:12:45 +01002839#ifdef FEAT_VIMINFO
2840typedef struct {
2841 buf_T *buf;
2842 char_u *match;
2843} bufmatch_T;
2844#endif
2845
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846/*
2847 * Find all buffer names that match.
2848 * For command line expansion of ":buf" and ":sbuf".
2849 * Return OK if matches found, FAIL otherwise.
2850 */
2851 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002852ExpandBufnames(
2853 char_u *pat,
2854 int *num_file,
2855 char_u ***file,
2856 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857{
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002858 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 buf_T *buf;
2860 int round;
2861 char_u *p;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002862 char_u *patc = NULL;
Bram Moolenaar52410572019-10-27 05:12:45 +01002863#ifdef FEAT_VIMINFO
2864 bufmatch_T *matches = NULL;
2865#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002866 int fuzzy;
2867 fuzmatch_str_T *fuzmatch = NULL;
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002868 regmatch_T regmatch;
2869 int score = 0;
2870 int to_free = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871
Bram Moolenaarc667da52019-11-30 20:52:27 +01002872 *num_file = 0; // return values in case of FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 *file = NULL;
2874
Bram Moolenaarefcc3292019-12-30 21:59:03 +01002875#ifdef FEAT_DIFF
2876 if ((options & BUF_DIFF_FILTER) && !curwin->w_p_diff)
2877 return FAIL;
2878#endif
2879
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002880 fuzzy = cmdline_fuzzy_complete(pat);
2881
2882 // Make a copy of "pat" and change "^" to "\(^\|[\/]\)" (if doing regular
2883 // expression matching)
2884 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002886 if (*pat == '^' && pat[1] != NUL)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002887 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002888 int len = (int)STRLEN(pat);
2889 patc = alloc(len);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002890 if (patc == NULL)
2891 return FAIL;
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002892 STRNCPY(patc, pat + 1, len - 1);
2893 patc[len - 1] = NUL;
2894 to_free = TRUE;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002895 }
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002896 else if (*pat == '^')
2897 patc = (char_u *)"";
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002898 else
2899 patc = pat;
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002900 regmatch.regprog = vim_regcomp(patc, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002901 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00002902
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002903 // round == 1: Count the matches.
2904 // round == 2: Build the array to keep the matches.
2905 for (round = 1; round <= 2; ++round)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002906 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002907 count = 0;
2908 FOR_ALL_BUFFERS(buf)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002909 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002910 if (!buf->b_p_bl) // skip unlisted buffers
2911 continue;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002912#ifdef FEAT_DIFF
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002913 if (options & BUF_DIFF_FILTER)
2914 // Skip buffers not suitable for
2915 // :diffget or :diffput completion.
2916 if (buf == curbuf || !diff_mode_buf(buf))
2917 continue;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002918#endif
2919
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002920 if (!fuzzy)
2921 {
2922 if (regmatch.regprog == NULL)
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002923 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002924 // invalid pattern, possibly after recompiling
2925 if (to_free)
2926 vim_free(patc);
2927 return FAIL;
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002928 }
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002929 p = buflist_match(&regmatch, buf, p_wic);
2930 }
2931 else
2932 {
2933 p = NULL;
2934 // first try matching with the short file name
2935 if ((score = fuzzy_match_str(buf->b_sfname, pat)) != 0)
2936 p = buf->b_sfname;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002937 if (p == NULL)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002938 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002939 // next try matching with the full path file name
2940 if ((score = fuzzy_match_str(buf->b_ffname, pat)) != 0)
2941 p = buf->b_ffname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 }
2943 }
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002944
2945 if (p == NULL)
2946 continue;
2947
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 if (round == 1)
2949 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002950 ++count;
2951 continue;
2952 }
2953
2954 if (options & WILD_HOME_REPLACE)
2955 p = home_replace_save(buf, p);
2956 else
2957 p = vim_strsave(p);
John Marriott7fb90812025-04-02 20:32:35 +02002958 if (p == NULL)
2959 return FAIL;
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002960
2961 if (!fuzzy)
2962 {
Bram Moolenaar52410572019-10-27 05:12:45 +01002963#ifdef FEAT_VIMINFO
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002964 if (matches != NULL)
2965 {
2966 matches[count].buf = buf;
2967 matches[count].match = p;
2968 count++;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002969 }
2970 else
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002971#endif
2972 (*file)[count++] = p;
2973 }
2974 else
2975 {
2976 fuzmatch[count].idx = count;
2977 fuzmatch[count].str = p;
2978 fuzmatch[count].score = score;
2979 count++;
2980 }
2981 }
2982 if (count == 0) // no match found, break here
2983 break;
2984 if (round == 1)
2985 {
2986 if (!fuzzy)
2987 {
2988 *file = ALLOC_MULT(char_u *, count);
2989 if (*file == NULL)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002990 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002991 vim_regfree(regmatch.regprog);
2992 if (to_free)
2993 vim_free(patc);
2994 return FAIL;
2995 }
2996#ifdef FEAT_VIMINFO
2997 if (options & WILD_BUFLASTUSED)
2998 matches = ALLOC_MULT(bufmatch_T, count);
2999#endif
3000 }
3001 else
3002 {
3003 fuzmatch = ALLOC_MULT(fuzmatch_str_T, count);
3004 if (fuzmatch == NULL)
3005 {
3006 *num_file = 0;
3007 *file = NULL;
3008 return FAIL;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 }
3011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 }
3013
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01003014 if (!fuzzy)
3015 {
3016 vim_regfree(regmatch.regprog);
3017 if (to_free)
3018 vim_free(patc);
3019 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003020
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003021 if (!fuzzy)
Bram Moolenaar52410572019-10-27 05:12:45 +01003022 {
826814741_6dff3c9c2024-12-10 17:15:14 +01003023#ifdef FEAT_VIMINFO
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003024 if (matches != NULL)
Bram Moolenaar52410572019-10-27 05:12:45 +01003025 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003026 int i;
3027 if (count > 1)
3028 qsort(matches, count, sizeof(bufmatch_T), buf_compare);
3029 // if the current buffer is first in the list, place it at the end
3030 if (matches[0].buf == curbuf)
3031 {
3032 for (i = 1; i < count; i++)
3033 (*file)[i-1] = matches[i].match;
3034 (*file)[count-1] = matches[0].match;
3035 }
3036 else
3037 {
3038 for (i = 0; i < count; i++)
3039 (*file)[i] = matches[i].match;
3040 }
3041 vim_free(matches);
Bram Moolenaar52410572019-10-27 05:12:45 +01003042 }
826814741_6dff3c9c2024-12-10 17:15:14 +01003043#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003044 }
3045 else
3046 {
3047 if (fuzzymatches_to_strmatches(fuzmatch, file, count, FALSE) == FAIL)
3048 return FAIL;
Bram Moolenaar52410572019-10-27 05:12:45 +01003049 }
Bram Moolenaar52410572019-10-27 05:12:45 +01003050
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 *num_file = count;
3052 return (count == 0 ? FAIL : OK);
3053}
3054
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055/*
3056 * Check for a match on the file name for buffer "buf" with regprog "prog".
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003057 * Note that rmp->regprog may become NULL when switching regexp engine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 */
3059 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003060buflist_match(
3061 regmatch_T *rmp,
3062 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003063 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064{
3065 char_u *match;
3066
Bram Moolenaarc667da52019-11-30 20:52:27 +01003067 // First try the short file name, then the long file name.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01003068 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaara59f2df2022-05-11 11:42:28 +01003069 if (match == NULL && rmp->regprog != NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01003070 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071
3072 return match;
3073}
3074
3075/*
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003076 * Try matching the regexp in "rmp->regprog" with file name "name".
3077 * Note that rmp->regprog may become NULL when switching regexp engine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 * Return "name" when there is a match, NULL when not.
3079 */
3080 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003081fname_match(
3082 regmatch_T *rmp,
3083 char_u *name,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003084 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085{
3086 char_u *match = NULL;
3087 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003089 // extra check for valid arguments
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003090 if (name == NULL || rmp->regprog == NULL)
3091 return NULL;
3092
3093 // Ignore case when 'fileignorecase' or the argument is set.
3094 rmp->rm_ic = p_fic || ignore_case;
3095 if (vim_regexec(rmp, name, (colnr_T)0))
3096 match = name;
3097 else if (rmp->regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003099 // Replace $(HOME) with '~' and try matching again.
3100 p = home_replace_save(NULL, name);
3101 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 match = name;
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003103 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 }
3105
3106 return match;
3107}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108
3109/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02003110 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 */
3112 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003113buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114{
Bram Moolenaar480778b2016-07-14 22:09:39 +02003115 char_u key[VIM_SIZEOF_INT * 2 + 1];
3116 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117
3118 if (nr == 0)
3119 nr = curwin->w_alt_fnum;
John Marriottec032de2025-04-10 21:34:19 +02003120 vim_snprintf((char *)key, sizeof(key), "%x", nr);
Bram Moolenaar480778b2016-07-14 22:09:39 +02003121 hi = hash_find(&buf_hashtab, key);
3122
3123 if (!HASHITEM_EMPTY(hi))
3124 return (buf_T *)(hi->hi_key
3125 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 return NULL;
3127}
3128
3129/*
3130 * Get name of file 'n' in the buffer list.
3131 * When the file has no name an empty string is returned.
3132 * home_replace() is used to shorten the file name (used for marks).
3133 * Returns a pointer to allocated memory, of NULL when failed.
3134 */
3135 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003136buflist_nr2name(
3137 int n,
3138 int fullname,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003139 int helptail) // for help buffers return tail only
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140{
3141 buf_T *buf;
3142
3143 buf = buflist_findnr(n);
3144 if (buf == NULL)
3145 return NULL;
3146 return home_replace_save(helptail ? buf : NULL,
3147 fullname ? buf->b_ffname : buf->b_fname);
3148}
3149
3150/*
3151 * Set the "lnum" and "col" for the buffer "buf" and the current window.
3152 * When "copy_options" is TRUE save the local window option values.
3153 * When "lnum" is 0 only do the options.
3154 */
Bram Moolenaardefa0672019-07-21 19:25:37 +02003155 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003156buflist_setfpos(
3157 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003158 win_T *win, // may be NULL when using :badd
Bram Moolenaar7454a062016-01-30 15:14:10 +01003159 linenr_T lnum,
3160 colnr_T col,
3161 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162{
3163 wininfo_T *wip;
3164
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003165 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 if (wip->wi_win == win)
3167 break;
3168 if (wip == NULL)
3169 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003170 // allocate a new entry
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003171 wip = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 if (wip == NULL)
3173 return;
3174 wip->wi_win = win;
Bram Moolenaarc667da52019-11-30 20:52:27 +01003175 if (lnum == 0) // set lnum even when it's 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 lnum = 1;
3177 }
3178 else
3179 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003180 // remove the entry from the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 if (wip->wi_prev)
3182 wip->wi_prev->wi_next = wip->wi_next;
3183 else
3184 buf->b_wininfo = wip->wi_next;
3185 if (wip->wi_next)
3186 wip->wi_next->wi_prev = wip->wi_prev;
3187 if (copy_options && wip->wi_optset)
3188 {
3189 clear_winopt(&wip->wi_opt);
3190#ifdef FEAT_FOLDING
3191 deleteFoldRecurse(&wip->wi_folds);
3192#endif
3193 }
3194 }
3195 if (lnum != 0)
3196 {
3197 wip->wi_fpos.lnum = lnum;
3198 wip->wi_fpos.col = col;
3199 }
LemonBoydb0ea7f2022-04-10 17:59:26 +01003200 if (win != NULL)
3201 wip->wi_changelistidx = win->w_changelistidx;
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003202 if (copy_options && win != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003204 // Save the window-specific option values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
3206#ifdef FEAT_FOLDING
3207 wip->wi_fold_manual = win->w_fold_manual;
3208 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
3209#endif
3210 wip->wi_optset = TRUE;
3211 }
3212
Bram Moolenaarc667da52019-11-30 20:52:27 +01003213 // insert the entry in front of the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 wip->wi_next = buf->b_wininfo;
3215 buf->b_wininfo = wip;
3216 wip->wi_prev = NULL;
3217 if (wip->wi_next)
3218 wip->wi_next->wi_prev = wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219}
3220
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003221#ifdef FEAT_DIFF
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003222/*
3223 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
3224 * page. That's because a diff is local to a tab page.
3225 */
3226 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003227wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003228{
3229 win_T *wp;
3230
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003231 if (!wip->wi_opt.wo_diff)
3232 return FALSE;
3233
3234 FOR_ALL_WINDOWS(wp)
3235 // return FALSE when it's a window in the current tab page, thus
3236 // the buffer was in diff mode here
3237 if (wip->wi_win == wp)
3238 return FALSE;
3239 return TRUE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003240}
3241#endif
3242
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243/*
3244 * Find info for the current window in buffer "buf".
3245 * If not found, return the info for the most recently used window.
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003246 * When "need_options" is TRUE skip entries where wi_optset is FALSE.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003247 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
3248 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 * Returns NULL when there isn't any info.
3250 */
3251 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003252find_wininfo(
3253 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003254 int need_options,
Bram Moolenaar7454a062016-01-30 15:14:10 +01003255 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256{
3257 wininfo_T *wip;
3258
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003259 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003260 if (wip->wi_win == curwin
3261#ifdef FEAT_DIFF
3262 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
3263#endif
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003264
3265 && (!need_options || wip->wi_optset))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003267
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003268 if (wip != NULL)
3269 return wip;
3270
Bram Moolenaarc667da52019-11-30 20:52:27 +01003271 // If no wininfo for curwin, use the first in the list (that doesn't have
3272 // 'diff' set and is in another tab page).
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003273 // If "need_options" is TRUE skip entries that don't have options set,
3274 // unless the window is editing "buf", so we can copy from the window
3275 // itself.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003276#ifdef FEAT_DIFF
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003277 if (skip_diff_buffer)
3278 {
3279 FOR_ALL_BUF_WININFO(buf, wip)
3280 if (!wininfo_other_tab_diff(wip)
3281 && (!need_options || wip->wi_optset
3282 || (wip->wi_win != NULL
3283 && wip->wi_win->w_buffer == buf)))
3284 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003285 }
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003286 else
3287#endif
3288 wip = buf->b_wininfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 return wip;
3290}
3291
3292/*
3293 * Reset the local window options to the values last used in this window.
3294 * If the buffer wasn't used in this window before, use the values from
3295 * the most recently used window. If the values were never set, use the
3296 * global values for the window.
3297 */
3298 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003299get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300{
3301 wininfo_T *wip;
3302
3303 clear_winopt(&curwin->w_onebuf_opt);
3304#ifdef FEAT_FOLDING
3305 clearFolding(curwin);
3306#endif
3307
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003308 wip = find_wininfo(buf, TRUE, TRUE);
Bram Moolenaar25782a72018-05-13 18:05:33 +02003309 if (wip != NULL && wip->wi_win != NULL
3310 && wip->wi_win != curwin && wip->wi_win->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003312 // The buffer is currently displayed in the window: use the actual
3313 // option values instead of the saved (possibly outdated) values.
Bram Moolenaar25782a72018-05-13 18:05:33 +02003314 win_T *wp = wip->wi_win;
3315
3316 copy_winopt(&wp->w_onebuf_opt, &curwin->w_onebuf_opt);
3317#ifdef FEAT_FOLDING
3318 curwin->w_fold_manual = wp->w_fold_manual;
3319 curwin->w_foldinvalid = TRUE;
3320 cloneFoldGrowArray(&wp->w_folds, &curwin->w_folds);
3321#endif
3322 }
3323 else if (wip != NULL && wip->wi_optset)
3324 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003325 // the buffer was displayed in the current window earlier
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
3327#ifdef FEAT_FOLDING
3328 curwin->w_fold_manual = wip->wi_fold_manual;
3329 curwin->w_foldinvalid = TRUE;
3330 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
3331#endif
3332 }
3333 else
3334 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
LemonBoydb0ea7f2022-04-10 17:59:26 +01003335 if (wip != NULL)
3336 curwin->w_changelistidx = wip->wi_changelistidx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337
3338#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +01003339 // Set 'foldlevel' to 'foldlevelstart' if it's not negative.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 if (p_fdls >= 0)
3341 curwin->w_p_fdl = p_fdls;
3342#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02003343 after_copy_winopt(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344}
3345
3346/*
3347 * Find the position (lnum and col) for the buffer 'buf' for the current
3348 * window.
3349 * Returns a pointer to no_position if no position is found.
3350 */
3351 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003352buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353{
3354 wininfo_T *wip;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003355 static pos_T no_position = {1, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003357 wip = find_wininfo(buf, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 if (wip != NULL)
3359 return &(wip->wi_fpos);
3360 else
3361 return &no_position;
3362}
3363
3364/*
3365 * Find the lnum for the buffer 'buf' for the current window.
3366 */
3367 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01003368buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369{
3370 return buflist_findfpos(buf)->lnum;
3371}
3372
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02003374 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003377buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378{
Bram Moolenaar52410572019-10-27 05:12:45 +01003379 buf_T *buf = firstbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 int len;
3381 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003382 int ro_char;
3383 int changed_char;
Bram Moolenaar0751f512018-03-29 16:37:16 +02003384#ifdef FEAT_TERMINAL
3385 int job_running;
3386 int job_none_open;
3387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388
Bram Moolenaar52410572019-10-27 05:12:45 +01003389#ifdef FEAT_VIMINFO
3390 garray_T buflist;
3391 buf_T **buflist_data = NULL, **p;
3392
3393 if (vim_strchr(eap->arg, 't'))
3394 {
3395 ga_init2(&buflist, sizeof(buf_T *), 50);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003396 FOR_ALL_BUFFERS(buf)
Bram Moolenaar52410572019-10-27 05:12:45 +01003397 {
3398 if (ga_grow(&buflist, 1) == OK)
3399 ((buf_T **)buflist.ga_data)[buflist.ga_len++] = buf;
3400 }
3401
3402 qsort(buflist.ga_data, (size_t)buflist.ga_len,
3403 sizeof(buf_T *), buf_compare);
3404
Bram Moolenaar3b991522019-11-06 23:26:20 +01003405 buflist_data = (buf_T **)buflist.ga_data;
3406 buf = *buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003407 }
Bram Moolenaar3b991522019-11-06 23:26:20 +01003408 p = buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003409
Bram Moolenaar3b991522019-11-06 23:26:20 +01003410 for (; buf != NULL && !got_int; buf = buflist_data != NULL
Bram Moolenaar52410572019-10-27 05:12:45 +01003411 ? (++p < buflist_data + buflist.ga_len ? *p : NULL)
3412 : buf->b_next)
3413#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
Bram Moolenaar52410572019-10-27 05:12:45 +01003415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 {
John Marriottec032de2025-04-10 21:34:19 +02003417 char_u *name;
3418
Bram Moolenaar0751f512018-03-29 16:37:16 +02003419#ifdef FEAT_TERMINAL
3420 job_running = term_job_running(buf->b_term);
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003421 job_none_open = term_none_open(buf->b_term);
Bram Moolenaar0751f512018-03-29 16:37:16 +02003422#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003423 // skip unlisted buffers, unless ! was used
Bram Moolenaard51cb702015-07-21 15:03:06 +02003424 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
3425 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
3426 || (vim_strchr(eap->arg, '+')
3427 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
3428 || (vim_strchr(eap->arg, 'a')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003429 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
Bram Moolenaard51cb702015-07-21 15:03:06 +02003430 || (vim_strchr(eap->arg, 'h')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003431 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3432#ifdef FEAT_TERMINAL
3433 || (vim_strchr(eap->arg, 'R')
3434 && (!job_running || (job_running && job_none_open)))
3435 || (vim_strchr(eap->arg, '?')
3436 && (!job_running || (job_running && !job_none_open)))
3437 || (vim_strchr(eap->arg, 'F')
3438 && (job_running || buf->b_term == NULL))
3439#endif
Bram Moolenaard51cb702015-07-21 15:03:06 +02003440 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3441 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3442 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3443 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3444 || (vim_strchr(eap->arg, '#')
3445 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 continue;
John Marriottec032de2025-04-10 21:34:19 +02003447 name = buf_spname(buf);
3448 if (name != NULL)
3449 vim_strncpy(NameBuff, name, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 else
3451 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003452 if (message_filtered(NameBuff))
3453 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003455 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3456 : (bufIsChanged(buf) ? '+' : ' ');
3457#ifdef FEAT_TERMINAL
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003458 if (job_running)
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003459 {
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003460 if (job_none_open)
Bram Moolenaar4033c552017-09-16 20:54:51 +02003461 ro_char = '?';
3462 else
3463 ro_char = 'R';
Bram Moolenaarc667da52019-11-30 20:52:27 +01003464 changed_char = ' '; // bufIsChanged() returns TRUE to avoid
3465 // closing, but it's not actually changed.
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003466 }
3467 else if (buf->b_term != NULL)
3468 ro_char = 'F';
3469 else
3470#endif
3471 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3472
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003473 msg_putchar('\n');
John Marriottec032de2025-04-10 21:34:19 +02003474 len = (int)vim_snprintf_safelen((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 buf->b_fnum,
3476 buf->b_p_bl ? ' ' : 'u',
3477 buf == curbuf ? '%' :
3478 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3479 buf->b_ml.ml_mfp == NULL ? ' ' :
3480 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003481 ro_char,
3482 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003483 NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484
Bram Moolenaarc667da52019-11-30 20:52:27 +01003485 // put "line 999" in column 40 or after the file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 i = 40 - vim_strsize(IObuff);
3487 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 IObuff[len++] = ' ';
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003489 while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar52410572019-10-27 05:12:45 +01003490#ifdef FEAT_VIMINFO
3491 if (vim_strchr(eap->arg, 't') && buf->b_last_used)
3492 add_time(IObuff + len, (size_t)(IOSIZE - len), buf->b_last_used);
3493 else
3494#endif
3495 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3496 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003497 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 msg_outtrans(IObuff);
Bram Moolenaarc667da52019-11-30 20:52:27 +01003499 out_flush(); // output one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 ui_breakcheck();
3501 }
Bram Moolenaar52410572019-10-27 05:12:45 +01003502
3503#ifdef FEAT_VIMINFO
3504 if (buflist_data)
3505 ga_clear(&buflist);
3506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508
3509/*
3510 * Get file name and line number for file 'fnum'.
3511 * Used by DoOneCmd() for translating '%' and '#'.
3512 * Used by insert_reg() and cmdline_paste() for '#' register.
3513 * Return FAIL if not found, OK for success.
3514 */
3515 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003516buflist_name_nr(
3517 int fnum,
3518 char_u **fname,
3519 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520{
3521 buf_T *buf;
3522
3523 buf = buflist_findnr(fnum);
3524 if (buf == NULL || buf->b_fname == NULL)
3525 return FAIL;
3526
3527 *fname = buf->b_fname;
3528 *lnum = buflist_findlnum(buf);
3529
3530 return OK;
3531}
3532
3533/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003534 * Set the file name for "buf"' to "ffname_arg", short file name to
3535 * "sfname_arg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 * The file name with the full path is also remembered, for when :cd is used.
3537 * Returns FAIL for failure (file name already in use by other buffer)
3538 * OK otherwise.
3539 */
3540 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003541setfname(
3542 buf_T *buf,
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003543 char_u *ffname_arg,
3544 char_u *sfname_arg,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003545 int message) // give message when buffer already exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003547 char_u *ffname = ffname_arg;
3548 char_u *sfname = sfname_arg;
Bram Moolenaar81695252004-12-29 20:58:21 +00003549 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003551 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552#endif
3553
3554 if (ffname == NULL || *ffname == NUL)
3555 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003556 // Removing the name.
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003557 if (buf->b_sfname != buf->b_ffname)
3558 VIM_CLEAR(buf->b_sfname);
3559 else
3560 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003561 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562#ifdef UNIX
3563 st.st_dev = (dev_T)-1;
3564#endif
3565 }
3566 else
3567 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003568 fname_expand(buf, &ffname, &sfname); // will allocate ffname
3569 if (ffname == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 return FAIL;
3571
3572 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01003573 * If the file name is already used in another buffer:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 * - if the buffer is loaded, fail
3575 * - if the buffer is not loaded, delete it from the list
3576 */
3577#ifdef UNIX
3578 if (mch_stat((char *)ffname, &st) < 0)
3579 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003580#endif
3581 if (!(buf->b_flags & BF_DUMMY))
3582#ifdef UNIX
3583 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003585 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586#endif
3587 if (obuf != NULL && obuf != buf)
3588 {
Bram Moolenaard3710cf2021-10-04 23:13:13 +01003589 win_T *win;
3590 tabpage_T *tab;
3591 int in_use = FALSE;
3592
3593 // during startup a window may use a buffer that is not loaded yet
3594 FOR_ALL_TAB_WINDOWS(tab, win)
3595 if (win->w_buffer == obuf)
3596 in_use = TRUE;
3597
3598 // it's loaded or used in a window, fail
3599 if (obuf->b_ml.ml_mfp != NULL || in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 {
3601 if (message)
Bram Moolenaare1242042021-12-16 20:56:57 +00003602 emsg(_(e_buffer_with_this_name_already_exists));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 vim_free(ffname);
3604 return FAIL;
3605 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003606 // delete from the list
Bram Moolenaara6e8f882019-12-14 16:18:15 +01003607 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 }
3609 sfname = vim_strsave(sfname);
3610 if (ffname == NULL || sfname == NULL)
3611 {
3612 vim_free(sfname);
3613 vim_free(ffname);
3614 return FAIL;
3615 }
3616#ifdef USE_FNAME_CASE
Bram Moolenaarc667da52019-11-30 20:52:27 +01003617 fname_case(sfname, 0); // set correct case for short file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618#endif
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003619 if (buf->b_sfname != buf->b_ffname)
3620 vim_free(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 vim_free(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 buf->b_ffname = ffname;
3623 buf->b_sfname = sfname;
3624 }
3625 buf->b_fname = buf->b_sfname;
3626#ifdef UNIX
3627 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003628 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 else
3630 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003631 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 buf->b_dev = st.st_dev;
3633 buf->b_ino = st.st_ino;
3634 }
3635#endif
3636
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638
3639 buf_name_changed(buf);
3640 return OK;
3641}
3642
3643/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003644 * Crude way of changing the name of a buffer. Use with care!
3645 * The name should be relative to the current directory.
3646 */
3647 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003648buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003649{
3650 buf_T *buf;
3651
3652 buf = buflist_findnr(fnum);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00003653 if (buf == NULL)
3654 return;
3655
3656 if (buf->b_sfname != buf->b_ffname)
3657 vim_free(buf->b_sfname);
3658 vim_free(buf->b_ffname);
3659 buf->b_ffname = vim_strsave(name);
3660 buf->b_sfname = NULL;
3661 // Allocate ffname and expand into full path. Also resolves .lnk
3662 // files on Win32.
3663 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
3664 buf->b_fname = buf->b_sfname;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003665}
3666
3667/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 * Take care of what needs to be done when the name of buffer "buf" has
3669 * changed.
3670 */
3671 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003672buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673{
3674 /*
3675 * If the file name changed, also change the name of the swapfile
3676 */
3677 if (buf->b_ml.ml_mfp != NULL)
3678 ml_setname(buf);
3679
Bram Moolenaar3ad69532021-11-19 17:01:08 +00003680#ifdef FEAT_TERMINAL
3681 if (buf->b_term != NULL)
3682 term_clear_status_text(buf->b_term);
3683#endif
3684
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 if (curwin->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003686 check_arg_idx(curwin); // check file name for arg list
Bram Moolenaarc667da52019-11-30 20:52:27 +01003687 maketitle(); // set window title
Bram Moolenaarc667da52019-11-30 20:52:27 +01003688 status_redraw_all(); // status lines need to be redrawn
3689 fmarks_check_names(buf); // check named file marks
3690 ml_timestamp(buf); // reset timestamp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691}
3692
3693/*
3694 * set alternate file name for current window
3695 *
3696 * Used by do_one_cmd(), do_write() and do_ecmd().
3697 * Return the buffer.
3698 */
3699 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003700setaltfname(
3701 char_u *ffname,
3702 char_u *sfname,
3703 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704{
3705 buf_T *buf;
3706
Bram Moolenaarc667da52019-11-30 20:52:27 +01003707 // Create a buffer. 'buflisted' is not set if it's a new buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaare1004402020-10-24 20:49:43 +02003709 if (buf != NULL && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 curwin->w_alt_fnum = buf->b_fnum;
3711 return buf;
3712}
3713
3714/*
3715 * Get alternate file name for current window.
3716 * Return NULL if there isn't any, and give error message if requested.
3717 */
3718 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003719getaltfname(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003720 int errmsg) // give error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721{
3722 char_u *fname;
3723 linenr_T dummy;
3724
3725 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3726 {
3727 if (errmsg)
Bram Moolenaar108010a2021-06-27 22:03:33 +02003728 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 return NULL;
3730 }
3731 return fname;
3732}
3733
3734/*
3735 * Add a file name to the buflist and return its number.
3736 * Uses same flags as buflist_new(), except BLN_DUMMY.
3737 *
3738 * used by qf_init(), main() and doarglist()
3739 */
3740 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003741buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742{
3743 buf_T *buf;
3744
3745 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3746 if (buf != NULL)
3747 return buf->b_fnum;
3748 return 0;
3749}
3750
3751#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3752/*
3753 * Adjust slashes in file names. Called after 'shellslash' was set.
3754 */
3755 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003756buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757{
3758 buf_T *bp;
3759
Bram Moolenaar29323592016-07-24 22:04:11 +02003760 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 {
3762 if (bp->b_ffname != NULL)
3763 slash_adjust(bp->b_ffname);
3764 if (bp->b_sfname != NULL)
3765 slash_adjust(bp->b_sfname);
3766 }
3767}
3768#endif
3769
3770/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003771 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 * Also save the local window option values.
3773 */
3774 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003775buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003777 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778}
3779
3780/*
3781 * Return TRUE if 'ffname' is not the same file as current file.
3782 * Fname must have a full path (expanded by mch_FullName()).
3783 */
3784 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003785otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786{
3787 return otherfile_buf(curbuf, ffname
3788#ifdef UNIX
3789 , NULL
3790#endif
3791 );
3792}
3793
3794 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003795otherfile_buf(
3796 buf_T *buf,
3797 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003799 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003801 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802{
Bram Moolenaarc667da52019-11-30 20:52:27 +01003803 // no name is different
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3805 return TRUE;
3806 if (fnamecmp(ffname, buf->b_ffname) == 0)
3807 return FALSE;
3808#ifdef UNIX
3809 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003810 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811
Bram Moolenaarc667da52019-11-30 20:52:27 +01003812 // If no stat_T given, get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 if (stp == NULL)
3814 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003815 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 st.st_dev = (dev_T)-1;
3817 stp = &st;
3818 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003819 // Use dev/ino to check if the files are the same, even when the names
3820 // are different (possible with links). Still need to compare the
3821 // name above, for when the file doesn't exist yet.
3822 // Problem: The dev/ino changes when a file is deleted (and created
3823 // again) and remains the same when renamed/moved. We don't want to
3824 // mch_stat() each buffer each time, that would be too slow. Get the
3825 // dev/ino again when they appear to match, but not when they appear
3826 // to be different: Could skip a buffer when it's actually the same
3827 // file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 if (buf_same_ino(buf, stp))
3829 {
3830 buf_setino(buf);
3831 if (buf_same_ino(buf, stp))
3832 return FALSE;
3833 }
3834 }
3835#endif
3836 return TRUE;
3837}
3838
3839#if defined(UNIX) || defined(PROTO)
3840/*
3841 * Set inode and device number for a buffer.
3842 * Must always be called when b_fname is changed!.
3843 */
3844 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003845buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003847 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848
3849 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3850 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003851 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 buf->b_dev = st.st_dev;
3853 buf->b_ino = st.st_ino;
3854 }
3855 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003856 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857}
3858
3859/*
3860 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3861 */
3862 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003863buf_same_ino(
3864 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003865 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003867 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 && stp->st_dev == buf->b_dev
3869 && stp->st_ino == buf->b_ino);
3870}
3871#endif
3872
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003873/*
3874 * Print info about the current buffer.
3875 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003877fileinfo(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003878 int fullname, // when non-zero print full path
Bram Moolenaar7454a062016-01-30 15:14:10 +01003879 int shorthelp,
3880 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881{
3882 char_u *name;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003883 char *buffer;
John Marriottec032de2025-04-10 21:34:19 +02003884 size_t bufferlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003886 buffer = alloc(IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 if (buffer == NULL)
3888 return;
3889
Bram Moolenaarc667da52019-11-30 20:52:27 +01003890 if (fullname > 1) // 2 CTRL-G: include buffer number
John Marriottec032de2025-04-10 21:34:19 +02003891 bufferlen = vim_snprintf_safelen(buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892
John Marriottec032de2025-04-10 21:34:19 +02003893 buffer[bufferlen++] = '"';
3894
3895 name = buf_spname(curbuf);
3896 if (name != NULL)
3897 bufferlen += vim_snprintf_safelen(buffer + bufferlen,
3898 IOSIZE - bufferlen, "%s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 else
3900 {
3901 if (!fullname && curbuf->b_fname != NULL)
3902 name = curbuf->b_fname;
3903 else
3904 name = curbuf->b_ffname;
John Marriottec032de2025-04-10 21:34:19 +02003905 home_replace(shorthelp ? curbuf : NULL, name, (char_u *)buffer + bufferlen,
3906 IOSIZE - (int)bufferlen, TRUE);
3907 bufferlen += STRLEN(buffer + bufferlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 }
3909
John Marriottec032de2025-04-10 21:34:19 +02003910 bufferlen += vim_snprintf_safelen(
3911 buffer + bufferlen,
3912 IOSIZE - bufferlen,
3913 "\"%s%s%s%s%s%s",
3914 curbufIsChanged() ? (shortmess(SHM_MOD)
3915 ? " [+]" : _(" [Modified]")) : " ",
3916 (curbuf->b_flags & BF_NOTEDITED) && !bt_dontwrite(curbuf)
3917 ? _("[Not edited]") : "",
3918 (curbuf->b_flags & BF_NEW) && !bt_dontwrite(curbuf)
3919 ? new_file_message() : "",
3920 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "", curbuf->b_p_ro
3921 ? (shortmess(SHM_RO) ? _("[RO]") : _("[readonly]")) : "",
3922 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK) || curbuf->b_p_ro)
3923 ? " " : "");
3924
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 if (curbuf->b_ml.ml_flags & ML_EMPTY)
John Marriottec032de2025-04-10 21:34:19 +02003926 bufferlen += vim_snprintf_safelen(buffer + bufferlen,
3927 IOSIZE - bufferlen, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 else if (p_ru)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003929 // Current line and column are already on the screen -- webb
John Marriottec032de2025-04-10 21:34:19 +02003930 bufferlen += vim_snprintf_safelen(
3931 buffer + bufferlen,
3932 IOSIZE - bufferlen,
3933 NGETTEXT("%ld line --%d%%--", "%ld lines --%d%%--", curbuf->b_ml.ml_line_count),
3934 (long)curbuf->b_ml.ml_line_count,
3935 calc_percentage(curwin->w_cursor.lnum, curbuf->b_ml.ml_line_count));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 else
3937 {
John Marriottec032de2025-04-10 21:34:19 +02003938 bufferlen += vim_snprintf_safelen(
3939 buffer + bufferlen,
3940 IOSIZE - bufferlen,
3941 _("line %ld of %ld --%d%%-- col "),
3942 (long)curwin->w_cursor.lnum,
3943 (long)curbuf->b_ml.ml_line_count,
3944 calc_percentage(curwin->w_cursor.lnum, curbuf->b_ml.ml_line_count));
3945
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 validate_virtcol();
John Marriottec032de2025-04-10 21:34:19 +02003947 bufferlen += col_print((char_u *)buffer + bufferlen, IOSIZE - bufferlen,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3949 }
3950
John Marriottec032de2025-04-10 21:34:19 +02003951 (void)append_arg_number(curwin, (char_u *)buffer + bufferlen,
3952 IOSIZE - bufferlen, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953
3954 if (dont_truncate)
3955 {
John Marriottec032de2025-04-10 21:34:19 +02003956 int n;
3957
Bram Moolenaarc667da52019-11-30 20:52:27 +01003958 // Temporarily set msg_scroll to avoid the message being truncated.
3959 // First call msg_start() to get the message in the right place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 msg_start();
3961 n = msg_scroll;
3962 msg_scroll = TRUE;
3963 msg(buffer);
3964 msg_scroll = n;
3965 }
3966 else
3967 {
John Marriottec032de2025-04-10 21:34:19 +02003968 char *p = msg_trunc_attr(buffer, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaarc667da52019-11-30 20:52:27 +01003970 // Need to repeat the message after redrawing when:
3971 // - When restart_edit is set (otherwise there will be a delay
3972 // before redrawing).
3973 // - When the screen was scrolled but there is no wait-return
3974 // prompt.
Bram Moolenaar32526b32019-01-19 17:43:09 +01003975 set_keep_msg((char_u *)p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 }
3977
3978 vim_free(buffer);
3979}
3980
John Marriotta21240b2025-01-08 20:10:59 +01003981 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003982col_print(
3983 char_u *buf,
3984 size_t buflen,
3985 int col,
3986 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987{
3988 if (col == vcol)
John Marriottec032de2025-04-10 21:34:19 +02003989 return (int)vim_snprintf_safelen((char *)buf, buflen, "%d", col);
John Marriotta21240b2025-01-08 20:10:59 +01003990
John Marriottec032de2025-04-10 21:34:19 +02003991 return (int)vim_snprintf_safelen((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992}
3993
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994static char_u *lasttitle = NULL;
3995static char_u *lasticon = NULL;
3996
Bram Moolenaar84a93082018-06-16 22:58:15 +02003997/*
3998 * Put the file name in the title bar and icon of the window.
3999 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004001maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002{
Bram Moolenaar84a93082018-06-16 22:58:15 +02004003 char_u *title_str = NULL;
4004 char_u *icon_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 int mustset;
4006 char_u buf[IOSIZE];
John Marriottec032de2025-04-10 21:34:19 +02004007 size_t buflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008
4009 if (!redrawing())
4010 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004011 // Postpone updating the title when 'lazyredraw' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 need_maketitle = TRUE;
4013 return;
4014 }
4015
4016 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02004017 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004018 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019
4020 if (p_title)
4021 {
John Marriottec032de2025-04-10 21:34:19 +02004022 int maxlen = 0;
4023
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 if (p_titlelen > 0)
4025 {
4026 maxlen = p_titlelen * Columns / 100;
4027 if (maxlen < 10)
4028 maxlen = 10;
4029 }
4030
Bram Moolenaar84a93082018-06-16 22:58:15 +02004031 title_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 if (*p_titlestring != NUL)
4033 {
4034#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00004035 if (stl_syntax & STL_IN_TITLE)
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004036 build_stl_str_hl(curwin, title_str, sizeof(buf), p_titlestring,
4037 (char_u *)"titlestring", 0,
4038 0, maxlen, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 else
4040#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02004041 title_str = p_titlestring;
John Marriottec032de2025-04-10 21:34:19 +02004042 buflen = STRLEN(title_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 }
4044 else
4045 {
John Marriottec032de2025-04-10 21:34:19 +02004046 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047
John Marriottec032de2025-04-10 21:34:19 +02004048 // format: "<filename> [flags] <(path)> [argument info] <- servername>"
4049 // example:
4050 // buffer.c + (/home/vim/src) (1 of 2) - VIM
4051
4052 // reserve some space for different parts of the title.
4053 // use sizeof() to introduce 'size_t' so we don't have to
4054 // cast sizes to it.
4055#define SPACE_FOR_FNAME (sizeof(buf) - 100)
4056#define SPACE_FOR_DIR (sizeof(buf) - 20)
4057#define SPACE_FOR_ARGNR (sizeof(buf) - 10) // at least room for " - VIM"
4058
4059 // file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 if (curbuf->b_fname == NULL)
John Marriottec032de2025-04-10 21:34:19 +02004061 buflen = vim_snprintf_safelen((char *)buf,
4062 SPACE_FOR_FNAME, "%s", _("[No Name]"));
Bram Moolenaar21554412017-07-24 21:44:43 +02004063#ifdef FEAT_TERMINAL
4064 else if (curbuf->b_term != NULL)
John Marriottec032de2025-04-10 21:34:19 +02004065 buflen = vim_snprintf_safelen((char *)buf,
4066 SPACE_FOR_FNAME, "%s",
4067 term_get_status_text(curbuf->b_term));
Bram Moolenaar21554412017-07-24 21:44:43 +02004068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 else
4070 {
John Marriottec032de2025-04-10 21:34:19 +02004071 buflen = vim_snprintf_safelen((char *)buf,
4072 SPACE_FOR_FNAME, "%s",
4073 ((p = transstr(gettail(curbuf->b_fname))) != NULL)
4074 ? p
4075 : (char_u *)"");
4076 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 }
4078
John Marriottec032de2025-04-10 21:34:19 +02004079 // flags
Bram Moolenaar21554412017-07-24 21:44:43 +02004080#ifdef FEAT_TERMINAL
4081 if (curbuf->b_term == NULL)
4082#endif
John Marriottec032de2025-04-10 21:34:19 +02004083 {
Bram Moolenaar21554412017-07-24 21:44:43 +02004084 switch (bufIsChanged(curbuf)
4085 + (curbuf->b_p_ro * 2)
4086 + (!curbuf->b_p_ma * 4))
4087 {
John Marriottec032de2025-04-10 21:34:19 +02004088 case 1:
4089 // file was modified
4090 buflen += vim_snprintf_safelen(
4091 (char *)buf + buflen,
4092 sizeof(buf) - buflen, " +");
4093 break;
4094 case 2:
4095 // file is readonly
4096 buflen += vim_snprintf_safelen(
4097 (char *)buf + buflen,
4098 sizeof(buf) - buflen, " =");
4099 break;
4100 case 3:
4101 // file was modified and is readonly
4102 buflen += vim_snprintf_safelen(
4103 (char *)buf + buflen,
4104 sizeof(buf) - buflen, " =+");
4105 break;
Bram Moolenaar21554412017-07-24 21:44:43 +02004106 case 4:
John Marriottec032de2025-04-10 21:34:19 +02004107 case 6:
4108 // file cannot be modified
4109 buflen += vim_snprintf_safelen(
4110 (char *)buf + buflen,
4111 sizeof(buf) - buflen, " -");
4112 break;
Bram Moolenaar21554412017-07-24 21:44:43 +02004113 case 5:
John Marriottec032de2025-04-10 21:34:19 +02004114 case 7:
4115 // file cannot be modified but was modified
4116 buflen += vim_snprintf_safelen(
4117 (char *)buf + buflen,
4118 sizeof(buf) - buflen, " -+");
4119 break;
4120 default:
4121 break;
Bram Moolenaar21554412017-07-24 21:44:43 +02004122 }
John Marriottec032de2025-04-10 21:34:19 +02004123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124
John Marriottec032de2025-04-10 21:34:19 +02004125 // path (surrounded by '()')
Bram Moolenaar21554412017-07-24 21:44:43 +02004126 if (curbuf->b_fname != NULL
4127#ifdef FEAT_TERMINAL
4128 && curbuf->b_term == NULL
4129#endif
4130 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004132 // Get path of file, replace home dir with ~
John Marriottec032de2025-04-10 21:34:19 +02004133 buflen += vim_snprintf_safelen((char *)buf + buflen,
4134 sizeof(buf) - buflen, " (");
4135
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 home_replace(curbuf, curbuf->b_ffname,
John Marriottec032de2025-04-10 21:34:19 +02004137 buf + buflen, (int)(SPACE_FOR_DIR - buflen), TRUE);
4138
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc667da52019-11-30 20:52:27 +01004140 // avoid "c:/name" to be reduced to "c"
John Marriottec032de2025-04-10 21:34:19 +02004141 if (SAFE_isalpha(buf[buflen]) && buf[buflen + 1] == ':')
4142 buflen += 2; // step over "c:"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143#endif
John Marriottec032de2025-04-10 21:34:19 +02004144
4145 // determine if we have a help or normal buffer
4146 p = gettail_sep(buf + buflen);
4147 if (p == buf + buflen)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02004148 {
John Marriottec032de2025-04-10 21:34:19 +02004149 // help buffer
4150 buflen += vim_snprintf_safelen((char *)buf + buflen,
4151 SPACE_FOR_DIR - buflen, "%s)", _("help"));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02004152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 else
Bram Moolenaar2c666692012-09-05 13:30:40 +02004154 {
John Marriottec032de2025-04-10 21:34:19 +02004155 // normal buffer
4156
4157 // Translate unprintable chars and concatenate. Keep some
4158 // room for the server name. When there is no room (very long
4159 // file name) use (...).
4160 if (buflen < SPACE_FOR_DIR)
John Marriott7fb90812025-04-02 20:32:35 +02004161 {
John Marriottec032de2025-04-10 21:34:19 +02004162 // remove the file name
4163 *p = NUL;
4164
4165 buflen += vim_snprintf_safelen((char *)buf + buflen,
4166 SPACE_FOR_DIR - buflen, "%s)",
4167 ((p = transstr(buf + buflen)) != NULL)
4168 ? p
4169 : (char_u *)"");
John Marriott7fb90812025-04-02 20:32:35 +02004170 vim_free(p);
4171 }
John Marriottec032de2025-04-10 21:34:19 +02004172 else
4173 buflen += vim_snprintf_safelen((char *)buf + buflen,
4174 SPACE_FOR_ARGNR - buflen, "...)");
Bram Moolenaar2c666692012-09-05 13:30:40 +02004175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 }
4177
John Marriottec032de2025-04-10 21:34:19 +02004178 // argument info
4179 buflen += append_arg_number(curwin, buf + buflen,
4180 SPACE_FOR_ARGNR - buflen, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181
John Marriottec032de2025-04-10 21:34:19 +02004182 // servername
4183 buflen += vim_snprintf_safelen((char *)buf + buflen,
4184 sizeof(buf) - buflen, " - %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185#if defined(FEAT_CLIENTSERVER)
John Marriottec032de2025-04-10 21:34:19 +02004186 (serverName != NULL)
4187 ? serverName :
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188#endif
John Marriottec032de2025-04-10 21:34:19 +02004189 (char_u *)"VIM");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190
4191 if (maxlen > 0)
4192 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004193 // make it shorter by removing a bit in the middle
Bram Moolenaarf31b7642012-01-20 20:44:43 +01004194 if (vim_strsize(buf) > maxlen)
John Marriottec032de2025-04-10 21:34:19 +02004195 trunc_string(buf, buf, maxlen, sizeof(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 }
4197 }
4198 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02004199 mustset = value_changed(title_str, &lasttitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200
4201 if (p_icon)
4202 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02004203 icon_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 if (*p_iconstring != NUL)
4205 {
4206#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00004207 if (stl_syntax & STL_IN_ICON)
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004208 build_stl_str_hl(curwin, icon_str, sizeof(buf), p_iconstring,
4209 (char_u *)"iconstring", 0, 0, 0, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 else
4211#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02004212 icon_str = p_iconstring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 }
4214 else
4215 {
John Marriottec032de2025-04-10 21:34:19 +02004216 char_u *name;
4217 int namelen;
4218
4219 name = buf_spname(curbuf);
4220 if (name == NULL)
4221 name = gettail(curbuf->b_ffname);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004222 // Truncate name at 100 bytes.
John Marriottec032de2025-04-10 21:34:19 +02004223 namelen = (int)STRLEN(name);
4224 if (namelen > 100)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 {
John Marriottec032de2025-04-10 21:34:19 +02004226 namelen -= 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 if (has_mbyte)
John Marriottec032de2025-04-10 21:34:19 +02004228 namelen += (*mb_tail_off)(name, name + namelen) + 1;
4229 name += namelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 }
John Marriottec032de2025-04-10 21:34:19 +02004231 STRCPY(buf, name);
4232 trans_characters(buf, sizeof(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 }
4234 }
4235
Bram Moolenaar84a93082018-06-16 22:58:15 +02004236 mustset |= value_changed(icon_str, &lasticon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237
4238 if (mustset)
4239 resettitle();
4240}
4241
4242/*
4243 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
4244 * from "str" if it does.
Bram Moolenaar84a93082018-06-16 22:58:15 +02004245 * Return TRUE if resettitle() is to be called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 */
4247 static int
Bram Moolenaar84a93082018-06-16 22:58:15 +02004248value_changed(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249{
4250 if ((str == NULL) != (*last == NULL)
4251 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
4252 {
4253 vim_free(*last);
4254 if (str == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004255 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 *last = NULL;
Bram Moolenaar40385db2018-08-07 22:31:44 +02004257 mch_restore_title(
4258 last == &lasttitle ? SAVE_RESTORE_TITLE : SAVE_RESTORE_ICON);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 else
Bram Moolenaar84a93082018-06-16 22:58:15 +02004261 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 *last = vim_strsave(str);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004263 return TRUE;
4264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 }
4266 return FALSE;
4267}
4268
4269/*
4270 * Put current window title back (used after calling a shell)
4271 */
4272 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004273resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274{
4275 mch_settitle(lasttitle, lasticon);
4276}
Bram Moolenaarea408852005-06-25 22:49:46 +00004277
4278# if defined(EXITFREE) || defined(PROTO)
4279 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004280free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00004281{
4282 vim_free(lasttitle);
4283 vim_free(lasticon);
4284}
4285# endif
4286
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004288#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004289
4290/*
4291 * Used for building in the status line.
4292 */
4293typedef struct
4294{
4295 char_u *stl_start;
4296 int stl_minwid;
4297 int stl_maxwid;
4298 enum {
4299 Normal,
4300 Empty,
4301 Group,
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004302 Separate,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004303 Highlight,
4304 TabPage,
4305 Trunc
4306 } stl_type;
4307} stl_item_T;
4308
4309static size_t stl_items_len = 20; // Initial value, grows as needed.
4310static stl_item_T *stl_items = NULL;
4311static int *stl_groupitem = NULL;
4312static stl_hlrec_T *stl_hltab = NULL;
4313static stl_hlrec_T *stl_tabtab = NULL;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004314static int *stl_separator_locations = NULL;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004315
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004317 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 * Return length of string in screen cells.
4319 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004320 * Normally works for window "wp", except when working for 'tabline' then it
4321 * is "curwin".
4322 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 * Items are drawn interspersed with the text that surrounds it
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004324 * Specials: %-<wid>(xxx%) => group, %= => separation marker, %< => truncation
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
4326 *
4327 * If maxwidth is not zero, the string will be filled at any middle marker
4328 * or truncated if too long, fillchar is used for all whitespace.
4329 */
4330 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004331build_stl_str_hl(
4332 win_T *wp,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004333 char_u *out, // buffer to write into != NameBuff
4334 size_t outlen, // length of out[]
Bram Moolenaar7454a062016-01-30 15:14:10 +01004335 char_u *fmt,
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004336 char_u *opt_name, // option name corresponding to "fmt"
4337 int opt_scope, // scope for "opt_name"
Bram Moolenaar7454a062016-01-30 15:14:10 +01004338 int fillchar,
4339 int maxwidth,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004340 stl_hlrec_T **hltab, // return: HL attributes (can be NULL)
4341 stl_hlrec_T **tabtab) // return: tab page nrs (can be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342{
Bram Moolenaar10772302019-01-20 18:25:54 +01004343 linenr_T lnum;
zeertzjq94b7c322024-03-12 21:50:32 +01004344 colnr_T len;
John Marriottec032de2025-04-10 21:34:19 +02004345 size_t outputlen; // length of out[] used (excluding the NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 char_u *p;
4347 char_u *s;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004348 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349#ifdef FEAT_EVAL
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004350 int use_sandbox;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004351 int save_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352#endif
4353 int empty_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 long l;
4355 long n;
4356 int prevchar_isflag;
4357 int prevchar_isitem;
4358 int itemisflag;
4359 int fillable;
4360 char_u *str;
4361 long num;
4362 int width;
4363 int itemcnt;
4364 int curitem;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 int groupdepth;
shadmansaleh30e3de22021-05-15 17:23:28 +02004366#ifdef FEAT_EVAL
4367 int evaldepth;
4368#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 int minwid;
4370 int maxwid;
4371 int zeropad;
4372 char_u base;
4373 char_u opt;
4374#define TMPLEN 70
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004375 char_u buf_tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004376 char_u *usefmt = fmt;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004377 stl_hlrec_T *sp;
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01004378 int save_redraw_not_allowed = redraw_not_allowed;
Bram Moolenaar0e1f36f2022-02-15 16:17:44 +00004379 int save_KeyTyped = KeyTyped;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004380 // TODO: find out why using called_emsg_before makes tests fail, does it
4381 // matter?
4382 // int called_emsg_before = called_emsg;
4383 int did_emsg_before = did_emsg;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004384
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01004385 // When inside update_screen() we do not want redrawing a statusline,
4386 // ruler, title, etc. to trigger another redraw, it may cause an endless
4387 // loop.
4388 if (updating_screen)
4389 redraw_not_allowed = TRUE;
4390
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004391 if (stl_items == NULL)
4392 {
4393 stl_items = ALLOC_MULT(stl_item_T, stl_items_len);
4394 stl_groupitem = ALLOC_MULT(int, stl_items_len);
Brandon Richardsona493b652022-02-19 11:45:03 +00004395
4396 // Allocate one more, because the last element is used to indicate the
4397 // end of the list.
4398 stl_hltab = ALLOC_MULT(stl_hlrec_T, stl_items_len + 1);
4399 stl_tabtab = ALLOC_MULT(stl_hlrec_T, stl_items_len + 1);
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004400
4401 stl_separator_locations = ALLOC_MULT(int, stl_items_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004402 }
4403
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004404#ifdef FEAT_EVAL
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004405 // if "fmt" was set insecurely it needs to be evaluated in the sandbox
4406 use_sandbox = was_set_insecurely(opt_name, opt_scope);
4407
4408 // When the format starts with "%!" then evaluate it as an expression and
4409 // use the result as the actual format string.
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004410 if (fmt[0] == '%' && fmt[1] == '!')
4411 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004412 typval_T tv;
4413
4414 tv.v_type = VAR_NUMBER;
4415 tv.vval.v_number = wp->w_id;
4416 set_var((char_u *)"g:statusline_winid", &tv, FALSE);
4417
Bram Moolenaara4e0b972022-10-01 19:43:52 +01004418 usefmt = eval_to_string_safe(fmt + 2, use_sandbox, FALSE, FALSE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004419 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00004420 usefmt = fmt;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004421
4422 do_unlet((char_u *)"g:statusline_winid", TRUE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004423 }
4424#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425
4426 if (fillchar == 0)
4427 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004428
Bram Moolenaar10772302019-01-20 18:25:54 +01004429 // The cursor in windows other than the current one isn't always
4430 // up-to-date, esp. because of autocommands and timers.
4431 lnum = wp->w_cursor.lnum;
4432 if (lnum > wp->w_buffer->b_ml.ml_line_count)
4433 {
4434 lnum = wp->w_buffer->b_ml.ml_line_count;
4435 wp->w_cursor.lnum = lnum;
4436 }
4437
4438 // Get line & check if empty (cursorpos will show "0-1"). Note that
4439 // p will become invalid when getting another buffer line.
4440 p = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar567199b2013-04-24 16:52:36 +02004441 empty_line = (*p == NUL);
4442
Bram Moolenaar10772302019-01-20 18:25:54 +01004443 // Get the byte value now, in case we need it below. This is more efficient
4444 // than making a copy of the line.
zeertzjq94b7c322024-03-12 21:50:32 +01004445 len = ml_get_buf_len(wp->w_buffer, lnum);
4446 if (wp->w_cursor.col > len)
Bram Moolenaar10772302019-01-20 18:25:54 +01004447 {
4448 // Line may have changed since checking the cursor column, or the lnum
4449 // was adjusted above.
zeertzjq94b7c322024-03-12 21:50:32 +01004450 wp->w_cursor.col = len;
Bram Moolenaar10772302019-01-20 18:25:54 +01004451 wp->w_cursor.coladd = 0;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004452 byteval = 0;
Bram Moolenaar10772302019-01-20 18:25:54 +01004453 }
Bram Moolenaar567199b2013-04-24 16:52:36 +02004454 else
Bram Moolenaar567199b2013-04-24 16:52:36 +02004455 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456
4457 groupdepth = 0;
shadmansaleh30e3de22021-05-15 17:23:28 +02004458#ifdef FEAT_EVAL
4459 evaldepth = 0;
4460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 p = out;
4462 curitem = 0;
4463 prevchar_isflag = TRUE;
4464 prevchar_isitem = FALSE;
zeertzjq122dea72022-07-27 15:48:45 +01004465 for (s = usefmt; *s != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004467 if (curitem == (int)stl_items_len)
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004468 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004469 size_t new_len = stl_items_len * 3 / 2;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004470
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004471 stl_item_T *new_items =
4472 vim_realloc(stl_items, sizeof(stl_item_T) * new_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004473 if (new_items == NULL)
4474 break;
4475 stl_items = new_items;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004476
4477 int *new_groupitem =
4478 vim_realloc(stl_groupitem, sizeof(int) * new_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004479 if (new_groupitem == NULL)
4480 break;
4481 stl_groupitem = new_groupitem;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004482
4483 stl_hlrec_T *new_hlrec = vim_realloc(stl_hltab,
Brandon Richardsona493b652022-02-19 11:45:03 +00004484 sizeof(stl_hlrec_T) * (new_len + 1));
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004485 if (new_hlrec == NULL)
4486 break;
4487 stl_hltab = new_hlrec;
Brandon Richardsona493b652022-02-19 11:45:03 +00004488 new_hlrec = vim_realloc(stl_tabtab,
4489 sizeof(stl_hlrec_T) * (new_len + 1));
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004490 if (new_hlrec == NULL)
4491 break;
4492 stl_tabtab = new_hlrec;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004493
4494 int *new_separator_locs = vim_realloc(stl_separator_locations,
4495 sizeof(int) * new_len);
4496 if (new_separator_locs == NULL)
4497 break;
John Marriottec032de2025-04-10 21:34:19 +02004498 stl_separator_locations = new_separator_locs;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004499
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004500 stl_items_len = new_len;
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004501 }
4502
zeertzjq122dea72022-07-27 15:48:45 +01004503 if (*s != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 prevchar_isflag = prevchar_isitem = FALSE;
4505
4506 /*
4507 * Handle up to the next '%' or the end.
4508 */
4509 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
4510 *p++ = *s++;
4511 if (*s == NUL || p + 1 >= out + outlen)
4512 break;
4513
4514 /*
4515 * Handle one '%' item.
4516 */
4517 s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004518 if (*s == NUL) // ignore trailing %
Bram Moolenaar1d87f512011-02-01 21:55:01 +01004519 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 if (*s == '%')
4521 {
4522 if (p + 1 >= out + outlen)
4523 break;
4524 *p++ = *s++;
4525 prevchar_isflag = prevchar_isitem = FALSE;
4526 continue;
4527 }
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004528 // STL_SEPARATE: Separation between items, filled with white space.
4529 if (*s == STL_SEPARATE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 {
4531 s++;
4532 if (groupdepth > 0)
4533 continue;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004534 stl_items[curitem].stl_type = Separate;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004535 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 continue;
4537 }
4538 if (*s == STL_TRUNCMARK)
4539 {
4540 s++;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004541 stl_items[curitem].stl_type = Trunc;
4542 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 continue;
4544 }
4545 if (*s == ')')
4546 {
John Marriottec032de2025-04-10 21:34:19 +02004547 char_u *t;
4548
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 s++;
4550 if (groupdepth < 1)
4551 continue;
4552 groupdepth--;
4553
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004554 t = stl_items[stl_groupitem[groupdepth]].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 *p = NUL;
4556 l = vim_strsize(t);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004557 if (curitem > stl_groupitem[groupdepth] + 1
4558 && stl_items[stl_groupitem[groupdepth]].stl_minwid == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 {
John Marriottec032de2025-04-10 21:34:19 +02004560 int group_start_userhl = 0;
4561 int group_end_userhl = 0;
4562
Bram Moolenaarc667da52019-11-30 20:52:27 +01004563 // remove group if all items are empty and highlight group
4564 // doesn't change
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004565 for (n = stl_groupitem[groupdepth] - 1; n >= 0; n--)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004566 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004567 if (stl_items[n].stl_type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004568 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004569 group_start_userhl = group_end_userhl =
4570 stl_items[n].stl_minwid;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004571 break;
4572 }
4573 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004574 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004575 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004576 if (stl_items[n].stl_type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 break;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004578 if (stl_items[n].stl_type == Highlight)
4579 group_end_userhl = stl_items[n].stl_minwid;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004580 }
4581 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 {
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004583 // empty group
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 p = t;
4585 l = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004586 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004587 {
4588 // do not use the highlighting from the removed group
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004589 if (stl_items[n].stl_type == Highlight)
4590 stl_items[n].stl_type = Empty;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004591 // adjust the start position of TabPage to the next
4592 // item position
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004593 if (stl_items[n].stl_type == TabPage)
4594 stl_items[n].stl_start = p;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 }
4597 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004598 if (l > stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004600 // truncate, remove n bytes of text at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 if (has_mbyte)
4602 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004603 // Find the first character that should be included.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 n = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004605 while (l >= stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 {
4607 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004608 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 }
4610 }
4611 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004612 n = (long)(p - t) - stl_items[stl_groupitem[groupdepth]]
4613 .stl_maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614
4615 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004616 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 p = p - n + 1;
Bram Moolenaar13505972019-01-24 15:04:48 +01004618
4619 // Fill up space left over by half a double-wide char.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004620 while (++l < stl_items[stl_groupitem[groupdepth]].stl_minwid)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004621 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622
Bram Moolenaarc667da52019-11-30 20:52:27 +01004623 // correct the start of the items for the truncation
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004624 for (l = stl_groupitem[groupdepth] + 1; l < curitem; l++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 {
LemonBoy57ff5262022-05-09 21:03:47 +01004626 // Minus one for the leading '<' added above.
4627 stl_items[l].stl_start -= n - 1;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004628 if (stl_items[l].stl_start < t)
4629 stl_items[l].stl_start = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 }
4631 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004632 else if (abs(stl_items[stl_groupitem[groupdepth]].stl_minwid) > l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004634 // fill
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004635 n = stl_items[stl_groupitem[groupdepth]].stl_minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 if (n < 0)
4637 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004638 // fill by appending characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 n = 0 - n;
4640 while (l++ < n && p + 1 < out + outlen)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004641 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 }
4643 else
4644 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004645 // fill by inserting characters
Bram Moolenaar008bff92021-03-04 21:55:58 +01004646 l = (n - l) * MB_CHAR2LEN(fillchar);
4647 mch_memmove(t + l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004649 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 p += l;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004651 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
4652 stl_items[n].stl_start += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 for ( ; l > 0; l--)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004654 MB_CHAR2BYTES(fillchar, t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 }
4656 }
4657 continue;
4658 }
4659 minwid = 0;
4660 maxwid = 9999;
4661 zeropad = FALSE;
4662 l = 1;
4663 if (*s == '0')
4664 {
4665 s++;
4666 zeropad = TRUE;
4667 }
4668 if (*s == '-')
4669 {
4670 s++;
4671 l = -1;
4672 }
4673 if (VIM_ISDIGIT(*s))
4674 {
4675 minwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004676 if (minwid < 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 minwid = 0;
4678 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004679 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004681 stl_items[curitem].stl_type = Highlight;
4682 stl_items[curitem].stl_start = p;
4683 stl_items[curitem].stl_minwid = minwid > 9 ? 1 : minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 s++;
4685 curitem++;
4686 continue;
4687 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004688 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4689 {
4690 if (*s == STL_TABCLOSENR)
4691 {
4692 if (minwid == 0)
4693 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004694 // %X ends the close label, go back to the previously
4695 // define tab label nr.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004696 for (n = curitem - 1; n >= 0; --n)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004697 if (stl_items[n].stl_type == TabPage
4698 && stl_items[n].stl_minwid >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004699 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004700 minwid = stl_items[n].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004701 break;
4702 }
4703 }
4704 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01004705 // close nrs are stored as negative values
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004706 minwid = - minwid;
4707 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004708 stl_items[curitem].stl_type = TabPage;
4709 stl_items[curitem].stl_start = p;
4710 stl_items[curitem].stl_minwid = minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004711 s++;
4712 curitem++;
4713 continue;
4714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 if (*s == '.')
4716 {
4717 s++;
4718 if (VIM_ISDIGIT(*s))
4719 {
4720 maxwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004721 if (maxwid <= 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 maxwid = 50;
4723 }
4724 }
4725 minwid = (minwid > 50 ? 50 : minwid) * l;
4726 if (*s == '(')
4727 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004728 stl_groupitem[groupdepth++] = curitem;
4729 stl_items[curitem].stl_type = Group;
4730 stl_items[curitem].stl_start = p;
4731 stl_items[curitem].stl_minwid = minwid;
4732 stl_items[curitem].stl_maxwid = maxwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 s++;
4734 curitem++;
4735 continue;
4736 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004737#ifdef FEAT_EVAL
4738 // Denotes end of expanded %{} block
4739 if (*s == '}' && evaldepth > 0)
4740 {
4741 s++;
4742 evaldepth--;
4743 continue;
4744 }
4745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 if (vim_strchr(STL_ALL, *s) == NULL)
4747 {
Bram Moolenaar7b17eb42023-01-04 14:31:49 +00004748 if (*s == NUL) // can happen with "%0"
4749 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 s++;
4751 continue;
4752 }
4753 opt = *s++;
4754
Bram Moolenaarc667da52019-11-30 20:52:27 +01004755 // OK - now for the real work
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 base = 'D';
4757 itemisflag = FALSE;
4758 fillable = TRUE;
4759 num = -1;
4760 str = NULL;
4761 switch (opt)
4762 {
4763 case STL_FILEPATH:
4764 case STL_FULLPATH:
4765 case STL_FILENAME:
John Marriottec032de2025-04-10 21:34:19 +02004766 {
4767 char_u *name;
4768
Bram Moolenaarc667da52019-11-30 20:52:27 +01004769 fillable = FALSE; // don't change ' ' to fillchar
John Marriottec032de2025-04-10 21:34:19 +02004770 name = buf_spname(wp->w_buffer);
4771 if (name != NULL)
4772 vim_strncpy(NameBuff, name, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 else
4774 {
John Marriottec032de2025-04-10 21:34:19 +02004775 char_u *t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004776 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4778 }
4779 trans_characters(NameBuff, MAXPATHL);
4780 if (opt != STL_FILENAME)
4781 str = NameBuff;
4782 else
4783 str = gettail(NameBuff);
4784 break;
John Marriottec032de2025-04-10 21:34:19 +02004785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786
Bram Moolenaarc667da52019-11-30 20:52:27 +01004787 case STL_VIM_EXPR: // '{'
shadmansaleh30e3de22021-05-15 17:23:28 +02004788 {
4789#ifdef FEAT_EVAL
John Marriottec032de2025-04-10 21:34:19 +02004790 char_u *block_start = s - 1;
shadmansaleh30e3de22021-05-15 17:23:28 +02004791#endif
John Marriottec032de2025-04-10 21:34:19 +02004792 int reevaluate = (*s == '%');
4793 char_u *t;
4794 buf_T *save_curbuf;
4795 win_T *save_curwin;
shadmansaleh30e3de22021-05-15 17:23:28 +02004796
4797 if (reevaluate)
4798 s++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 itemisflag = TRUE;
4800 t = p;
shadmansaleh30e3de22021-05-15 17:23:28 +02004801 while ((*s != '}' || (reevaluate && s[-1] != '%'))
4802 && *s != NUL && p + 1 < out + outlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 *p++ = *s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004804 if (*s != '}') // missing '}' or out of space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 break;
4806 s++;
shadmansaleh30e3de22021-05-15 17:23:28 +02004807 if (reevaluate)
John Marriottec032de2025-04-10 21:34:19 +02004808 p[-1] = NUL; // remove the % at the end of %{% expr %}
shadmansaleh30e3de22021-05-15 17:23:28 +02004809 else
John Marriottec032de2025-04-10 21:34:19 +02004810 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 p = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812#ifdef FEAT_EVAL
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004813 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp),
4814 "%d", curbuf->b_fnum);
4815 set_internal_string_var((char_u *)"g:actual_curbuf", buf_tmp);
John Marriottec032de2025-04-10 21:34:19 +02004816 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), "%d", curwin->w_id);
4817 set_internal_string_var((char_u *)"g:actual_curwin", buf_tmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004819 save_curbuf = curbuf;
4820 save_curwin = curwin;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004821 save_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 curwin = wp;
4823 curbuf = wp->w_buffer;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004824 // Visual mode is only valid in the current window.
4825 if (curwin != save_curwin)
4826 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827
Bram Moolenaara4e0b972022-10-01 19:43:52 +01004828 str = eval_to_string_safe(p, use_sandbox, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004830 curwin = save_curwin;
4831 curbuf = save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004832 VIsual_active = save_VIsual_active;
Bram Moolenaar01824652005-01-31 18:58:23 +00004833 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004834 do_unlet((char_u *)"g:actual_curwin", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835
John Marriottec032de2025-04-10 21:34:19 +02004836 if (str != NULL && *str != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 {
4838 if (*skipdigits(str) == NUL)
4839 {
4840 num = atoi((char *)str);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004841 VIM_CLEAR(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 itemisflag = FALSE;
4843 }
4844 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004845
4846 // If the output of the expression needs to be evaluated
4847 // replace the %{} block with the result of evaluation
John Marriottec032de2025-04-10 21:34:19 +02004848 if (reevaluate && str != NULL && *str != NUL
shadmansaleh30e3de22021-05-15 17:23:28 +02004849 && strchr((const char *)str, '%') != NULL
4850 && evaldepth < MAX_STL_EVAL_DEPTH)
4851 {
4852 size_t parsed_usefmt = (size_t)(block_start - usefmt);
Hirohito Higashic8ce81b2025-04-12 11:28:18 +02004853 size_t str_length = strlen((const char *)str);
4854 size_t fmt_length = strlen((const char *)s);
4855 size_t new_fmt_len = parsed_usefmt
4856 + str_length + fmt_length + 3;
4857 char_u *new_fmt = (char_u *)alloc(new_fmt_len * sizeof(char_u));
shadmansaleh30e3de22021-05-15 17:23:28 +02004858
John Marriott7fb90812025-04-02 20:32:35 +02004859 if (new_fmt != NULL)
4860 {
Hirohito Higashic8ce81b2025-04-12 11:28:18 +02004861 char_u *new_fmt_p = new_fmt;
4862
4863 new_fmt_p = (char_u *)memcpy(new_fmt_p, usefmt, parsed_usefmt)
4864 + parsed_usefmt;
4865 new_fmt_p = (char_u *)memcpy(new_fmt_p , str, str_length)
4866 + str_length;
4867 new_fmt_p = (char_u *)memcpy(new_fmt_p, "%}", 2) + 2;
4868 new_fmt_p = (char_u *)memcpy(new_fmt_p , s, fmt_length)
4869 + fmt_length;
4870 *new_fmt_p = 0;
4871 new_fmt_p = NULL;
John Marriott7fb90812025-04-02 20:32:35 +02004872
4873 if (usefmt != fmt)
4874 vim_free(usefmt);
4875 VIM_CLEAR(str);
4876 usefmt = new_fmt;
4877 s = usefmt + parsed_usefmt;
4878 evaldepth++;
4879 continue;
4880 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882#endif
4883 break;
shadmansaleh30e3de22021-05-15 17:23:28 +02004884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 case STL_LINE:
4886 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4887 ? 0L : (long)(wp->w_cursor.lnum);
4888 break;
4889
4890 case STL_NUMLINES:
4891 num = wp->w_buffer->b_ml.ml_line_count;
4892 break;
4893
4894 case STL_COLUMN:
Bram Moolenaar24959102022-05-07 20:01:16 +01004895 num = (State & MODE_INSERT) == 0 && empty_line
4896 ? 0 : (int)wp->w_cursor.col + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 break;
4898
4899 case STL_VIRTCOL:
4900 case STL_VIRTCOL_ALT:
John Marriottec032de2025-04-10 21:34:19 +02004901 {
4902 colnr_T virtcol = wp->w_virtcol + 1;
4903
Bram Moolenaarc667da52019-11-30 20:52:27 +01004904 // Don't display %V if it's the same as %c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 if (opt == STL_VIRTCOL_ALT
Bram Moolenaar24959102022-05-07 20:01:16 +01004906 && (virtcol == (colnr_T)((State & MODE_INSERT) == 0
4907 && empty_line ? 0 : (int)wp->w_cursor.col + 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 break;
4909 num = (long)virtcol;
4910 break;
John Marriottec032de2025-04-10 21:34:19 +02004911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912
4913 case STL_PERCENTAGE:
John Marriottec032de2025-04-10 21:34:19 +02004914 num = calc_percentage((long)wp->w_cursor.lnum, (long)wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 break;
4916
4917 case STL_ALTPERCENT:
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004918 str = buf_tmp;
John Marriotta21240b2025-01-08 20:10:59 +01004919 (void)get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 break;
4921
Luuk van Baalba936f62022-12-15 13:15:39 +00004922 case STL_SHOWCMD:
4923 if (p_sc && STRCMP(opt_name, p_sloc) == 0)
4924 str = showcmd_buf;
4925 break;
4926
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 case STL_ARGLISTSTAT:
4928 fillable = FALSE;
John Marriottec032de2025-04-10 21:34:19 +02004929 buf_tmp[0] = NUL;
4930 if (append_arg_number(wp, buf_tmp, sizeof(buf_tmp), FALSE) > 0)
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004931 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 break;
4933
4934 case STL_KEYMAP:
4935 fillable = FALSE;
John Marriotta21240b2025-01-08 20:10:59 +01004936 if (get_keymap_str(wp, (char_u *)"<%s>", buf_tmp, TMPLEN) > 0)
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004937 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 break;
4939 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004940#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004941 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942#else
4943 num = 0;
4944#endif
4945 break;
4946
4947 case STL_BUFNO:
4948 num = wp->w_buffer->b_fnum;
4949 break;
4950
4951 case STL_OFFSET_X:
4952 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004953 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 case STL_OFFSET:
4955#ifdef FEAT_BYTEOFF
4956 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
Bram Moolenaar24959102022-05-07 20:01:16 +01004957 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0
4958 ? 0L : l + 1 + ((State & MODE_INSERT) == 0 && empty_line
4959 ? 0 : (int)wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960#endif
4961 break;
4962
4963 case STL_BYTEVAL_X:
4964 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004965 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004967 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 if (num == NL)
4969 num = 0;
4970 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4971 num = NL;
4972 break;
4973
4974 case STL_ROFLAG:
4975 case STL_ROFLAG_ALT:
4976 itemisflag = TRUE;
4977 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004978 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 break;
4980
4981 case STL_HELPFLAG:
4982 case STL_HELPFLAG_ALT:
4983 itemisflag = TRUE;
4984 if (wp->w_buffer->b_help)
4985 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004986 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 break;
4988
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 case STL_FILETYPE:
4990 if (*wp->w_buffer->b_p_ft != NUL
4991 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4992 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004993 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), "[%s]",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004994 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004995 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 }
4997 break;
4998
4999 case STL_FILETYPE_ALT:
5000 itemisflag = TRUE;
5001 if (*wp->w_buffer->b_p_ft != NUL
5002 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
5003 {
John Marriottec032de2025-04-10 21:34:19 +02005004 char_u *t;
5005
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02005006 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), ",%s",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005007 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02005008 for (t = buf_tmp; *t != 0; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 *t = TOUPPER_LOC(*t);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02005010 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 }
5012 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013
Bram Moolenaar4033c552017-09-16 20:54:51 +02005014#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 case STL_PREVIEWFLAG:
5016 case STL_PREVIEWFLAG_ALT:
5017 itemisflag = TRUE;
5018 if (wp->w_p_pvw)
5019 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
5020 : _("[Preview]"));
5021 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005022
5023 case STL_QUICKFIX:
5024 if (bt_quickfix(wp->w_buffer))
5025 str = (char_u *)(wp->w_llist_ref
5026 ? _(msg_loclist)
5027 : _(msg_qflist));
5028 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029#endif
5030
5031 case STL_MODIFIED:
5032 case STL_MODIFIED_ALT:
5033 itemisflag = TRUE;
5034 switch ((opt == STL_MODIFIED_ALT)
5035 + bufIsChanged(wp->w_buffer) * 2
5036 + (!wp->w_buffer->b_p_ma) * 4)
5037 {
5038 case 2: str = (char_u *)"[+]"; break;
5039 case 3: str = (char_u *)",+"; break;
5040 case 4: str = (char_u *)"[-]"; break;
5041 case 5: str = (char_u *)",-"; break;
5042 case 6: str = (char_u *)"[+-]"; break;
5043 case 7: str = (char_u *)",+-"; break;
5044 }
5045 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005046
5047 case STL_HIGHLIGHT:
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005048 {
John Marriottec032de2025-04-10 21:34:19 +02005049 char_u *t = s;
5050
5051 while (*s != '#' && *s != NUL)
5052 ++s;
5053 if (*s == '#')
5054 {
5055 stl_items[curitem].stl_type = Highlight;
5056 stl_items[curitem].stl_start = p;
5057 stl_items[curitem].stl_minwid = -syn_namen2id(t, (int)(s - t));
5058 curitem++;
5059 }
5060 if (*s != NUL)
5061 ++s;
5062 continue;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 }
5065
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005066 stl_items[curitem].stl_start = p;
5067 stl_items[curitem].stl_type = Normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 if (str != NULL && *str)
5069 {
John Marriottec032de2025-04-10 21:34:19 +02005070 char_u *t = str;
5071
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 if (itemisflag)
5073 {
5074 if ((t[0] && t[1])
5075 && ((!prevchar_isitem && *t == ',')
5076 || (prevchar_isflag && *t == ' ')))
5077 t++;
5078 prevchar_isflag = TRUE;
5079 }
5080 l = vim_strsize(t);
5081 if (l > 0)
5082 prevchar_isitem = TRUE;
5083 if (l > maxwid)
5084 {
5085 while (l >= maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 if (has_mbyte)
5087 {
5088 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005089 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 }
5091 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 l -= byte2cells(*t++);
5093 if (p + 1 >= out + outlen)
5094 break;
5095 *p++ = '<';
5096 }
5097 if (minwid > 0)
5098 {
5099 for (; l < minwid && p + 1 < out + outlen; l++)
5100 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005101 // Don't put a "-" in front of a digit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
5103 *p++ = ' ';
5104 else
Bram Moolenaar008bff92021-03-04 21:55:58 +01005105 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 }
5107 minwid = 0;
5108 }
5109 else
5110 minwid *= -1;
Bram Moolenaar008bff92021-03-04 21:55:58 +01005111 for (; *t && p + 1 < out + outlen; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005113 // Change a space by fillchar, unless fillchar is '-' and a
5114 // digit follows.
Bram Moolenaar008bff92021-03-04 21:55:58 +01005115 if (fillable && *t == ' '
5116 && (!VIM_ISDIGIT(*(t + 1)) || fillchar != '-'))
5117 MB_CHAR2BYTES(fillchar, p);
5118 else
5119 *p++ = *t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 }
5121 for (; l < minwid && p + 1 < out + outlen; l++)
Bram Moolenaar008bff92021-03-04 21:55:58 +01005122 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 }
5124 else if (num >= 0)
5125 {
John Marriottec032de2025-04-10 21:34:19 +02005126 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
5127 char_u nstr[20];
5128 char_u *t = nstr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129
5130 if (p + 20 >= out + outlen)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005131 break; // not sufficient space
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 prevchar_isitem = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 if (opt == STL_VIRTCOL_ALT)
5134 {
5135 *t++ = '-';
5136 minwid--;
5137 }
5138 *t++ = '%';
5139 if (zeropad)
5140 *t++ = '0';
5141 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005142 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
John Marriottec032de2025-04-10 21:34:19 +02005143 *t = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144
5145 for (n = num, l = 1; n >= nbase; n /= nbase)
5146 l++;
5147 if (opt == STL_VIRTCOL_ALT)
5148 l++;
John Marriottec032de2025-04-10 21:34:19 +02005149
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 if (l > maxwid)
5151 {
5152 l += 2;
5153 n = l - maxwid;
5154 while (l-- > maxwid)
5155 num /= nbase;
5156 *t++ = '>';
5157 *t++ = '%';
5158 *t = t[-3];
John Marriottec032de2025-04-10 21:34:19 +02005159 *++t = NUL;
5160 p += vim_snprintf_safelen((char *)p, outlen - (p - out),
5161 (char *)nstr, 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 }
5163 else
John Marriottec032de2025-04-10 21:34:19 +02005164 p += vim_snprintf_safelen((char *)p, outlen - (p - out),
5165 (char *)nstr, minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 }
5167 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005168 stl_items[curitem].stl_type = Empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01005170 if (num >= 0 || (!itemisflag && str != NULL && *str != NUL))
5171 prevchar_isflag = FALSE; // Item not NULL, but not a flag
5172 //
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 if (opt == STL_VIM_EXPR)
5174 vim_free(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 curitem++;
5176 }
5177 *p = NUL;
John Marriottec032de2025-04-10 21:34:19 +02005178 outputlen = (size_t)(p - out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 itemcnt = curitem;
5180
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005181#ifdef FEAT_EVAL
5182 if (usefmt != fmt)
5183 vim_free(usefmt);
5184#endif
5185
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 width = vim_strsize(out);
5187 if (maxwidth > 0 && width > maxwidth)
5188 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005189 // Result is too long, must truncate somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 l = 0;
5191 if (itemcnt == 0)
5192 s = out;
5193 else
5194 {
5195 for ( ; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005196 if (stl_items[l].stl_type == Trunc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005198 // Truncate at %< item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005199 s = stl_items[l].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 break;
5201 }
5202 if (l == itemcnt)
5203 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005204 // No %< item, truncate first item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005205 s = stl_items[0].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 l = 0;
5207 }
5208 }
5209
5210 if (width - vim_strsize(s) >= maxwidth)
5211 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005212 // Truncation mark is beyond max length
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 if (has_mbyte)
5214 {
5215 s = out;
5216 width = 0;
5217 for (;;)
5218 {
5219 width += ptr2cells(s);
5220 if (width >= maxwidth)
5221 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005222 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005224 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 while (++width < maxwidth)
Bram Moolenaar008bff92021-03-04 21:55:58 +01005226 MB_CHAR2BYTES(fillchar, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 }
5228 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 s = out + maxwidth - 1;
5230 for (l = 0; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005231 if (stl_items[l].stl_start > s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 break;
5233 itemcnt = l;
5234 *s++ = '>';
John Marriottec032de2025-04-10 21:34:19 +02005235 *s = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 }
5237 else
5238 {
John Marriottec032de2025-04-10 21:34:19 +02005239 char_u *end = out + outputlen;
5240
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 if (has_mbyte)
5242 {
5243 n = 0;
5244 while (width >= maxwidth)
5245 {
5246 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005247 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 }
5249 }
5250 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 n = width - maxwidth + 1;
5252 p = s + n;
John Marriottec032de2025-04-10 21:34:19 +02005253 mch_memmove(s + 1, p, (size_t)(end - p) + 1); // +1 for NUL
5254 end -= (size_t)(p - (s + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 *s = '<';
5256
Bram Moolenaarc667da52019-11-30 20:52:27 +01005257 --n; // count the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 for (; l < itemcnt; l++)
5259 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005260 if (stl_items[l].stl_start - n >= s)
5261 stl_items[l].stl_start -= n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005263 stl_items[l].stl_start = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 }
zeertzjqd392a742023-07-01 20:24:40 +01005265
5266 // Fill up for half a double-wide character.
5267 while (++width < maxwidth)
5268 {
John Marriottec032de2025-04-10 21:34:19 +02005269 s = end;
zeertzjqd392a742023-07-01 20:24:40 +01005270 MB_CHAR2BYTES(fillchar, s);
5271 *s = NUL;
John Marriottec032de2025-04-10 21:34:19 +02005272 end = s;
zeertzjqd392a742023-07-01 20:24:40 +01005273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 }
5275 width = maxwidth;
5276 }
John Marriottec032de2025-04-10 21:34:19 +02005277 else if (width < maxwidth && outputlen + maxwidth - width + 1 < outlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 {
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00005279 // Find how many separators there are, which we will use when
5280 // figuring out how many groups there are.
5281 int num_separators = 0;
5282
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 for (l = 0; l < itemcnt; l++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 {
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00005285 if (stl_items[l].stl_type == Separate)
5286 {
5287 // Create an array of the start location for each separator
5288 // mark.
5289 stl_separator_locations[num_separators] = l;
5290 num_separators++;
5291 }
5292 }
5293
5294 // If we have separated groups, then we deal with it now
5295 if (num_separators)
5296 {
5297 int standard_spaces;
5298 int final_spaces;
5299
5300 standard_spaces = (maxwidth - width) / num_separators;
5301 final_spaces = (maxwidth - width) -
5302 standard_spaces * (num_separators - 1);
5303 for (l = 0; l < num_separators; l++)
5304 {
5305 int dislocation = (l == (num_separators - 1)) ?
5306 final_spaces : standard_spaces;
5307 dislocation *= MB_CHAR2LEN(fillchar);
5308 char_u *start = stl_items[stl_separator_locations[l]].stl_start;
5309 char_u *seploc = start + dislocation;
5310 STRMOVE(seploc, start);
5311 for (s = start; s < seploc;)
5312 MB_CHAR2BYTES(fillchar, s);
5313
5314 for (int i = stl_separator_locations[l] + 1; i < itemcnt; i++)
5315 stl_items[i].stl_start += dislocation;
5316 }
5317
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 width = maxwidth;
5319 }
5320 }
5321
Bram Moolenaarc667da52019-11-30 20:52:27 +01005322 // Store the info about highlighting.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005323 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005325 *hltab = stl_hltab;
5326 sp = stl_hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 for (l = 0; l < itemcnt; l++)
5328 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005329 if (stl_items[l].stl_type == Highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005331 sp->start = stl_items[l].stl_start;
5332 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005333 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 }
5335 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005336 sp->start = NULL;
5337 sp->userhl = 0;
5338 }
5339
Bram Moolenaarc667da52019-11-30 20:52:27 +01005340 // Store the info about tab pages labels.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005341 if (tabtab != NULL)
5342 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005343 *tabtab = stl_tabtab;
5344 sp = stl_tabtab;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005345 for (l = 0; l < itemcnt; l++)
5346 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005347 if (stl_items[l].stl_type == TabPage)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005348 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005349 sp->start = stl_items[l].stl_start;
5350 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005351 sp++;
5352 }
5353 }
5354 sp->start = NULL;
5355 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 }
5357
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01005358 redraw_not_allowed = save_redraw_not_allowed;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02005359
Bram Moolenaar0e1f36f2022-02-15 16:17:44 +00005360 // A user function may reset KeyTyped, restore it.
5361 KeyTyped = save_KeyTyped;
5362
Luuk van Baal7b224fd2022-11-07 12:16:51 +00005363 // Check for an error. If there is one the display will be messed up and
5364 // might loop redrawing. Avoid that by making the corresponding option
5365 // empty.
5366 // TODO: find out why using called_emsg_before makes tests fail, does it
5367 // matter?
5368 // if (called_emsg > called_emsg_before)
5369 if (did_emsg > did_emsg_before)
5370 set_string_option_direct(opt_name, -1, (char_u *)"",
5371 OPT_FREE | opt_scope, SID_ERROR);
5372
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 return width;
5374}
Bram Moolenaarc667da52019-11-30 20:52:27 +01005375#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377/*
John Marriottec032de2025-04-10 21:34:19 +02005378 * Get relative cursor position in window into "buf[]", in the localized
Emir SARI971cd2b2023-04-29 12:09:53 +01005379 * percentage form like %99, 99%; using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 */
John Marriotta21240b2025-01-08 20:10:59 +01005381 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005382get_rel_pos(
5383 win_T *wp,
5384 char_u *buf,
5385 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005387 long above; // number of lines above window
5388 long below; // number of lines below window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389
Bram Moolenaarc667da52019-11-30 20:52:27 +01005390 if (buflen < 3) // need at least 3 chars for writing
John Marriotta21240b2025-01-08 20:10:59 +01005391 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 above = wp->w_topline - 1;
5393#ifdef FEAT_DIFF
5394 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02005395 if (wp->w_topline == 1 && wp->w_topfill >= 1)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005396 above = 0; // All buffer lines are displayed and there is an
5397 // indication of filler lines, that can be considered
5398 // seeing all lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399#endif
5400 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
5401 if (below <= 0)
John Marriottec032de2025-04-10 21:34:19 +02005402 return (int)vim_snprintf_safelen((char *)buf, buflen,
5403 "%s", (above == 0) ? _("All") : _("Bot"));
Emir SARI971cd2b2023-04-29 12:09:53 +01005404
John Marriottec032de2025-04-10 21:34:19 +02005405 if (above <= 0)
5406 return (int)vim_snprintf_safelen((char *)buf, buflen,
5407 "%s", _("Top"));
John Marriotta21240b2025-01-08 20:10:59 +01005408
John Marriottec032de2025-04-10 21:34:19 +02005409 // localized percentage value
5410 return (int)vim_snprintf_safelen((char *)buf, buflen,
5411 _("%2d%%"), calc_percentage(above, above + below));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413
5414/*
John Marriottec032de2025-04-10 21:34:19 +02005415 * Append (file 2 of 8) to "buf[]", if editing more than one file.
5416 * Return the number of characters appended.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005418 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005419append_arg_number(
5420 win_T *wp,
5421 char_u *buf,
John Marriottec032de2025-04-10 21:34:19 +02005422 size_t buflen,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005423 int add_file) // Add "file" before the arg number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005425 if (ARGCOUNT <= 1) // nothing to do
John Marriottec032de2025-04-10 21:34:19 +02005426 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427
Bram Moolenaara8490a42023-05-23 18:00:58 +01005428 char *msg;
5429 switch ((wp->w_arg_idx_invalid ? 1 : 0) + (add_file ? 2 : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 {
Bram Moolenaara8490a42023-05-23 18:00:58 +01005431 case 0: msg = _(" (%d of %d)"); break;
5432 case 1: msg = _(" ((%d) of %d)"); break;
5433 case 2: msg = _(" (file %d of %d)"); break;
5434 case 3: msg = _(" (file (%d) of %d)"); break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 }
Bram Moolenaara8490a42023-05-23 18:00:58 +01005436
John Marriottec032de2025-04-10 21:34:19 +02005437 return (int)vim_snprintf_safelen((char *)buf, buflen, msg,
Bram Moolenaara8490a42023-05-23 18:00:58 +01005438 wp->w_arg_idx + 1, ARGCOUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439}
5440
5441/*
5442 * If fname is not a full path, make it a full path.
5443 * Returns pointer to allocated memory (NULL for failure).
5444 */
5445 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005446fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447{
5448 /*
5449 * Force expanding the path always for Unix, because symbolic links may
5450 * mess up the full path name, even though it starts with a '/'.
5451 * Also expand when there is ".." in the file name, try to remove it,
5452 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00005453 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 * For MS-Windows also expand names like "longna~1" to "longname".
5455 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00005456#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 return FullName_save(fname, TRUE);
5458#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00005459 if (!vim_isAbsName(fname)
5460 || strstr((char *)fname, "..") != NULL
5461 || strstr((char *)fname, "//") != NULL
5462# ifdef BACKSLASH_IN_FILENAME
5463 || strstr((char *)fname, "\\\\") != NULL
5464# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005465# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00005467# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 )
5469 return FullName_save(fname, FALSE);
5470
5471 fname = vim_strsave(fname);
5472
Bram Moolenaar9b942202007-10-03 12:31:33 +00005473# ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01005474 if (fname != NULL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005475 fname_case(fname, 0); // set correct case for file name
Bram Moolenaar9b942202007-10-03 12:31:33 +00005476# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477
5478 return fname;
5479#endif
5480}
5481
5482/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005483 * Make "*ffname" a full file name, set "*sfname" to "*ffname" if not NULL.
5484 * "*ffname" becomes a pointer to allocated memory (or NULL).
5485 * When resolving a link both "*sfname" and "*ffname" will point to the same
5486 * allocated memory.
5487 * The "*ffname" and "*sfname" pointer values on call will not be freed.
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005488 * Note that the resulting "*ffname" pointer should be considered not allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005491fname_expand(
5492 buf_T *buf UNUSED,
5493 char_u **ffname,
5494 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005496 if (*ffname == NULL) // no file name given, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 return;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005498 if (*sfname == NULL) // no short file name given, use ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 *sfname = *ffname;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005500 *ffname = fix_fname(*ffname); // expand to full path
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501
5502#ifdef FEAT_SHORTCUT
5503 if (!buf->b_p_bin)
5504 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005505 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005507 // If the file name is a shortcut file, use the file it links to.
Bram Moolenaardce1e892019-02-10 23:18:53 +01005508 rfname = mch_resolve_path(*ffname, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005509 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 {
5511 vim_free(*ffname);
5512 *ffname = rfname;
5513 *sfname = rfname;
5514 }
5515 }
5516#endif
5517}
5518
5519/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 * Open a window for a number of buffers.
5521 */
5522 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005523ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524{
5525 buf_T *buf;
5526 win_T *wp, *wpnext;
5527 int split_ret = OK;
5528 int p_ea_save;
5529 int open_wins = 0;
5530 int r;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005531 int count; // Maximum number of windows to open.
5532 int all; // When TRUE also load inactive buffers.
Bram Moolenaare1004402020-10-24 20:49:43 +02005533 int had_tab = cmdmod.cmod_tab;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005534 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535
Bram Moolenaarc667da52019-11-30 20:52:27 +01005536 if (eap->addr_count == 0) // make as many windows as possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 count = 9999;
5538 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01005539 count = eap->line2; // make as many windows as specified
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5541 all = FALSE;
5542 else
5543 all = TRUE;
5544
Pavel Mayorove1121b12023-02-20 14:35:20 +00005545 // Stop Visual mode, the cursor and "VIsual" may very well be invalid after
5546 // switching to another buffer.
5547 reset_VIsual_and_resel();
5548
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 setpcmark();
5550
5551#ifdef FEAT_GUI
5552 need_mouse_correct = TRUE;
5553#endif
5554
5555 /*
5556 * Close superfluous windows (two windows for the same buffer).
5557 * Also close windows that are not full-width.
5558 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005559 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005560 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005561 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005563 tpnext = curtab->tp_next;
5564 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005566 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005567 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005568 || ((cmdmod.cmod_split & WSP_VERT)
5569 ? wp->w_height + wp->w_status_height < Rows - p_ch
5570 - tabline_height()
5571 : wp->w_width != Columns)
5572 || (had_tab > 0 && wp != firstwin))
5573 && !ONE_WINDOW
Christian Brabandt0a6e57b2024-08-15 22:15:28 +02005574 && !(win_locked(wp) || wp->w_buffer->b_locked > 0)
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005575 && !win_unlisted(wp))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005576 {
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005577 if (win_close(wp, FALSE) == FAIL)
5578 break;
5579 // Just in case an autocommand does something strange with
5580 // windows: start all over...
5581 wpnext = firstwin;
5582 tpnext = first_tabpage;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005583 open_wins = 0;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005584 }
5585 else
5586 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005588
Bram Moolenaarc667da52019-11-30 20:52:27 +01005589 // Without the ":tab" modifier only do the current tab page.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005590 if (had_tab == 0 || tpnext == NULL)
5591 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005592 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 }
5594
5595 /*
5596 * Go through the buffer list. When a buffer doesn't have a window yet,
5597 * open one. Otherwise move the window to the right position.
5598 * Watch out for autocommands that delete buffers or windows!
5599 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01005600 // Don't execute Win/Buf Enter/Leave autocommands here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 ++autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 win_enter(lastwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5605 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005606 // Check if this buffer needs a window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5608 continue;
5609
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005610 if (had_tab != 0)
5611 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005612 // With the ":tab" modifier don't move the window.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005613 if (buf->b_nwindows > 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005614 wp = lastwin; // buffer has a window, skip it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005615 else
5616 wp = NULL;
5617 }
5618 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005619 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005620 // Check if this buffer already has a window
Bram Moolenaar29323592016-07-24 22:04:11 +02005621 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005622 if (wp->w_buffer == buf)
5623 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005624 // If the buffer already has a window, move it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005625 if (wp != NULL)
5626 win_move_after(wp, curwin);
5627 }
5628
5629 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005631 bufref_T bufref;
5632
5633 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005634
Bram Moolenaarc667da52019-11-30 20:52:27 +01005635 // Split the window and put the buffer in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 p_ea_save = p_ea;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005637 p_ea = TRUE; // use space from all windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5639 ++open_wins;
5640 p_ea = p_ea_save;
5641 if (split_ret == FAIL)
5642 continue;
5643
Bram Moolenaarc667da52019-11-30 20:52:27 +01005644 // Open the buffer in this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005647 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005649 // autocommands deleted the buffer!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 swap_exists_action = SEA_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 break;
5652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 if (swap_exists_action == SEA_QUIT)
5654 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005655#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005656 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005657
Bram Moolenaarc667da52019-11-30 20:52:27 +01005658 // Reset the error/interrupt/exception state here so that
5659 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005660 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005661#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005662
Bram Moolenaarc667da52019-11-30 20:52:27 +01005663 // User selected Quit at ATTENTION prompt; close this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 win_close(curwin, TRUE);
5665 --open_wins;
5666 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005667 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005668
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005669#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005670 // Restore the error/interrupt/exception state if not
5671 // discarded by a new aborting error, interrupt, or uncaught
5672 // exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005673 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005674#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 }
5676 else
5677 handle_swap_exists(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 }
5679
5680 ui_breakcheck();
5681 if (got_int)
5682 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005683 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 break;
5685 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005686#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01005687 // Autocommands deleted the buffer or aborted script processing!!!
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005688 if (aborting())
5689 break;
5690#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01005691 // When ":tab" was used open a new tab for a new window repeatedly.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005692 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
Bram Moolenaare1004402020-10-24 20:49:43 +02005693 cmdmod.cmod_tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 --autocmd_no_enter;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005696 win_enter(firstwin, FALSE); // back to first window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698
5699 /*
5700 * Close superfluous windows.
5701 */
5702 for (wp = lastwin; open_wins > count; )
5703 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005704 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 || autowrite(wp->w_buffer, FALSE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 if (!win_valid(wp))
5707 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005708 // BufWrite Autocommands made the window invalid, start over
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 wp = lastwin;
5710 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005711 else if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005713 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 --open_wins;
5715 wp = lastwin;
5716 }
5717 else
5718 {
5719 wp = wp->w_prev;
5720 if (wp == NULL)
5721 break;
5722 }
5723 }
5724}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005727static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005728
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729/*
5730 * do_modelines() - process mode lines for the current file
5731 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005732 * "flags" can be:
5733 * OPT_WINONLY only set options local to window
5734 * OPT_NOWIN don't set options local to window
5735 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 * Returns immediately if the "ml" option isn't set.
5737 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005739do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005741 linenr_T lnum;
5742 int nmlines;
5743 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744
5745 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5746 return;
5747
Bram Moolenaarc667da52019-11-30 20:52:27 +01005748 // Disallow recursive entry here. Can happen when executing a modeline
5749 // triggers an autocommand, which reloads modelines with a ":do".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 if (entered)
5751 return;
5752
5753 ++entered;
Hu Jialun9dcd3492021-08-28 20:42:50 +02005754 for (lnum = 1; curbuf->b_p_ml && lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005756 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 nmlines = 0;
5758
Hu Jialun9dcd3492021-08-28 20:42:50 +02005759 for (lnum = curbuf->b_ml.ml_line_count; curbuf->b_p_ml && lnum > 0 && lnum > nmlines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005761 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 nmlines = 0;
5763 --entered;
5764}
5765
Bram Moolenaarc667da52019-11-30 20:52:27 +01005766#include "version.h" // for version number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767
5768/*
5769 * chk_modeline() - check a single line for a mode string
5770 * Return FAIL if an error encountered.
5771 */
5772 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005773chk_modeline(
5774 linenr_T lnum,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005775 int flags) // Same as for do_modelines().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776{
5777 char_u *s;
John Marriottec032de2025-04-10 21:34:19 +02005778 char_u *line_end; // point to the end of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 char_u *e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 int prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 int retval = OK;
ichizok7e5fe382023-04-15 13:17:50 +01005782 ESTACK_CHECK_DECLARATION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783
5784 prev = -1;
John Marriottec032de2025-04-10 21:34:19 +02005785 s = ml_get(lnum);
5786 line_end = s + ml_get_len(lnum);
5787 for (; *s != NUL; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 {
5789 if (prev == -1 || vim_isspace(prev))
5790 {
5791 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5792 || STRNCMP(s, "vi:", (size_t)3) == 0)
5793 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005794 // Accept both "vim" and "Vim".
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005795 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 {
John Marriottec032de2025-04-10 21:34:19 +02005797 int vers;
5798
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5800 e = s + 4;
5801 else
5802 e = s + 3;
5803 vers = getdigits(&e);
5804 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005805 && (s[0] != 'V'
5806 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 && (s[3] == ':'
Keith Thompson184f71c2024-01-04 21:19:04 +01005808 || (VIM_VERSION_100 >= vers && SAFE_isdigit(s[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 || (VIM_VERSION_100 < vers && s[3] == '<')
5810 || (VIM_VERSION_100 > vers && s[3] == '>')
5811 || (VIM_VERSION_100 == vers && s[3] == '=')))
5812 break;
5813 }
5814 }
5815 prev = *s;
5816 }
5817
5818 if (*s)
5819 {
John Marriottec032de2025-04-10 21:34:19 +02005820 size_t len;
5821 char_u *linecopy; // local copy of any modeline found
5822 int end;
5823
Bram Moolenaarc667da52019-11-30 20:52:27 +01005824 do // skip over "ex:", "vi:" or "vim:"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 ++s;
5826 while (s[-1] != ':');
5827
John Marriottec032de2025-04-10 21:34:19 +02005828 len = (size_t)(line_end - s); // remember the line length
5829 // so we can restore 'line_end'
5830 // after the copy
5831 s = linecopy = vim_strnsave(s, len); // copy the line, it will change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 if (linecopy == NULL)
5833 return FAIL;
5834
John Marriottec032de2025-04-10 21:34:19 +02005835 line_end = s + len; // restore 'line_end'
5836
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005837 // prepare for emsg()
5838 estack_push(ETYPE_MODELINE, (char_u *)"modelines", lnum);
ichizok7e5fe382023-04-15 13:17:50 +01005839 ESTACK_CHECK_SETUP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840
5841 end = FALSE;
5842 while (end == FALSE)
5843 {
5844 s = skipwhite(s);
5845 if (*s == NUL)
5846 break;
5847
5848 /*
5849 * Find end of set command: ':' or end of line.
5850 * Skip over "\:", replacing it with ":".
5851 */
5852 for (e = s; *e != ':' && *e != NUL; ++e)
5853 if (e[0] == '\\' && e[1] == ':')
John Marriottec032de2025-04-10 21:34:19 +02005854 {
5855 mch_memmove(e, e + 1, (size_t)(line_end - (e + 1)) + 1); // +1 for NUL
5856 --line_end;
5857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 if (*e == NUL)
5859 end = TRUE;
5860
5861 /*
5862 * If there is a "set" command, require a terminating ':' and
5863 * ignore the stuff after the ':'.
5864 * "vi:set opt opt opt: foo" -- foo not interpreted
5865 * "vi:opt opt opt: foo" -- foo interpreted
5866 * Accept "se" for compatibility with Elvis.
5867 */
5868 if (STRNCMP(s, "set ", (size_t)4) == 0
5869 || STRNCMP(s, "se ", (size_t)3) == 0)
5870 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005871 if (*e != ':') // no terminating ':'?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 break;
5873 end = TRUE;
John Marriottec032de2025-04-10 21:34:19 +02005874 s += (*(s + 2) == ' ') ? 3 : 4;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005876 *e = NUL; // truncate the set command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877
Bram Moolenaarc667da52019-11-30 20:52:27 +01005878 if (*s != NUL) // skip over an empty "::"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 {
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005880 int secure_save = secure;
John Marriottec032de2025-04-10 21:34:19 +02005881 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005882
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005883 current_sctx.sc_version = 1;
5884#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005885 current_sctx.sc_sid = SID_MODELINE;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005886 current_sctx.sc_seq = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005887 current_sctx.sc_lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005889
Bram Moolenaar5958f952018-11-20 04:25:21 +01005890 // Make sure no risky things are executed as a side effect.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01005891 secure = 1;
Bram Moolenaar5958f952018-11-20 04:25:21 +01005892
Bram Moolenaara3227e22006-03-08 21:32:40 +00005893 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar5958f952018-11-20 04:25:21 +01005894
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005895 secure = secure_save;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005896 current_sctx = save_current_sctx;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005897 if (retval == FAIL) // stop if error found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 break;
5899 }
John Marriottec032de2025-04-10 21:34:19 +02005900 s = (e == line_end) ? e : e + 1; // advance to next part
5901 // careful not to go off the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 }
5903
ichizok7e5fe382023-04-15 13:17:50 +01005904 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005905 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 vim_free(linecopy);
5907 }
5908 return retval;
5909}
5910
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005911/*
Bram Moolenaar91335e52018-08-01 17:53:12 +02005912 * Return TRUE if "buf" is a normal buffer, 'buftype' is empty.
5913 */
5914 int
5915bt_normal(buf_T *buf)
5916{
5917 return buf != NULL && buf->b_p_bt[0] == NUL;
5918}
5919
5920/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005921 * Return TRUE if "buf" is the quickfix buffer.
5922 */
5923 int
Bram Moolenaar340dafd2022-08-25 16:16:45 +01005924bt_quickfix(buf_T *buf UNUSED)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005925{
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005926#ifdef FEAT_QUICKFIX
Christian Brabandt6e60cf42023-09-03 21:43:46 +02005927 return buf != NULL && buf_valid(buf) && buf->b_p_bt[0] == 'q';
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005928#else
5929 return FALSE;
Bram Moolenaar113e1072019-01-20 15:30:40 +01005930#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005931}
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005932
5933/*
5934 * Return TRUE if "buf" is a terminal buffer.
5935 */
5936 int
Bram Moolenaar340dafd2022-08-25 16:16:45 +01005937bt_terminal(buf_T *buf UNUSED)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005938{
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005939#if defined(FEAT_TERMINAL)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005940 return buf != NULL && buf->b_p_bt[0] == 't';
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005941#else
5942 return FALSE;
Bram Moolenaar113e1072019-01-20 15:30:40 +01005943#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005944}
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005945
5946/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005947 * Return TRUE if "buf" is a help buffer.
5948 */
5949 int
5950bt_help(buf_T *buf)
5951{
5952 return buf != NULL && buf->b_help;
5953}
5954
5955/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02005956 * Return TRUE if "buf" is a prompt buffer.
5957 */
5958 int
5959bt_prompt(buf_T *buf)
5960{
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005961 return buf != NULL && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'r';
5962}
5963
Dominique Pelle748b3082022-01-08 12:41:16 +00005964#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005965/*
5966 * Return TRUE if "buf" is a buffer for a popup window.
5967 */
5968 int
5969bt_popup(buf_T *buf)
5970{
5971 return buf != NULL && buf->b_p_bt != NULL
5972 && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'o';
Bram Moolenaarf2732452018-06-03 14:47:35 +02005973}
Dominique Pelle748b3082022-01-08 12:41:16 +00005974#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005975
5976/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005977 * Return TRUE if "buf" is a "nofile", "acwrite", "terminal" or "prompt"
Bram Moolenaarc3126192022-08-26 12:58:17 +01005978 * buffer. This means the buffer name may not be a file name, at least not for
5979 * writing the buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005980 */
5981 int
Bram Moolenaar26910de2019-06-15 19:37:15 +02005982bt_nofilename(buf_T *buf)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005983{
5984 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5985 || buf->b_p_bt[0] == 'a'
Bram Moolenaarf2732452018-06-03 14:47:35 +02005986 || buf->b_p_bt[0] == 't'
5987 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005988}
5989
Bram Moolenaarc3126192022-08-26 12:58:17 +01005990/*
5991 * Return TRUE if "buf" is a "nofile", "quickfix", "terminal" or "prompt"
5992 * buffer. This means the buffer is not to be read from a file.
5993 */
5994 static int
5995bt_nofileread(buf_T *buf)
5996{
5997 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5998 || buf->b_p_bt[0] == 't'
5999 || buf->b_p_bt[0] == 'q'
6000 || buf->b_p_bt[0] == 'p');
6001}
6002
Dominique Pelle748b3082022-01-08 12:41:16 +00006003#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02006004/*
Bram Moolenaar26910de2019-06-15 19:37:15 +02006005 * Return TRUE if "buf" has 'buftype' set to "nofile".
6006 */
6007 int
6008bt_nofile(buf_T *buf)
6009{
6010 return buf != NULL && buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f';
6011}
Dominique Pelle748b3082022-01-08 12:41:16 +00006012#endif
Bram Moolenaar26910de2019-06-15 19:37:15 +02006013
6014/*
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01006015 * Return TRUE if "buf" is a "nowrite", "nofile", "terminal", "prompt", or
6016 * "popup" buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02006017 */
6018 int
6019bt_dontwrite(buf_T *buf)
6020{
Bram Moolenaarf2732452018-06-03 14:47:35 +02006021 return buf != NULL && (buf->b_p_bt[0] == 'n'
Bram Moolenaar8133cc62020-10-26 21:05:27 +01006022 || buf->b_p_bt[0] == 't'
6023 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02006024}
6025
6026 int
6027bt_dontwrite_msg(buf_T *buf)
6028{
6029 if (bt_dontwrite(buf))
6030 {
Bram Moolenaarf1474d82021-12-31 19:59:55 +00006031 emsg(_(e_cannot_write_buftype_option_is_set));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02006032 return TRUE;
6033 }
6034 return FALSE;
6035}
6036
6037/*
6038 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
6039 * and 'bufhidden'.
6040 */
6041 int
6042buf_hide(buf_T *buf)
6043{
Bram Moolenaarc667da52019-11-30 20:52:27 +01006044 // 'bufhidden' overrules 'hidden' and ":hide", check it first
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02006045 switch (buf->b_p_bh[0])
6046 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01006047 case 'u': // "unload"
6048 case 'w': // "wipe"
6049 case 'd': return FALSE; // "delete"
6050 case 'h': return TRUE; // "hide"
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02006051 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006052 return (p_hid || (cmdmod.cmod_flags & CMOD_HIDE));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02006053}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054
6055/*
6056 * Return special buffer name.
6057 * Returns NULL when the buffer has a normal file name.
6058 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006059 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006060buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061{
Bram Moolenaar4033c552017-09-16 20:54:51 +02006062#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006064 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006065 /*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01006066 * Differentiate between the quickfix and location list buffers using
6067 * the buffer number stored in the global quickfix stack.
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006068 */
Bram Moolenaaree8188f2019-02-05 21:23:04 +01006069 if (buf->b_fnum == qf_stack_get_bufnr())
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006070 return (char_u *)_(msg_qflist);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01006071 else
6072 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02006075
Bram Moolenaarc667da52019-11-30 20:52:27 +01006076 // There is no _file_ when 'buftype' is "nofile", b_sfname
6077 // contains the name as specified by the user.
Bram Moolenaar26910de2019-06-15 19:37:15 +02006078 if (bt_nofilename(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 {
Bram Moolenaar21554412017-07-24 21:44:43 +02006080#ifdef FEAT_TERMINAL
6081 if (buf->b_term != NULL)
6082 return term_get_status_text(buf->b_term);
6083#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02006084 if (buf->b_fname != NULL)
6085 return buf->b_fname;
Sean Dewar1fb41032023-08-16 17:15:05 +01006086 if (buf == cmdwin_buf)
6087 return (char_u *)_("[Command Line]");
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02006088#ifdef FEAT_JOB_CHANNEL
6089 if (bt_prompt(buf))
6090 return (char_u *)_("[Prompt]");
6091#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01006092#ifdef FEAT_PROP_POPUP
Bram Moolenaarc6896e22019-05-30 22:32:34 +02006093 if (bt_popup(buf))
6094 return (char_u *)_("[Popup]");
6095#endif
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006096 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 }
Bram Moolenaar21554412017-07-24 21:44:43 +02006098
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 if (buf->b_fname == NULL)
Bram Moolenaar00806bc2020-11-05 19:36:38 +01006100 return buf_get_fname(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 return NULL;
6102}
6103
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104/*
Bram Moolenaar00806bc2020-11-05 19:36:38 +01006105 * Get "buf->b_fname", use "[No Name]" if it is NULL.
6106 */
6107 char_u *
6108buf_get_fname(buf_T *buf)
6109{
6110 if (buf->b_fname == NULL)
6111 return (char_u *)_("[No Name]");
6112 return buf->b_fname;
6113}
6114
6115/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
6117 */
6118 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006119set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00006121 if (on == curbuf->b_p_bl)
6122 return;
6123
6124 curbuf->b_p_bl = on;
6125 if (on)
6126 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
6127 else
6128 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129}
6130
6131/*
6132 * Read the file for "buf" again and check if the contents changed.
6133 * Return TRUE if it changed or this could not be checked.
6134 */
6135 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006136buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137{
6138 buf_T *newbuf;
6139 int differ = TRUE;
6140 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 exarg_T ea;
6143
Bram Moolenaarc667da52019-11-30 20:52:27 +01006144 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6146 if (newbuf == NULL)
6147 return TRUE;
6148
Bram Moolenaarc667da52019-11-30 20:52:27 +01006149 // Force the 'fileencoding' and 'fileformat' to be equal.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 if (prep_exarg(&ea, buf) == FAIL)
6151 {
6152 wipe_buffer(newbuf, FALSE);
6153 return TRUE;
6154 }
6155
Bram Moolenaare76062c2022-11-28 18:51:43 +00006156 // Set curwin/curbuf to buf and save a few things.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00006158 if (curbuf != newbuf)
6159 {
6160 // Failed to find a window for "newbuf".
6161 wipe_buffer(newbuf, FALSE);
6162 return TRUE;
6163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164
Christian Brabandt41e6f7d2023-10-11 21:08:13 +02006165 // We don't want to trigger autocommands now, they may have nasty
6166 // side-effects like wiping buffers
6167 block_autocmds();
Bram Moolenaar4770d092006-01-12 23:22:24 +00006168 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 && readfile(buf->b_ffname, buf->b_fname,
6170 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
6171 &ea, READ_NEW | READ_DUMMY) == OK)
6172 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01006173 // compare the two files line by line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
6175 {
6176 differ = FALSE;
6177 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6178 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
6179 {
6180 differ = TRUE;
6181 break;
6182 }
6183 }
6184 }
6185 vim_free(ea.cmd);
6186
Bram Moolenaarc667da52019-11-30 20:52:27 +01006187 // restore curwin/curbuf and a few other things
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189
Bram Moolenaarc667da52019-11-30 20:52:27 +01006190 if (curbuf != newbuf) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 wipe_buffer(newbuf, FALSE);
6192
Christian Brabandt41e6f7d2023-10-11 21:08:13 +02006193 unblock_autocmds();
6194
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 return differ;
6196}
6197
6198/*
6199 * Wipe out a buffer and decrement the last buffer number if it was used for
6200 * this buffer. Call this to wipe out a temp buffer that does not contain any
6201 * marks.
6202 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006204wipe_buffer(
6205 buf_T *buf,
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006206 int aucmd) // When TRUE trigger autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207{
6208 if (buf->b_fnum == top_file_num - 1)
6209 --top_file_num;
6210
Bram Moolenaarc667da52019-11-30 20:52:27 +01006211 if (!aucmd) // Don't trigger BufDelete autocommands here.
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006212 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006213
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006214 close_buffer(NULL, buf, DOBUF_WIPE, FALSE, TRUE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006215
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006217 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218}