blob: eed3e8de13f5339e8d0787a3abb32c4a7ef49178 [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
75/*
John Marriottec032de2025-04-10 21:34:19 +020076 * Calculate the percentage that `part` is of the `whole`.
77 */
78 static int
79calc_percentage(long part, long whole)
80{
81 // With 32 bit longs and more than 21,474,836 lines multiplying by 100
82 // causes an overflow, thus for large numbers divide instead.
83 return (part > 1000000L)
84 ? (int)(part / (whole / 100L))
85 : (int)((part * 100L) / whole);
86}
87
88/*
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +020089 * Return the highest possible buffer number.
90 */
91 int
92get_highest_fnum(void)
93{
94 return top_file_num - 1;
95}
96
Bram Moolenaarc024b462019-06-08 18:07:21 +020097/*
98 * Read data from buffer for retrying.
99 */
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200100 static int
101read_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +0100102 int read_stdin, // read file from stdin, otherwise fifo
103 exarg_T *eap, // for forced 'ff' and 'fenc' or NULL
104 int flags) // extra flags for readfile()
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200105{
106 int retval = OK;
107 linenr_T line_count;
108
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100109 // Read from the buffer which the text is already filled in and append at
110 // the end. This makes it possible to retry when 'fileformat' or
111 // 'fileencoding' was guessed wrong.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200112 line_count = curbuf->b_ml.ml_line_count;
113 retval = readfile(
114 read_stdin ? NULL : curbuf->b_ffname,
115 read_stdin ? NULL : curbuf->b_fname,
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +0100116 line_count, (linenr_T)0, (linenr_T)MAXLNUM, eap,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200117 flags | READ_BUFFER);
118 if (retval == OK)
119 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100120 // Delete the binary lines.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200121 while (--line_count >= 0)
Bram Moolenaarca70c072020-05-30 20:30:46 +0200122 ml_delete((linenr_T)1);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200123 }
124 else
125 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100126 // Delete the converted lines.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200127 while (curbuf->b_ml.ml_line_count > line_count)
Bram Moolenaarca70c072020-05-30 20:30:46 +0200128 ml_delete(line_count);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200129 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100130 // Put the cursor on the first line.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200131 curwin->w_cursor.lnum = 1;
132 curwin->w_cursor.col = 0;
133
134 if (read_stdin)
135 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100136 // Set or reset 'modified' before executing autocommands, so that
137 // it can be changed there.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100138 if (!readonlymode && !BUFEMPTY())
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200139 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100140 else if (retval == OK)
Bram Moolenaarc024b462019-06-08 18:07:21 +0200141 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200142
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100143 if (retval == OK)
144 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100145#ifdef FEAT_EVAL
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100146 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100147 curbuf, &retval);
148#else
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100149 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200150#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100151 }
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200152 }
153 return retval;
154}
155
Dominique Pelle748b3082022-01-08 12:41:16 +0000156#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157/*
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200158 * Ensure buffer "buf" is loaded. Does not trigger the swap-exists action.
159 */
160 void
161buffer_ensure_loaded(buf_T *buf)
162{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +0000163 if (buf->b_ml.ml_mfp != NULL)
164 return;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200165
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +0000166 aco_save_T aco;
167
168 // Make sure the buffer is in a window. If not then skip it.
169 aucmd_prepbuf(&aco, buf);
170 if (curbuf == buf)
171 {
172 if (swap_exists_action != SEA_READONLY)
173 swap_exists_action = SEA_NONE;
174 open_buffer(FALSE, NULL, 0);
175 aucmd_restbuf(&aco);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200176 }
177}
Dominique Pelle748b3082022-01-08 12:41:16 +0000178#endif
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200179
180/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200181 * Open current buffer, that is: open the memfile and read the file into
182 * memory.
183 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 */
185 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100186open_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +0100187 int read_stdin, // read file from stdin
188 exarg_T *eap, // for forced 'ff' and 'fenc' or NULL
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100189 int flags_arg) // extra flags for readfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190{
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100191 int flags = flags_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 int retval = OK;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200193 bufref_T old_curbuf;
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100194#ifdef FEAT_SYN_HL
195 long old_tw = curbuf->b_p_tw;
196#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200197 int read_fifo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100199 // The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
200 // When re-entering the same buffer, it should not change, because the
201 // user may have reset the flag by hand.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 if (readonlymode && curbuf->b_ffname != NULL
203 && (curbuf->b_flags & BF_NEVERLOADED))
204 curbuf->b_p_ro = TRUE;
205
Bram Moolenaar4770d092006-01-12 23:22:24 +0000206 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100208 // There MUST be a memfile, otherwise we can't do anything
209 // If we can't create one for the current buffer, take another buffer
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100210 close_buffer(NULL, curbuf, 0, FALSE, FALSE);
Bram Moolenaar29323592016-07-24 22:04:11 +0200211 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212 if (curbuf->b_ml.ml_mfp != NULL)
213 break;
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100214 // If there is no memfile at all, exit.
215 // This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216 if (curbuf == NULL)
217 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +0000218 emsg(_(e_cannot_allocate_any_buffer_exiting));
Bram Moolenaar6f10c702019-08-20 22:58:37 +0200219
220 // Don't try to do any saving, with "curbuf" NULL almost nothing
221 // will work.
222 v_dying = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 getout(2);
224 }
Bram Moolenaar6f10c702019-08-20 22:58:37 +0200225
Bram Moolenaar40bcec12021-12-05 22:19:27 +0000226 emsg(_(e_cannot_allocate_buffer_using_other_one));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 enter_buffer(curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100228#ifdef FEAT_SYN_HL
229 if (old_tw != curbuf->b_p_tw)
Millya441a3e2024-10-22 22:43:01 +0200230 check_colorcolumn(NULL, curwin);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232 return FAIL;
233 }
234
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100235 // Do not sync this buffer yet, may first want to read the file.
236 if (curbuf->b_ml.ml_mfp != NULL)
237 curbuf->b_ml.ml_mfp->mf_dirty = MF_DIRTY_YES_NOSYNC;
238
Bram Moolenaarc667da52019-11-30 20:52:27 +0100239 // The autocommands in readfile() may change the buffer, but only AFTER
240 // reading the file.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200241 set_bufref(&old_curbuf, curbuf);
zeertzjq5bf6c212024-03-31 18:41:27 +0200242 curbuf->b_modified_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243
Bram Moolenaarc667da52019-11-30 20:52:27 +0100244 // mark cursor position as being invalid
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100245 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100247 // A buffer without an actual file should not use the buffer name to read a
248 // file.
Bram Moolenaarc3126192022-08-26 12:58:17 +0100249 if (bt_nofileread(curbuf))
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100250 flags |= READ_NOFILE;
251
Bram Moolenaar2eddbac2022-08-25 12:45:21 +0100252 // Read the file if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 if (curbuf->b_ffname != NULL
254#ifdef FEAT_NETBEANS_INTG
255 && netbeansReadFile
256#endif
257 )
258 {
Bram Moolenaar426dd022016-03-15 15:09:29 +0100259 int old_msg_silent = msg_silent;
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200260#ifdef UNIX
261 int save_bin = curbuf->b_p_bin;
262 int perm;
263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264#ifdef FEAT_NETBEANS_INTG
265 int oldFire = netbeansFireChanges;
266
267 netbeansFireChanges = 0;
268#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200269#ifdef UNIX
270 perm = mch_getperm(curbuf->b_ffname);
Bram Moolenaard569bb02018-08-11 13:57:20 +0200271 if (perm >= 0 && (S_ISFIFO(perm)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200272 || S_ISSOCK(perm)
Bram Moolenaarf04507d2016-08-20 15:05:39 +0200273# ifdef OPEN_CHR_FILES
274 || (S_ISCHR(perm) && is_dev_fd_file(curbuf->b_ffname))
275# endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200276 ))
277 read_fifo = TRUE;
278 if (read_fifo)
279 curbuf->b_p_bin = TRUE;
280#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100281 if (shortmess(SHM_FILEINFO))
282 msg_silent = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200284 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200285 flags | READ_NEW | (read_fifo ? READ_FIFO : 0));
286#ifdef UNIX
287 if (read_fifo)
288 {
289 curbuf->b_p_bin = save_bin;
290 if (retval == OK)
Christian Brabandtf1c31342025-02-23 09:36:56 +0100291 // don't add READ_FIFO here, otherwise we won't be able to
292 // detect the encoding
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200293 retval = read_buffer(FALSE, eap, flags);
294 }
295#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100296 msg_silent = old_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297#ifdef FEAT_NETBEANS_INTG
298 netbeansFireChanges = oldFire;
299#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100300 // Help buffer is filtered.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +0200301 if (bt_help(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302 fix_help_buffer();
303 }
304 else if (read_stdin)
305 {
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200306 int save_bin = curbuf->b_p_bin;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100308 // First read the text in binary mode into the buffer.
309 // Then read from that same buffer and append at the end. This makes
310 // it possible to retry when 'fileformat' or 'fileencoding' was
311 // guessed wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 curbuf->b_p_bin = TRUE;
313 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200314 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
315 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 curbuf->b_p_bin = save_bin;
317 if (retval == OK)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200318 retval = read_buffer(TRUE, eap, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319 }
320
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100321 // Can now sync this buffer in ml_sync_all().
322 if (curbuf->b_ml.ml_mfp != NULL
323 && curbuf->b_ml.ml_mfp->mf_dirty == MF_DIRTY_YES_NOSYNC)
324 curbuf->b_ml.ml_mfp->mf_dirty = MF_DIRTY_YES;
325
Bram Moolenaarc667da52019-11-30 20:52:27 +0100326 // if first time loading this buffer, init b_chartab[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 if (curbuf->b_flags & BF_NEVERLOADED)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100328 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 (void)buf_init_chartab(curbuf, FALSE);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100330 parse_cino(curbuf);
331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100333 // Set/reset the Changed flag first, autocmds may change the buffer.
334 // Apply the automatic commands, before processing the modelines.
335 // So the modelines have priority over autocommands.
336 //
Bram Moolenaarc667da52019-11-30 20:52:27 +0100337 // When reading stdin, the buffer contents always needs writing, so set
338 // the changed flag. Unless in readonly mode: "ls | gview -".
339 // When interrupted and 'cpoptions' contains 'i' set changed flag.
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000340 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
zeertzjq5bf6c212024-03-31 18:41:27 +0200341 || curbuf->b_modified_was_set // autocmd did ":set modified"
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100342#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000345 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100347 else if (retval == OK && !read_stdin && !read_fifo)
Bram Moolenaarc024b462019-06-08 18:07:21 +0200348 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaarc667da52019-11-30 20:52:27 +0100349 save_file_ff(curbuf); // keep this fileformat
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350
Bram Moolenaarc667da52019-11-30 20:52:27 +0100351 // Set last_changedtick to avoid triggering a TextChanged autocommand right
352 // after it was added.
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100353 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Christian Brabandtdb3b4462021-10-16 11:58:55 +0100354 curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100355 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100356
Bram Moolenaarc667da52019-11-30 20:52:27 +0100357 // require "!" to overwrite the file, because it wasn't read completely
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358#ifdef FEAT_EVAL
359 if (aborting())
360#else
361 if (got_int)
362#endif
363 curbuf->b_flags |= BF_READERR;
364
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000365#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +0100366 // Need to update automatic folding. Do this before the autocommands,
367 // they may use the fold info.
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000368 foldUpdateAll(curwin);
369#endif
370
Bram Moolenaarc667da52019-11-30 20:52:27 +0100371 // need to set w_topline, unless some autocommand already did that.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 if (!(curwin->w_valid & VALID_TOPLINE))
373 {
374 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100375#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100379#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100381#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383#endif
384
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000385 if (retval != OK)
386 return retval;
387
388 // The autocommands may have changed the current buffer. Apply the
389 // modelines to the correct buffer, if it still exists and is loaded.
390 if (bufref_valid(&old_curbuf) && old_curbuf.br_buf->b_ml.ml_mfp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000392 aco_save_T aco;
393
394 // Go to the buffer that was opened, make sure it is in a window.
395 // If not then skip it.
396 aucmd_prepbuf(&aco, old_curbuf.br_buf);
397 if (curbuf == old_curbuf.br_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000399 do_modelines(0);
400 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000402 if ((flags & READ_NOWINENTER) == 0)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100403#ifdef FEAT_EVAL
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000404 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL,
405 FALSE, curbuf, &retval);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100406#else
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000407 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL,
408 FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000411 // restore curwin/curbuf and a few other things
412 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 }
415
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 return retval;
417}
418
419/*
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200420 * Store "buf" in "bufref" and set the free count.
421 */
422 void
423set_bufref(bufref_T *bufref, buf_T *buf)
424{
425 bufref->br_buf = buf;
Bram Moolenaarfadacf02017-06-19 20:35:32 +0200426 bufref->br_fnum = buf == NULL ? 0 : buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200427 bufref->br_buf_free_count = buf_free_count;
428}
429
430/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200431 * Return TRUE if "bufref->br_buf" points to the same buffer as when
432 * set_bufref() was called and it is a valid buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200433 * Only goes through the buffer list if buf_free_count changed.
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200434 * Also checks if b_fnum is still the same, a :bwipe followed by :new might get
435 * the same allocated memory, but it's a different buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200436 */
437 int
438bufref_valid(bufref_T *bufref)
439{
440 return bufref->br_buf_free_count == buf_free_count
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200441 ? TRUE : buf_valid(bufref->br_buf)
442 && bufref->br_fnum == bufref->br_buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200443}
444
445/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200447 * This can be slow if there are many buffers, prefer using bufref_valid().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 */
449 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100450buf_valid(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451{
452 buf_T *bp;
453
Bram Moolenaarc667da52019-11-30 20:52:27 +0100454 // Assume that we more often have a recent buffer, start with the last
455 // one.
Bram Moolenaar00d253e2020-04-06 22:13:01 +0200456 FOR_ALL_BUFS_FROM_LAST(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 if (bp == buf)
458 return TRUE;
459 return FALSE;
460}
461
462/*
Bram Moolenaar480778b2016-07-14 22:09:39 +0200463 * A hash table used to quickly lookup a buffer by its number.
464 */
465static hashtab_T buf_hashtab;
466
467 static void
468buf_hashtab_add(buf_T *buf)
469{
John Marriottec032de2025-04-10 21:34:19 +0200470 vim_snprintf((char *)buf->b_key, sizeof(buf->b_key), "%x", buf->b_fnum);
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000471 if (hash_add(&buf_hashtab, buf->b_key, "create buffer") == FAIL)
Bram Moolenaarf1474d82021-12-31 19:59:55 +0000472 emsg(_(e_buffer_cannot_be_registered));
Bram Moolenaar480778b2016-07-14 22:09:39 +0200473}
474
475 static void
476buf_hashtab_remove(buf_T *buf)
477{
478 hashitem_T *hi = hash_find(&buf_hashtab, buf->b_key);
479
480 if (!HASHITEM_EMPTY(hi))
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000481 hash_remove(&buf_hashtab, hi, "close buffer");
Bram Moolenaar480778b2016-07-14 22:09:39 +0200482}
483
Bram Moolenaar94f01952018-09-01 15:30:03 +0200484/*
485 * Return TRUE when buffer "buf" can be unloaded.
486 * Give an error message and return FALSE when the buffer is locked or the
487 * screen is being redrawn and the buffer is in a window.
488 */
489 static int
490can_unload_buffer(buf_T *buf)
491{
492 int can_unload = !buf->b_locked;
493
494 if (can_unload && updating_screen)
495 {
496 win_T *wp;
497
498 FOR_ALL_WINDOWS(wp)
499 if (wp->w_buffer == buf)
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200500 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200501 can_unload = FALSE;
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200502 break;
503 }
Bram Moolenaar94f01952018-09-01 15:30:03 +0200504 }
505 if (!can_unload)
Bram Moolenaaref976322022-09-28 11:48:30 +0100506 {
507 char_u *fname = buf->b_fname != NULL ? buf->b_fname : buf->b_ffname;
508
509 semsg(_(e_attempt_to_delete_buffer_that_is_in_use_str),
510 fname != NULL ? fname : (char_u *)"[No Name]");
511 }
Bram Moolenaar94f01952018-09-01 15:30:03 +0200512 return can_unload;
513}
Bram Moolenaara997b452018-04-17 23:24:06 +0200514
Christian Brabandt51b62382024-10-06 17:31:10 +0200515 int
516buf_locked(buf_T *buf)
517{
518 return buf->b_locked || buf->b_locked_split;
519}
520
Bram Moolenaar480778b2016-07-14 22:09:39 +0200521/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 * Close the link to a buffer.
523 * "action" is used when there is no longer a window for the buffer.
524 * It can be:
525 * 0 buffer becomes hidden
526 * DOBUF_UNLOAD buffer is unloaded
zeertzjqd392a742023-07-01 20:24:40 +0100527 * DOBUF_DEL buffer is unloaded and removed from buffer list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 * DOBUF_WIPE buffer is unloaded and really deleted
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200529 * DOBUF_WIPE_REUSE idem, and add to buf_reuse list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 * When doing all but the first one on the current buffer, the caller should
531 * get a new buffer very soon!
532 *
533 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100534 *
535 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
536 * cause there to be only one window with this buffer. e.g. when ":quit" is
537 * supposed to close the window but autocommands close all other windows.
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100538 *
539 * When "ignore_abort" is TRUE don't abort even when aborting() returns TRUE.
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100540 *
541 * Return TRUE when we got to the end and b_nwindows was decremented.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 */
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100543 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100544close_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +0100545 win_T *win, // if not NULL, set b_last_cursor
Bram Moolenaar7454a062016-01-30 15:14:10 +0100546 buf_T *buf,
547 int action,
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100548 int abort_if_last,
549 int ignore_abort)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100552 int nwindows;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200553 bufref_T bufref;
Bram Moolenaar3a117e12016-10-30 21:57:52 +0100554 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200555 win_T *the_curwin = curwin;
556 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 int unload_buf = (action != 0);
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200558 int wipe_buf = (action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
559 int del_buf = (action == DOBUF_DEL || wipe_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560
Bram Moolenaarcee52202020-03-11 14:19:58 +0100561 CHECK_CURBUF;
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100562
563 // Force unloading or deleting when 'bufhidden' says so.
564 // The caller must take care of NOT deleting/freeing when 'bufhidden' is
565 // "hide" (otherwise we could never free or delete a buffer).
Bram Moolenaarc667da52019-11-30 20:52:27 +0100566 if (buf->b_p_bh[0] == 'd') // 'bufhidden' == "delete"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200567 {
568 del_buf = TRUE;
569 unload_buf = TRUE;
570 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100571 else if (buf->b_p_bh[0] == 'w') // 'bufhidden' == "wipe"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200572 {
573 del_buf = TRUE;
574 unload_buf = TRUE;
575 wipe_buf = TRUE;
576 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100577 else if (buf->b_p_bh[0] == 'u') // 'bufhidden' == "unload"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200578 unload_buf = TRUE;
579
Bram Moolenaar94053a52017-08-01 21:44:33 +0200580#ifdef FEAT_TERMINAL
Yee Cheng Chin42826332022-10-10 11:46:16 +0100581 // depending on how we get here b_nwindows may already be zero
582 if (bt_terminal(buf) && (buf->b_nwindows <= 1 || del_buf))
Bram Moolenaar94053a52017-08-01 21:44:33 +0200583 {
Bram Moolenaarcee52202020-03-11 14:19:58 +0100584 CHECK_CURBUF;
Bram Moolenaar94053a52017-08-01 21:44:33 +0200585 if (term_job_running(buf->b_term))
586 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200587 if (wipe_buf || unload_buf)
Bram Moolenaara997b452018-04-17 23:24:06 +0200588 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200589 if (!can_unload_buffer(buf))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100590 return FALSE;
Bram Moolenaar94f01952018-09-01 15:30:03 +0200591
Bram Moolenaarc667da52019-11-30 20:52:27 +0100592 // Wiping out or unloading a terminal buffer kills the job.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200593 free_terminal(buf);
Yee Cheng Chin42826332022-10-10 11:46:16 +0100594
595 // A terminal buffer is wiped out when job has finished.
596 del_buf = TRUE;
597 unload_buf = TRUE;
598 wipe_buf = TRUE;
Bram Moolenaara997b452018-04-17 23:24:06 +0200599 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200600 else
601 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100602 // The job keeps running, hide the buffer.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200603 del_buf = FALSE;
604 unload_buf = FALSE;
605 }
606 }
Bram Moolenaarc9f8b842020-11-24 19:36:16 +0100607 else if (buf->b_p_bh[0] == 'h' && !del_buf)
608 {
609 // Hide a terminal buffer.
610 unload_buf = FALSE;
611 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200612 else
613 {
Yee Cheng Chin42826332022-10-10 11:46:16 +0100614 if (del_buf || unload_buf)
615 {
616 // A terminal buffer is wiped out if the job has finished.
617 // We only do this when there's an intention to unload the
618 // buffer. This way, :hide and other similar commands won't
619 // wipe the buffer.
620 del_buf = TRUE;
621 unload_buf = TRUE;
622 wipe_buf = TRUE;
623 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200624 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100625 CHECK_CURBUF;
Bram Moolenaar94053a52017-08-01 21:44:33 +0200626 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200627#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628
Bram Moolenaarc667da52019-11-30 20:52:27 +0100629 // Disallow deleting the buffer when it is locked (already being closed or
630 // halfway a command that relies on it). Unloading is allowed.
Bram Moolenaar94f01952018-09-01 15:30:03 +0200631 if ((del_buf || wipe_buf) && !can_unload_buffer(buf))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100632 return FALSE;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200633
Bram Moolenaarc667da52019-11-30 20:52:27 +0100634 // check no autocommands closed the window
Bram Moolenaar4033c552017-09-16 20:54:51 +0200635 if (win != NULL && win_valid_any_tab(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100637 // Set b_last_cursor when closing the last window for the buffer.
638 // Remember the last cursor position and window options of the buffer.
639 // This used to be only for the current window, but then options like
640 // 'foldmethod' may be lost with a ":only" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 if (buf->b_nwindows == 1)
642 set_last_cursor(win);
643 buflist_setfpos(buf, win,
644 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
645 win->w_cursor.col, TRUE);
646 }
647
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200648 set_bufref(&bufref, buf);
649
Bram Moolenaarc667da52019-11-30 20:52:27 +0100650 // When the buffer is no longer in a window, trigger BufWinLeave
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 if (buf->b_nwindows == 1)
652 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200653 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100654 ++buf->b_locked_split;
Bram Moolenaar82404332016-07-10 17:00:38 +0200655 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
656 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200657 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100658 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100659 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200660aucmd_abort:
Bram Moolenaarf1474d82021-12-31 19:59:55 +0000661 emsg(_(e_autocommands_caused_command_to_abort));
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100662 return FALSE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100663 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200664 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100665 --buf->b_locked_split;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200666 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100667 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200668 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669
Bram Moolenaarc667da52019-11-30 20:52:27 +0100670 // When the buffer becomes hidden, but is not unloaded, trigger
671 // BufHidden
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 if (!unload_buf)
673 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200674 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100675 ++buf->b_locked_split;
Bram Moolenaar82404332016-07-10 17:00:38 +0200676 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
677 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200678 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100679 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200680 goto aucmd_abort;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200681 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100682 --buf->b_locked_split;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200683 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100684 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200685 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100687#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100688 // autocmds may abort script processing
689 if (!ignore_abort && aborting())
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100690 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200693
Bram Moolenaarc667da52019-11-30 20:52:27 +0100694 // If the buffer was in curwin and the window has changed, go back to that
695 // window, if it still exists. This avoids that ":edit x" triggering a
696 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200697 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
698 {
699 block_autocmds();
700 goto_tabpage_win(the_curtab, the_curwin);
701 unblock_autocmds();
702 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200703
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 nwindows = buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705
Bram Moolenaarc667da52019-11-30 20:52:27 +0100706 // decrease the link count from windows (unless not in any window)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 if (buf->b_nwindows > 0)
708 --buf->b_nwindows;
709
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100710#ifdef FEAT_DIFF
711 if (diffopt_hiddenoff() && !unload_buf && buf->b_nwindows == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100712 diff_buf_delete(buf); // Clear 'diff' for hidden buffer.
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100713#endif
714
Bram Moolenaarc667da52019-11-30 20:52:27 +0100715 // Return when a window is displaying the buffer or when it's not
716 // unloaded.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100718 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719
Bram Moolenaarc667da52019-11-30 20:52:27 +0100720 // Always remove the buffer when there is no file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 if (buf->b_ffname == NULL)
722 del_buf = TRUE;
723
Bram Moolenaarc667da52019-11-30 20:52:27 +0100724 // When closing the current buffer stop Visual mode before freeing
725 // anything.
Bram Moolenaar4930a762016-09-11 14:39:53 +0200726 if (buf == curbuf && VIsual_active
Bram Moolenaar9a27c7f2016-09-09 12:57:09 +0200727#if defined(EXITFREE)
728 && !entered_free_all_mem
729#endif
730 )
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200731 end_visual_mode();
732
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100733 // Free all things allocated for this buffer.
734 // Also calls the "BufDelete" autocommands when del_buf is TRUE.
735 //
Bram Moolenaarc667da52019-11-30 20:52:27 +0100736 // Remember if we are closing the current buffer. Restore the number of
737 // windows, so that autocommands in buf_freeall() don't get confused.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 is_curbuf = (buf == curbuf);
739 buf->b_nwindows = nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100741 buf_freeall(buf, (del_buf ? BFA_DEL : 0)
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100742 + (wipe_buf ? BFA_WIPE : 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100743 + (ignore_abort ? BFA_IGNORE_ABORT : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744
Bram Moolenaarc667da52019-11-30 20:52:27 +0100745 // Autocommands may have deleted the buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200746 if (!bufref_valid(&bufref))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100747 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100748#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100749 // autocmds may abort script processing
750 if (!ignore_abort && aborting())
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100751 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100754 // It's possible that autocommands change curbuf to the one being deleted.
755 // This might cause the previous curbuf to be deleted unexpectedly. But
756 // in some cases it's OK to delete the curbuf, because a new one is
757 // obtained anyway. Therefore only return if curbuf changed to the
758 // deleted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 if (buf == curbuf && !is_curbuf)
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100760 return FALSE;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200761
Bram Moolenaar4033c552017-09-16 20:54:51 +0200762 if (win_valid_any_tab(win) && win->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100763 win->w_buffer = NULL; // make sure we don't use the buffer now
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200764
Bram Moolenaarc667da52019-11-30 20:52:27 +0100765 // Autocommands may have opened or closed windows for this buffer.
766 // Decrement the count for the close we do here.
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200767 if (buf->b_nwindows > 0)
768 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 /*
771 * Remove the buffer from the list.
772 */
773 if (wipe_buf)
774 {
zeertzjq2e7d89b2024-07-10 19:36:36 +0200775 tabpage_T *tp;
776 win_T *wp;
LemonBoy4ff3a9b2024-07-09 20:03:24 +0200777
Bram Moolenaar347538f2022-03-26 16:28:06 +0000778 // Do not wipe out the buffer if it is used in a window.
779 if (buf->b_nwindows > 0)
780 return FALSE;
781
zeertzjq2e7d89b2024-07-10 19:36:36 +0200782 FOR_ALL_TAB_WINDOWS(tp, wp)
LemonBoy4ff3a9b2024-07-09 20:03:24 +0200783 mark_forget_file(wp, buf->b_fnum);
784
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200785 if (action == DOBUF_WIPE_REUSE)
786 {
787 // we can re-use this buffer number, store it
788 if (buf_reuse.ga_itemsize == 0)
789 ga_init2(&buf_reuse, sizeof(int), 50);
790 if (ga_grow(&buf_reuse, 1) == OK)
791 ((int *)buf_reuse.ga_data)[buf_reuse.ga_len++] = buf->b_fnum;
792 }
Bram Moolenaar3d6014f2018-10-11 19:27:47 +0200793 if (buf->b_sfname != buf->b_ffname)
794 VIM_CLEAR(buf->b_sfname);
795 else
796 buf->b_sfname = NULL;
797 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 if (buf->b_prev == NULL)
799 firstbuf = buf->b_next;
800 else
801 buf->b_prev->b_next = buf->b_next;
802 if (buf->b_next == NULL)
803 lastbuf = buf->b_prev;
804 else
805 buf->b_next->b_prev = buf->b_prev;
806 free_buffer(buf);
807 }
808 else
809 {
810 if (del_buf)
811 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100812 // Free all internal variables and reset option values, to make
813 // ":bdel" compatible with Vim 5.7.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 free_buffer_stuff(buf, TRUE);
815
Bram Moolenaarc667da52019-11-30 20:52:27 +0100816 // Make it look like a new buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
818
Bram Moolenaarc667da52019-11-30 20:52:27 +0100819 // Init the options when loaded again.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 buf->b_p_initialized = FALSE;
821 }
822 buf_clear_file(buf);
823 if (del_buf)
824 buf->b_p_bl = FALSE;
825 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100826 // NOTE: at this point "curbuf" may be invalid!
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100827 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828}
829
830/*
831 * Make buffer not contain a file.
832 */
833 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100834buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835{
836 buf->b_ml.ml_line_count = 1;
Bram Moolenaarc024b462019-06-08 18:07:21 +0200837 unchanged(buf, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 buf->b_shortname = FALSE;
Bram Moolenaar15775372022-10-29 20:01:52 +0100839 buf->b_p_eof = FALSE;
840 buf->b_start_eof = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 buf->b_p_eol = TRUE;
842 buf->b_start_eol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000844 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 buf->b_ml.ml_mfp = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +0100846 buf->b_ml.ml_flags = ML_EMPTY; // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847#ifdef FEAT_NETBEANS_INTG
848 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849#endif
850}
851
852/*
853 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200854 * the file. Careful: get here with "curwin" NULL when exiting.
855 * flags:
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100856 * BFA_DEL buffer is going to be deleted
857 * BFA_WIPE buffer is going to be wiped out
858 * BFA_KEEP_UNDO do not free undo information
859 * BFA_IGNORE_ABORT don't abort even when aborting() returns TRUE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100862buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200865 bufref_T bufref;
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200866 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaar5a497892016-09-03 16:29:04 +0200867 win_T *the_curwin = curwin;
868 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869
Bram Moolenaarc667da52019-11-30 20:52:27 +0100870 // Make sure the buffer isn't closed by autocommands.
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200871 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100872 ++buf->b_locked_split;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200873 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200874 if (buf->b_ml.ml_mfp != NULL)
875 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200876 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
877 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200878 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100879 // autocommands deleted the buffer
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200880 return;
881 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200882 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200884 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
885 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200886 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100887 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 return;
889 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200890 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200892 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
893 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200894 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100895 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 return;
897 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200898 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100899 --buf->b_locked_split;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200900
Bram Moolenaarc667da52019-11-30 20:52:27 +0100901 // If the buffer was in curwin and the window has changed, go back to that
902 // window, if it still exists. This avoids that ":edit x" triggering a
903 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaar5a497892016-09-03 16:29:04 +0200904 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
905 {
906 block_autocmds();
907 goto_tabpage_win(the_curtab, the_curwin);
908 unblock_autocmds();
909 }
Bram Moolenaar5a497892016-09-03 16:29:04 +0200910
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100911#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100912 // autocmds may abort script processing
913 if ((flags & BFA_IGNORE_ABORT) == 0 && aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100917 // It's possible that autocommands change curbuf to the one being deleted.
918 // This might cause curbuf to be deleted unexpectedly. But in some cases
919 // it's OK to delete the curbuf, because a new one is obtained anyway.
920 // Therefore only return if curbuf changed to the deleted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 if (buf == curbuf && !is_curbuf)
922 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923#ifdef FEAT_DIFF
Bram Moolenaarc667da52019-11-30 20:52:27 +0100924 diff_buf_delete(buf); // Can't use 'diff' for unloaded buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200926#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100927 // Remove any ownsyntax, unless exiting.
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200928 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaar89c71222011-11-30 15:40:56 +0100929 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200930#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000931
932#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +0100933 // No folds in an empty buffer.
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000934 {
935 win_T *win;
936 tabpage_T *tp;
937
938 FOR_ALL_TAB_WINDOWS(tp, win)
939 if (win->w_buffer == buf)
940 clearFolding(win);
941 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000942#endif
943
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944#ifdef FEAT_TCL
945 tcl_buffer_free(buf);
946#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100947 ml_close(buf, TRUE); // close and delete the memline/memfile
948 buf->b_ml.ml_line_count = 0; // no lines in buffer
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200949 if ((flags & BFA_KEEP_UNDO) == 0)
Christian Brabandt9071ed82024-02-15 20:17:37 +0100950 // free the memory allocated for undo
951 // and reset all undo information
952 u_clearallandblockfree(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100954 syntax_clear(&buf->b_s); // reset syntax info
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100956#ifdef FEAT_PROP_POPUP
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100957 clear_buf_prop_types(buf);
958#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100959 buf->b_flags &= ~BF_READERR; // a read error is no longer relevant
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960}
961
962/*
963 * Free a buffer structure and the things it contains related to the buffer
964 * itself (not the file, that must have been done already).
965 */
966 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100967free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200969 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200971#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100972 // b:changedtick uses an item in buf_T, remove it now
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000973 dictitem_remove(buf->b_vars, (dictitem_T *)&buf->b_ct_di, "free buffer");
Bram Moolenaar429fa852013-04-15 12:27:36 +0200974 unref_var_dict(buf->b_vars);
Bram Moolenaar86173482019-10-01 17:02:16 +0200975 remove_listeners(buf);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200976#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200977#ifdef FEAT_LUA
978 lua_buffer_free(buf);
979#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000980#ifdef FEAT_MZSCHEME
981 mzscheme_buffer_free(buf);
982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983#ifdef FEAT_PERL
984 perl_buf_free(buf);
985#endif
986#ifdef FEAT_PYTHON
987 python_buffer_free(buf);
988#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200989#ifdef FEAT_PYTHON3
990 python3_buffer_free(buf);
991#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992#ifdef FEAT_RUBY
993 ruby_buffer_free(buf);
994#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200995#ifdef FEAT_JOB_CHANNEL
996 channel_buffer_free(buf);
997#endif
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200998#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +0200999 free_terminal(buf);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001000#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02001001#ifdef FEAT_JOB_CHANNEL
1002 vim_free(buf->b_prompt_text);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001003 free_callback(&buf->b_prompt_callback);
Bram Moolenaar86173482019-10-01 17:02:16 +02001004 free_callback(&buf->b_prompt_interrupt);
Bram Moolenaarf2732452018-06-03 14:47:35 +02001005#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +02001006
1007 buf_hashtab_remove(buf);
1008
Bram Moolenaar9280e3f2016-07-14 23:03:19 +02001009 aubuflocal_remove(buf);
1010
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001011 if (autocmd_busy)
1012 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001013 // Do not free the buffer structure while autocommands are executing,
1014 // it's still needed. Free it when autocmd_busy is reset.
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001015 buf->b_next = au_pending_free_buf;
1016 au_pending_free_buf = buf;
1017 }
1018 else
Bram Moolenaarcee52202020-03-11 14:19:58 +01001019 {
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001020 vim_free(buf);
Bram Moolenaarcee52202020-03-11 14:19:58 +01001021 if (curbuf == buf)
1022 curbuf = NULL; // make clear it's not to be used
1023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024}
1025
1026/*
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001027 * Initializes b:changedtick.
Bram Moolenaar79518e22017-02-17 16:31:35 +01001028 */
1029 static void
1030init_changedtick(buf_T *buf)
1031{
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001032 dictitem_T *di = (dictitem_T *)&buf->b_ct_di;
Bram Moolenaar79518e22017-02-17 16:31:35 +01001033
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001034 di->di_flags = DI_FLAGS_FIX | DI_FLAGS_RO;
1035 di->di_tv.v_type = VAR_NUMBER;
1036 di->di_tv.v_lock = VAR_FIXED;
1037 di->di_tv.vval.v_number = 0;
1038
Bram Moolenaar92769c32017-02-25 15:41:37 +01001039#ifdef FEAT_EVAL
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001040 STRCPY(buf->b_ct_di.di_key, "changedtick");
1041 (void)dict_add(buf->b_vars, di);
Bram Moolenaar92769c32017-02-25 15:41:37 +01001042#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01001043}
1044
1045/*
Bram Moolenaarc3126192022-08-26 12:58:17 +01001046 * Free the b_wininfo list for buffer "buf".
1047 */
1048 static void
1049clear_wininfo(buf_T *buf)
1050{
1051 wininfo_T *wip;
1052
1053 while (buf->b_wininfo != NULL)
1054 {
1055 wip = buf->b_wininfo;
1056 buf->b_wininfo = wip->wi_next;
1057 free_wininfo(wip);
1058 }
1059}
1060
1061/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
1063 */
1064 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001065free_buffer_stuff(
1066 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001067 int free_options) // free options as well
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068{
1069 if (free_options)
1070 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001071 clear_wininfo(buf); // including window-local options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +02001073#ifdef FEAT_SPELL
1074 ga_clear(&buf->b_s.b_langp);
1075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 }
1077#ifdef FEAT_EVAL
Bram Moolenaar79518e22017-02-17 16:31:35 +01001078 {
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001079 varnumber_T tick = CHANGEDTICK(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001080
Bram Moolenaarc667da52019-11-30 20:52:27 +01001081 vars_clear(&buf->b_vars->dv_hashtab); // free all buffer variables
Bram Moolenaar79518e22017-02-17 16:31:35 +01001082 hash_init(&buf->b_vars->dv_hashtab);
1083 init_changedtick(buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001084 CHANGEDTICK(buf) = tick;
Bram Moolenaarf10997a2020-01-03 21:00:02 +01001085 remove_listeners(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087#endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001088 uc_clear(&buf->b_ucmds); // clear local user commands
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089#ifdef FEAT_SIGNS
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001090 buf_delete_signs(buf, (char_u *)"*"); // delete any signs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00001092#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001093 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00001094#endif
Bram Moolenaar0c740e72022-07-25 19:07:04 +01001095#ifdef FEAT_PROP_POPUP
1096 ga_clear_strings(&buf->b_textprop_text);
1097#endif
zeertzjqc207fd22022-06-29 10:37:40 +01001098 map_clear_mode(buf, MAP_ALL_MODES, TRUE, FALSE); // clear local mappings
1099 map_clear_mode(buf, MAP_ALL_MODES, TRUE, TRUE); // clear local abbrevs
Bram Moolenaard23a8232018-02-10 18:45:26 +01001100 VIM_CLEAR(buf->b_start_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101}
1102
1103/*
Bram Moolenaar4882d982020-10-25 17:55:09 +01001104 * Free one wininfo_T.
1105 */
1106 void
1107free_wininfo(wininfo_T *wip)
1108{
1109 if (wip->wi_optset)
1110 {
1111 clear_winopt(&wip->wi_opt);
1112#ifdef FEAT_FOLDING
1113 deleteFoldRecurse(&wip->wi_folds);
1114#endif
1115 }
1116 vim_free(wip);
1117}
1118
1119/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 * Go to another buffer. Handles the result of the ATTENTION dialog.
1121 */
1122 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001123goto_buffer(
1124 exarg_T *eap,
1125 int start,
1126 int dir,
1127 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001129 bufref_T old_curbuf;
Bram Moolenaar188639d2022-04-04 16:57:21 +01001130 int save_sea = swap_exists_action;
LemonBoy893eeeb2024-07-10 20:20:48 +02001131 int skip_help_buf;
1132
1133 switch (eap->cmdidx)
1134 {
1135 case CMD_bnext:
1136 case CMD_sbnext:
1137 case CMD_bNext:
1138 case CMD_bprevious:
1139 case CMD_sbNext:
1140 case CMD_sbprevious:
1141 skip_help_buf = TRUE;
1142 break;
1143 default:
1144 skip_help_buf = FALSE;
1145 break;
1146 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001147
1148 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149
Bram Moolenaar188639d2022-04-04 16:57:21 +01001150 if (swap_exists_action == SEA_NONE)
1151 swap_exists_action = SEA_DIALOG;
LemonBoy893eeeb2024-07-10 20:20:48 +02001152 (void)do_buffer_ext(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO, start, dir, count,
1153 (eap->forceit ? DOBUF_FORCEIT : 0) |
1154 (skip_help_buf ? DOBUF_SKIPHELP : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
1156 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001157#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001158 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001159
Bram Moolenaarc667da52019-11-30 20:52:27 +01001160 // Reset the error/interrupt/exception state here so that
1161 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001162 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001163#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001164
Bram Moolenaarc667da52019-11-30 20:52:27 +01001165 // Quitting means closing the split window, nothing else.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 win_close(curwin, TRUE);
Bram Moolenaar188639d2022-04-04 16:57:21 +01001167 swap_exists_action = save_sea;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001168 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001169
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001170#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001171 // Restore the error/interrupt/exception state if not discarded by a
1172 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001173 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 }
1176 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001177 handle_swap_exists(&old_curbuf);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001178}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180/*
1181 * Handle the situation of swap_exists_action being set.
1182 * It is allowed for "old_curbuf" to be NULL or invalid.
1183 */
1184 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001185handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186{
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001187#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001188 cleanup_T cs;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001189#endif
1190#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001191 long old_tw = curbuf->b_p_tw;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001192#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001193 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001194
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 if (swap_exists_action == SEA_QUIT)
1196 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001197#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001198 // Reset the error/interrupt/exception state here so that
1199 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001200 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001201#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001202
Bram Moolenaarc667da52019-11-30 20:52:27 +01001203 // User selected Quit at ATTENTION prompt. Go back to previous
1204 // buffer. If that buffer is gone or the same as the current one,
1205 // open a new, empty buffer.
1206 swap_exists_action = SEA_NONE; // don't want it again
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001207 swap_exists_did_quit = TRUE;
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001208 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001209 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1210 || old_curbuf->br_buf == curbuf)
Bram Moolenaar1d97efc2021-07-04 13:27:11 +02001211 {
1212 // Block autocommands here because curwin->w_buffer is NULL.
1213 block_autocmds();
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001214 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar1d97efc2021-07-04 13:27:11 +02001215 unblock_autocmds();
1216 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001217 else
1218 buf = old_curbuf->br_buf;
1219 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001220 {
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001221 int old_msg_silent = msg_silent;
1222
1223 if (shortmess(SHM_FILEINFO))
1224 msg_silent = 1; // prevent fileinfo message
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001225 enter_buffer(buf);
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001226 // restore msg_silent, so that the command line will be shown
1227 msg_silent = old_msg_silent;
1228
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001229#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001230 if (old_tw != curbuf->b_p_tw)
Millya441a3e2024-10-22 22:43:01 +02001231 check_colorcolumn(NULL, curwin);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001232#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001233 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001234 // If "old_curbuf" is NULL we are in big trouble here...
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001235
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001236#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001237 // Restore the error/interrupt/exception state if not discarded by a
1238 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001239 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 }
1242 else if (swap_exists_action == SEA_RECOVER)
1243 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001244#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001245 // Reset the error/interrupt/exception state here so that
1246 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001247 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001248#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001249
Bram Moolenaarc667da52019-11-30 20:52:27 +01001250 // User selected Recover at ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 msg_scroll = TRUE;
Bram Moolenaar99499b12019-05-23 21:35:48 +02001252 ml_recover(FALSE);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001253 msg_puts("\n"); // don't overwrite the last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001255 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001256
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001257#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001258 // Restore the error/interrupt/exception state if not discarded by a
1259 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001260 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 }
1263 swap_exists_action = SEA_NONE;
1264}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266/*
Bram Moolenaara02471e2014-01-10 16:43:14 +01001267 * Make the current buffer empty.
1268 * Used when it is wiped out and it's the last buffer.
1269 */
1270 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001271empty_curbuf(
1272 int close_others,
1273 int forceit,
1274 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001275{
1276 int retval;
1277 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001278 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001279
1280 if (action == DOBUF_UNLOAD)
1281 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001282 emsg(_(e_cannot_unload_last_buffer));
Bram Moolenaara02471e2014-01-10 16:43:14 +01001283 return FAIL;
1284 }
1285
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001286 set_bufref(&bufref, buf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001287 if (close_others)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001288 // Close any other windows on this buffer, then make it empty.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001289 close_windows(buf, TRUE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001290
1291 setpcmark();
1292 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1293 forceit ? ECMD_FORCEIT : 0, curwin);
1294
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001295 // do_ecmd() may create a new buffer, then we have to delete
1296 // the old one. But do_ecmd() may have done that already, check
1297 // if the buffer still exists.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001298 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001299 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001300 if (!close_others)
1301 need_fileinfo = FALSE;
1302 return retval;
1303}
Bram Moolenaar94f01952018-09-01 15:30:03 +02001304
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305/*
1306 * Implementation of the commands for the buffer list.
1307 *
1308 * action == DOBUF_GOTO go to specified buffer
1309 * action == DOBUF_SPLIT split window and go to specified buffer
1310 * action == DOBUF_UNLOAD unload specified buffer(s)
1311 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1312 * action == DOBUF_WIPE delete specified buffer(s) really
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001313 * action == DOBUF_WIPE_REUSE idem, and add number to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 *
1315 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1316 * start == DOBUF_FIRST go to "count" buffer from first buffer
1317 * start == DOBUF_LAST go to "count" buffer from last buffer
1318 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1319 *
1320 * Return FAIL or OK.
1321 */
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001322 static int
1323do_buffer_ext(
Bram Moolenaar7454a062016-01-30 15:14:10 +01001324 int action,
1325 int start,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001326 int dir, // FORWARD or BACKWARD
1327 int count, // buffer number or number of buffers
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001328 int flags) // DOBUF_FORCEIT etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329{
1330 buf_T *buf;
1331 buf_T *bp;
1332 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001333 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334
1335 switch (start)
1336 {
1337 case DOBUF_FIRST: buf = firstbuf; break;
1338 case DOBUF_LAST: buf = lastbuf; break;
1339 default: buf = curbuf; break;
1340 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001341 if (start == DOBUF_MOD) // find next modified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 {
1343 while (count-- > 0)
1344 {
1345 do
1346 {
1347 buf = buf->b_next;
1348 if (buf == NULL)
1349 buf = firstbuf;
1350 }
1351 while (buf != curbuf && !bufIsChanged(buf));
1352 }
1353 if (!bufIsChanged(buf))
1354 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001355 emsg(_(e_no_modified_buffer_found));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 return FAIL;
1357 }
1358 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001359 else if (start == DOBUF_FIRST && count) // find specified buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 {
1361 while (buf != NULL && buf->b_fnum != count)
1362 buf = buf->b_next;
1363 }
1364 else
1365 {
1366 bp = NULL;
1367 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1368 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001369 // remember the buffer where we start, we come back there when all
1370 // buffers are unlisted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 if (bp == NULL)
1372 bp = buf;
1373 if (dir == FORWARD)
1374 {
1375 buf = buf->b_next;
1376 if (buf == NULL)
1377 buf = firstbuf;
1378 }
1379 else
1380 {
1381 buf = buf->b_prev;
1382 if (buf == NULL)
1383 buf = lastbuf;
1384 }
LemonBoy893eeeb2024-07-10 20:20:48 +02001385 // Don't count unlisted buffers.
1386 // Avoid non-help buffers if the starting point was a non-help buffer and
1387 // vice-versa.
1388 if (unload || (buf->b_p_bl
1389 && ((flags & DOBUF_SKIPHELP) == 0 || buf->b_help == bp->b_help)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 {
1391 --count;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001392 bp = NULL; // use this buffer as new starting point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 }
1394 if (bp == buf)
1395 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001396 // back where we started, didn't find anything.
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001397 emsg(_(e_there_is_no_listed_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 return FAIL;
1399 }
1400 }
1401 }
1402
Bram Moolenaarc667da52019-11-30 20:52:27 +01001403 if (buf == NULL) // could not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 {
1405 if (start == DOBUF_FIRST)
1406 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001407 // don't warn when deleting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 if (!unload)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001409 semsg(_(e_buffer_nr_does_not_exist), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 }
1411 else if (dir == FORWARD)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001412 emsg(_(e_cannot_go_beyond_last_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 else
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001414 emsg(_(e_cannot_go_before_first_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 return FAIL;
1416 }
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001417#ifdef FEAT_PROP_POPUP
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001418 if ((flags & DOBUF_NOPOPUP) && bt_popup(buf) && !bt_terminal(buf))
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001419 return OK;
1420#endif
Colin Kennedy21570352024-03-03 16:16:47 +01001421 if (
1422 action == DOBUF_GOTO
1423 && buf != curbuf
1424 && !check_can_set_curbuf_forceit((flags & DOBUF_FORCEIT) ? TRUE : FALSE))
1425 // disallow navigating to another buffer when 'winfixbuf' is applied
1426 return FAIL;
1427
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01001428 if ((action == DOBUF_GOTO || action == DOBUF_SPLIT)
1429 && (buf->b_flags & BF_DUMMY))
1430 {
1431 // disallow navigating to the dummy buffer
1432 semsg(_(e_buffer_nr_does_not_exist), count);
1433 return FAIL;
1434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435
1436#ifdef FEAT_GUI
1437 need_mouse_correct = TRUE;
1438#endif
1439
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001441 * delete buffer "buf" from memory and/or the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 */
1443 if (unload)
1444 {
1445 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001446 bufref_T bufref;
1447
Bram Moolenaar94f01952018-09-01 15:30:03 +02001448 if (!can_unload_buffer(buf))
Bram Moolenaara997b452018-04-17 23:24:06 +02001449 return FAIL;
Bram Moolenaara997b452018-04-17 23:24:06 +02001450
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001451 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452
Bram Moolenaarc667da52019-11-30 20:52:27 +01001453 // When unloading or deleting a buffer that's already unloaded and
1454 // unlisted: fail silently.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001455 if (action != DOBUF_WIPE && action != DOBUF_WIPE_REUSE
1456 && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 return FAIL;
1458
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001459 if ((flags & DOBUF_FORCEIT) == 0 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001461#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001462 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001464# ifdef FEAT_TERMINAL
1465 if (term_job_running(buf->b_term))
1466 {
1467 if (term_confirm_stop(buf) == FAIL)
1468 return FAIL;
1469 }
1470 else
1471# endif
1472 {
1473 dialog_changed(buf, FALSE);
1474 if (!bufref_valid(&bufref))
1475 // Autocommand deleted buffer, oops! It's not changed
1476 // now.
1477 return FAIL;
1478 // If it's still changed fail silently, the dialog already
1479 // mentioned why it fails.
1480 if (bufIsChanged(buf))
1481 return FAIL;
1482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001484 else
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001485#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001487 no_write_message_buf(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 return FAIL;
1489 }
1490 }
1491
Bram Moolenaarc667da52019-11-30 20:52:27 +01001492 // When closing the current buffer stop Visual mode.
Bram Moolenaar4930a762016-09-11 14:39:53 +02001493 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001494 end_visual_mode();
1495
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001496 // If deleting the last (listed) buffer, make it empty.
1497 // The last (listed) buffer cannot be unloaded.
Bram Moolenaar29323592016-07-24 22:04:11 +02001498 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 if (bp->b_p_bl && bp != buf)
1500 break;
1501 if (bp == NULL && buf == curbuf)
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001502 return empty_curbuf(TRUE, (flags & DOBUF_FORCEIT), action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001504 // If the deleted buffer is the current one, close the current window
1505 // (unless it's the only window). Repeat this so long as we end up in
1506 // a window with this buffer.
Bram Moolenaarf740b292006-02-16 22:11:02 +00001507 while (buf == curbuf
Christian Brabandt0a6e57b2024-08-15 22:15:28 +02001508 && !(win_locked(curwin) || curwin->w_buffer->b_locked > 0)
Bram Moolenaar459ca562016-11-10 18:16:33 +01001509 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001510 {
1511 if (win_close(curwin, FALSE) == FAIL)
1512 break;
1513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001515 // If the buffer to be deleted is not the current one, delete it here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 if (buf != curbuf)
1517 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001518 close_windows(buf, FALSE);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001519 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows <= 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001520 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 return OK;
1522 }
1523
1524 /*
1525 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001526 * There should be another, otherwise it would have been handled
1527 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001528 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 * Then prefer the buffer we most recently visited.
1530 * Else try to find one that is loaded, after the current buffer,
1531 * then before the current buffer.
1532 * Finally use any buffer.
1533 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001534 buf = NULL; // selected buffer
1535 bp = NULL; // used when no loaded buffer found
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001536 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1537 buf = au_new_curbuf.br_buf;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001538 else if (curwin->w_jumplistlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 {
1540 int jumpidx;
1541
1542 jumpidx = curwin->w_jumplistidx - 1;
1543 if (jumpidx < 0)
1544 jumpidx = curwin->w_jumplistlen - 1;
1545
1546 forward = jumpidx;
1547 while (jumpidx != curwin->w_jumplistidx)
1548 {
1549 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1550 if (buf != NULL)
1551 {
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001552 // Skip current and unlisted bufs. Also skip a quickfix
1553 // buffer, it might be deleted soon.
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001554 if (buf == curbuf || !buf->b_p_bl || bt_quickfix(buf))
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001555 buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 else if (buf->b_ml.ml_mfp == NULL)
1557 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001558 // skip unloaded buf, but may keep it for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 if (bp == NULL)
1560 bp = buf;
1561 buf = NULL;
1562 }
1563 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001564 if (buf != NULL) // found a valid buffer: stop searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001566 // advance to older entry in jump list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1568 break;
1569 if (--jumpidx < 0)
1570 jumpidx = curwin->w_jumplistlen - 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001571 if (jumpidx == forward) // List exhausted for sure
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 break;
1573 }
1574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575
Bram Moolenaarc667da52019-11-30 20:52:27 +01001576 if (buf == NULL) // No previous buffer, Try 2'nd approach
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 {
1578 forward = TRUE;
1579 buf = curbuf->b_next;
1580 for (;;)
1581 {
1582 if (buf == NULL)
1583 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001584 if (!forward) // tried both directions
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 break;
1586 buf = curbuf->b_prev;
1587 forward = FALSE;
1588 continue;
1589 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001590 // in non-help buffer, try to skip help buffers, and vv
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001591 if (buf->b_help == curbuf->b_help && buf->b_p_bl
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001592 && !bt_quickfix(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001594 if (buf->b_ml.ml_mfp != NULL) // found loaded buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001596 if (bp == NULL) // remember unloaded buf for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 bp = buf;
1598 }
1599 if (forward)
1600 buf = buf->b_next;
1601 else
1602 buf = buf->b_prev;
1603 }
1604 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001605 if (buf == NULL) // No loaded buffer, use unloaded one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 buf = bp;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001607 if (buf == NULL) // No loaded buffer, find listed one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001609 FOR_ALL_BUFFERS(buf)
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001610 if (buf->b_p_bl && buf != curbuf && !bt_quickfix(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 break;
1612 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001613 if (buf == NULL) // Still no buffer, just take one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 {
1615 if (curbuf->b_next != NULL)
1616 buf = curbuf->b_next;
1617 else
1618 buf = curbuf->b_prev;
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001619 if (bt_quickfix(buf))
1620 buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 }
1622 }
1623
Bram Moolenaara02471e2014-01-10 16:43:14 +01001624 if (buf == NULL)
1625 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001626 // Autocommands must have wiped out all other buffers. Only option
1627 // now is to make the current buffer empty.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001628 return empty_curbuf(FALSE, (flags & DOBUF_FORCEIT), action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001629 }
1630
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001632 * make "buf" the current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001634 if (action == DOBUF_SPLIT) // split window first
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 {
Yegappan Lakshmanane42c27d2023-05-14 17:24:22 +01001636 // If 'switchbuf' is set jump to the window containing "buf".
1637 if (swbuf_goto_win_with_buf(buf) != NULL)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001638 return OK;
Yegappan Lakshmanane42c27d2023-05-14 17:24:22 +01001639
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 return FAIL;
1642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643
Bram Moolenaarc667da52019-11-30 20:52:27 +01001644 // go to current buffer - nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 if (buf == curbuf)
1646 return OK;
1647
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001648 // Check if the current buffer may be abandoned.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001649 if (action == DOBUF_GOTO && !can_abandon(curbuf, (flags & DOBUF_FORCEIT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 {
1651#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001652 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001654# ifdef FEAT_TERMINAL
1655 if (term_job_running(curbuf->b_term))
1656 {
1657 if (term_confirm_stop(curbuf) == FAIL)
1658 return FAIL;
1659 // Manually kill the terminal here because this command will
1660 // hide it otherwise.
1661 free_terminal(curbuf);
1662 }
1663 else
1664# endif
1665 {
1666 bufref_T bufref;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001667
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001668 set_bufref(&bufref, buf);
1669 dialog_changed(curbuf, FALSE);
1670 if (!bufref_valid(&bufref))
1671 // Autocommand deleted buffer, oops!
1672 return FAIL;
1673
1674 if (bufIsChanged(curbuf))
1675 {
1676 no_write_message();
1677 return FAIL;
1678 }
1679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 }
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001681 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682#endif
1683 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001684 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 return FAIL;
1686 }
1687 }
1688
Bram Moolenaarc667da52019-11-30 20:52:27 +01001689 // Go to the other buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 set_curbuf(buf, action);
1691
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 if (action == DOBUF_SPLIT)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001693 RESET_BINDING(curwin); // reset 'scrollbind' and 'cursorbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001695#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001696 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 return FAIL;
1698#endif
1699
1700 return OK;
1701}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001703 int
1704do_buffer(
1705 int action,
1706 int start,
1707 int dir, // FORWARD or BACKWARD
1708 int count, // buffer number or number of buffers
1709 int forceit) // TRUE when using !
1710{
1711 return do_buffer_ext(action, start, dir, count,
1712 forceit ? DOBUF_FORCEIT : 0);
1713}
1714
1715/*
1716 * do_bufdel() - delete or unload buffer(s)
1717 *
1718 * addr_count == 0: ":bdel" - delete current buffer
1719 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1720 * buffer "end_bnr", then any other arguments.
1721 * addr_count == 2: ":N,N bdel" - delete buffers in range
1722 *
1723 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1724 * DOBUF_DEL (":bdel")
1725 *
1726 * Returns error message or NULL
1727 */
1728 char *
1729do_bufdel(
1730 int command,
1731 char_u *arg, // pointer to extra arguments
1732 int addr_count,
1733 int start_bnr, // first buffer number in a range
1734 int end_bnr, // buffer nr or last buffer nr in a range
1735 int forceit)
1736{
1737 int do_current = 0; // delete current buffer?
1738 int deleted = 0; // number of buffers deleted
1739 char *errormsg = NULL; // return value
1740 int bnr; // buffer number
1741 char_u *p;
1742
1743 if (addr_count == 0)
1744 {
1745 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1746 }
1747 else
1748 {
1749 if (addr_count == 2)
1750 {
1751 if (*arg) // both range and argument is not allowed
Bram Moolenaar74409f62022-01-01 15:58:22 +00001752 return ex_errmsg(e_trailing_characters_str, arg);
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001753 bnr = start_bnr;
1754 }
1755 else // addr_count == 1
1756 bnr = end_bnr;
1757
1758 for ( ;!got_int; ui_breakcheck())
1759 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001760 // Delete the current buffer last, otherwise when the
1761 // current buffer is deleted, the next buffer becomes
1762 // the current one and will be loaded, which may then
1763 // also be deleted, etc.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001764 if (bnr == curbuf->b_fnum)
1765 do_current = bnr;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00001766 else if (do_buffer_ext(command, DOBUF_FIRST, FORWARD, bnr,
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001767 DOBUF_NOPOPUP | (forceit ? DOBUF_FORCEIT : 0)) == OK)
1768 ++deleted;
1769
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001770 // find next buffer number to delete/unload
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001771 if (addr_count == 2)
1772 {
1773 if (++bnr > end_bnr)
1774 break;
1775 }
1776 else // addr_count == 1
1777 {
1778 arg = skipwhite(arg);
1779 if (*arg == NUL)
1780 break;
1781 if (!VIM_ISDIGIT(*arg))
1782 {
1783 p = skiptowhite_esc(arg);
1784 bnr = buflist_findpat(arg, p,
1785 command == DOBUF_WIPE || command == DOBUF_WIPE_REUSE,
1786 FALSE, FALSE);
1787 if (bnr < 0) // failed
1788 break;
1789 arg = p;
1790 }
1791 else
1792 bnr = getdigits(&arg);
1793 }
1794 }
1795 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1796 FORWARD, do_current, forceit) == OK)
1797 ++deleted;
1798
1799 if (deleted == 0)
1800 {
1801 if (command == DOBUF_UNLOAD)
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001802 STRCPY(IObuff, _(e_no_buffers_were_unloaded));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001803 else if (command == DOBUF_DEL)
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001804 STRCPY(IObuff, _(e_no_buffers_were_deleted));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001805 else
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001806 STRCPY(IObuff, _(e_no_buffers_were_wiped_out));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001807 errormsg = (char *)IObuff;
1808 }
1809 else if (deleted >= p_report)
1810 {
1811 if (command == DOBUF_UNLOAD)
1812 smsg(NGETTEXT("%d buffer unloaded",
1813 "%d buffers unloaded", deleted), deleted);
1814 else if (command == DOBUF_DEL)
1815 smsg(NGETTEXT("%d buffer deleted",
1816 "%d buffers deleted", deleted), deleted);
1817 else
1818 smsg(NGETTEXT("%d buffer wiped out",
1819 "%d buffers wiped out", deleted), deleted);
1820 }
1821 }
1822
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001823 return errormsg;
1824}
1825
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826/*
1827 * Set current buffer to "buf". Executes autocommands and closes current
1828 * buffer. "action" tells how to close the current buffer:
1829 * DOBUF_GOTO free or hide it
1830 * DOBUF_SPLIT nothing
1831 * DOBUF_UNLOAD unload it
1832 * DOBUF_DEL delete it
1833 * DOBUF_WIPE wipe it out
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001834 * DOBUF_WIPE_REUSE wipe it out and add to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 */
1836 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001837set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838{
1839 buf_T *prevbuf;
1840 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001841 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001842#ifdef FEAT_SYN_HL
1843 long old_tw = curbuf->b_p_tw;
1844#endif
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001845 bufref_T newbufref;
1846 bufref_T prevbufref;
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001847 int valid;
Christian Brabandt55f8bba2024-02-28 23:32:00 +01001848 int last_winid = get_last_winid();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849
1850 setpcmark();
Bram Moolenaare1004402020-10-24 20:49:43 +02001851 if ((cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001852 curwin->w_alt_fnum = curbuf->b_fnum; // remember alternate file
1853 buflist_altfpos(curwin); // remember curpos
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854
Bram Moolenaarc667da52019-11-30 20:52:27 +01001855 // Don't restart Select mode after switching to another buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857
Bram Moolenaarc667da52019-11-30 20:52:27 +01001858 // close_windows() or apply_autocmds() may change curbuf and wipe out "buf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 prevbuf = curbuf;
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001860 set_bufref(&prevbufref, prevbuf);
1861 set_bufref(&newbufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862
Bram Moolenaar983d83f2021-02-07 12:12:43 +01001863 // Autocommands may delete the current buffer and/or the buffer we want to
1864 // go to. In those cases don't close the buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001865 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001866 || (bufref_valid(&prevbufref)
1867 && bufref_valid(&newbufref)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001868#ifdef FEAT_EVAL
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001869 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001871 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001873#ifdef FEAT_SYN_HL
1874 if (prevbuf == curwin->w_buffer)
1875 reset_synblock(curwin);
1876#endif
Christian Brabandt55f8bba2024-02-28 23:32:00 +01001877 // autocommands may have opened a new window
1878 // with prevbuf, grr
1879 if (unload ||
1880 (last_winid != get_last_winid() &&
1881 strchr((char *)"wdu", prevbuf->b_p_bh[0]) != NULL))
Bram Moolenaarf740b292006-02-16 22:11:02 +00001882 close_windows(prevbuf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001883#if defined(FEAT_EVAL)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001884 if (bufref_valid(&prevbufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885#else
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001886 if (bufref_valid(&prevbufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001888 {
Bram Moolenaare25865a2012-07-06 16:22:02 +02001889 win_T *previouswin = curwin;
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001890
Bram Moolenaare615db02022-01-20 21:00:54 +00001891 // Do not sync when in Insert mode and the buffer is open in
1892 // another window, might be a timer doing something in another
1893 // window.
1894 if (prevbuf == curbuf
Bram Moolenaar24959102022-05-07 20:01:16 +01001895 && ((State & MODE_INSERT) == 0 || curbuf->b_nwindows <= 1))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001896 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1898 unload ? action : (action == DOBUF_GOTO
Bram Moolenaareb44a682017-08-03 22:44:55 +02001899 && !buf_hide(prevbuf)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001900 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0,
1901 FALSE, FALSE);
Bram Moolenaare25865a2012-07-06 16:22:02 +02001902 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001903 // autocommands changed curwin, Grr!
Bram Moolenaare25865a2012-07-06 16:22:02 +02001904 curwin = previouswin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001907 // An autocommand may have deleted "buf", already entered it (e.g., when
1908 // it did ":bunload") or aborted the script processing.
1909 // If curwin->w_buffer is null, enter_buffer() will make it valid again
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001910 valid = buf_valid(buf);
1911 if ((valid && buf != curbuf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001912#ifdef FEAT_EVAL
Bram Moolenaar4033c552017-09-16 20:54:51 +02001913 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001915 ) || curwin->w_buffer == NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001916 {
Christian Brabandt55f8bba2024-02-28 23:32:00 +01001917 // autocommands changed curbuf and we will move to another
1918 // buffer soon, so decrement curbuf->b_nwindows
1919 if (curbuf != NULL && prevbuf != curbuf)
1920 curbuf->b_nwindows--;
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001921 // If the buffer is not valid but curwin->w_buffer is NULL we must
1922 // enter some buffer. Using the last one is hopefully OK.
1923 if (!valid)
1924 enter_buffer(lastbuf);
1925 else
1926 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001927#ifdef FEAT_SYN_HL
1928 if (old_tw != curbuf->b_p_tw)
Millya441a3e2024-10-22 22:43:01 +02001929 check_colorcolumn(NULL, curwin);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001930#endif
1931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932}
1933
1934/*
1935 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001936 * Old curbuf must have been abandoned already! This also means "curbuf" may
1937 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001939 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001940enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941{
Bram Moolenaarcfeb8a52022-08-13 14:09:44 +01001942 // when closing the current buffer stop Visual mode
1943 if (VIsual_active
1944#if defined(EXITFREE)
1945 && !entered_free_all_mem
1946#endif
1947 )
1948 end_visual_mode();
1949
Bram Moolenaar010ee962019-09-25 20:37:36 +02001950 // Get the buffer in the current window.
1951 curwin->w_buffer = buf;
1952 curbuf = buf;
1953 ++curbuf->b_nwindows;
1954
1955 // Copy buffer and window local option values. Not for a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1957 if (!buf->b_help)
1958 get_winopts(buf);
1959#ifdef FEAT_FOLDING
1960 else
Bram Moolenaar010ee962019-09-25 20:37:36 +02001961 // Remove all folds in the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 clearFolding(curwin);
Bram Moolenaar010ee962019-09-25 20:37:36 +02001963 foldUpdateAll(curwin); // update folds (later).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964#endif
1965
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001967 if (curwin->w_p_diff)
1968 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969#endif
1970
Bram Moolenaara971b822011-09-14 14:43:25 +02001971#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001972 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001973#endif
1974
Bram Moolenaarc667da52019-11-30 20:52:27 +01001975 // Cursor on first line by default.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 curwin->w_cursor.lnum = 1;
1977 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001980 curwin->w_topline_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981
Bram Moolenaarc667da52019-11-30 20:52:27 +01001982 // mark cursor position as being invalid
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001983 curwin->w_valid = 0;
1984
Bram Moolenaar9cea87c2018-09-21 16:59:45 +02001985 buflist_setfpos(curbuf, curwin, curbuf->b_last_cursor.lnum,
1986 curbuf->b_last_cursor.col, TRUE);
1987
Bram Moolenaarc667da52019-11-30 20:52:27 +01001988 // Make sure the buffer is loaded.
1989 if (curbuf->b_ml.ml_mfp == NULL) // need to load the file
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001990 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001991 // If there is no filetype, allow for detecting one. Esp. useful for
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001992 // ":ball" used in an autocommand. If there already is a filetype we
Bram Moolenaarc667da52019-11-30 20:52:27 +01001993 // might prefer to keep it.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001994 if (*curbuf->b_p_ft == NUL)
zeertzjq5bf6c212024-03-31 18:41:27 +02001995 curbuf->b_did_filetype = FALSE;
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001996
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001997 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 else
2000 {
Bram Moolenaareda65222019-05-16 20:29:44 +02002001 if (!msg_silent && !shortmess(SHM_FILEINFO))
2002 need_fileinfo = TRUE; // display file info after redraw
2003
2004 // check if file changed
2005 (void)buf_check_timestamp(curbuf, FALSE);
2006
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002008#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2012 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 }
2014
Bram Moolenaarc667da52019-11-30 20:52:27 +01002015 // If autocommands did not change the cursor position, restore cursor lnum
2016 // and possibly cursor col.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 if (curwin->w_cursor.lnum == 1 && inindent(0))
2018 buflist_getfpos();
2019
Bram Moolenaarc667da52019-11-30 20:52:27 +01002020 check_arg_idx(curwin); // check for valid arg_idx
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 maketitle();
Bram Moolenaarc667da52019-11-30 20:52:27 +01002022 // when autocmds didn't change it
Bram Moolenaard4153d42008-11-15 15:06:17 +00002023 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00002024 scroll_cursor_halfway(FALSE, FALSE); // redisplay at correct position
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025
Bram Moolenaar009b2592004-10-24 19:18:58 +00002026#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarc667da52019-11-30 20:52:27 +01002027 // Send fileOpened event because we've changed buffers.
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002028 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00002029#endif
2030
Bram Moolenaarc667da52019-11-30 20:52:27 +01002031 // Change directories when the 'acd' option is set.
Bram Moolenaar6f470022018-04-10 18:47:20 +02002032 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033
2034#ifdef FEAT_KEYMAP
2035 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002036 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002038#ifdef FEAT_SPELL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002039 // May need to set the spell language. Can only do this after the buffer
2040 // has been properly setup.
Bram Moolenaar860cae12010-06-05 23:22:07 +02002041 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00002042 (void)parse_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002043#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02002044#ifdef FEAT_VIMINFO
2045 curbuf->b_last_used = vim_time();
2046#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00002047
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002048 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049}
2050
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002051#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
2052/*
2053 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01002054 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002055 */
2056 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002057do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002058{
Bram Moolenaar5c719942016-07-09 23:40:45 +02002059 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01002060 && curbuf->b_ffname != NULL
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002061 && vim_chdirfile(curbuf->b_ffname, "auto") == OK)
Bram Moolenaar05268152021-11-18 18:53:45 +00002062 {
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002063 shorten_fnames(TRUE);
Bram Moolenaar05268152021-11-18 18:53:45 +00002064 last_chdir_reason = "autochdir";
2065 }
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002066}
2067#endif
2068
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01002069 static void
2070no_write_message_buf(buf_T *buf UNUSED)
2071{
2072#ifdef FEAT_TERMINAL
2073 if (term_job_running(buf->b_term))
2074 emsg(_(e_job_still_running_add_bang_to_end_the_job));
2075 else
2076#endif
2077 semsg(_(e_no_write_since_last_change_for_buffer_nr_add_bang_to_override),
2078 buf->b_fnum);
2079}
2080
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002081 void
2082no_write_message(void)
2083{
2084#ifdef FEAT_TERMINAL
2085 if (term_job_running(curbuf->b_term))
Bram Moolenaarf1474d82021-12-31 19:59:55 +00002086 emsg(_(e_job_still_running_add_bang_to_end_the_job));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002087 else
2088#endif
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002089 emsg(_(e_no_write_since_last_change_add_bang_to_override));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002090}
2091
2092 void
Bram Moolenaar7a760922018-02-19 23:10:02 +01002093no_write_message_nobang(buf_T *buf UNUSED)
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002094{
2095#ifdef FEAT_TERMINAL
Bram Moolenaar7a760922018-02-19 23:10:02 +01002096 if (term_job_running(buf->b_term))
Bram Moolenaarf1474d82021-12-31 19:59:55 +00002097 emsg(_(e_job_still_running));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002098 else
2099#endif
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002100 emsg(_(e_no_write_since_last_change));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002101}
2102
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103/*
2104 * functions for dealing with the buffer list
2105 */
2106
2107/*
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002108 * Return TRUE if the current buffer is empty, unnamed, unmodified and used in
2109 * only one window. That means it can be re-used.
2110 */
2111 int
2112curbuf_reusable(void)
2113{
2114 return (curbuf != NULL
2115 && curbuf->b_ffname == NULL
2116 && curbuf->b_nwindows <= 1
2117 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY())
Bram Moolenaar39803d82019-04-07 12:04:51 +02002118 && !bt_quickfix(curbuf)
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002119 && !curbufIsChanged());
2120}
2121
2122/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 * Add a file name to the buffer list. Return a pointer to the buffer.
2124 * If the same file name already exists return a pointer to that buffer.
2125 * If it does not exist, or if fname == NULL, a new entry is created.
2126 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
2127 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
2128 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02002129 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02002130 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
2131 * if the buffer already exists.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002132 * If (flags & BLN_REUSE) is TRUE, may use buffer number from "buf_reuse".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 * This is the ONLY way to create a new buffer.
2134 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002136buflist_new(
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002137 char_u *ffname_arg, // full path of fname or relative
2138 char_u *sfname_arg, // short fname or NULL
2139 linenr_T lnum, // preferred cursor line
2140 int flags) // BLN_ defines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002142 char_u *ffname = ffname_arg;
2143 char_u *sfname = sfname_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 buf_T *buf;
2145#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002146 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147#endif
2148
Bram Moolenaar480778b2016-07-14 22:09:39 +02002149 if (top_file_num == 1)
2150 hash_init(&buf_hashtab);
2151
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002152 fname_expand(curbuf, &ffname, &sfname); // will allocate ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153
2154 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002155 * If the file name already exists in the list, update the entry.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 */
2157#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002158 // On Unix we can use inode numbers when the file exists. Works better
2159 // for hard links.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
2161 st.st_dev = (dev_T)-1;
2162#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02002163 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164#ifdef UNIX
2165 buflist_findname_stat(ffname, &st)
2166#else
2167 buflist_findname(ffname)
2168#endif
2169 ) != NULL)
2170 {
2171 vim_free(ffname);
2172 if (lnum != 0)
Bram Moolenaar89b693e2020-10-25 17:09:50 +01002173 buflist_setfpos(buf, (flags & BLN_NOCURWIN) ? NULL : curwin,
2174 lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02002175
2176 if ((flags & BLN_NOOPT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002177 // copy the options now, if 'cpo' doesn't have 's' and not done
2178 // already
Bram Moolenaar82404332016-07-10 17:00:38 +02002179 buf_copy_options(buf, 0);
2180
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 if ((flags & BLN_LISTED) && !buf->b_p_bl)
2182 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002183 bufref_T bufref;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002184
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 buf->b_p_bl = TRUE;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002186 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002188 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002189 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002190 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002191 return NULL;
2192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 }
2194 return buf;
2195 }
2196
2197 /*
2198 * If the current buffer has no name and no contents, use the current
2199 * buffer. Otherwise: Need to allocate a new buffer structure.
2200 *
2201 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00002202 * (A spell file buffer is allocated in spell.c, but that's not a normal
2203 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 */
2205 buf = NULL;
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002206 if ((flags & BLN_CURBUF) && curbuf_reusable())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 {
2208 buf = curbuf;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002209 // It's like this buffer is deleted. Watch out for autocommands that
2210 // change curbuf! If that happens, allocate a new buffer anyway.
Charlie Grovesfef44852022-04-19 16:24:12 +01002211 buf_freeall(buf, BFA_WIPE | BFA_DEL);
2212 if (buf != curbuf) // autocommands deleted the buffer!
2213 return NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002214#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002215 if (aborting()) // autocmds may abort script processing
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002216 {
2217 vim_free(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 return NULL;
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 }
2222 if (buf != curbuf || curbuf == NULL)
2223 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002224 buf = ALLOC_CLEAR_ONE(buf_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 if (buf == NULL)
2226 {
2227 vim_free(ffname);
2228 return NULL;
2229 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02002230#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002231 // init b: variables
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01002232 buf->b_vars = dict_alloc_id(aid_newbuf_bvars);
Bram Moolenaar429fa852013-04-15 12:27:36 +02002233 if (buf->b_vars == NULL)
2234 {
2235 vim_free(ffname);
2236 vim_free(buf);
2237 return NULL;
2238 }
2239 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
2240#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01002241 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 }
2243
2244 if (ffname != NULL)
2245 {
2246 buf->b_ffname = ffname;
2247 buf->b_sfname = vim_strsave(sfname);
2248 }
2249
2250 clear_wininfo(buf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002251 buf->b_wininfo = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252
2253 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
2254 || buf->b_wininfo == NULL)
2255 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002256 if (buf->b_sfname != buf->b_ffname)
2257 VIM_CLEAR(buf->b_sfname);
2258 else
2259 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01002260 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 if (buf != curbuf)
2262 free_buffer(buf);
2263 return NULL;
2264 }
2265
2266 if (buf == curbuf)
2267 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002268 free_buffer_stuff(buf, FALSE); // delete local variables et al.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002269
Bram Moolenaarc667da52019-11-30 20:52:27 +01002270 // Init the options.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002271 buf->b_p_initialized = FALSE;
2272 buf_copy_options(buf, BCO_ENTER);
2273
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274#ifdef FEAT_KEYMAP
Bram Moolenaarc667da52019-11-30 20:52:27 +01002275 // need to reload lmaps and set b:keymap_name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 curbuf->b_kmap_state |= KEYMAP_INIT;
2277#endif
2278 }
2279 else
2280 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002281 // put the new buffer at the end of the buffer list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 buf->b_next = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002283 if (firstbuf == NULL) // buffer list is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 {
2285 buf->b_prev = NULL;
2286 firstbuf = buf;
2287 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002288 else // append new buffer at end of list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 {
2290 lastbuf->b_next = buf;
2291 buf->b_prev = lastbuf;
2292 }
2293 lastbuf = buf;
2294
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002295 if ((flags & BLN_REUSE) && buf_reuse.ga_len > 0)
2296 {
2297 // Recycle a previously used buffer number. Used for buffers which
2298 // are normally hidden, e.g. in a popup window. Avoids that the
2299 // buffer number grows rapidly.
2300 --buf_reuse.ga_len;
2301 buf->b_fnum = ((int *)buf_reuse.ga_data)[buf_reuse.ga_len];
Bram Moolenaar99ebf222019-12-10 23:44:48 +01002302
2303 // Move buffer to the right place in the buffer list.
2304 while (buf->b_prev != NULL && buf->b_fnum < buf->b_prev->b_fnum)
2305 {
2306 buf_T *prev = buf->b_prev;
2307
2308 prev->b_next = buf->b_next;
2309 if (prev->b_next != NULL)
2310 prev->b_next->b_prev = prev;
2311 buf->b_next = prev;
2312 buf->b_prev = prev->b_prev;
2313 if (buf->b_prev != NULL)
2314 buf->b_prev->b_next = buf;
2315 prev->b_prev = buf;
2316 if (lastbuf == buf)
2317 lastbuf = prev;
2318 if (firstbuf == prev)
2319 firstbuf = buf;
2320 }
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002321 }
2322 else
2323 buf->b_fnum = top_file_num++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002324 if (top_file_num < 0) // wrap around (may cause duplicates)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002326 emsg(_("W14: Warning: List of file names overflow"));
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002327 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 {
2329 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002330 ui_delay(3001L, TRUE); // make sure it is noticed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 }
2332 top_file_num = 1;
2333 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002334 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002336 // Always copy the options from the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 buf_copy_options(buf, BCO_ALWAYS);
2338 }
2339
2340 buf->b_wininfo->wi_fpos.lnum = lnum;
2341 buf->b_wininfo->wi_win = curwin;
2342
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002343#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002344 hash_init(&buf->b_s.b_keywtab);
2345 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346#endif
2347
2348 buf->b_fname = buf->b_sfname;
2349#ifdef UNIX
2350 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002351 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 else
2353 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002354 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 buf->b_dev = st.st_dev;
2356 buf->b_ino = st.st_ino;
2357 }
2358#endif
2359 buf->b_u_synced = TRUE;
2360 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002361 if (flags & BLN_DUMMY)
2362 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 buf_clear_file(buf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002364 clrallmarks(buf); // clear marks
2365 fmarks_check_names(buf); // check file marks for this file
2366 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; // init 'buflisted'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 if (!(flags & BLN_DUMMY))
2368 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002369 bufref_T bufref;
2370
Bram Moolenaarc667da52019-11-30 20:52:27 +01002371 // Tricky: these autocommands may change the buffer list. They could
2372 // also split the window with re-using the one empty buffer. This may
2373 // result in unexpectedly losing the empty buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002374 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002375 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002376 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002377 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002379 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002380 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002381 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002382 return NULL;
2383 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002384#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002385 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389
2390 return buf;
2391}
2392
2393/*
2394 * Free the memory for the options of a buffer.
2395 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2396 * 'fileencoding'.
2397 */
2398 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002399free_buf_options(
2400 buf_T *buf,
2401 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402{
2403 if (free_p_ff)
2404 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 clear_string_option(&buf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 clear_string_option(&buf->b_p_bh);
2408 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 }
2410#ifdef FEAT_FIND_ID
2411 clear_string_option(&buf->b_p_def);
2412 clear_string_option(&buf->b_p_inc);
2413# ifdef FEAT_EVAL
2414 clear_string_option(&buf->b_p_inex);
2415# endif
2416#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002417#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 clear_string_option(&buf->b_p_inde);
2419 clear_string_option(&buf->b_p_indk);
2420#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002421#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2422 clear_string_option(&buf->b_p_bexpr);
2423#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002424#if defined(FEAT_CRYPT)
2425 clear_string_option(&buf->b_p_cm);
2426#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002427 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002428#if defined(FEAT_EVAL)
2429 clear_string_option(&buf->b_p_fex);
2430#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431#ifdef FEAT_CRYPT
Bram Moolenaar131530a2021-07-29 20:37:49 +02002432# ifdef FEAT_SODIUM
Christian Brabandtaae58342023-04-23 17:50:22 +01002433 if (buf->b_p_key != NULL && *buf->b_p_key != NUL
2434 && crypt_method_is_sodium(crypt_get_method_nr(buf)))
K.Takata1a8825d2022-01-19 13:32:57 +00002435 crypt_sodium_munlock(buf->b_p_key, STRLEN(buf->b_p_key));
Bram Moolenaar131530a2021-07-29 20:37:49 +02002436# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 clear_string_option(&buf->b_p_key);
2438#endif
2439 clear_string_option(&buf->b_p_kp);
2440 clear_string_option(&buf->b_p_mps);
2441 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002442 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 clear_string_option(&buf->b_p_isk);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002444#ifdef FEAT_VARTABS
2445 clear_string_option(&buf->b_p_vsts);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00002446 VIM_CLEAR(buf->b_p_vsts_nopaste);
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00002447 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002448 clear_string_option(&buf->b_p_vts);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002449 VIM_CLEAR(buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451#ifdef FEAT_KEYMAP
2452 clear_string_option(&buf->b_p_keymap);
Bram Moolenaar50138322018-01-28 17:05:16 +01002453 keymap_clear(&buf->b_kmap_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 ga_clear(&buf->b_kmap_ga);
2455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 clear_string_option(&buf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457#ifdef FEAT_FOLDING
2458 clear_string_option(&buf->b_p_cms);
2459#endif
2460 clear_string_option(&buf->b_p_nf);
2461#ifdef FEAT_SYN_HL
2462 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002463 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002464#endif
2465#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002466 clear_string_option(&buf->b_s.b_p_spc);
2467 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002468 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002469 buf->b_s.b_cap_prog = NULL;
2470 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02002471 clear_string_option(&buf->b_s.b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 clear_string_option(&buf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 clear_string_option(&buf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 clear_string_option(&buf->b_p_cink);
2476 clear_string_option(&buf->b_p_cino);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002477 clear_string_option(&buf->b_p_lop);
Tom Praschan3506cf32022-04-07 12:39:08 +01002478 clear_string_option(&buf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 clear_string_option(&buf->b_p_cinw);
zeertzjq529b9ad2024-06-05 20:27:06 +02002480 clear_string_option(&buf->b_p_cot);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 clear_string_option(&buf->b_p_cpt);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002482#ifdef FEAT_COMPL_FUNC
2483 clear_string_option(&buf->b_p_cfu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002484 free_callback(&buf->b_cfu_cb);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002485 clear_string_option(&buf->b_p_ofu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002486 free_callback(&buf->b_ofu_cb);
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +01002487 clear_string_option(&buf->b_p_tsrfu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002488 free_callback(&buf->b_tsrfu_cb);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490#ifdef FEAT_QUICKFIX
2491 clear_string_option(&buf->b_p_gp);
2492 clear_string_option(&buf->b_p_mp);
2493 clear_string_option(&buf->b_p_efm);
2494#endif
2495 clear_string_option(&buf->b_p_ep);
2496 clear_string_option(&buf->b_p_path);
2497 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002498 clear_string_option(&buf->b_p_tc);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002499#ifdef FEAT_EVAL
2500 clear_string_option(&buf->b_p_tfu);
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +00002501 free_callback(&buf->b_tfu_cb);
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +01002502 clear_string_option(&buf->b_p_ffu);
2503 free_callback(&buf->b_ffu_cb);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 clear_string_option(&buf->b_p_dict);
2506 clear_string_option(&buf->b_p_tsr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002507 clear_string_option(&buf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002509 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002510 clear_string_option(&buf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002511 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002512 clear_string_option(&buf->b_p_menc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513}
2514
2515/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002516 * Get alternate file "n".
2517 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2518 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 * if (options & GETF_SETMARK) call setpcmark()
2520 * if (options & GETF_ALT) we are jumping to an alternate file.
2521 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2522 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002523 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 */
2525 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002526buflist_getfile(
2527 int n,
2528 linenr_T lnum,
2529 int options,
2530 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531{
2532 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 pos_T *fpos;
2535 colnr_T col;
2536
2537 buf = buflist_findnr(n);
2538 if (buf == NULL)
2539 {
2540 if ((options & GETF_ALT) && n == 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02002541 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 else
Bram Moolenaare1242042021-12-16 20:56:57 +00002543 semsg(_(e_buffer_nr_not_found), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 return FAIL;
2545 }
2546
Bram Moolenaarc667da52019-11-30 20:52:27 +01002547 // if alternate file is the current buffer, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 if (buf == curbuf)
2549 return OK;
2550
Bram Moolenaar71223e22022-05-30 15:23:09 +01002551 if (text_or_buf_locked())
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002552 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553
Bram Moolenaarc667da52019-11-30 20:52:27 +01002554 // altfpos may be changed by getfile(), get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 if (lnum == 0)
2556 {
2557 fpos = buflist_findfpos(buf);
2558 lnum = fpos->lnum;
2559 col = fpos->col;
2560 }
2561 else
2562 col = 0;
2563
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 if (options & GETF_SWITCH)
2565 {
Yegappan Lakshmanane42c27d2023-05-14 17:24:22 +01002566 // If 'switchbuf' is set jump to the window containing "buf".
2567 wp = swbuf_goto_win_with_buf(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002568
Bram Moolenaarc667da52019-11-30 20:52:27 +01002569 // If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2570 // current buffer isn't empty: open new tab or window
Bram Moolenaara594d772015-06-19 14:41:49 +02002571 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002572 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002574 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002575 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002576 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2577 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002579 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 }
2581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582
2583 ++RedrawingDisabled;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002584 int retval = FAIL;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002585 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2586 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002588 // cursor is at to BOL and w_cursor.lnum is checked due to getfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 if (!p_sol && col != 0)
2590 {
2591 curwin->w_cursor.col = col;
2592 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 curwin->w_set_curswant = TRUE;
2595 }
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002596 retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 }
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002598
2599 if (RedrawingDisabled > 0)
2600 --RedrawingDisabled;
2601 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602}
2603
2604/*
2605 * go to the last know line number for the current buffer
2606 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002607 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002608buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609{
2610 pos_T *fpos;
2611
2612 fpos = buflist_findfpos(curbuf);
2613
2614 curwin->w_cursor.lnum = fpos->lnum;
2615 check_cursor_lnum();
2616
2617 if (p_sol)
2618 curwin->w_cursor.col = 0;
2619 else
2620 {
2621 curwin->w_cursor.col = fpos->col;
2622 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 curwin->w_set_curswant = TRUE;
2625 }
2626}
2627
Bram Moolenaar81695252004-12-29 20:58:21 +00002628/*
2629 * Find file in buffer list by name (it has to be for the current window).
2630 * Returns NULL if not found.
2631 */
2632 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002633buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002634{
2635 char_u *ffname;
2636 buf_T *buf = NULL;
2637
Bram Moolenaarc667da52019-11-30 20:52:27 +01002638 // First make the name into a full path name
Bram Moolenaar81695252004-12-29 20:58:21 +00002639 ffname = FullName_save(fname,
2640#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002641 TRUE // force expansion, get rid of symbolic links
Bram Moolenaar81695252004-12-29 20:58:21 +00002642#else
2643 FALSE
2644#endif
2645 );
2646 if (ffname != NULL)
2647 {
2648 buf = buflist_findname(ffname);
2649 vim_free(ffname);
2650 }
2651 return buf;
2652}
Bram Moolenaar81695252004-12-29 20:58:21 +00002653
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654/*
2655 * Find file in buffer list by name (it has to be for the current window).
2656 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002657 * Skips dummy buffers.
2658 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 */
2660 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002661buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662{
2663#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002664 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665
2666 if (mch_stat((char *)ffname, &st) < 0)
2667 st.st_dev = (dev_T)-1;
2668 return buflist_findname_stat(ffname, &st);
2669}
2670
2671/*
2672 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2673 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002674 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 */
2676 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002677buflist_findname_stat(
2678 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002679 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680{
2681#endif
2682 buf_T *buf;
2683
Bram Moolenaarc667da52019-11-30 20:52:27 +01002684 // Start at the last buffer, expect to find a match sooner.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002685 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaar81695252004-12-29 20:58:21 +00002686 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687#ifdef UNIX
2688 , stp
2689#endif
2690 ))
2691 return buf;
2692 return NULL;
2693}
2694
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695/*
2696 * Find file in buffer list by a regexp pattern.
2697 * Return fnum of the found buffer.
2698 * Return < 0 for error.
2699 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002701buflist_findpat(
2702 char_u *pattern,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002703 char_u *pattern_end, // pointer to first char after pattern
2704 int unlisted, // find unlisted buffers
2705 int diffmode UNUSED, // find diff-mode buffers only
2706 int curtab_only) // find buffers in current tab only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707{
2708 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 int match = -1;
2710 int find_listed;
2711 char_u *pat;
2712 char_u *patend;
2713 int attempt;
2714 char_u *p;
2715 int toggledollar;
2716
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002717 // "%" is current file, "%%" or "#" is alternate file
2718 if ((pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2719 || (in_vim9script() && pattern_end == pattern + 2
2720 && pattern[0] == '%' && pattern[1] == '%'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 {
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002722 if (*pattern == '#' || pattern_end == pattern + 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 match = curwin->w_alt_fnum;
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002724 else
2725 match = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726#ifdef FEAT_DIFF
2727 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2728 match = -1;
2729#endif
2730 }
2731
2732 /*
2733 * Try four ways of matching a listed buffer:
2734 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002735 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 * attempt == 2: with '$' at end (only match at end)
2737 * attempt == 3: with '^' at start and '$' at end (only full match)
2738 * Repeat this for finding an unlisted buffer if there was no matching
2739 * listed buffer.
2740 */
2741 else
2742 {
2743 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2744 if (pat == NULL)
2745 return -1;
2746 patend = pat + STRLEN(pat) - 1;
2747 toggledollar = (patend > pat && *patend == '$');
2748
Bram Moolenaarc667da52019-11-30 20:52:27 +01002749 // First try finding a listed buffer. If not found and "unlisted"
2750 // is TRUE, try finding an unlisted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 find_listed = TRUE;
2752 for (;;)
2753 {
2754 for (attempt = 0; attempt <= 3; ++attempt)
2755 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002756 regmatch_T regmatch;
2757
Bram Moolenaarc667da52019-11-30 20:52:27 +01002758 // may add '^' and '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 if (toggledollar)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002760 *patend = (attempt < 2) ? NUL : '$'; // add/remove '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 p = pat;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002762 if (*p == '^' && !(attempt & 1)) // add/remove '^'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 ++p;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01002764 regmatch.regprog = vim_regcomp(p, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002766 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002767 {
2768 if (regmatch.regprog == NULL)
2769 {
2770 // invalid pattern, possibly after switching engine
2771 vim_free(pat);
2772 return -1;
2773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 if (buf->b_p_bl == find_listed
2775#ifdef FEAT_DIFF
2776 && (!diffmode || diff_mode_buf(buf))
2777#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002778 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002780 if (curtab_only)
2781 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002782 // Ignore the match if the buffer is not open in
2783 // the current tab.
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002784 win_T *wp;
2785
Bram Moolenaar29323592016-07-24 22:04:11 +02002786 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002787 if (wp->w_buffer == buf)
2788 break;
2789 if (wp == NULL)
2790 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002791 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002792 if (match >= 0) // already found a match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 {
2794 match = -2;
2795 break;
2796 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002797 match = buf->b_fnum; // remember first match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 }
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002801 vim_regfree(regmatch.regprog);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002802 if (match >= 0) // found one match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 break;
2804 }
2805
Bram Moolenaarc667da52019-11-30 20:52:27 +01002806 // Only search for unlisted buffers if there was no match with
2807 // a listed buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 if (!unlisted || !find_listed || match != -1)
2809 break;
2810 find_listed = FALSE;
2811 }
2812
2813 vim_free(pat);
2814 }
2815
2816 if (match == -2)
Bram Moolenaare1242042021-12-16 20:56:57 +00002817 semsg(_(e_more_than_one_match_for_str), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 else if (match < 0)
Bram Moolenaare1242042021-12-16 20:56:57 +00002819 semsg(_(e_no_matching_buffer_for_str), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 return match;
2821}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822
Bram Moolenaar52410572019-10-27 05:12:45 +01002823#ifdef FEAT_VIMINFO
2824typedef struct {
2825 buf_T *buf;
2826 char_u *match;
2827} bufmatch_T;
2828#endif
2829
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830/*
2831 * Find all buffer names that match.
2832 * For command line expansion of ":buf" and ":sbuf".
2833 * Return OK if matches found, FAIL otherwise.
2834 */
2835 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002836ExpandBufnames(
2837 char_u *pat,
2838 int *num_file,
2839 char_u ***file,
2840 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841{
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002842 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 buf_T *buf;
2844 int round;
2845 char_u *p;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002846 char_u *patc = NULL;
Bram Moolenaar52410572019-10-27 05:12:45 +01002847#ifdef FEAT_VIMINFO
2848 bufmatch_T *matches = NULL;
2849#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002850 int fuzzy;
2851 fuzmatch_str_T *fuzmatch = NULL;
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002852 regmatch_T regmatch;
2853 int score = 0;
2854 int to_free = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855
Bram Moolenaarc667da52019-11-30 20:52:27 +01002856 *num_file = 0; // return values in case of FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 *file = NULL;
2858
Bram Moolenaarefcc3292019-12-30 21:59:03 +01002859#ifdef FEAT_DIFF
2860 if ((options & BUF_DIFF_FILTER) && !curwin->w_p_diff)
2861 return FAIL;
2862#endif
2863
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002864 fuzzy = cmdline_fuzzy_complete(pat);
2865
2866 // Make a copy of "pat" and change "^" to "\(^\|[\/]\)" (if doing regular
2867 // expression matching)
2868 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002870 if (*pat == '^' && pat[1] != NUL)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002871 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002872 int len = (int)STRLEN(pat);
2873 patc = alloc(len);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002874 if (patc == NULL)
2875 return FAIL;
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002876 STRNCPY(patc, pat + 1, len - 1);
2877 patc[len - 1] = NUL;
2878 to_free = TRUE;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002879 }
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002880 else if (*pat == '^')
2881 patc = (char_u *)"";
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002882 else
2883 patc = pat;
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002884 regmatch.regprog = vim_regcomp(patc, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002885 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00002886
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002887 // round == 1: Count the matches.
2888 // round == 2: Build the array to keep the matches.
2889 for (round = 1; round <= 2; ++round)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002890 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002891 count = 0;
2892 FOR_ALL_BUFFERS(buf)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002893 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002894 if (!buf->b_p_bl) // skip unlisted buffers
2895 continue;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002896#ifdef FEAT_DIFF
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002897 if (options & BUF_DIFF_FILTER)
2898 // Skip buffers not suitable for
2899 // :diffget or :diffput completion.
2900 if (buf == curbuf || !diff_mode_buf(buf))
2901 continue;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002902#endif
2903
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002904 if (!fuzzy)
2905 {
2906 if (regmatch.regprog == NULL)
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002907 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002908 // invalid pattern, possibly after recompiling
2909 if (to_free)
2910 vim_free(patc);
2911 return FAIL;
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002912 }
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002913 p = buflist_match(&regmatch, buf, p_wic);
2914 }
2915 else
2916 {
2917 p = NULL;
2918 // first try matching with the short file name
2919 if ((score = fuzzy_match_str(buf->b_sfname, pat)) != 0)
2920 p = buf->b_sfname;
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002921 if (p == NULL)
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002922 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002923 // next try matching with the full path file name
2924 if ((score = fuzzy_match_str(buf->b_ffname, pat)) != 0)
2925 p = buf->b_ffname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 }
2927 }
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002928
2929 if (p == NULL)
2930 continue;
2931
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 if (round == 1)
2933 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002934 ++count;
2935 continue;
2936 }
2937
2938 if (options & WILD_HOME_REPLACE)
2939 p = home_replace_save(buf, p);
2940 else
2941 p = vim_strsave(p);
John Marriott7fb90812025-04-02 20:32:35 +02002942 if (p == NULL)
2943 return FAIL;
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002944
2945 if (!fuzzy)
2946 {
Bram Moolenaar52410572019-10-27 05:12:45 +01002947#ifdef FEAT_VIMINFO
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002948 if (matches != NULL)
2949 {
2950 matches[count].buf = buf;
2951 matches[count].match = p;
2952 count++;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002953 }
2954 else
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002955#endif
2956 (*file)[count++] = p;
2957 }
2958 else
2959 {
2960 fuzmatch[count].idx = count;
2961 fuzmatch[count].str = p;
2962 fuzmatch[count].score = score;
2963 count++;
2964 }
2965 }
2966 if (count == 0) // no match found, break here
2967 break;
2968 if (round == 1)
2969 {
2970 if (!fuzzy)
2971 {
2972 *file = ALLOC_MULT(char_u *, count);
2973 if (*file == NULL)
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002974 {
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002975 vim_regfree(regmatch.regprog);
2976 if (to_free)
2977 vim_free(patc);
2978 return FAIL;
2979 }
2980#ifdef FEAT_VIMINFO
2981 if (options & WILD_BUFLASTUSED)
2982 matches = ALLOC_MULT(bufmatch_T, count);
2983#endif
2984 }
2985 else
2986 {
2987 fuzmatch = ALLOC_MULT(fuzmatch_str_T, count);
2988 if (fuzmatch == NULL)
2989 {
2990 *num_file = 0;
2991 *file = NULL;
2992 return FAIL;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 }
2995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 }
2997
Christian Brabandt0dc0bff2024-02-24 14:12:13 +01002998 if (!fuzzy)
2999 {
3000 vim_regfree(regmatch.regprog);
3001 if (to_free)
3002 vim_free(patc);
3003 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003004
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003005 if (!fuzzy)
Bram Moolenaar52410572019-10-27 05:12:45 +01003006 {
826814741_6dff3c9c2024-12-10 17:15:14 +01003007#ifdef FEAT_VIMINFO
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003008 if (matches != NULL)
Bram Moolenaar52410572019-10-27 05:12:45 +01003009 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003010 int i;
3011 if (count > 1)
3012 qsort(matches, count, sizeof(bufmatch_T), buf_compare);
3013 // if the current buffer is first in the list, place it at the end
3014 if (matches[0].buf == curbuf)
3015 {
3016 for (i = 1; i < count; i++)
3017 (*file)[i-1] = matches[i].match;
3018 (*file)[count-1] = matches[0].match;
3019 }
3020 else
3021 {
3022 for (i = 0; i < count; i++)
3023 (*file)[i] = matches[i].match;
3024 }
3025 vim_free(matches);
Bram Moolenaar52410572019-10-27 05:12:45 +01003026 }
826814741_6dff3c9c2024-12-10 17:15:14 +01003027#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00003028 }
3029 else
3030 {
3031 if (fuzzymatches_to_strmatches(fuzmatch, file, count, FALSE) == FAIL)
3032 return FAIL;
Bram Moolenaar52410572019-10-27 05:12:45 +01003033 }
Bram Moolenaar52410572019-10-27 05:12:45 +01003034
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 *num_file = count;
3036 return (count == 0 ? FAIL : OK);
3037}
3038
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039/*
3040 * Check for a match on the file name for buffer "buf" with regprog "prog".
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003041 * Note that rmp->regprog may become NULL when switching regexp engine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 */
3043 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003044buflist_match(
3045 regmatch_T *rmp,
3046 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003047 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048{
3049 char_u *match;
3050
Bram Moolenaarc667da52019-11-30 20:52:27 +01003051 // First try the short file name, then the long file name.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01003052 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaara59f2df2022-05-11 11:42:28 +01003053 if (match == NULL && rmp->regprog != NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01003054 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055
3056 return match;
3057}
3058
3059/*
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003060 * Try matching the regexp in "rmp->regprog" with file name "name".
3061 * Note that rmp->regprog may become NULL when switching regexp engine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 * Return "name" when there is a match, NULL when not.
3063 */
3064 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003065fname_match(
3066 regmatch_T *rmp,
3067 char_u *name,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003068 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069{
3070 char_u *match = NULL;
3071 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003073 // extra check for valid arguments
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003074 if (name == NULL || rmp->regprog == NULL)
3075 return NULL;
3076
3077 // Ignore case when 'fileignorecase' or the argument is set.
3078 rmp->rm_ic = p_fic || ignore_case;
3079 if (vim_regexec(rmp, name, (colnr_T)0))
3080 match = name;
3081 else if (rmp->regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003083 // Replace $(HOME) with '~' and try matching again.
3084 p = home_replace_save(NULL, name);
3085 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 match = name;
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003087 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 }
3089
3090 return match;
3091}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092
3093/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02003094 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 */
3096 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003097buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098{
Bram Moolenaar480778b2016-07-14 22:09:39 +02003099 char_u key[VIM_SIZEOF_INT * 2 + 1];
3100 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101
3102 if (nr == 0)
3103 nr = curwin->w_alt_fnum;
John Marriottec032de2025-04-10 21:34:19 +02003104 vim_snprintf((char *)key, sizeof(key), "%x", nr);
Bram Moolenaar480778b2016-07-14 22:09:39 +02003105 hi = hash_find(&buf_hashtab, key);
3106
3107 if (!HASHITEM_EMPTY(hi))
3108 return (buf_T *)(hi->hi_key
3109 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 return NULL;
3111}
3112
3113/*
3114 * Get name of file 'n' in the buffer list.
3115 * When the file has no name an empty string is returned.
3116 * home_replace() is used to shorten the file name (used for marks).
3117 * Returns a pointer to allocated memory, of NULL when failed.
3118 */
3119 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003120buflist_nr2name(
3121 int n,
3122 int fullname,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003123 int helptail) // for help buffers return tail only
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124{
3125 buf_T *buf;
3126
3127 buf = buflist_findnr(n);
3128 if (buf == NULL)
3129 return NULL;
3130 return home_replace_save(helptail ? buf : NULL,
3131 fullname ? buf->b_ffname : buf->b_fname);
3132}
3133
3134/*
3135 * Set the "lnum" and "col" for the buffer "buf" and the current window.
3136 * When "copy_options" is TRUE save the local window option values.
3137 * When "lnum" is 0 only do the options.
3138 */
Bram Moolenaardefa0672019-07-21 19:25:37 +02003139 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003140buflist_setfpos(
3141 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003142 win_T *win, // may be NULL when using :badd
Bram Moolenaar7454a062016-01-30 15:14:10 +01003143 linenr_T lnum,
3144 colnr_T col,
3145 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146{
3147 wininfo_T *wip;
3148
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003149 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 if (wip->wi_win == win)
3151 break;
3152 if (wip == NULL)
3153 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003154 // allocate a new entry
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003155 wip = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 if (wip == NULL)
3157 return;
3158 wip->wi_win = win;
Bram Moolenaarc667da52019-11-30 20:52:27 +01003159 if (lnum == 0) // set lnum even when it's 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 lnum = 1;
3161 }
3162 else
3163 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003164 // remove the entry from the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 if (wip->wi_prev)
3166 wip->wi_prev->wi_next = wip->wi_next;
3167 else
3168 buf->b_wininfo = wip->wi_next;
3169 if (wip->wi_next)
3170 wip->wi_next->wi_prev = wip->wi_prev;
3171 if (copy_options && wip->wi_optset)
3172 {
3173 clear_winopt(&wip->wi_opt);
3174#ifdef FEAT_FOLDING
3175 deleteFoldRecurse(&wip->wi_folds);
3176#endif
3177 }
3178 }
3179 if (lnum != 0)
3180 {
3181 wip->wi_fpos.lnum = lnum;
3182 wip->wi_fpos.col = col;
3183 }
LemonBoydb0ea7f2022-04-10 17:59:26 +01003184 if (win != NULL)
3185 wip->wi_changelistidx = win->w_changelistidx;
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003186 if (copy_options && win != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003188 // Save the window-specific option values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
3190#ifdef FEAT_FOLDING
3191 wip->wi_fold_manual = win->w_fold_manual;
3192 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
3193#endif
3194 wip->wi_optset = TRUE;
3195 }
3196
Bram Moolenaarc667da52019-11-30 20:52:27 +01003197 // insert the entry in front of the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 wip->wi_next = buf->b_wininfo;
3199 buf->b_wininfo = wip;
3200 wip->wi_prev = NULL;
3201 if (wip->wi_next)
3202 wip->wi_next->wi_prev = wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203}
3204
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003205#ifdef FEAT_DIFF
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003206/*
3207 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
3208 * page. That's because a diff is local to a tab page.
3209 */
3210 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003211wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003212{
3213 win_T *wp;
3214
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003215 if (!wip->wi_opt.wo_diff)
3216 return FALSE;
3217
3218 FOR_ALL_WINDOWS(wp)
3219 // return FALSE when it's a window in the current tab page, thus
3220 // the buffer was in diff mode here
3221 if (wip->wi_win == wp)
3222 return FALSE;
3223 return TRUE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003224}
3225#endif
3226
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227/*
3228 * Find info for the current window in buffer "buf".
3229 * If not found, return the info for the most recently used window.
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003230 * When "need_options" is TRUE skip entries where wi_optset is FALSE.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003231 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
3232 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 * Returns NULL when there isn't any info.
3234 */
3235 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003236find_wininfo(
3237 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003238 int need_options,
Bram Moolenaar7454a062016-01-30 15:14:10 +01003239 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240{
3241 wininfo_T *wip;
3242
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003243 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003244 if (wip->wi_win == curwin
3245#ifdef FEAT_DIFF
3246 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
3247#endif
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003248
3249 && (!need_options || wip->wi_optset))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003251
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003252 if (wip != NULL)
3253 return wip;
3254
Bram Moolenaarc667da52019-11-30 20:52:27 +01003255 // If no wininfo for curwin, use the first in the list (that doesn't have
3256 // 'diff' set and is in another tab page).
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003257 // If "need_options" is TRUE skip entries that don't have options set,
3258 // unless the window is editing "buf", so we can copy from the window
3259 // itself.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003260#ifdef FEAT_DIFF
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003261 if (skip_diff_buffer)
3262 {
3263 FOR_ALL_BUF_WININFO(buf, wip)
3264 if (!wininfo_other_tab_diff(wip)
3265 && (!need_options || wip->wi_optset
3266 || (wip->wi_win != NULL
3267 && wip->wi_win->w_buffer == buf)))
3268 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003269 }
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003270 else
3271#endif
3272 wip = buf->b_wininfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 return wip;
3274}
3275
3276/*
3277 * Reset the local window options to the values last used in this window.
3278 * If the buffer wasn't used in this window before, use the values from
3279 * the most recently used window. If the values were never set, use the
3280 * global values for the window.
3281 */
3282 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003283get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284{
3285 wininfo_T *wip;
3286
3287 clear_winopt(&curwin->w_onebuf_opt);
3288#ifdef FEAT_FOLDING
3289 clearFolding(curwin);
3290#endif
3291
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003292 wip = find_wininfo(buf, TRUE, TRUE);
Bram Moolenaar25782a72018-05-13 18:05:33 +02003293 if (wip != NULL && wip->wi_win != NULL
3294 && wip->wi_win != curwin && wip->wi_win->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003296 // The buffer is currently displayed in the window: use the actual
3297 // option values instead of the saved (possibly outdated) values.
Bram Moolenaar25782a72018-05-13 18:05:33 +02003298 win_T *wp = wip->wi_win;
3299
3300 copy_winopt(&wp->w_onebuf_opt, &curwin->w_onebuf_opt);
3301#ifdef FEAT_FOLDING
3302 curwin->w_fold_manual = wp->w_fold_manual;
3303 curwin->w_foldinvalid = TRUE;
3304 cloneFoldGrowArray(&wp->w_folds, &curwin->w_folds);
3305#endif
3306 }
3307 else if (wip != NULL && wip->wi_optset)
3308 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003309 // the buffer was displayed in the current window earlier
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
3311#ifdef FEAT_FOLDING
3312 curwin->w_fold_manual = wip->wi_fold_manual;
3313 curwin->w_foldinvalid = TRUE;
3314 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
3315#endif
3316 }
3317 else
3318 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
LemonBoydb0ea7f2022-04-10 17:59:26 +01003319 if (wip != NULL)
3320 curwin->w_changelistidx = wip->wi_changelistidx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321
3322#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +01003323 // Set 'foldlevel' to 'foldlevelstart' if it's not negative.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 if (p_fdls >= 0)
3325 curwin->w_p_fdl = p_fdls;
3326#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02003327 after_copy_winopt(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328}
3329
3330/*
3331 * Find the position (lnum and col) for the buffer 'buf' for the current
3332 * window.
3333 * Returns a pointer to no_position if no position is found.
3334 */
3335 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003336buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337{
3338 wininfo_T *wip;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003339 static pos_T no_position = {1, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003341 wip = find_wininfo(buf, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 if (wip != NULL)
3343 return &(wip->wi_fpos);
3344 else
3345 return &no_position;
3346}
3347
3348/*
3349 * Find the lnum for the buffer 'buf' for the current window.
3350 */
3351 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01003352buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353{
3354 return buflist_findfpos(buf)->lnum;
3355}
3356
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02003358 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003361buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362{
Bram Moolenaar52410572019-10-27 05:12:45 +01003363 buf_T *buf = firstbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 int len;
3365 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003366 int ro_char;
3367 int changed_char;
Bram Moolenaar0751f512018-03-29 16:37:16 +02003368#ifdef FEAT_TERMINAL
3369 int job_running;
3370 int job_none_open;
3371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372
Bram Moolenaar52410572019-10-27 05:12:45 +01003373#ifdef FEAT_VIMINFO
3374 garray_T buflist;
3375 buf_T **buflist_data = NULL, **p;
3376
3377 if (vim_strchr(eap->arg, 't'))
3378 {
3379 ga_init2(&buflist, sizeof(buf_T *), 50);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003380 FOR_ALL_BUFFERS(buf)
Bram Moolenaar52410572019-10-27 05:12:45 +01003381 {
3382 if (ga_grow(&buflist, 1) == OK)
3383 ((buf_T **)buflist.ga_data)[buflist.ga_len++] = buf;
3384 }
3385
3386 qsort(buflist.ga_data, (size_t)buflist.ga_len,
3387 sizeof(buf_T *), buf_compare);
3388
Bram Moolenaar3b991522019-11-06 23:26:20 +01003389 buflist_data = (buf_T **)buflist.ga_data;
3390 buf = *buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003391 }
Bram Moolenaar3b991522019-11-06 23:26:20 +01003392 p = buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003393
Bram Moolenaar3b991522019-11-06 23:26:20 +01003394 for (; buf != NULL && !got_int; buf = buflist_data != NULL
Bram Moolenaar52410572019-10-27 05:12:45 +01003395 ? (++p < buflist_data + buflist.ga_len ? *p : NULL)
3396 : buf->b_next)
3397#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
Bram Moolenaar52410572019-10-27 05:12:45 +01003399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 {
John Marriottec032de2025-04-10 21:34:19 +02003401 char_u *name;
3402
Bram Moolenaar0751f512018-03-29 16:37:16 +02003403#ifdef FEAT_TERMINAL
3404 job_running = term_job_running(buf->b_term);
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003405 job_none_open = term_none_open(buf->b_term);
Bram Moolenaar0751f512018-03-29 16:37:16 +02003406#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003407 // skip unlisted buffers, unless ! was used
Bram Moolenaard51cb702015-07-21 15:03:06 +02003408 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
3409 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
3410 || (vim_strchr(eap->arg, '+')
3411 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
3412 || (vim_strchr(eap->arg, 'a')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003413 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
Bram Moolenaard51cb702015-07-21 15:03:06 +02003414 || (vim_strchr(eap->arg, 'h')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003415 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3416#ifdef FEAT_TERMINAL
3417 || (vim_strchr(eap->arg, 'R')
3418 && (!job_running || (job_running && job_none_open)))
3419 || (vim_strchr(eap->arg, '?')
3420 && (!job_running || (job_running && !job_none_open)))
3421 || (vim_strchr(eap->arg, 'F')
3422 && (job_running || buf->b_term == NULL))
3423#endif
Bram Moolenaard51cb702015-07-21 15:03:06 +02003424 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3425 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3426 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3427 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3428 || (vim_strchr(eap->arg, '#')
3429 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 continue;
John Marriottec032de2025-04-10 21:34:19 +02003431 name = buf_spname(buf);
3432 if (name != NULL)
3433 vim_strncpy(NameBuff, name, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 else
3435 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003436 if (message_filtered(NameBuff))
3437 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003439 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3440 : (bufIsChanged(buf) ? '+' : ' ');
3441#ifdef FEAT_TERMINAL
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003442 if (job_running)
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003443 {
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003444 if (job_none_open)
Bram Moolenaar4033c552017-09-16 20:54:51 +02003445 ro_char = '?';
3446 else
3447 ro_char = 'R';
Bram Moolenaarc667da52019-11-30 20:52:27 +01003448 changed_char = ' '; // bufIsChanged() returns TRUE to avoid
3449 // closing, but it's not actually changed.
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003450 }
3451 else if (buf->b_term != NULL)
3452 ro_char = 'F';
3453 else
3454#endif
3455 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3456
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003457 msg_putchar('\n');
John Marriottec032de2025-04-10 21:34:19 +02003458 len = (int)vim_snprintf_safelen((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 buf->b_fnum,
3460 buf->b_p_bl ? ' ' : 'u',
3461 buf == curbuf ? '%' :
3462 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3463 buf->b_ml.ml_mfp == NULL ? ' ' :
3464 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003465 ro_char,
3466 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003467 NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468
Bram Moolenaarc667da52019-11-30 20:52:27 +01003469 // put "line 999" in column 40 or after the file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 i = 40 - vim_strsize(IObuff);
3471 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 IObuff[len++] = ' ';
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003473 while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar52410572019-10-27 05:12:45 +01003474#ifdef FEAT_VIMINFO
3475 if (vim_strchr(eap->arg, 't') && buf->b_last_used)
3476 add_time(IObuff + len, (size_t)(IOSIZE - len), buf->b_last_used);
3477 else
3478#endif
3479 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3480 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003481 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 msg_outtrans(IObuff);
Bram Moolenaarc667da52019-11-30 20:52:27 +01003483 out_flush(); // output one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 ui_breakcheck();
3485 }
Bram Moolenaar52410572019-10-27 05:12:45 +01003486
3487#ifdef FEAT_VIMINFO
3488 if (buflist_data)
3489 ga_clear(&buflist);
3490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492
3493/*
3494 * Get file name and line number for file 'fnum'.
3495 * Used by DoOneCmd() for translating '%' and '#'.
3496 * Used by insert_reg() and cmdline_paste() for '#' register.
3497 * Return FAIL if not found, OK for success.
3498 */
3499 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003500buflist_name_nr(
3501 int fnum,
3502 char_u **fname,
3503 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504{
3505 buf_T *buf;
3506
3507 buf = buflist_findnr(fnum);
3508 if (buf == NULL || buf->b_fname == NULL)
3509 return FAIL;
3510
3511 *fname = buf->b_fname;
3512 *lnum = buflist_findlnum(buf);
3513
3514 return OK;
3515}
3516
3517/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003518 * Set the file name for "buf"' to "ffname_arg", short file name to
3519 * "sfname_arg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 * The file name with the full path is also remembered, for when :cd is used.
3521 * Returns FAIL for failure (file name already in use by other buffer)
3522 * OK otherwise.
3523 */
3524 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003525setfname(
3526 buf_T *buf,
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003527 char_u *ffname_arg,
3528 char_u *sfname_arg,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003529 int message) // give message when buffer already exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003531 char_u *ffname = ffname_arg;
3532 char_u *sfname = sfname_arg;
Bram Moolenaar81695252004-12-29 20:58:21 +00003533 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003535 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536#endif
3537
3538 if (ffname == NULL || *ffname == NUL)
3539 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003540 // Removing the name.
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003541 if (buf->b_sfname != buf->b_ffname)
3542 VIM_CLEAR(buf->b_sfname);
3543 else
3544 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003545 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546#ifdef UNIX
3547 st.st_dev = (dev_T)-1;
3548#endif
3549 }
3550 else
3551 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003552 fname_expand(buf, &ffname, &sfname); // will allocate ffname
3553 if (ffname == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 return FAIL;
3555
3556 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01003557 * If the file name is already used in another buffer:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 * - if the buffer is loaded, fail
3559 * - if the buffer is not loaded, delete it from the list
3560 */
3561#ifdef UNIX
3562 if (mch_stat((char *)ffname, &st) < 0)
3563 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003564#endif
3565 if (!(buf->b_flags & BF_DUMMY))
3566#ifdef UNIX
3567 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003569 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570#endif
3571 if (obuf != NULL && obuf != buf)
3572 {
Bram Moolenaard3710cf2021-10-04 23:13:13 +01003573 win_T *win;
3574 tabpage_T *tab;
3575 int in_use = FALSE;
3576
3577 // during startup a window may use a buffer that is not loaded yet
3578 FOR_ALL_TAB_WINDOWS(tab, win)
3579 if (win->w_buffer == obuf)
3580 in_use = TRUE;
3581
3582 // it's loaded or used in a window, fail
3583 if (obuf->b_ml.ml_mfp != NULL || in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 {
3585 if (message)
Bram Moolenaare1242042021-12-16 20:56:57 +00003586 emsg(_(e_buffer_with_this_name_already_exists));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 vim_free(ffname);
3588 return FAIL;
3589 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003590 // delete from the list
Bram Moolenaara6e8f882019-12-14 16:18:15 +01003591 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 }
3593 sfname = vim_strsave(sfname);
3594 if (ffname == NULL || sfname == NULL)
3595 {
3596 vim_free(sfname);
3597 vim_free(ffname);
3598 return FAIL;
3599 }
3600#ifdef USE_FNAME_CASE
Bram Moolenaarc667da52019-11-30 20:52:27 +01003601 fname_case(sfname, 0); // set correct case for short file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602#endif
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003603 if (buf->b_sfname != buf->b_ffname)
3604 vim_free(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 vim_free(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 buf->b_ffname = ffname;
3607 buf->b_sfname = sfname;
3608 }
3609 buf->b_fname = buf->b_sfname;
3610#ifdef UNIX
3611 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003612 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 else
3614 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003615 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 buf->b_dev = st.st_dev;
3617 buf->b_ino = st.st_ino;
3618 }
3619#endif
3620
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622
3623 buf_name_changed(buf);
3624 return OK;
3625}
3626
3627/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003628 * Crude way of changing the name of a buffer. Use with care!
3629 * The name should be relative to the current directory.
3630 */
3631 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003632buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003633{
3634 buf_T *buf;
3635
3636 buf = buflist_findnr(fnum);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00003637 if (buf == NULL)
3638 return;
3639
3640 if (buf->b_sfname != buf->b_ffname)
3641 vim_free(buf->b_sfname);
3642 vim_free(buf->b_ffname);
3643 buf->b_ffname = vim_strsave(name);
3644 buf->b_sfname = NULL;
3645 // Allocate ffname and expand into full path. Also resolves .lnk
3646 // files on Win32.
3647 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
3648 buf->b_fname = buf->b_sfname;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003649}
3650
3651/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 * Take care of what needs to be done when the name of buffer "buf" has
3653 * changed.
3654 */
3655 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003656buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657{
3658 /*
3659 * If the file name changed, also change the name of the swapfile
3660 */
3661 if (buf->b_ml.ml_mfp != NULL)
3662 ml_setname(buf);
3663
Bram Moolenaar3ad69532021-11-19 17:01:08 +00003664#ifdef FEAT_TERMINAL
3665 if (buf->b_term != NULL)
3666 term_clear_status_text(buf->b_term);
3667#endif
3668
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 if (curwin->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003670 check_arg_idx(curwin); // check file name for arg list
Bram Moolenaarc667da52019-11-30 20:52:27 +01003671 maketitle(); // set window title
Bram Moolenaarc667da52019-11-30 20:52:27 +01003672 status_redraw_all(); // status lines need to be redrawn
3673 fmarks_check_names(buf); // check named file marks
3674 ml_timestamp(buf); // reset timestamp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675}
3676
3677/*
3678 * set alternate file name for current window
3679 *
3680 * Used by do_one_cmd(), do_write() and do_ecmd().
3681 * Return the buffer.
3682 */
3683 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003684setaltfname(
3685 char_u *ffname,
3686 char_u *sfname,
3687 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688{
3689 buf_T *buf;
3690
Bram Moolenaarc667da52019-11-30 20:52:27 +01003691 // Create a buffer. 'buflisted' is not set if it's a new buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaare1004402020-10-24 20:49:43 +02003693 if (buf != NULL && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 curwin->w_alt_fnum = buf->b_fnum;
3695 return buf;
3696}
3697
3698/*
3699 * Get alternate file name for current window.
3700 * Return NULL if there isn't any, and give error message if requested.
3701 */
3702 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003703getaltfname(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003704 int errmsg) // give error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705{
3706 char_u *fname;
3707 linenr_T dummy;
3708
3709 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3710 {
3711 if (errmsg)
Bram Moolenaar108010a2021-06-27 22:03:33 +02003712 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 return NULL;
3714 }
3715 return fname;
3716}
3717
3718/*
3719 * Add a file name to the buflist and return its number.
3720 * Uses same flags as buflist_new(), except BLN_DUMMY.
3721 *
3722 * used by qf_init(), main() and doarglist()
3723 */
3724 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003725buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726{
3727 buf_T *buf;
3728
3729 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3730 if (buf != NULL)
3731 return buf->b_fnum;
3732 return 0;
3733}
3734
3735#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3736/*
3737 * Adjust slashes in file names. Called after 'shellslash' was set.
3738 */
3739 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003740buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741{
3742 buf_T *bp;
3743
Bram Moolenaar29323592016-07-24 22:04:11 +02003744 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 {
3746 if (bp->b_ffname != NULL)
3747 slash_adjust(bp->b_ffname);
3748 if (bp->b_sfname != NULL)
3749 slash_adjust(bp->b_sfname);
3750 }
3751}
3752#endif
3753
3754/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003755 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 * Also save the local window option values.
3757 */
3758 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003759buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003761 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762}
3763
3764/*
3765 * Return TRUE if 'ffname' is not the same file as current file.
3766 * Fname must have a full path (expanded by mch_FullName()).
3767 */
3768 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003769otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770{
3771 return otherfile_buf(curbuf, ffname
3772#ifdef UNIX
3773 , NULL
3774#endif
3775 );
3776}
3777
3778 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003779otherfile_buf(
3780 buf_T *buf,
3781 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003783 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003785 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786{
Bram Moolenaarc667da52019-11-30 20:52:27 +01003787 // no name is different
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3789 return TRUE;
3790 if (fnamecmp(ffname, buf->b_ffname) == 0)
3791 return FALSE;
3792#ifdef UNIX
3793 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003794 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795
Bram Moolenaarc667da52019-11-30 20:52:27 +01003796 // If no stat_T given, get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 if (stp == NULL)
3798 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003799 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 st.st_dev = (dev_T)-1;
3801 stp = &st;
3802 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003803 // Use dev/ino to check if the files are the same, even when the names
3804 // are different (possible with links). Still need to compare the
3805 // name above, for when the file doesn't exist yet.
3806 // Problem: The dev/ino changes when a file is deleted (and created
3807 // again) and remains the same when renamed/moved. We don't want to
3808 // mch_stat() each buffer each time, that would be too slow. Get the
3809 // dev/ino again when they appear to match, but not when they appear
3810 // to be different: Could skip a buffer when it's actually the same
3811 // file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 if (buf_same_ino(buf, stp))
3813 {
3814 buf_setino(buf);
3815 if (buf_same_ino(buf, stp))
3816 return FALSE;
3817 }
3818 }
3819#endif
3820 return TRUE;
3821}
3822
3823#if defined(UNIX) || defined(PROTO)
3824/*
3825 * Set inode and device number for a buffer.
3826 * Must always be called when b_fname is changed!.
3827 */
3828 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003829buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003831 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832
3833 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3834 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003835 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 buf->b_dev = st.st_dev;
3837 buf->b_ino = st.st_ino;
3838 }
3839 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003840 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841}
3842
3843/*
3844 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3845 */
3846 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003847buf_same_ino(
3848 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003849 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003851 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 && stp->st_dev == buf->b_dev
3853 && stp->st_ino == buf->b_ino);
3854}
3855#endif
3856
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003857/*
3858 * Print info about the current buffer.
3859 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003861fileinfo(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003862 int fullname, // when non-zero print full path
Bram Moolenaar7454a062016-01-30 15:14:10 +01003863 int shorthelp,
3864 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865{
3866 char_u *name;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003867 char *buffer;
John Marriottec032de2025-04-10 21:34:19 +02003868 size_t bufferlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003870 buffer = alloc(IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 if (buffer == NULL)
3872 return;
3873
Bram Moolenaarc667da52019-11-30 20:52:27 +01003874 if (fullname > 1) // 2 CTRL-G: include buffer number
John Marriottec032de2025-04-10 21:34:19 +02003875 bufferlen = vim_snprintf_safelen(buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876
John Marriottec032de2025-04-10 21:34:19 +02003877 buffer[bufferlen++] = '"';
3878
3879 name = buf_spname(curbuf);
3880 if (name != NULL)
3881 bufferlen += vim_snprintf_safelen(buffer + bufferlen,
3882 IOSIZE - bufferlen, "%s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 else
3884 {
3885 if (!fullname && curbuf->b_fname != NULL)
3886 name = curbuf->b_fname;
3887 else
3888 name = curbuf->b_ffname;
John Marriottec032de2025-04-10 21:34:19 +02003889 home_replace(shorthelp ? curbuf : NULL, name, (char_u *)buffer + bufferlen,
3890 IOSIZE - (int)bufferlen, TRUE);
3891 bufferlen += STRLEN(buffer + bufferlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 }
3893
John Marriottec032de2025-04-10 21:34:19 +02003894 bufferlen += vim_snprintf_safelen(
3895 buffer + bufferlen,
3896 IOSIZE - bufferlen,
3897 "\"%s%s%s%s%s%s",
3898 curbufIsChanged() ? (shortmess(SHM_MOD)
3899 ? " [+]" : _(" [Modified]")) : " ",
3900 (curbuf->b_flags & BF_NOTEDITED) && !bt_dontwrite(curbuf)
3901 ? _("[Not edited]") : "",
3902 (curbuf->b_flags & BF_NEW) && !bt_dontwrite(curbuf)
3903 ? new_file_message() : "",
3904 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "", curbuf->b_p_ro
3905 ? (shortmess(SHM_RO) ? _("[RO]") : _("[readonly]")) : "",
3906 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK) || curbuf->b_p_ro)
3907 ? " " : "");
3908
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 if (curbuf->b_ml.ml_flags & ML_EMPTY)
John Marriottec032de2025-04-10 21:34:19 +02003910 bufferlen += vim_snprintf_safelen(buffer + bufferlen,
3911 IOSIZE - bufferlen, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 else if (p_ru)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003913 // Current line and column are already on the screen -- webb
John Marriottec032de2025-04-10 21:34:19 +02003914 bufferlen += vim_snprintf_safelen(
3915 buffer + bufferlen,
3916 IOSIZE - bufferlen,
3917 NGETTEXT("%ld line --%d%%--", "%ld lines --%d%%--", curbuf->b_ml.ml_line_count),
3918 (long)curbuf->b_ml.ml_line_count,
3919 calc_percentage(curwin->w_cursor.lnum, curbuf->b_ml.ml_line_count));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 else
3921 {
John Marriottec032de2025-04-10 21:34:19 +02003922 bufferlen += vim_snprintf_safelen(
3923 buffer + bufferlen,
3924 IOSIZE - bufferlen,
3925 _("line %ld of %ld --%d%%-- col "),
3926 (long)curwin->w_cursor.lnum,
3927 (long)curbuf->b_ml.ml_line_count,
3928 calc_percentage(curwin->w_cursor.lnum, curbuf->b_ml.ml_line_count));
3929
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 validate_virtcol();
John Marriottec032de2025-04-10 21:34:19 +02003931 bufferlen += col_print((char_u *)buffer + bufferlen, IOSIZE - bufferlen,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3933 }
3934
John Marriottec032de2025-04-10 21:34:19 +02003935 (void)append_arg_number(curwin, (char_u *)buffer + bufferlen,
3936 IOSIZE - bufferlen, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937
3938 if (dont_truncate)
3939 {
John Marriottec032de2025-04-10 21:34:19 +02003940 int n;
3941
Bram Moolenaarc667da52019-11-30 20:52:27 +01003942 // Temporarily set msg_scroll to avoid the message being truncated.
3943 // First call msg_start() to get the message in the right place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 msg_start();
3945 n = msg_scroll;
3946 msg_scroll = TRUE;
3947 msg(buffer);
3948 msg_scroll = n;
3949 }
3950 else
3951 {
John Marriottec032de2025-04-10 21:34:19 +02003952 char *p = msg_trunc_attr(buffer, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaarc667da52019-11-30 20:52:27 +01003954 // Need to repeat the message after redrawing when:
3955 // - When restart_edit is set (otherwise there will be a delay
3956 // before redrawing).
3957 // - When the screen was scrolled but there is no wait-return
3958 // prompt.
Bram Moolenaar32526b32019-01-19 17:43:09 +01003959 set_keep_msg((char_u *)p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 }
3961
3962 vim_free(buffer);
3963}
3964
John Marriotta21240b2025-01-08 20:10:59 +01003965 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003966col_print(
3967 char_u *buf,
3968 size_t buflen,
3969 int col,
3970 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971{
3972 if (col == vcol)
John Marriottec032de2025-04-10 21:34:19 +02003973 return (int)vim_snprintf_safelen((char *)buf, buflen, "%d", col);
John Marriotta21240b2025-01-08 20:10:59 +01003974
John Marriottec032de2025-04-10 21:34:19 +02003975 return (int)vim_snprintf_safelen((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976}
3977
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978static char_u *lasttitle = NULL;
3979static char_u *lasticon = NULL;
3980
Bram Moolenaar84a93082018-06-16 22:58:15 +02003981/*
3982 * Put the file name in the title bar and icon of the window.
3983 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003985maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986{
Bram Moolenaar84a93082018-06-16 22:58:15 +02003987 char_u *title_str = NULL;
3988 char_u *icon_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 int mustset;
3990 char_u buf[IOSIZE];
John Marriottec032de2025-04-10 21:34:19 +02003991 size_t buflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992
3993 if (!redrawing())
3994 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003995 // Postpone updating the title when 'lazyredraw' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 need_maketitle = TRUE;
3997 return;
3998 }
3999
4000 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02004001 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004002 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003
4004 if (p_title)
4005 {
John Marriottec032de2025-04-10 21:34:19 +02004006 int maxlen = 0;
4007
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 if (p_titlelen > 0)
4009 {
4010 maxlen = p_titlelen * Columns / 100;
4011 if (maxlen < 10)
4012 maxlen = 10;
4013 }
4014
Bram Moolenaar84a93082018-06-16 22:58:15 +02004015 title_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 if (*p_titlestring != NUL)
4017 {
4018#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00004019 if (stl_syntax & STL_IN_TITLE)
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004020 build_stl_str_hl(curwin, title_str, sizeof(buf), p_titlestring,
4021 (char_u *)"titlestring", 0,
4022 0, maxlen, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 else
4024#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02004025 title_str = p_titlestring;
John Marriottec032de2025-04-10 21:34:19 +02004026 buflen = STRLEN(title_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 }
4028 else
4029 {
John Marriottec032de2025-04-10 21:34:19 +02004030 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031
John Marriottec032de2025-04-10 21:34:19 +02004032 // format: "<filename> [flags] <(path)> [argument info] <- servername>"
4033 // example:
4034 // buffer.c + (/home/vim/src) (1 of 2) - VIM
4035
4036 // reserve some space for different parts of the title.
4037 // use sizeof() to introduce 'size_t' so we don't have to
4038 // cast sizes to it.
4039#define SPACE_FOR_FNAME (sizeof(buf) - 100)
4040#define SPACE_FOR_DIR (sizeof(buf) - 20)
4041#define SPACE_FOR_ARGNR (sizeof(buf) - 10) // at least room for " - VIM"
4042
4043 // file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 if (curbuf->b_fname == NULL)
John Marriottec032de2025-04-10 21:34:19 +02004045 buflen = vim_snprintf_safelen((char *)buf,
4046 SPACE_FOR_FNAME, "%s", _("[No Name]"));
Bram Moolenaar21554412017-07-24 21:44:43 +02004047#ifdef FEAT_TERMINAL
4048 else if (curbuf->b_term != NULL)
John Marriottec032de2025-04-10 21:34:19 +02004049 buflen = vim_snprintf_safelen((char *)buf,
4050 SPACE_FOR_FNAME, "%s",
4051 term_get_status_text(curbuf->b_term));
Bram Moolenaar21554412017-07-24 21:44:43 +02004052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 else
4054 {
John Marriottec032de2025-04-10 21:34:19 +02004055 buflen = vim_snprintf_safelen((char *)buf,
4056 SPACE_FOR_FNAME, "%s",
4057 ((p = transstr(gettail(curbuf->b_fname))) != NULL)
4058 ? p
4059 : (char_u *)"");
4060 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 }
4062
John Marriottec032de2025-04-10 21:34:19 +02004063 // flags
Bram Moolenaar21554412017-07-24 21:44:43 +02004064#ifdef FEAT_TERMINAL
4065 if (curbuf->b_term == NULL)
4066#endif
John Marriottec032de2025-04-10 21:34:19 +02004067 {
Bram Moolenaar21554412017-07-24 21:44:43 +02004068 switch (bufIsChanged(curbuf)
4069 + (curbuf->b_p_ro * 2)
4070 + (!curbuf->b_p_ma * 4))
4071 {
John Marriottec032de2025-04-10 21:34:19 +02004072 case 1:
4073 // file was modified
4074 buflen += vim_snprintf_safelen(
4075 (char *)buf + buflen,
4076 sizeof(buf) - buflen, " +");
4077 break;
4078 case 2:
4079 // file is readonly
4080 buflen += vim_snprintf_safelen(
4081 (char *)buf + buflen,
4082 sizeof(buf) - buflen, " =");
4083 break;
4084 case 3:
4085 // file was modified and is readonly
4086 buflen += vim_snprintf_safelen(
4087 (char *)buf + buflen,
4088 sizeof(buf) - buflen, " =+");
4089 break;
Bram Moolenaar21554412017-07-24 21:44:43 +02004090 case 4:
John Marriottec032de2025-04-10 21:34:19 +02004091 case 6:
4092 // file cannot be modified
4093 buflen += vim_snprintf_safelen(
4094 (char *)buf + buflen,
4095 sizeof(buf) - buflen, " -");
4096 break;
Bram Moolenaar21554412017-07-24 21:44:43 +02004097 case 5:
John Marriottec032de2025-04-10 21:34:19 +02004098 case 7:
4099 // file cannot be modified but was modified
4100 buflen += vim_snprintf_safelen(
4101 (char *)buf + buflen,
4102 sizeof(buf) - buflen, " -+");
4103 break;
4104 default:
4105 break;
Bram Moolenaar21554412017-07-24 21:44:43 +02004106 }
John Marriottec032de2025-04-10 21:34:19 +02004107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108
John Marriottec032de2025-04-10 21:34:19 +02004109 // path (surrounded by '()')
Bram Moolenaar21554412017-07-24 21:44:43 +02004110 if (curbuf->b_fname != NULL
4111#ifdef FEAT_TERMINAL
4112 && curbuf->b_term == NULL
4113#endif
4114 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004116 // Get path of file, replace home dir with ~
John Marriottec032de2025-04-10 21:34:19 +02004117 buflen += vim_snprintf_safelen((char *)buf + buflen,
4118 sizeof(buf) - buflen, " (");
4119
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 home_replace(curbuf, curbuf->b_ffname,
John Marriottec032de2025-04-10 21:34:19 +02004121 buf + buflen, (int)(SPACE_FOR_DIR - buflen), TRUE);
4122
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc667da52019-11-30 20:52:27 +01004124 // avoid "c:/name" to be reduced to "c"
John Marriottec032de2025-04-10 21:34:19 +02004125 if (SAFE_isalpha(buf[buflen]) && buf[buflen + 1] == ':')
4126 buflen += 2; // step over "c:"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127#endif
John Marriottec032de2025-04-10 21:34:19 +02004128
4129 // determine if we have a help or normal buffer
4130 p = gettail_sep(buf + buflen);
4131 if (p == buf + buflen)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02004132 {
John Marriottec032de2025-04-10 21:34:19 +02004133 // help buffer
4134 buflen += vim_snprintf_safelen((char *)buf + buflen,
4135 SPACE_FOR_DIR - buflen, "%s)", _("help"));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02004136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 else
Bram Moolenaar2c666692012-09-05 13:30:40 +02004138 {
John Marriottec032de2025-04-10 21:34:19 +02004139 // normal buffer
4140
4141 // Translate unprintable chars and concatenate. Keep some
4142 // room for the server name. When there is no room (very long
4143 // file name) use (...).
4144 if (buflen < SPACE_FOR_DIR)
John Marriott7fb90812025-04-02 20:32:35 +02004145 {
John Marriottec032de2025-04-10 21:34:19 +02004146 // remove the file name
4147 *p = NUL;
4148
4149 buflen += vim_snprintf_safelen((char *)buf + buflen,
4150 SPACE_FOR_DIR - buflen, "%s)",
4151 ((p = transstr(buf + buflen)) != NULL)
4152 ? p
4153 : (char_u *)"");
John Marriott7fb90812025-04-02 20:32:35 +02004154 vim_free(p);
4155 }
John Marriottec032de2025-04-10 21:34:19 +02004156 else
4157 buflen += vim_snprintf_safelen((char *)buf + buflen,
4158 SPACE_FOR_ARGNR - buflen, "...)");
Bram Moolenaar2c666692012-09-05 13:30:40 +02004159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 }
4161
John Marriottec032de2025-04-10 21:34:19 +02004162 // argument info
4163 buflen += append_arg_number(curwin, buf + buflen,
4164 SPACE_FOR_ARGNR - buflen, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165
John Marriottec032de2025-04-10 21:34:19 +02004166 // servername
4167 buflen += vim_snprintf_safelen((char *)buf + buflen,
4168 sizeof(buf) - buflen, " - %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169#if defined(FEAT_CLIENTSERVER)
John Marriottec032de2025-04-10 21:34:19 +02004170 (serverName != NULL)
4171 ? serverName :
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172#endif
John Marriottec032de2025-04-10 21:34:19 +02004173 (char_u *)"VIM");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174
4175 if (maxlen > 0)
4176 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004177 // make it shorter by removing a bit in the middle
Bram Moolenaarf31b7642012-01-20 20:44:43 +01004178 if (vim_strsize(buf) > maxlen)
John Marriottec032de2025-04-10 21:34:19 +02004179 trunc_string(buf, buf, maxlen, sizeof(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 }
4181 }
4182 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02004183 mustset = value_changed(title_str, &lasttitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184
4185 if (p_icon)
4186 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02004187 icon_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 if (*p_iconstring != NUL)
4189 {
4190#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00004191 if (stl_syntax & STL_IN_ICON)
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004192 build_stl_str_hl(curwin, icon_str, sizeof(buf), p_iconstring,
4193 (char_u *)"iconstring", 0, 0, 0, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 else
4195#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02004196 icon_str = p_iconstring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 }
4198 else
4199 {
John Marriottec032de2025-04-10 21:34:19 +02004200 char_u *name;
4201 int namelen;
4202
4203 name = buf_spname(curbuf);
4204 if (name == NULL)
4205 name = gettail(curbuf->b_ffname);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004206 // Truncate name at 100 bytes.
John Marriottec032de2025-04-10 21:34:19 +02004207 namelen = (int)STRLEN(name);
4208 if (namelen > 100)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 {
John Marriottec032de2025-04-10 21:34:19 +02004210 namelen -= 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 if (has_mbyte)
John Marriottec032de2025-04-10 21:34:19 +02004212 namelen += (*mb_tail_off)(name, name + namelen) + 1;
4213 name += namelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 }
John Marriottec032de2025-04-10 21:34:19 +02004215 STRCPY(buf, name);
4216 trans_characters(buf, sizeof(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 }
4218 }
4219
Bram Moolenaar84a93082018-06-16 22:58:15 +02004220 mustset |= value_changed(icon_str, &lasticon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221
4222 if (mustset)
4223 resettitle();
4224}
4225
4226/*
4227 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
4228 * from "str" if it does.
Bram Moolenaar84a93082018-06-16 22:58:15 +02004229 * Return TRUE if resettitle() is to be called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 */
4231 static int
Bram Moolenaar84a93082018-06-16 22:58:15 +02004232value_changed(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233{
4234 if ((str == NULL) != (*last == NULL)
4235 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
4236 {
4237 vim_free(*last);
4238 if (str == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004239 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 *last = NULL;
Bram Moolenaar40385db2018-08-07 22:31:44 +02004241 mch_restore_title(
4242 last == &lasttitle ? SAVE_RESTORE_TITLE : SAVE_RESTORE_ICON);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 else
Bram Moolenaar84a93082018-06-16 22:58:15 +02004245 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 *last = vim_strsave(str);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004247 return TRUE;
4248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 }
4250 return FALSE;
4251}
4252
4253/*
4254 * Put current window title back (used after calling a shell)
4255 */
4256 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004257resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258{
4259 mch_settitle(lasttitle, lasticon);
4260}
Bram Moolenaarea408852005-06-25 22:49:46 +00004261
4262# if defined(EXITFREE) || defined(PROTO)
4263 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004264free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00004265{
4266 vim_free(lasttitle);
4267 vim_free(lasticon);
4268}
4269# endif
4270
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004272#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004273
4274/*
4275 * Used for building in the status line.
4276 */
4277typedef struct
4278{
4279 char_u *stl_start;
4280 int stl_minwid;
4281 int stl_maxwid;
4282 enum {
4283 Normal,
4284 Empty,
4285 Group,
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004286 Separate,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004287 Highlight,
4288 TabPage,
4289 Trunc
4290 } stl_type;
4291} stl_item_T;
4292
4293static size_t stl_items_len = 20; // Initial value, grows as needed.
4294static stl_item_T *stl_items = NULL;
4295static int *stl_groupitem = NULL;
4296static stl_hlrec_T *stl_hltab = NULL;
4297static stl_hlrec_T *stl_tabtab = NULL;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004298static int *stl_separator_locations = NULL;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004299
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004301 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 * Return length of string in screen cells.
4303 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004304 * Normally works for window "wp", except when working for 'tabline' then it
4305 * is "curwin".
4306 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 * Items are drawn interspersed with the text that surrounds it
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004308 * Specials: %-<wid>(xxx%) => group, %= => separation marker, %< => truncation
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
4310 *
4311 * If maxwidth is not zero, the string will be filled at any middle marker
4312 * or truncated if too long, fillchar is used for all whitespace.
4313 */
4314 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004315build_stl_str_hl(
4316 win_T *wp,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004317 char_u *out, // buffer to write into != NameBuff
4318 size_t outlen, // length of out[]
Bram Moolenaar7454a062016-01-30 15:14:10 +01004319 char_u *fmt,
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004320 char_u *opt_name, // option name corresponding to "fmt"
4321 int opt_scope, // scope for "opt_name"
Bram Moolenaar7454a062016-01-30 15:14:10 +01004322 int fillchar,
4323 int maxwidth,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004324 stl_hlrec_T **hltab, // return: HL attributes (can be NULL)
4325 stl_hlrec_T **tabtab) // return: tab page nrs (can be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326{
Bram Moolenaar10772302019-01-20 18:25:54 +01004327 linenr_T lnum;
zeertzjq94b7c322024-03-12 21:50:32 +01004328 colnr_T len;
John Marriottec032de2025-04-10 21:34:19 +02004329 size_t outputlen; // length of out[] used (excluding the NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 char_u *p;
4331 char_u *s;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004332 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333#ifdef FEAT_EVAL
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004334 int use_sandbox;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004335 int save_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336#endif
4337 int empty_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 long l;
4339 long n;
4340 int prevchar_isflag;
4341 int prevchar_isitem;
4342 int itemisflag;
4343 int fillable;
4344 char_u *str;
4345 long num;
4346 int width;
4347 int itemcnt;
4348 int curitem;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 int groupdepth;
shadmansaleh30e3de22021-05-15 17:23:28 +02004350#ifdef FEAT_EVAL
4351 int evaldepth;
4352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 int minwid;
4354 int maxwid;
4355 int zeropad;
4356 char_u base;
4357 char_u opt;
4358#define TMPLEN 70
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004359 char_u buf_tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004360 char_u *usefmt = fmt;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004361 stl_hlrec_T *sp;
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01004362 int save_redraw_not_allowed = redraw_not_allowed;
Bram Moolenaar0e1f36f2022-02-15 16:17:44 +00004363 int save_KeyTyped = KeyTyped;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004364 // TODO: find out why using called_emsg_before makes tests fail, does it
4365 // matter?
4366 // int called_emsg_before = called_emsg;
4367 int did_emsg_before = did_emsg;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004368
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01004369 // When inside update_screen() we do not want redrawing a statusline,
4370 // ruler, title, etc. to trigger another redraw, it may cause an endless
4371 // loop.
4372 if (updating_screen)
4373 redraw_not_allowed = TRUE;
4374
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004375 if (stl_items == NULL)
4376 {
4377 stl_items = ALLOC_MULT(stl_item_T, stl_items_len);
4378 stl_groupitem = ALLOC_MULT(int, stl_items_len);
Brandon Richardsona493b652022-02-19 11:45:03 +00004379
4380 // Allocate one more, because the last element is used to indicate the
4381 // end of the list.
4382 stl_hltab = ALLOC_MULT(stl_hlrec_T, stl_items_len + 1);
4383 stl_tabtab = ALLOC_MULT(stl_hlrec_T, stl_items_len + 1);
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004384
4385 stl_separator_locations = ALLOC_MULT(int, stl_items_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004386 }
4387
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004388#ifdef FEAT_EVAL
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004389 // if "fmt" was set insecurely it needs to be evaluated in the sandbox
4390 use_sandbox = was_set_insecurely(opt_name, opt_scope);
4391
4392 // When the format starts with "%!" then evaluate it as an expression and
4393 // use the result as the actual format string.
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004394 if (fmt[0] == '%' && fmt[1] == '!')
4395 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004396 typval_T tv;
4397
4398 tv.v_type = VAR_NUMBER;
4399 tv.vval.v_number = wp->w_id;
4400 set_var((char_u *)"g:statusline_winid", &tv, FALSE);
4401
Bram Moolenaara4e0b972022-10-01 19:43:52 +01004402 usefmt = eval_to_string_safe(fmt + 2, use_sandbox, FALSE, FALSE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004403 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00004404 usefmt = fmt;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004405
4406 do_unlet((char_u *)"g:statusline_winid", TRUE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004407 }
4408#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409
4410 if (fillchar == 0)
4411 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004412
Bram Moolenaar10772302019-01-20 18:25:54 +01004413 // The cursor in windows other than the current one isn't always
4414 // up-to-date, esp. because of autocommands and timers.
4415 lnum = wp->w_cursor.lnum;
4416 if (lnum > wp->w_buffer->b_ml.ml_line_count)
4417 {
4418 lnum = wp->w_buffer->b_ml.ml_line_count;
4419 wp->w_cursor.lnum = lnum;
4420 }
4421
4422 // Get line & check if empty (cursorpos will show "0-1"). Note that
4423 // p will become invalid when getting another buffer line.
4424 p = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar567199b2013-04-24 16:52:36 +02004425 empty_line = (*p == NUL);
4426
Bram Moolenaar10772302019-01-20 18:25:54 +01004427 // Get the byte value now, in case we need it below. This is more efficient
4428 // than making a copy of the line.
zeertzjq94b7c322024-03-12 21:50:32 +01004429 len = ml_get_buf_len(wp->w_buffer, lnum);
4430 if (wp->w_cursor.col > len)
Bram Moolenaar10772302019-01-20 18:25:54 +01004431 {
4432 // Line may have changed since checking the cursor column, or the lnum
4433 // was adjusted above.
zeertzjq94b7c322024-03-12 21:50:32 +01004434 wp->w_cursor.col = len;
Bram Moolenaar10772302019-01-20 18:25:54 +01004435 wp->w_cursor.coladd = 0;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004436 byteval = 0;
Bram Moolenaar10772302019-01-20 18:25:54 +01004437 }
Bram Moolenaar567199b2013-04-24 16:52:36 +02004438 else
Bram Moolenaar567199b2013-04-24 16:52:36 +02004439 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440
4441 groupdepth = 0;
shadmansaleh30e3de22021-05-15 17:23:28 +02004442#ifdef FEAT_EVAL
4443 evaldepth = 0;
4444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 p = out;
4446 curitem = 0;
4447 prevchar_isflag = TRUE;
4448 prevchar_isitem = FALSE;
zeertzjq122dea72022-07-27 15:48:45 +01004449 for (s = usefmt; *s != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004451 if (curitem == (int)stl_items_len)
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004452 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004453 size_t new_len = stl_items_len * 3 / 2;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004454
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004455 stl_item_T *new_items =
4456 vim_realloc(stl_items, sizeof(stl_item_T) * new_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004457 if (new_items == NULL)
4458 break;
4459 stl_items = new_items;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004460
4461 int *new_groupitem =
4462 vim_realloc(stl_groupitem, sizeof(int) * new_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004463 if (new_groupitem == NULL)
4464 break;
4465 stl_groupitem = new_groupitem;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004466
4467 stl_hlrec_T *new_hlrec = vim_realloc(stl_hltab,
Brandon Richardsona493b652022-02-19 11:45:03 +00004468 sizeof(stl_hlrec_T) * (new_len + 1));
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004469 if (new_hlrec == NULL)
4470 break;
4471 stl_hltab = new_hlrec;
Brandon Richardsona493b652022-02-19 11:45:03 +00004472 new_hlrec = vim_realloc(stl_tabtab,
4473 sizeof(stl_hlrec_T) * (new_len + 1));
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004474 if (new_hlrec == NULL)
4475 break;
4476 stl_tabtab = new_hlrec;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004477
4478 int *new_separator_locs = vim_realloc(stl_separator_locations,
4479 sizeof(int) * new_len);
4480 if (new_separator_locs == NULL)
4481 break;
John Marriottec032de2025-04-10 21:34:19 +02004482 stl_separator_locations = new_separator_locs;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004483
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004484 stl_items_len = new_len;
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004485 }
4486
zeertzjq122dea72022-07-27 15:48:45 +01004487 if (*s != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 prevchar_isflag = prevchar_isitem = FALSE;
4489
4490 /*
4491 * Handle up to the next '%' or the end.
4492 */
4493 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
4494 *p++ = *s++;
4495 if (*s == NUL || p + 1 >= out + outlen)
4496 break;
4497
4498 /*
4499 * Handle one '%' item.
4500 */
4501 s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004502 if (*s == NUL) // ignore trailing %
Bram Moolenaar1d87f512011-02-01 21:55:01 +01004503 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 if (*s == '%')
4505 {
4506 if (p + 1 >= out + outlen)
4507 break;
4508 *p++ = *s++;
4509 prevchar_isflag = prevchar_isitem = FALSE;
4510 continue;
4511 }
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004512 // STL_SEPARATE: Separation between items, filled with white space.
4513 if (*s == STL_SEPARATE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 {
4515 s++;
4516 if (groupdepth > 0)
4517 continue;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004518 stl_items[curitem].stl_type = Separate;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004519 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 continue;
4521 }
4522 if (*s == STL_TRUNCMARK)
4523 {
4524 s++;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004525 stl_items[curitem].stl_type = Trunc;
4526 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 continue;
4528 }
4529 if (*s == ')')
4530 {
John Marriottec032de2025-04-10 21:34:19 +02004531 char_u *t;
4532
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 s++;
4534 if (groupdepth < 1)
4535 continue;
4536 groupdepth--;
4537
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004538 t = stl_items[stl_groupitem[groupdepth]].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 *p = NUL;
4540 l = vim_strsize(t);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004541 if (curitem > stl_groupitem[groupdepth] + 1
4542 && stl_items[stl_groupitem[groupdepth]].stl_minwid == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 {
John Marriottec032de2025-04-10 21:34:19 +02004544 int group_start_userhl = 0;
4545 int group_end_userhl = 0;
4546
Bram Moolenaarc667da52019-11-30 20:52:27 +01004547 // remove group if all items are empty and highlight group
4548 // doesn't change
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004549 for (n = stl_groupitem[groupdepth] - 1; n >= 0; n--)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004550 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004551 if (stl_items[n].stl_type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004552 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004553 group_start_userhl = group_end_userhl =
4554 stl_items[n].stl_minwid;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004555 break;
4556 }
4557 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004558 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004559 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004560 if (stl_items[n].stl_type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 break;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004562 if (stl_items[n].stl_type == Highlight)
4563 group_end_userhl = stl_items[n].stl_minwid;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004564 }
4565 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 {
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004567 // empty group
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 p = t;
4569 l = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004570 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004571 {
4572 // do not use the highlighting from the removed group
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004573 if (stl_items[n].stl_type == Highlight)
4574 stl_items[n].stl_type = Empty;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004575 // adjust the start position of TabPage to the next
4576 // item position
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004577 if (stl_items[n].stl_type == TabPage)
4578 stl_items[n].stl_start = p;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 }
4581 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004582 if (l > stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004584 // truncate, remove n bytes of text at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 if (has_mbyte)
4586 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004587 // Find the first character that should be included.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 n = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004589 while (l >= stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 {
4591 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004592 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 }
4594 }
4595 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004596 n = (long)(p - t) - stl_items[stl_groupitem[groupdepth]]
4597 .stl_maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598
4599 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004600 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 p = p - n + 1;
Bram Moolenaar13505972019-01-24 15:04:48 +01004602
4603 // Fill up space left over by half a double-wide char.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004604 while (++l < stl_items[stl_groupitem[groupdepth]].stl_minwid)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004605 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606
Bram Moolenaarc667da52019-11-30 20:52:27 +01004607 // correct the start of the items for the truncation
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004608 for (l = stl_groupitem[groupdepth] + 1; l < curitem; l++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 {
LemonBoy57ff5262022-05-09 21:03:47 +01004610 // Minus one for the leading '<' added above.
4611 stl_items[l].stl_start -= n - 1;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004612 if (stl_items[l].stl_start < t)
4613 stl_items[l].stl_start = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 }
4615 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004616 else if (abs(stl_items[stl_groupitem[groupdepth]].stl_minwid) > l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004618 // fill
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004619 n = stl_items[stl_groupitem[groupdepth]].stl_minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 if (n < 0)
4621 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004622 // fill by appending characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 n = 0 - n;
4624 while (l++ < n && p + 1 < out + outlen)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004625 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 }
4627 else
4628 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004629 // fill by inserting characters
Bram Moolenaar008bff92021-03-04 21:55:58 +01004630 l = (n - l) * MB_CHAR2LEN(fillchar);
4631 mch_memmove(t + l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004633 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 p += l;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004635 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
4636 stl_items[n].stl_start += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 for ( ; l > 0; l--)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004638 MB_CHAR2BYTES(fillchar, t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 }
4640 }
4641 continue;
4642 }
4643 minwid = 0;
4644 maxwid = 9999;
4645 zeropad = FALSE;
4646 l = 1;
4647 if (*s == '0')
4648 {
4649 s++;
4650 zeropad = TRUE;
4651 }
4652 if (*s == '-')
4653 {
4654 s++;
4655 l = -1;
4656 }
4657 if (VIM_ISDIGIT(*s))
4658 {
4659 minwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004660 if (minwid < 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 minwid = 0;
4662 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004663 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004665 stl_items[curitem].stl_type = Highlight;
4666 stl_items[curitem].stl_start = p;
4667 stl_items[curitem].stl_minwid = minwid > 9 ? 1 : minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 s++;
4669 curitem++;
4670 continue;
4671 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004672 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4673 {
4674 if (*s == STL_TABCLOSENR)
4675 {
4676 if (minwid == 0)
4677 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004678 // %X ends the close label, go back to the previously
4679 // define tab label nr.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004680 for (n = curitem - 1; n >= 0; --n)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004681 if (stl_items[n].stl_type == TabPage
4682 && stl_items[n].stl_minwid >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004683 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004684 minwid = stl_items[n].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004685 break;
4686 }
4687 }
4688 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01004689 // close nrs are stored as negative values
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004690 minwid = - minwid;
4691 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004692 stl_items[curitem].stl_type = TabPage;
4693 stl_items[curitem].stl_start = p;
4694 stl_items[curitem].stl_minwid = minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004695 s++;
4696 curitem++;
4697 continue;
4698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 if (*s == '.')
4700 {
4701 s++;
4702 if (VIM_ISDIGIT(*s))
4703 {
4704 maxwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004705 if (maxwid <= 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 maxwid = 50;
4707 }
4708 }
4709 minwid = (minwid > 50 ? 50 : minwid) * l;
4710 if (*s == '(')
4711 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004712 stl_groupitem[groupdepth++] = curitem;
4713 stl_items[curitem].stl_type = Group;
4714 stl_items[curitem].stl_start = p;
4715 stl_items[curitem].stl_minwid = minwid;
4716 stl_items[curitem].stl_maxwid = maxwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 s++;
4718 curitem++;
4719 continue;
4720 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004721#ifdef FEAT_EVAL
4722 // Denotes end of expanded %{} block
4723 if (*s == '}' && evaldepth > 0)
4724 {
4725 s++;
4726 evaldepth--;
4727 continue;
4728 }
4729#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 if (vim_strchr(STL_ALL, *s) == NULL)
4731 {
Bram Moolenaar7b17eb42023-01-04 14:31:49 +00004732 if (*s == NUL) // can happen with "%0"
4733 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 s++;
4735 continue;
4736 }
4737 opt = *s++;
4738
Bram Moolenaarc667da52019-11-30 20:52:27 +01004739 // OK - now for the real work
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 base = 'D';
4741 itemisflag = FALSE;
4742 fillable = TRUE;
4743 num = -1;
4744 str = NULL;
4745 switch (opt)
4746 {
4747 case STL_FILEPATH:
4748 case STL_FULLPATH:
4749 case STL_FILENAME:
John Marriottec032de2025-04-10 21:34:19 +02004750 {
4751 char_u *name;
4752
Bram Moolenaarc667da52019-11-30 20:52:27 +01004753 fillable = FALSE; // don't change ' ' to fillchar
John Marriottec032de2025-04-10 21:34:19 +02004754 name = buf_spname(wp->w_buffer);
4755 if (name != NULL)
4756 vim_strncpy(NameBuff, name, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 else
4758 {
John Marriottec032de2025-04-10 21:34:19 +02004759 char_u *t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004760 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4762 }
4763 trans_characters(NameBuff, MAXPATHL);
4764 if (opt != STL_FILENAME)
4765 str = NameBuff;
4766 else
4767 str = gettail(NameBuff);
4768 break;
John Marriottec032de2025-04-10 21:34:19 +02004769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770
Bram Moolenaarc667da52019-11-30 20:52:27 +01004771 case STL_VIM_EXPR: // '{'
shadmansaleh30e3de22021-05-15 17:23:28 +02004772 {
4773#ifdef FEAT_EVAL
John Marriottec032de2025-04-10 21:34:19 +02004774 char_u *block_start = s - 1;
shadmansaleh30e3de22021-05-15 17:23:28 +02004775#endif
John Marriottec032de2025-04-10 21:34:19 +02004776 int reevaluate = (*s == '%');
4777 char_u *t;
4778 buf_T *save_curbuf;
4779 win_T *save_curwin;
shadmansaleh30e3de22021-05-15 17:23:28 +02004780
4781 if (reevaluate)
4782 s++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 itemisflag = TRUE;
4784 t = p;
shadmansaleh30e3de22021-05-15 17:23:28 +02004785 while ((*s != '}' || (reevaluate && s[-1] != '%'))
4786 && *s != NUL && p + 1 < out + outlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 *p++ = *s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004788 if (*s != '}') // missing '}' or out of space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 break;
4790 s++;
shadmansaleh30e3de22021-05-15 17:23:28 +02004791 if (reevaluate)
John Marriottec032de2025-04-10 21:34:19 +02004792 p[-1] = NUL; // remove the % at the end of %{% expr %}
shadmansaleh30e3de22021-05-15 17:23:28 +02004793 else
John Marriottec032de2025-04-10 21:34:19 +02004794 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 p = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796#ifdef FEAT_EVAL
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004797 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp),
4798 "%d", curbuf->b_fnum);
4799 set_internal_string_var((char_u *)"g:actual_curbuf", buf_tmp);
John Marriottec032de2025-04-10 21:34:19 +02004800 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), "%d", curwin->w_id);
4801 set_internal_string_var((char_u *)"g:actual_curwin", buf_tmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004803 save_curbuf = curbuf;
4804 save_curwin = curwin;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004805 save_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 curwin = wp;
4807 curbuf = wp->w_buffer;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004808 // Visual mode is only valid in the current window.
4809 if (curwin != save_curwin)
4810 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811
Bram Moolenaara4e0b972022-10-01 19:43:52 +01004812 str = eval_to_string_safe(p, use_sandbox, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004814 curwin = save_curwin;
4815 curbuf = save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004816 VIsual_active = save_VIsual_active;
Bram Moolenaar01824652005-01-31 18:58:23 +00004817 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004818 do_unlet((char_u *)"g:actual_curwin", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819
John Marriottec032de2025-04-10 21:34:19 +02004820 if (str != NULL && *str != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 {
4822 if (*skipdigits(str) == NUL)
4823 {
4824 num = atoi((char *)str);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004825 VIM_CLEAR(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 itemisflag = FALSE;
4827 }
4828 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004829
4830 // If the output of the expression needs to be evaluated
4831 // replace the %{} block with the result of evaluation
John Marriottec032de2025-04-10 21:34:19 +02004832 if (reevaluate && str != NULL && *str != NUL
shadmansaleh30e3de22021-05-15 17:23:28 +02004833 && strchr((const char *)str, '%') != NULL
4834 && evaldepth < MAX_STL_EVAL_DEPTH)
4835 {
4836 size_t parsed_usefmt = (size_t)(block_start - usefmt);
Hirohito Higashic8ce81b2025-04-12 11:28:18 +02004837 size_t str_length = strlen((const char *)str);
4838 size_t fmt_length = strlen((const char *)s);
4839 size_t new_fmt_len = parsed_usefmt
4840 + str_length + fmt_length + 3;
4841 char_u *new_fmt = (char_u *)alloc(new_fmt_len * sizeof(char_u));
shadmansaleh30e3de22021-05-15 17:23:28 +02004842
John Marriott7fb90812025-04-02 20:32:35 +02004843 if (new_fmt != NULL)
4844 {
Hirohito Higashic8ce81b2025-04-12 11:28:18 +02004845 char_u *new_fmt_p = new_fmt;
4846
4847 new_fmt_p = (char_u *)memcpy(new_fmt_p, usefmt, parsed_usefmt)
4848 + parsed_usefmt;
4849 new_fmt_p = (char_u *)memcpy(new_fmt_p , str, str_length)
4850 + str_length;
4851 new_fmt_p = (char_u *)memcpy(new_fmt_p, "%}", 2) + 2;
4852 new_fmt_p = (char_u *)memcpy(new_fmt_p , s, fmt_length)
4853 + fmt_length;
4854 *new_fmt_p = 0;
4855 new_fmt_p = NULL;
John Marriott7fb90812025-04-02 20:32:35 +02004856
4857 if (usefmt != fmt)
4858 vim_free(usefmt);
4859 VIM_CLEAR(str);
4860 usefmt = new_fmt;
4861 s = usefmt + parsed_usefmt;
4862 evaldepth++;
4863 continue;
4864 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866#endif
4867 break;
shadmansaleh30e3de22021-05-15 17:23:28 +02004868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 case STL_LINE:
4870 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4871 ? 0L : (long)(wp->w_cursor.lnum);
4872 break;
4873
4874 case STL_NUMLINES:
4875 num = wp->w_buffer->b_ml.ml_line_count;
4876 break;
4877
4878 case STL_COLUMN:
Bram Moolenaar24959102022-05-07 20:01:16 +01004879 num = (State & MODE_INSERT) == 0 && empty_line
4880 ? 0 : (int)wp->w_cursor.col + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 break;
4882
4883 case STL_VIRTCOL:
4884 case STL_VIRTCOL_ALT:
John Marriottec032de2025-04-10 21:34:19 +02004885 {
4886 colnr_T virtcol = wp->w_virtcol + 1;
4887
Bram Moolenaarc667da52019-11-30 20:52:27 +01004888 // Don't display %V if it's the same as %c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 if (opt == STL_VIRTCOL_ALT
Bram Moolenaar24959102022-05-07 20:01:16 +01004890 && (virtcol == (colnr_T)((State & MODE_INSERT) == 0
4891 && empty_line ? 0 : (int)wp->w_cursor.col + 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 break;
4893 num = (long)virtcol;
4894 break;
John Marriottec032de2025-04-10 21:34:19 +02004895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896
4897 case STL_PERCENTAGE:
John Marriottec032de2025-04-10 21:34:19 +02004898 num = calc_percentage((long)wp->w_cursor.lnum, (long)wp->w_buffer->b_ml.ml_line_count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 break;
4900
4901 case STL_ALTPERCENT:
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004902 str = buf_tmp;
John Marriotta21240b2025-01-08 20:10:59 +01004903 (void)get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 break;
4905
Luuk van Baalba936f62022-12-15 13:15:39 +00004906 case STL_SHOWCMD:
4907 if (p_sc && STRCMP(opt_name, p_sloc) == 0)
4908 str = showcmd_buf;
4909 break;
4910
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 case STL_ARGLISTSTAT:
4912 fillable = FALSE;
John Marriottec032de2025-04-10 21:34:19 +02004913 buf_tmp[0] = NUL;
4914 if (append_arg_number(wp, buf_tmp, sizeof(buf_tmp), FALSE) > 0)
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004915 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 break;
4917
4918 case STL_KEYMAP:
4919 fillable = FALSE;
John Marriotta21240b2025-01-08 20:10:59 +01004920 if (get_keymap_str(wp, (char_u *)"<%s>", buf_tmp, TMPLEN) > 0)
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004921 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 break;
4923 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004924#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004925 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926#else
4927 num = 0;
4928#endif
4929 break;
4930
4931 case STL_BUFNO:
4932 num = wp->w_buffer->b_fnum;
4933 break;
4934
4935 case STL_OFFSET_X:
4936 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004937 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 case STL_OFFSET:
4939#ifdef FEAT_BYTEOFF
4940 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
Bram Moolenaar24959102022-05-07 20:01:16 +01004941 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0
4942 ? 0L : l + 1 + ((State & MODE_INSERT) == 0 && empty_line
4943 ? 0 : (int)wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944#endif
4945 break;
4946
4947 case STL_BYTEVAL_X:
4948 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004949 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004951 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 if (num == NL)
4953 num = 0;
4954 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4955 num = NL;
4956 break;
4957
4958 case STL_ROFLAG:
4959 case STL_ROFLAG_ALT:
4960 itemisflag = TRUE;
4961 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004962 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 break;
4964
4965 case STL_HELPFLAG:
4966 case STL_HELPFLAG_ALT:
4967 itemisflag = TRUE;
4968 if (wp->w_buffer->b_help)
4969 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004970 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 break;
4972
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 case STL_FILETYPE:
4974 if (*wp->w_buffer->b_p_ft != NUL
4975 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4976 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004977 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), "[%s]",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004978 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004979 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 }
4981 break;
4982
4983 case STL_FILETYPE_ALT:
4984 itemisflag = TRUE;
4985 if (*wp->w_buffer->b_p_ft != NUL
4986 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4987 {
John Marriottec032de2025-04-10 21:34:19 +02004988 char_u *t;
4989
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004990 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), ",%s",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004991 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004992 for (t = buf_tmp; *t != 0; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 *t = TOUPPER_LOC(*t);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004994 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 }
4996 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997
Bram Moolenaar4033c552017-09-16 20:54:51 +02004998#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 case STL_PREVIEWFLAG:
5000 case STL_PREVIEWFLAG_ALT:
5001 itemisflag = TRUE;
5002 if (wp->w_p_pvw)
5003 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
5004 : _("[Preview]"));
5005 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005006
5007 case STL_QUICKFIX:
5008 if (bt_quickfix(wp->w_buffer))
5009 str = (char_u *)(wp->w_llist_ref
5010 ? _(msg_loclist)
5011 : _(msg_qflist));
5012 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013#endif
5014
5015 case STL_MODIFIED:
5016 case STL_MODIFIED_ALT:
5017 itemisflag = TRUE;
5018 switch ((opt == STL_MODIFIED_ALT)
5019 + bufIsChanged(wp->w_buffer) * 2
5020 + (!wp->w_buffer->b_p_ma) * 4)
5021 {
5022 case 2: str = (char_u *)"[+]"; break;
5023 case 3: str = (char_u *)",+"; break;
5024 case 4: str = (char_u *)"[-]"; break;
5025 case 5: str = (char_u *)",-"; break;
5026 case 6: str = (char_u *)"[+-]"; break;
5027 case 7: str = (char_u *)",+-"; break;
5028 }
5029 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005030
5031 case STL_HIGHLIGHT:
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005032 {
John Marriottec032de2025-04-10 21:34:19 +02005033 char_u *t = s;
5034
5035 while (*s != '#' && *s != NUL)
5036 ++s;
5037 if (*s == '#')
5038 {
5039 stl_items[curitem].stl_type = Highlight;
5040 stl_items[curitem].stl_start = p;
5041 stl_items[curitem].stl_minwid = -syn_namen2id(t, (int)(s - t));
5042 curitem++;
5043 }
5044 if (*s != NUL)
5045 ++s;
5046 continue;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 }
5049
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005050 stl_items[curitem].stl_start = p;
5051 stl_items[curitem].stl_type = Normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 if (str != NULL && *str)
5053 {
John Marriottec032de2025-04-10 21:34:19 +02005054 char_u *t = str;
5055
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 if (itemisflag)
5057 {
5058 if ((t[0] && t[1])
5059 && ((!prevchar_isitem && *t == ',')
5060 || (prevchar_isflag && *t == ' ')))
5061 t++;
5062 prevchar_isflag = TRUE;
5063 }
5064 l = vim_strsize(t);
5065 if (l > 0)
5066 prevchar_isitem = TRUE;
5067 if (l > maxwid)
5068 {
5069 while (l >= maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 if (has_mbyte)
5071 {
5072 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005073 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 }
5075 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 l -= byte2cells(*t++);
5077 if (p + 1 >= out + outlen)
5078 break;
5079 *p++ = '<';
5080 }
5081 if (minwid > 0)
5082 {
5083 for (; l < minwid && p + 1 < out + outlen; l++)
5084 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005085 // Don't put a "-" in front of a digit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
5087 *p++ = ' ';
5088 else
Bram Moolenaar008bff92021-03-04 21:55:58 +01005089 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 }
5091 minwid = 0;
5092 }
5093 else
5094 minwid *= -1;
Bram Moolenaar008bff92021-03-04 21:55:58 +01005095 for (; *t && p + 1 < out + outlen; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005097 // Change a space by fillchar, unless fillchar is '-' and a
5098 // digit follows.
Bram Moolenaar008bff92021-03-04 21:55:58 +01005099 if (fillable && *t == ' '
5100 && (!VIM_ISDIGIT(*(t + 1)) || fillchar != '-'))
5101 MB_CHAR2BYTES(fillchar, p);
5102 else
5103 *p++ = *t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 }
5105 for (; l < minwid && p + 1 < out + outlen; l++)
Bram Moolenaar008bff92021-03-04 21:55:58 +01005106 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 }
5108 else if (num >= 0)
5109 {
John Marriottec032de2025-04-10 21:34:19 +02005110 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
5111 char_u nstr[20];
5112 char_u *t = nstr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113
5114 if (p + 20 >= out + outlen)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005115 break; // not sufficient space
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 prevchar_isitem = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 if (opt == STL_VIRTCOL_ALT)
5118 {
5119 *t++ = '-';
5120 minwid--;
5121 }
5122 *t++ = '%';
5123 if (zeropad)
5124 *t++ = '0';
5125 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005126 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
John Marriottec032de2025-04-10 21:34:19 +02005127 *t = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128
5129 for (n = num, l = 1; n >= nbase; n /= nbase)
5130 l++;
5131 if (opt == STL_VIRTCOL_ALT)
5132 l++;
John Marriottec032de2025-04-10 21:34:19 +02005133
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 if (l > maxwid)
5135 {
5136 l += 2;
5137 n = l - maxwid;
5138 while (l-- > maxwid)
5139 num /= nbase;
5140 *t++ = '>';
5141 *t++ = '%';
5142 *t = t[-3];
John Marriottec032de2025-04-10 21:34:19 +02005143 *++t = NUL;
5144 p += vim_snprintf_safelen((char *)p, outlen - (p - out),
5145 (char *)nstr, 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 }
5147 else
John Marriottec032de2025-04-10 21:34:19 +02005148 p += vim_snprintf_safelen((char *)p, outlen - (p - out),
5149 (char *)nstr, minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 }
5151 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005152 stl_items[curitem].stl_type = Empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01005154 if (num >= 0 || (!itemisflag && str != NULL && *str != NUL))
5155 prevchar_isflag = FALSE; // Item not NULL, but not a flag
5156 //
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 if (opt == STL_VIM_EXPR)
5158 vim_free(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 curitem++;
5160 }
5161 *p = NUL;
John Marriottec032de2025-04-10 21:34:19 +02005162 outputlen = (size_t)(p - out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 itemcnt = curitem;
5164
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005165#ifdef FEAT_EVAL
5166 if (usefmt != fmt)
5167 vim_free(usefmt);
5168#endif
5169
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 width = vim_strsize(out);
5171 if (maxwidth > 0 && width > maxwidth)
5172 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005173 // Result is too long, must truncate somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 l = 0;
5175 if (itemcnt == 0)
5176 s = out;
5177 else
5178 {
5179 for ( ; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005180 if (stl_items[l].stl_type == Trunc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005182 // Truncate at %< item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005183 s = stl_items[l].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 break;
5185 }
5186 if (l == itemcnt)
5187 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005188 // No %< item, truncate first item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005189 s = stl_items[0].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 l = 0;
5191 }
5192 }
5193
5194 if (width - vim_strsize(s) >= maxwidth)
5195 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005196 // Truncation mark is beyond max length
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 if (has_mbyte)
5198 {
5199 s = out;
5200 width = 0;
5201 for (;;)
5202 {
5203 width += ptr2cells(s);
5204 if (width >= maxwidth)
5205 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005206 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005208 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 while (++width < maxwidth)
Bram Moolenaar008bff92021-03-04 21:55:58 +01005210 MB_CHAR2BYTES(fillchar, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 }
5212 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 s = out + maxwidth - 1;
5214 for (l = 0; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005215 if (stl_items[l].stl_start > s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 break;
5217 itemcnt = l;
5218 *s++ = '>';
John Marriottec032de2025-04-10 21:34:19 +02005219 *s = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 }
5221 else
5222 {
John Marriottec032de2025-04-10 21:34:19 +02005223 char_u *end = out + outputlen;
5224
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 if (has_mbyte)
5226 {
5227 n = 0;
5228 while (width >= maxwidth)
5229 {
5230 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005231 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 }
5233 }
5234 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 n = width - maxwidth + 1;
5236 p = s + n;
John Marriottec032de2025-04-10 21:34:19 +02005237 mch_memmove(s + 1, p, (size_t)(end - p) + 1); // +1 for NUL
5238 end -= (size_t)(p - (s + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 *s = '<';
5240
Bram Moolenaarc667da52019-11-30 20:52:27 +01005241 --n; // count the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 for (; l < itemcnt; l++)
5243 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005244 if (stl_items[l].stl_start - n >= s)
5245 stl_items[l].stl_start -= n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005247 stl_items[l].stl_start = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 }
zeertzjqd392a742023-07-01 20:24:40 +01005249
5250 // Fill up for half a double-wide character.
5251 while (++width < maxwidth)
5252 {
John Marriottec032de2025-04-10 21:34:19 +02005253 s = end;
zeertzjqd392a742023-07-01 20:24:40 +01005254 MB_CHAR2BYTES(fillchar, s);
5255 *s = NUL;
John Marriottec032de2025-04-10 21:34:19 +02005256 end = s;
zeertzjqd392a742023-07-01 20:24:40 +01005257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 }
5259 width = maxwidth;
5260 }
John Marriottec032de2025-04-10 21:34:19 +02005261 else if (width < maxwidth && outputlen + maxwidth - width + 1 < outlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 {
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00005263 // Find how many separators there are, which we will use when
5264 // figuring out how many groups there are.
5265 int num_separators = 0;
5266
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 for (l = 0; l < itemcnt; l++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 {
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00005269 if (stl_items[l].stl_type == Separate)
5270 {
5271 // Create an array of the start location for each separator
5272 // mark.
5273 stl_separator_locations[num_separators] = l;
5274 num_separators++;
5275 }
5276 }
5277
5278 // If we have separated groups, then we deal with it now
5279 if (num_separators)
5280 {
5281 int standard_spaces;
5282 int final_spaces;
5283
5284 standard_spaces = (maxwidth - width) / num_separators;
5285 final_spaces = (maxwidth - width) -
5286 standard_spaces * (num_separators - 1);
5287 for (l = 0; l < num_separators; l++)
5288 {
5289 int dislocation = (l == (num_separators - 1)) ?
5290 final_spaces : standard_spaces;
5291 dislocation *= MB_CHAR2LEN(fillchar);
5292 char_u *start = stl_items[stl_separator_locations[l]].stl_start;
5293 char_u *seploc = start + dislocation;
5294 STRMOVE(seploc, start);
5295 for (s = start; s < seploc;)
5296 MB_CHAR2BYTES(fillchar, s);
5297
5298 for (int i = stl_separator_locations[l] + 1; i < itemcnt; i++)
5299 stl_items[i].stl_start += dislocation;
5300 }
5301
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 width = maxwidth;
5303 }
5304 }
5305
Bram Moolenaarc667da52019-11-30 20:52:27 +01005306 // Store the info about highlighting.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005307 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005309 *hltab = stl_hltab;
5310 sp = stl_hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 for (l = 0; l < itemcnt; l++)
5312 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005313 if (stl_items[l].stl_type == Highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005315 sp->start = stl_items[l].stl_start;
5316 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005317 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 }
5319 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005320 sp->start = NULL;
5321 sp->userhl = 0;
5322 }
5323
Bram Moolenaarc667da52019-11-30 20:52:27 +01005324 // Store the info about tab pages labels.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005325 if (tabtab != NULL)
5326 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005327 *tabtab = stl_tabtab;
5328 sp = stl_tabtab;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005329 for (l = 0; l < itemcnt; l++)
5330 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005331 if (stl_items[l].stl_type == TabPage)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005332 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005333 sp->start = stl_items[l].stl_start;
5334 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005335 sp++;
5336 }
5337 }
5338 sp->start = NULL;
5339 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 }
5341
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01005342 redraw_not_allowed = save_redraw_not_allowed;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02005343
Bram Moolenaar0e1f36f2022-02-15 16:17:44 +00005344 // A user function may reset KeyTyped, restore it.
5345 KeyTyped = save_KeyTyped;
5346
Luuk van Baal7b224fd2022-11-07 12:16:51 +00005347 // Check for an error. If there is one the display will be messed up and
5348 // might loop redrawing. Avoid that by making the corresponding option
5349 // empty.
5350 // TODO: find out why using called_emsg_before makes tests fail, does it
5351 // matter?
5352 // if (called_emsg > called_emsg_before)
5353 if (did_emsg > did_emsg_before)
5354 set_string_option_direct(opt_name, -1, (char_u *)"",
5355 OPT_FREE | opt_scope, SID_ERROR);
5356
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 return width;
5358}
Bram Moolenaarc667da52019-11-30 20:52:27 +01005359#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361/*
John Marriottec032de2025-04-10 21:34:19 +02005362 * Get relative cursor position in window into "buf[]", in the localized
Emir SARI971cd2b2023-04-29 12:09:53 +01005363 * percentage form like %99, 99%; using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 */
John Marriotta21240b2025-01-08 20:10:59 +01005365 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005366get_rel_pos(
5367 win_T *wp,
5368 char_u *buf,
5369 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005371 long above; // number of lines above window
5372 long below; // number of lines below window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373
Bram Moolenaarc667da52019-11-30 20:52:27 +01005374 if (buflen < 3) // need at least 3 chars for writing
John Marriotta21240b2025-01-08 20:10:59 +01005375 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 above = wp->w_topline - 1;
5377#ifdef FEAT_DIFF
5378 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02005379 if (wp->w_topline == 1 && wp->w_topfill >= 1)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005380 above = 0; // All buffer lines are displayed and there is an
5381 // indication of filler lines, that can be considered
5382 // seeing all lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383#endif
5384 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
5385 if (below <= 0)
John Marriottec032de2025-04-10 21:34:19 +02005386 return (int)vim_snprintf_safelen((char *)buf, buflen,
5387 "%s", (above == 0) ? _("All") : _("Bot"));
Emir SARI971cd2b2023-04-29 12:09:53 +01005388
John Marriottec032de2025-04-10 21:34:19 +02005389 if (above <= 0)
5390 return (int)vim_snprintf_safelen((char *)buf, buflen,
5391 "%s", _("Top"));
John Marriotta21240b2025-01-08 20:10:59 +01005392
John Marriottec032de2025-04-10 21:34:19 +02005393 // localized percentage value
5394 return (int)vim_snprintf_safelen((char *)buf, buflen,
5395 _("%2d%%"), calc_percentage(above, above + below));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397
5398/*
John Marriottec032de2025-04-10 21:34:19 +02005399 * Append (file 2 of 8) to "buf[]", if editing more than one file.
5400 * Return the number of characters appended.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005402 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005403append_arg_number(
5404 win_T *wp,
5405 char_u *buf,
John Marriottec032de2025-04-10 21:34:19 +02005406 size_t buflen,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005407 int add_file) // Add "file" before the arg number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005409 if (ARGCOUNT <= 1) // nothing to do
John Marriottec032de2025-04-10 21:34:19 +02005410 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411
Bram Moolenaara8490a42023-05-23 18:00:58 +01005412 char *msg;
5413 switch ((wp->w_arg_idx_invalid ? 1 : 0) + (add_file ? 2 : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 {
Bram Moolenaara8490a42023-05-23 18:00:58 +01005415 case 0: msg = _(" (%d of %d)"); break;
5416 case 1: msg = _(" ((%d) of %d)"); break;
5417 case 2: msg = _(" (file %d of %d)"); break;
5418 case 3: msg = _(" (file (%d) of %d)"); break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 }
Bram Moolenaara8490a42023-05-23 18:00:58 +01005420
John Marriottec032de2025-04-10 21:34:19 +02005421 return (int)vim_snprintf_safelen((char *)buf, buflen, msg,
Bram Moolenaara8490a42023-05-23 18:00:58 +01005422 wp->w_arg_idx + 1, ARGCOUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423}
5424
5425/*
5426 * If fname is not a full path, make it a full path.
5427 * Returns pointer to allocated memory (NULL for failure).
5428 */
5429 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005430fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431{
5432 /*
5433 * Force expanding the path always for Unix, because symbolic links may
5434 * mess up the full path name, even though it starts with a '/'.
5435 * Also expand when there is ".." in the file name, try to remove it,
5436 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00005437 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 * For MS-Windows also expand names like "longna~1" to "longname".
5439 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00005440#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 return FullName_save(fname, TRUE);
5442#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00005443 if (!vim_isAbsName(fname)
5444 || strstr((char *)fname, "..") != NULL
5445 || strstr((char *)fname, "//") != NULL
5446# ifdef BACKSLASH_IN_FILENAME
5447 || strstr((char *)fname, "\\\\") != NULL
5448# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005449# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00005451# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 )
5453 return FullName_save(fname, FALSE);
5454
5455 fname = vim_strsave(fname);
5456
Bram Moolenaar9b942202007-10-03 12:31:33 +00005457# ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01005458 if (fname != NULL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005459 fname_case(fname, 0); // set correct case for file name
Bram Moolenaar9b942202007-10-03 12:31:33 +00005460# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461
5462 return fname;
5463#endif
5464}
5465
5466/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005467 * Make "*ffname" a full file name, set "*sfname" to "*ffname" if not NULL.
5468 * "*ffname" becomes a pointer to allocated memory (or NULL).
5469 * When resolving a link both "*sfname" and "*ffname" will point to the same
5470 * allocated memory.
5471 * The "*ffname" and "*sfname" pointer values on call will not be freed.
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005472 * Note that the resulting "*ffname" pointer should be considered not allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005475fname_expand(
5476 buf_T *buf UNUSED,
5477 char_u **ffname,
5478 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005480 if (*ffname == NULL) // no file name given, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 return;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005482 if (*sfname == NULL) // no short file name given, use ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 *sfname = *ffname;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005484 *ffname = fix_fname(*ffname); // expand to full path
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485
5486#ifdef FEAT_SHORTCUT
5487 if (!buf->b_p_bin)
5488 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005489 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005491 // If the file name is a shortcut file, use the file it links to.
Bram Moolenaardce1e892019-02-10 23:18:53 +01005492 rfname = mch_resolve_path(*ffname, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005493 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 {
5495 vim_free(*ffname);
5496 *ffname = rfname;
5497 *sfname = rfname;
5498 }
5499 }
5500#endif
5501}
5502
5503/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 * Open a window for a number of buffers.
5505 */
5506 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005507ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508{
5509 buf_T *buf;
5510 win_T *wp, *wpnext;
5511 int split_ret = OK;
5512 int p_ea_save;
5513 int open_wins = 0;
5514 int r;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005515 int count; // Maximum number of windows to open.
5516 int all; // When TRUE also load inactive buffers.
Bram Moolenaare1004402020-10-24 20:49:43 +02005517 int had_tab = cmdmod.cmod_tab;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005518 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519
Bram Moolenaarc667da52019-11-30 20:52:27 +01005520 if (eap->addr_count == 0) // make as many windows as possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 count = 9999;
5522 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01005523 count = eap->line2; // make as many windows as specified
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5525 all = FALSE;
5526 else
5527 all = TRUE;
5528
Pavel Mayorove1121b12023-02-20 14:35:20 +00005529 // Stop Visual mode, the cursor and "VIsual" may very well be invalid after
5530 // switching to another buffer.
5531 reset_VIsual_and_resel();
5532
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 setpcmark();
5534
5535#ifdef FEAT_GUI
5536 need_mouse_correct = TRUE;
5537#endif
5538
5539 /*
5540 * Close superfluous windows (two windows for the same buffer).
5541 * Also close windows that are not full-width.
5542 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005543 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005544 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005545 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005547 tpnext = curtab->tp_next;
5548 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005550 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005551 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005552 || ((cmdmod.cmod_split & WSP_VERT)
5553 ? wp->w_height + wp->w_status_height < Rows - p_ch
5554 - tabline_height()
5555 : wp->w_width != Columns)
5556 || (had_tab > 0 && wp != firstwin))
5557 && !ONE_WINDOW
Christian Brabandt0a6e57b2024-08-15 22:15:28 +02005558 && !(win_locked(wp) || wp->w_buffer->b_locked > 0)
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005559 && !win_unlisted(wp))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005560 {
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005561 if (win_close(wp, FALSE) == FAIL)
5562 break;
5563 // Just in case an autocommand does something strange with
5564 // windows: start all over...
5565 wpnext = firstwin;
5566 tpnext = first_tabpage;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005567 open_wins = 0;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005568 }
5569 else
5570 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005572
Bram Moolenaarc667da52019-11-30 20:52:27 +01005573 // Without the ":tab" modifier only do the current tab page.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005574 if (had_tab == 0 || tpnext == NULL)
5575 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005576 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 }
5578
5579 /*
5580 * Go through the buffer list. When a buffer doesn't have a window yet,
5581 * open one. Otherwise move the window to the right position.
5582 * Watch out for autocommands that delete buffers or windows!
5583 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01005584 // Don't execute Win/Buf Enter/Leave autocommands here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 ++autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 win_enter(lastwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5589 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005590 // Check if this buffer needs a window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5592 continue;
5593
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005594 if (had_tab != 0)
5595 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005596 // With the ":tab" modifier don't move the window.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005597 if (buf->b_nwindows > 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005598 wp = lastwin; // buffer has a window, skip it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005599 else
5600 wp = NULL;
5601 }
5602 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005603 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005604 // Check if this buffer already has a window
Bram Moolenaar29323592016-07-24 22:04:11 +02005605 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005606 if (wp->w_buffer == buf)
5607 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005608 // If the buffer already has a window, move it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005609 if (wp != NULL)
5610 win_move_after(wp, curwin);
5611 }
5612
5613 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005615 bufref_T bufref;
5616
5617 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005618
Bram Moolenaarc667da52019-11-30 20:52:27 +01005619 // Split the window and put the buffer in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 p_ea_save = p_ea;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005621 p_ea = TRUE; // use space from all windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5623 ++open_wins;
5624 p_ea = p_ea_save;
5625 if (split_ret == FAIL)
5626 continue;
5627
Bram Moolenaarc667da52019-11-30 20:52:27 +01005628 // Open the buffer in this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005631 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005633 // autocommands deleted the buffer!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 swap_exists_action = SEA_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 break;
5636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 if (swap_exists_action == SEA_QUIT)
5638 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005639#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005640 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005641
Bram Moolenaarc667da52019-11-30 20:52:27 +01005642 // Reset the error/interrupt/exception state here so that
5643 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005644 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005645#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005646
Bram Moolenaarc667da52019-11-30 20:52:27 +01005647 // User selected Quit at ATTENTION prompt; close this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 win_close(curwin, TRUE);
5649 --open_wins;
5650 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005651 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005652
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005653#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005654 // Restore the error/interrupt/exception state if not
5655 // discarded by a new aborting error, interrupt, or uncaught
5656 // exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005657 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 }
5660 else
5661 handle_swap_exists(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 }
5663
5664 ui_breakcheck();
5665 if (got_int)
5666 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005667 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 break;
5669 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005670#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01005671 // Autocommands deleted the buffer or aborted script processing!!!
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005672 if (aborting())
5673 break;
5674#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01005675 // When ":tab" was used open a new tab for a new window repeatedly.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005676 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
Bram Moolenaare1004402020-10-24 20:49:43 +02005677 cmdmod.cmod_tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 --autocmd_no_enter;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005680 win_enter(firstwin, FALSE); // back to first window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682
5683 /*
5684 * Close superfluous windows.
5685 */
5686 for (wp = lastwin; open_wins > count; )
5687 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005688 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 || autowrite(wp->w_buffer, FALSE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 if (!win_valid(wp))
5691 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005692 // BufWrite Autocommands made the window invalid, start over
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 wp = lastwin;
5694 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005695 else if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005697 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 --open_wins;
5699 wp = lastwin;
5700 }
5701 else
5702 {
5703 wp = wp->w_prev;
5704 if (wp == NULL)
5705 break;
5706 }
5707 }
5708}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005711static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005712
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713/*
5714 * do_modelines() - process mode lines for the current file
5715 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005716 * "flags" can be:
5717 * OPT_WINONLY only set options local to window
5718 * OPT_NOWIN don't set options local to window
5719 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 * Returns immediately if the "ml" option isn't set.
5721 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005723do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005725 linenr_T lnum;
5726 int nmlines;
5727 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728
5729 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5730 return;
5731
Bram Moolenaarc667da52019-11-30 20:52:27 +01005732 // Disallow recursive entry here. Can happen when executing a modeline
5733 // triggers an autocommand, which reloads modelines with a ":do".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 if (entered)
5735 return;
5736
5737 ++entered;
Hu Jialun9dcd3492021-08-28 20:42:50 +02005738 for (lnum = 1; curbuf->b_p_ml && lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005740 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 nmlines = 0;
5742
Hu Jialun9dcd3492021-08-28 20:42:50 +02005743 for (lnum = curbuf->b_ml.ml_line_count; curbuf->b_p_ml && lnum > 0 && lnum > nmlines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005745 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 nmlines = 0;
5747 --entered;
5748}
5749
Bram Moolenaarc667da52019-11-30 20:52:27 +01005750#include "version.h" // for version number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751
5752/*
5753 * chk_modeline() - check a single line for a mode string
5754 * Return FAIL if an error encountered.
5755 */
5756 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005757chk_modeline(
5758 linenr_T lnum,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005759 int flags) // Same as for do_modelines().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760{
5761 char_u *s;
John Marriottec032de2025-04-10 21:34:19 +02005762 char_u *line_end; // point to the end of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 char_u *e;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 int prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 int retval = OK;
ichizok7e5fe382023-04-15 13:17:50 +01005766 ESTACK_CHECK_DECLARATION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767
5768 prev = -1;
John Marriottec032de2025-04-10 21:34:19 +02005769 s = ml_get(lnum);
5770 line_end = s + ml_get_len(lnum);
5771 for (; *s != NUL; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 {
5773 if (prev == -1 || vim_isspace(prev))
5774 {
5775 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5776 || STRNCMP(s, "vi:", (size_t)3) == 0)
5777 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005778 // Accept both "vim" and "Vim".
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005779 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 {
John Marriottec032de2025-04-10 21:34:19 +02005781 int vers;
5782
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5784 e = s + 4;
5785 else
5786 e = s + 3;
5787 vers = getdigits(&e);
5788 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005789 && (s[0] != 'V'
5790 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 && (s[3] == ':'
Keith Thompson184f71c2024-01-04 21:19:04 +01005792 || (VIM_VERSION_100 >= vers && SAFE_isdigit(s[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 || (VIM_VERSION_100 < vers && s[3] == '<')
5794 || (VIM_VERSION_100 > vers && s[3] == '>')
5795 || (VIM_VERSION_100 == vers && s[3] == '=')))
5796 break;
5797 }
5798 }
5799 prev = *s;
5800 }
5801
5802 if (*s)
5803 {
John Marriottec032de2025-04-10 21:34:19 +02005804 size_t len;
5805 char_u *linecopy; // local copy of any modeline found
5806 int end;
5807
Bram Moolenaarc667da52019-11-30 20:52:27 +01005808 do // skip over "ex:", "vi:" or "vim:"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 ++s;
5810 while (s[-1] != ':');
5811
John Marriottec032de2025-04-10 21:34:19 +02005812 len = (size_t)(line_end - s); // remember the line length
5813 // so we can restore 'line_end'
5814 // after the copy
5815 s = linecopy = vim_strnsave(s, len); // copy the line, it will change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 if (linecopy == NULL)
5817 return FAIL;
5818
John Marriottec032de2025-04-10 21:34:19 +02005819 line_end = s + len; // restore 'line_end'
5820
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005821 // prepare for emsg()
5822 estack_push(ETYPE_MODELINE, (char_u *)"modelines", lnum);
ichizok7e5fe382023-04-15 13:17:50 +01005823 ESTACK_CHECK_SETUP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824
5825 end = FALSE;
5826 while (end == FALSE)
5827 {
5828 s = skipwhite(s);
5829 if (*s == NUL)
5830 break;
5831
5832 /*
5833 * Find end of set command: ':' or end of line.
5834 * Skip over "\:", replacing it with ":".
5835 */
5836 for (e = s; *e != ':' && *e != NUL; ++e)
5837 if (e[0] == '\\' && e[1] == ':')
John Marriottec032de2025-04-10 21:34:19 +02005838 {
5839 mch_memmove(e, e + 1, (size_t)(line_end - (e + 1)) + 1); // +1 for NUL
5840 --line_end;
5841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 if (*e == NUL)
5843 end = TRUE;
5844
5845 /*
5846 * If there is a "set" command, require a terminating ':' and
5847 * ignore the stuff after the ':'.
5848 * "vi:set opt opt opt: foo" -- foo not interpreted
5849 * "vi:opt opt opt: foo" -- foo interpreted
5850 * Accept "se" for compatibility with Elvis.
5851 */
5852 if (STRNCMP(s, "set ", (size_t)4) == 0
5853 || STRNCMP(s, "se ", (size_t)3) == 0)
5854 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005855 if (*e != ':') // no terminating ':'?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 break;
5857 end = TRUE;
John Marriottec032de2025-04-10 21:34:19 +02005858 s += (*(s + 2) == ' ') ? 3 : 4;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005860 *e = NUL; // truncate the set command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861
Bram Moolenaarc667da52019-11-30 20:52:27 +01005862 if (*s != NUL) // skip over an empty "::"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 {
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005864 int secure_save = secure;
John Marriottec032de2025-04-10 21:34:19 +02005865 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005866
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005867 current_sctx.sc_version = 1;
5868#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005869 current_sctx.sc_sid = SID_MODELINE;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005870 current_sctx.sc_seq = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005871 current_sctx.sc_lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005873
Bram Moolenaar5958f952018-11-20 04:25:21 +01005874 // Make sure no risky things are executed as a side effect.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01005875 secure = 1;
Bram Moolenaar5958f952018-11-20 04:25:21 +01005876
Bram Moolenaara3227e22006-03-08 21:32:40 +00005877 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar5958f952018-11-20 04:25:21 +01005878
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005879 secure = secure_save;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005880 current_sctx = save_current_sctx;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005881 if (retval == FAIL) // stop if error found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 break;
5883 }
John Marriottec032de2025-04-10 21:34:19 +02005884 s = (e == line_end) ? e : e + 1; // advance to next part
5885 // careful not to go off the end
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 }
5887
ichizok7e5fe382023-04-15 13:17:50 +01005888 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005889 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 vim_free(linecopy);
5891 }
5892 return retval;
5893}
5894
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005895/*
Bram Moolenaar91335e52018-08-01 17:53:12 +02005896 * Return TRUE if "buf" is a normal buffer, 'buftype' is empty.
5897 */
5898 int
5899bt_normal(buf_T *buf)
5900{
5901 return buf != NULL && buf->b_p_bt[0] == NUL;
5902}
5903
5904/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005905 * Return TRUE if "buf" is the quickfix buffer.
5906 */
5907 int
Bram Moolenaar340dafd2022-08-25 16:16:45 +01005908bt_quickfix(buf_T *buf UNUSED)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005909{
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005910#ifdef FEAT_QUICKFIX
Christian Brabandt6e60cf42023-09-03 21:43:46 +02005911 return buf != NULL && buf_valid(buf) && buf->b_p_bt[0] == 'q';
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005912#else
5913 return FALSE;
Bram Moolenaar113e1072019-01-20 15:30:40 +01005914#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005915}
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005916
5917/*
5918 * Return TRUE if "buf" is a terminal buffer.
5919 */
5920 int
Bram Moolenaar340dafd2022-08-25 16:16:45 +01005921bt_terminal(buf_T *buf UNUSED)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005922{
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005923#if defined(FEAT_TERMINAL)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005924 return buf != NULL && buf->b_p_bt[0] == 't';
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005925#else
5926 return FALSE;
Bram Moolenaar113e1072019-01-20 15:30:40 +01005927#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005928}
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005929
5930/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005931 * Return TRUE if "buf" is a help buffer.
5932 */
5933 int
5934bt_help(buf_T *buf)
5935{
5936 return buf != NULL && buf->b_help;
5937}
5938
5939/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02005940 * Return TRUE if "buf" is a prompt buffer.
5941 */
5942 int
5943bt_prompt(buf_T *buf)
5944{
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005945 return buf != NULL && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'r';
5946}
5947
Dominique Pelle748b3082022-01-08 12:41:16 +00005948#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005949/*
5950 * Return TRUE if "buf" is a buffer for a popup window.
5951 */
5952 int
5953bt_popup(buf_T *buf)
5954{
5955 return buf != NULL && buf->b_p_bt != NULL
5956 && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'o';
Bram Moolenaarf2732452018-06-03 14:47:35 +02005957}
Dominique Pelle748b3082022-01-08 12:41:16 +00005958#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005959
5960/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005961 * Return TRUE if "buf" is a "nofile", "acwrite", "terminal" or "prompt"
Bram Moolenaarc3126192022-08-26 12:58:17 +01005962 * buffer. This means the buffer name may not be a file name, at least not for
5963 * writing the buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005964 */
5965 int
Bram Moolenaar26910de2019-06-15 19:37:15 +02005966bt_nofilename(buf_T *buf)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005967{
5968 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5969 || buf->b_p_bt[0] == 'a'
Bram Moolenaarf2732452018-06-03 14:47:35 +02005970 || buf->b_p_bt[0] == 't'
5971 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005972}
5973
Bram Moolenaarc3126192022-08-26 12:58:17 +01005974/*
5975 * Return TRUE if "buf" is a "nofile", "quickfix", "terminal" or "prompt"
5976 * buffer. This means the buffer is not to be read from a file.
5977 */
5978 static int
5979bt_nofileread(buf_T *buf)
5980{
5981 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5982 || buf->b_p_bt[0] == 't'
5983 || buf->b_p_bt[0] == 'q'
5984 || buf->b_p_bt[0] == 'p');
5985}
5986
Dominique Pelle748b3082022-01-08 12:41:16 +00005987#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005988/*
Bram Moolenaar26910de2019-06-15 19:37:15 +02005989 * Return TRUE if "buf" has 'buftype' set to "nofile".
5990 */
5991 int
5992bt_nofile(buf_T *buf)
5993{
5994 return buf != NULL && buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f';
5995}
Dominique Pelle748b3082022-01-08 12:41:16 +00005996#endif
Bram Moolenaar26910de2019-06-15 19:37:15 +02005997
5998/*
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01005999 * Return TRUE if "buf" is a "nowrite", "nofile", "terminal", "prompt", or
6000 * "popup" buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02006001 */
6002 int
6003bt_dontwrite(buf_T *buf)
6004{
Bram Moolenaarf2732452018-06-03 14:47:35 +02006005 return buf != NULL && (buf->b_p_bt[0] == 'n'
Bram Moolenaar8133cc62020-10-26 21:05:27 +01006006 || buf->b_p_bt[0] == 't'
6007 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02006008}
6009
6010 int
6011bt_dontwrite_msg(buf_T *buf)
6012{
6013 if (bt_dontwrite(buf))
6014 {
Bram Moolenaarf1474d82021-12-31 19:59:55 +00006015 emsg(_(e_cannot_write_buftype_option_is_set));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02006016 return TRUE;
6017 }
6018 return FALSE;
6019}
6020
6021/*
6022 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
6023 * and 'bufhidden'.
6024 */
6025 int
6026buf_hide(buf_T *buf)
6027{
Bram Moolenaarc667da52019-11-30 20:52:27 +01006028 // 'bufhidden' overrules 'hidden' and ":hide", check it first
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02006029 switch (buf->b_p_bh[0])
6030 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01006031 case 'u': // "unload"
6032 case 'w': // "wipe"
6033 case 'd': return FALSE; // "delete"
6034 case 'h': return TRUE; // "hide"
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02006035 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006036 return (p_hid || (cmdmod.cmod_flags & CMOD_HIDE));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02006037}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038
6039/*
6040 * Return special buffer name.
6041 * Returns NULL when the buffer has a normal file name.
6042 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006043 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006044buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045{
Bram Moolenaar4033c552017-09-16 20:54:51 +02006046#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006048 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006049 /*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01006050 * Differentiate between the quickfix and location list buffers using
6051 * the buffer number stored in the global quickfix stack.
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006052 */
Bram Moolenaaree8188f2019-02-05 21:23:04 +01006053 if (buf->b_fnum == qf_stack_get_bufnr())
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006054 return (char_u *)_(msg_qflist);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01006055 else
6056 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00006057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02006059
Bram Moolenaarc667da52019-11-30 20:52:27 +01006060 // There is no _file_ when 'buftype' is "nofile", b_sfname
6061 // contains the name as specified by the user.
Bram Moolenaar26910de2019-06-15 19:37:15 +02006062 if (bt_nofilename(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 {
Bram Moolenaar21554412017-07-24 21:44:43 +02006064#ifdef FEAT_TERMINAL
6065 if (buf->b_term != NULL)
6066 return term_get_status_text(buf->b_term);
6067#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02006068 if (buf->b_fname != NULL)
6069 return buf->b_fname;
Sean Dewar1fb41032023-08-16 17:15:05 +01006070 if (buf == cmdwin_buf)
6071 return (char_u *)_("[Command Line]");
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02006072#ifdef FEAT_JOB_CHANNEL
6073 if (bt_prompt(buf))
6074 return (char_u *)_("[Prompt]");
6075#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01006076#ifdef FEAT_PROP_POPUP
Bram Moolenaarc6896e22019-05-30 22:32:34 +02006077 if (bt_popup(buf))
6078 return (char_u *)_("[Popup]");
6079#endif
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006080 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 }
Bram Moolenaar21554412017-07-24 21:44:43 +02006082
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 if (buf->b_fname == NULL)
Bram Moolenaar00806bc2020-11-05 19:36:38 +01006084 return buf_get_fname(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 return NULL;
6086}
6087
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088/*
Bram Moolenaar00806bc2020-11-05 19:36:38 +01006089 * Get "buf->b_fname", use "[No Name]" if it is NULL.
6090 */
6091 char_u *
6092buf_get_fname(buf_T *buf)
6093{
6094 if (buf->b_fname == NULL)
6095 return (char_u *)_("[No Name]");
6096 return buf->b_fname;
6097}
6098
6099/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
6101 */
6102 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006103set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00006105 if (on == curbuf->b_p_bl)
6106 return;
6107
6108 curbuf->b_p_bl = on;
6109 if (on)
6110 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
6111 else
6112 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113}
6114
6115/*
6116 * Read the file for "buf" again and check if the contents changed.
6117 * Return TRUE if it changed or this could not be checked.
6118 */
6119 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006120buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121{
6122 buf_T *newbuf;
6123 int differ = TRUE;
6124 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 exarg_T ea;
6127
Bram Moolenaarc667da52019-11-30 20:52:27 +01006128 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6130 if (newbuf == NULL)
6131 return TRUE;
6132
Bram Moolenaarc667da52019-11-30 20:52:27 +01006133 // Force the 'fileencoding' and 'fileformat' to be equal.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 if (prep_exarg(&ea, buf) == FAIL)
6135 {
6136 wipe_buffer(newbuf, FALSE);
6137 return TRUE;
6138 }
6139
Bram Moolenaare76062c2022-11-28 18:51:43 +00006140 // Set curwin/curbuf to buf and save a few things.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00006142 if (curbuf != newbuf)
6143 {
6144 // Failed to find a window for "newbuf".
6145 wipe_buffer(newbuf, FALSE);
6146 return TRUE;
6147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148
Christian Brabandt41e6f7d2023-10-11 21:08:13 +02006149 // We don't want to trigger autocommands now, they may have nasty
6150 // side-effects like wiping buffers
6151 block_autocmds();
Bram Moolenaar4770d092006-01-12 23:22:24 +00006152 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 && readfile(buf->b_ffname, buf->b_fname,
6154 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
6155 &ea, READ_NEW | READ_DUMMY) == OK)
6156 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01006157 // compare the two files line by line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
6159 {
6160 differ = FALSE;
6161 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6162 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
6163 {
6164 differ = TRUE;
6165 break;
6166 }
6167 }
6168 }
6169 vim_free(ea.cmd);
6170
Bram Moolenaarc667da52019-11-30 20:52:27 +01006171 // restore curwin/curbuf and a few other things
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173
Bram Moolenaarc667da52019-11-30 20:52:27 +01006174 if (curbuf != newbuf) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 wipe_buffer(newbuf, FALSE);
6176
Christian Brabandt41e6f7d2023-10-11 21:08:13 +02006177 unblock_autocmds();
6178
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 return differ;
6180}
6181
6182/*
6183 * Wipe out a buffer and decrement the last buffer number if it was used for
6184 * this buffer. Call this to wipe out a temp buffer that does not contain any
6185 * marks.
6186 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006188wipe_buffer(
6189 buf_T *buf,
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006190 int aucmd) // When TRUE trigger autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191{
6192 if (buf->b_fnum == top_file_num - 1)
6193 --top_file_num;
6194
Bram Moolenaarc667da52019-11-30 20:52:27 +01006195 if (!aucmd) // Don't trigger BufDelete autocommands here.
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006196 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006197
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006198 close_buffer(NULL, buf, DOBUF_WIPE, FALSE, TRUE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006199
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006201 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202}