blob: 6d4c4359f0e5cba5e6e393afa17212e79f8137f2 [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);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010048static int append_arg_number(win_T *wp, char_u *buf, int buflen, int add_file);
49static 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);
Bram Moolenaar071d4272004-06-13 20:20:40 +000053
54#ifdef UNIX
55# define dev_T dev_t
56#else
57# define dev_T unsigned
58#endif
59
Bram Moolenaar00d253e2020-04-06 22:13:01 +020060#define FOR_ALL_BUFS_FROM_LAST(buf) \
61 for ((buf) = lastbuf; (buf) != NULL; (buf) = (buf)->b_prev)
62
Bram Moolenaar4033c552017-09-16 20:54:51 +020063#if defined(FEAT_QUICKFIX)
Bram Moolenaar7fd73202010-07-25 16:58:46 +020064static char *msg_loclist = N_("[Location List]");
65static char *msg_qflist = N_("[Quickfix List]");
66#endif
67
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +020068// Number of times free_buffer() was called.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +020069static int buf_free_count = 0;
70
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +020071static int top_file_num = 1; // highest file number
72static garray_T buf_reuse = GA_EMPTY; // file numbers to recycle
73
74/*
75 * Return the highest possible buffer number.
76 */
77 int
78get_highest_fnum(void)
79{
80 return top_file_num - 1;
81}
82
Bram Moolenaarc024b462019-06-08 18:07:21 +020083/*
84 * Read data from buffer for retrying.
85 */
Bram Moolenaarf71d7b92016-08-09 22:14:05 +020086 static int
87read_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +010088 int read_stdin, // read file from stdin, otherwise fifo
89 exarg_T *eap, // for forced 'ff' and 'fenc' or NULL
90 int flags) // extra flags for readfile()
Bram Moolenaarf71d7b92016-08-09 22:14:05 +020091{
92 int retval = OK;
93 linenr_T line_count;
94
Bram Moolenaarc5935a82021-10-19 20:48:52 +010095 // Read from the buffer which the text is already filled in and append at
96 // the end. This makes it possible to retry when 'fileformat' or
97 // 'fileencoding' was guessed wrong.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +020098 line_count = curbuf->b_ml.ml_line_count;
99 retval = readfile(
100 read_stdin ? NULL : curbuf->b_ffname,
101 read_stdin ? NULL : curbuf->b_fname,
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +0100102 line_count, (linenr_T)0, (linenr_T)MAXLNUM, eap,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200103 flags | READ_BUFFER);
104 if (retval == OK)
105 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100106 // Delete the binary lines.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200107 while (--line_count >= 0)
Bram Moolenaarca70c072020-05-30 20:30:46 +0200108 ml_delete((linenr_T)1);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200109 }
110 else
111 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100112 // Delete the converted lines.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200113 while (curbuf->b_ml.ml_line_count > line_count)
Bram Moolenaarca70c072020-05-30 20:30:46 +0200114 ml_delete(line_count);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200115 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100116 // Put the cursor on the first line.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200117 curwin->w_cursor.lnum = 1;
118 curwin->w_cursor.col = 0;
119
120 if (read_stdin)
121 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100122 // Set or reset 'modified' before executing autocommands, so that
123 // it can be changed there.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100124 if (!readonlymode && !BUFEMPTY())
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200125 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100126 else if (retval == OK)
Bram Moolenaarc024b462019-06-08 18:07:21 +0200127 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200128
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100129 if (retval == OK)
130 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100131#ifdef FEAT_EVAL
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100132 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100133 curbuf, &retval);
134#else
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100135 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200136#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100137 }
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200138 }
139 return retval;
140}
141
Dominique Pelle748b3082022-01-08 12:41:16 +0000142#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143/*
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200144 * Ensure buffer "buf" is loaded. Does not trigger the swap-exists action.
145 */
146 void
147buffer_ensure_loaded(buf_T *buf)
148{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +0000149 if (buf->b_ml.ml_mfp != NULL)
150 return;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200151
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +0000152 aco_save_T aco;
153
154 // Make sure the buffer is in a window. If not then skip it.
155 aucmd_prepbuf(&aco, buf);
156 if (curbuf == buf)
157 {
158 if (swap_exists_action != SEA_READONLY)
159 swap_exists_action = SEA_NONE;
160 open_buffer(FALSE, NULL, 0);
161 aucmd_restbuf(&aco);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200162 }
163}
Dominique Pelle748b3082022-01-08 12:41:16 +0000164#endif
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200165
166/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200167 * Open current buffer, that is: open the memfile and read the file into
168 * memory.
169 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 */
171 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100172open_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +0100173 int read_stdin, // read file from stdin
174 exarg_T *eap, // for forced 'ff' and 'fenc' or NULL
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100175 int flags_arg) // extra flags for readfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176{
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100177 int flags = flags_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178 int retval = OK;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200179 bufref_T old_curbuf;
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100180#ifdef FEAT_SYN_HL
181 long old_tw = curbuf->b_p_tw;
182#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200183 int read_fifo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100185 // The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
186 // When re-entering the same buffer, it should not change, because the
187 // user may have reset the flag by hand.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 if (readonlymode && curbuf->b_ffname != NULL
189 && (curbuf->b_flags & BF_NEVERLOADED))
190 curbuf->b_p_ro = TRUE;
191
Bram Moolenaar4770d092006-01-12 23:22:24 +0000192 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100194 // There MUST be a memfile, otherwise we can't do anything
195 // If we can't create one for the current buffer, take another buffer
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100196 close_buffer(NULL, curbuf, 0, FALSE, FALSE);
Bram Moolenaar29323592016-07-24 22:04:11 +0200197 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 if (curbuf->b_ml.ml_mfp != NULL)
199 break;
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100200 // If there is no memfile at all, exit.
201 // This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 if (curbuf == NULL)
203 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +0000204 emsg(_(e_cannot_allocate_any_buffer_exiting));
Bram Moolenaar6f10c702019-08-20 22:58:37 +0200205
206 // Don't try to do any saving, with "curbuf" NULL almost nothing
207 // will work.
208 v_dying = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 getout(2);
210 }
Bram Moolenaar6f10c702019-08-20 22:58:37 +0200211
Bram Moolenaar40bcec12021-12-05 22:19:27 +0000212 emsg(_(e_cannot_allocate_buffer_using_other_one));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213 enter_buffer(curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100214#ifdef FEAT_SYN_HL
215 if (old_tw != curbuf->b_p_tw)
216 check_colorcolumn(curwin);
217#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218 return FAIL;
219 }
220
Bram Moolenaarc667da52019-11-30 20:52:27 +0100221 // The autocommands in readfile() may change the buffer, but only AFTER
222 // reading the file.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200223 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224 modified_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225
Bram Moolenaarc667da52019-11-30 20:52:27 +0100226 // mark cursor position as being invalid
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100227 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100229 // A buffer without an actual file should not use the buffer name to read a
230 // file.
Bram Moolenaarc3126192022-08-26 12:58:17 +0100231 if (bt_nofileread(curbuf))
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100232 flags |= READ_NOFILE;
233
Bram Moolenaar2eddbac2022-08-25 12:45:21 +0100234 // Read the file if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 if (curbuf->b_ffname != NULL
236#ifdef FEAT_NETBEANS_INTG
237 && netbeansReadFile
238#endif
239 )
240 {
Bram Moolenaar426dd022016-03-15 15:09:29 +0100241 int old_msg_silent = msg_silent;
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200242#ifdef UNIX
243 int save_bin = curbuf->b_p_bin;
244 int perm;
245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246#ifdef FEAT_NETBEANS_INTG
247 int oldFire = netbeansFireChanges;
248
249 netbeansFireChanges = 0;
250#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200251#ifdef UNIX
252 perm = mch_getperm(curbuf->b_ffname);
Bram Moolenaard569bb02018-08-11 13:57:20 +0200253 if (perm >= 0 && (S_ISFIFO(perm)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200254 || S_ISSOCK(perm)
Bram Moolenaarf04507d2016-08-20 15:05:39 +0200255# ifdef OPEN_CHR_FILES
256 || (S_ISCHR(perm) && is_dev_fd_file(curbuf->b_ffname))
257# endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200258 ))
259 read_fifo = TRUE;
260 if (read_fifo)
261 curbuf->b_p_bin = TRUE;
262#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100263 if (shortmess(SHM_FILEINFO))
264 msg_silent = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200266 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200267 flags | READ_NEW | (read_fifo ? READ_FIFO : 0));
268#ifdef UNIX
269 if (read_fifo)
270 {
271 curbuf->b_p_bin = save_bin;
272 if (retval == OK)
273 retval = read_buffer(FALSE, eap, flags);
274 }
275#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100276 msg_silent = old_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277#ifdef FEAT_NETBEANS_INTG
278 netbeansFireChanges = oldFire;
279#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100280 // Help buffer is filtered.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +0200281 if (bt_help(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282 fix_help_buffer();
283 }
284 else if (read_stdin)
285 {
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200286 int save_bin = curbuf->b_p_bin;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100288 // First read the text in binary mode into the buffer.
289 // Then read from that same buffer and append at the end. This makes
290 // it possible to retry when 'fileformat' or 'fileencoding' was
291 // guessed wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292 curbuf->b_p_bin = TRUE;
293 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200294 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
295 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 curbuf->b_p_bin = save_bin;
297 if (retval == OK)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200298 retval = read_buffer(TRUE, eap, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299 }
300
Bram Moolenaarc667da52019-11-30 20:52:27 +0100301 // if first time loading this buffer, init b_chartab[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302 if (curbuf->b_flags & BF_NEVERLOADED)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100303 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304 (void)buf_init_chartab(curbuf, FALSE);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100305 parse_cino(curbuf);
306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100308 // Set/reset the Changed flag first, autocmds may change the buffer.
309 // Apply the automatic commands, before processing the modelines.
310 // So the modelines have priority over autocommands.
311 //
Bram Moolenaarc667da52019-11-30 20:52:27 +0100312 // When reading stdin, the buffer contents always needs writing, so set
313 // the changed flag. Unless in readonly mode: "ls | gview -".
314 // When interrupted and 'cpoptions' contains 'i' set changed flag.
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000315 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100316 || modified_was_set // ":set modified" used in autocmd
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100317#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000320 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100322 else if (retval == OK && !read_stdin && !read_fifo)
Bram Moolenaarc024b462019-06-08 18:07:21 +0200323 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaarc667da52019-11-30 20:52:27 +0100324 save_file_ff(curbuf); // keep this fileformat
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325
Bram Moolenaarc667da52019-11-30 20:52:27 +0100326 // Set last_changedtick to avoid triggering a TextChanged autocommand right
327 // after it was added.
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100328 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Christian Brabandtdb3b4462021-10-16 11:58:55 +0100329 curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100330 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100331
Bram Moolenaarc667da52019-11-30 20:52:27 +0100332 // require "!" to overwrite the file, because it wasn't read completely
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333#ifdef FEAT_EVAL
334 if (aborting())
335#else
336 if (got_int)
337#endif
338 curbuf->b_flags |= BF_READERR;
339
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000340#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +0100341 // Need to update automatic folding. Do this before the autocommands,
342 // they may use the fold info.
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000343 foldUpdateAll(curwin);
344#endif
345
Bram Moolenaarc667da52019-11-30 20:52:27 +0100346 // need to set w_topline, unless some autocommand already did that.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 if (!(curwin->w_valid & VALID_TOPLINE))
348 {
349 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100350#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100354#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100356#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358#endif
359
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000360 if (retval != OK)
361 return retval;
362
363 // The autocommands may have changed the current buffer. Apply the
364 // modelines to the correct buffer, if it still exists and is loaded.
365 if (bufref_valid(&old_curbuf) && old_curbuf.br_buf->b_ml.ml_mfp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000367 aco_save_T aco;
368
369 // Go to the buffer that was opened, make sure it is in a window.
370 // If not then skip it.
371 aucmd_prepbuf(&aco, old_curbuf.br_buf);
372 if (curbuf == old_curbuf.br_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000374 do_modelines(0);
375 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000377 if ((flags & READ_NOWINENTER) == 0)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100378#ifdef FEAT_EVAL
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000379 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL,
380 FALSE, curbuf, &retval);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100381#else
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000382 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL,
383 FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100384#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000386 // restore curwin/curbuf and a few other things
387 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 }
390
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 return retval;
392}
393
394/*
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200395 * Store "buf" in "bufref" and set the free count.
396 */
397 void
398set_bufref(bufref_T *bufref, buf_T *buf)
399{
400 bufref->br_buf = buf;
Bram Moolenaarfadacf02017-06-19 20:35:32 +0200401 bufref->br_fnum = buf == NULL ? 0 : buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200402 bufref->br_buf_free_count = buf_free_count;
403}
404
405/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200406 * Return TRUE if "bufref->br_buf" points to the same buffer as when
407 * set_bufref() was called and it is a valid buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200408 * Only goes through the buffer list if buf_free_count changed.
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200409 * Also checks if b_fnum is still the same, a :bwipe followed by :new might get
410 * the same allocated memory, but it's a different buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200411 */
412 int
413bufref_valid(bufref_T *bufref)
414{
415 return bufref->br_buf_free_count == buf_free_count
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200416 ? TRUE : buf_valid(bufref->br_buf)
417 && bufref->br_fnum == bufref->br_buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200418}
419
420/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200422 * This can be slow if there are many buffers, prefer using bufref_valid().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 */
424 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100425buf_valid(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426{
427 buf_T *bp;
428
Bram Moolenaarc667da52019-11-30 20:52:27 +0100429 // Assume that we more often have a recent buffer, start with the last
430 // one.
Bram Moolenaar00d253e2020-04-06 22:13:01 +0200431 FOR_ALL_BUFS_FROM_LAST(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432 if (bp == buf)
433 return TRUE;
434 return FALSE;
435}
436
437/*
Bram Moolenaar480778b2016-07-14 22:09:39 +0200438 * A hash table used to quickly lookup a buffer by its number.
439 */
440static hashtab_T buf_hashtab;
441
442 static void
443buf_hashtab_add(buf_T *buf)
444{
445 sprintf((char *)buf->b_key, "%x", buf->b_fnum);
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000446 if (hash_add(&buf_hashtab, buf->b_key, "create buffer") == FAIL)
Bram Moolenaarf1474d82021-12-31 19:59:55 +0000447 emsg(_(e_buffer_cannot_be_registered));
Bram Moolenaar480778b2016-07-14 22:09:39 +0200448}
449
450 static void
451buf_hashtab_remove(buf_T *buf)
452{
453 hashitem_T *hi = hash_find(&buf_hashtab, buf->b_key);
454
455 if (!HASHITEM_EMPTY(hi))
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000456 hash_remove(&buf_hashtab, hi, "close buffer");
Bram Moolenaar480778b2016-07-14 22:09:39 +0200457}
458
Bram Moolenaar94f01952018-09-01 15:30:03 +0200459/*
460 * Return TRUE when buffer "buf" can be unloaded.
461 * Give an error message and return FALSE when the buffer is locked or the
462 * screen is being redrawn and the buffer is in a window.
463 */
464 static int
465can_unload_buffer(buf_T *buf)
466{
467 int can_unload = !buf->b_locked;
468
469 if (can_unload && updating_screen)
470 {
471 win_T *wp;
472
473 FOR_ALL_WINDOWS(wp)
474 if (wp->w_buffer == buf)
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200475 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200476 can_unload = FALSE;
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200477 break;
478 }
Bram Moolenaar94f01952018-09-01 15:30:03 +0200479 }
480 if (!can_unload)
Bram Moolenaaref976322022-09-28 11:48:30 +0100481 {
482 char_u *fname = buf->b_fname != NULL ? buf->b_fname : buf->b_ffname;
483
484 semsg(_(e_attempt_to_delete_buffer_that_is_in_use_str),
485 fname != NULL ? fname : (char_u *)"[No Name]");
486 }
Bram Moolenaar94f01952018-09-01 15:30:03 +0200487 return can_unload;
488}
Bram Moolenaara997b452018-04-17 23:24:06 +0200489
Bram Moolenaar480778b2016-07-14 22:09:39 +0200490/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 * Close the link to a buffer.
492 * "action" is used when there is no longer a window for the buffer.
493 * It can be:
494 * 0 buffer becomes hidden
495 * DOBUF_UNLOAD buffer is unloaded
496 * DOBUF_DELETE buffer is unloaded and removed from buffer list
497 * DOBUF_WIPE buffer is unloaded and really deleted
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200498 * DOBUF_WIPE_REUSE idem, and add to buf_reuse list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 * When doing all but the first one on the current buffer, the caller should
500 * get a new buffer very soon!
501 *
502 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100503 *
504 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
505 * cause there to be only one window with this buffer. e.g. when ":quit" is
506 * supposed to close the window but autocommands close all other windows.
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100507 *
508 * When "ignore_abort" is TRUE don't abort even when aborting() returns TRUE.
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100509 *
510 * Return TRUE when we got to the end and b_nwindows was decremented.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 */
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100512 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100513close_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +0100514 win_T *win, // if not NULL, set b_last_cursor
Bram Moolenaar7454a062016-01-30 15:14:10 +0100515 buf_T *buf,
516 int action,
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100517 int abort_if_last,
518 int ignore_abort)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100521 int nwindows;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200522 bufref_T bufref;
Bram Moolenaar3a117e12016-10-30 21:57:52 +0100523 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200524 win_T *the_curwin = curwin;
525 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 int unload_buf = (action != 0);
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200527 int wipe_buf = (action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
528 int del_buf = (action == DOBUF_DEL || wipe_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529
Bram Moolenaarcee52202020-03-11 14:19:58 +0100530 CHECK_CURBUF;
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100531
532 // Force unloading or deleting when 'bufhidden' says so.
533 // The caller must take care of NOT deleting/freeing when 'bufhidden' is
534 // "hide" (otherwise we could never free or delete a buffer).
Bram Moolenaarc667da52019-11-30 20:52:27 +0100535 if (buf->b_p_bh[0] == 'd') // 'bufhidden' == "delete"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200536 {
537 del_buf = TRUE;
538 unload_buf = TRUE;
539 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100540 else if (buf->b_p_bh[0] == 'w') // 'bufhidden' == "wipe"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200541 {
542 del_buf = TRUE;
543 unload_buf = TRUE;
544 wipe_buf = TRUE;
545 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100546 else if (buf->b_p_bh[0] == 'u') // 'bufhidden' == "unload"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200547 unload_buf = TRUE;
548
Bram Moolenaar94053a52017-08-01 21:44:33 +0200549#ifdef FEAT_TERMINAL
Yee Cheng Chin42826332022-10-10 11:46:16 +0100550 // depending on how we get here b_nwindows may already be zero
551 if (bt_terminal(buf) && (buf->b_nwindows <= 1 || del_buf))
Bram Moolenaar94053a52017-08-01 21:44:33 +0200552 {
Bram Moolenaarcee52202020-03-11 14:19:58 +0100553 CHECK_CURBUF;
Bram Moolenaar94053a52017-08-01 21:44:33 +0200554 if (term_job_running(buf->b_term))
555 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200556 if (wipe_buf || unload_buf)
Bram Moolenaara997b452018-04-17 23:24:06 +0200557 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200558 if (!can_unload_buffer(buf))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100559 return FALSE;
Bram Moolenaar94f01952018-09-01 15:30:03 +0200560
Bram Moolenaarc667da52019-11-30 20:52:27 +0100561 // Wiping out or unloading a terminal buffer kills the job.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200562 free_terminal(buf);
Yee Cheng Chin42826332022-10-10 11:46:16 +0100563
564 // A terminal buffer is wiped out when job has finished.
565 del_buf = TRUE;
566 unload_buf = TRUE;
567 wipe_buf = TRUE;
Bram Moolenaara997b452018-04-17 23:24:06 +0200568 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200569 else
570 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100571 // The job keeps running, hide the buffer.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200572 del_buf = FALSE;
573 unload_buf = FALSE;
574 }
575 }
Bram Moolenaarc9f8b842020-11-24 19:36:16 +0100576 else if (buf->b_p_bh[0] == 'h' && !del_buf)
577 {
578 // Hide a terminal buffer.
579 unload_buf = FALSE;
580 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200581 else
582 {
Yee Cheng Chin42826332022-10-10 11:46:16 +0100583 if (del_buf || unload_buf)
584 {
585 // A terminal buffer is wiped out if the job has finished.
586 // We only do this when there's an intention to unload the
587 // buffer. This way, :hide and other similar commands won't
588 // wipe the buffer.
589 del_buf = TRUE;
590 unload_buf = TRUE;
591 wipe_buf = TRUE;
592 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200593 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100594 CHECK_CURBUF;
Bram Moolenaar94053a52017-08-01 21:44:33 +0200595 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597
Bram Moolenaarc667da52019-11-30 20:52:27 +0100598 // Disallow deleting the buffer when it is locked (already being closed or
599 // halfway a command that relies on it). Unloading is allowed.
Bram Moolenaar94f01952018-09-01 15:30:03 +0200600 if ((del_buf || wipe_buf) && !can_unload_buffer(buf))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100601 return FALSE;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200602
Bram Moolenaarc667da52019-11-30 20:52:27 +0100603 // check no autocommands closed the window
Bram Moolenaar4033c552017-09-16 20:54:51 +0200604 if (win != NULL && win_valid_any_tab(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100606 // Set b_last_cursor when closing the last window for the buffer.
607 // Remember the last cursor position and window options of the buffer.
608 // This used to be only for the current window, but then options like
609 // 'foldmethod' may be lost with a ":only" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 if (buf->b_nwindows == 1)
611 set_last_cursor(win);
612 buflist_setfpos(buf, win,
613 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
614 win->w_cursor.col, TRUE);
615 }
616
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200617 set_bufref(&bufref, buf);
618
Bram Moolenaarc667da52019-11-30 20:52:27 +0100619 // When the buffer is no longer in a window, trigger BufWinLeave
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 if (buf->b_nwindows == 1)
621 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200622 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100623 ++buf->b_locked_split;
Bram Moolenaar82404332016-07-10 17:00:38 +0200624 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
625 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200626 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100627 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100628 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200629aucmd_abort:
Bram Moolenaarf1474d82021-12-31 19:59:55 +0000630 emsg(_(e_autocommands_caused_command_to_abort));
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100631 return FALSE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100632 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200633 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100634 --buf->b_locked_split;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200635 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100636 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200637 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638
Bram Moolenaarc667da52019-11-30 20:52:27 +0100639 // When the buffer becomes hidden, but is not unloaded, trigger
640 // BufHidden
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 if (!unload_buf)
642 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200643 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100644 ++buf->b_locked_split;
Bram Moolenaar82404332016-07-10 17:00:38 +0200645 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
646 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200647 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100648 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200649 goto aucmd_abort;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200650 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100651 --buf->b_locked_split;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200652 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100653 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200654 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100656#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100657 // autocmds may abort script processing
658 if (!ignore_abort && aborting())
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100659 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200662
Bram Moolenaarc667da52019-11-30 20:52:27 +0100663 // If the buffer was in curwin and the window has changed, go back to that
664 // window, if it still exists. This avoids that ":edit x" triggering a
665 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200666 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
667 {
668 block_autocmds();
669 goto_tabpage_win(the_curtab, the_curwin);
670 unblock_autocmds();
671 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200672
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 nwindows = buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674
Bram Moolenaarc667da52019-11-30 20:52:27 +0100675 // decrease the link count from windows (unless not in any window)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 if (buf->b_nwindows > 0)
677 --buf->b_nwindows;
678
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100679#ifdef FEAT_DIFF
680 if (diffopt_hiddenoff() && !unload_buf && buf->b_nwindows == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100681 diff_buf_delete(buf); // Clear 'diff' for hidden buffer.
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100682#endif
683
Bram Moolenaarc667da52019-11-30 20:52:27 +0100684 // Return when a window is displaying the buffer or when it's not
685 // unloaded.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100687 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688
Bram Moolenaarc667da52019-11-30 20:52:27 +0100689 // Always remove the buffer when there is no file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 if (buf->b_ffname == NULL)
691 del_buf = TRUE;
692
Bram Moolenaarc667da52019-11-30 20:52:27 +0100693 // When closing the current buffer stop Visual mode before freeing
694 // anything.
Bram Moolenaar4930a762016-09-11 14:39:53 +0200695 if (buf == curbuf && VIsual_active
Bram Moolenaar9a27c7f2016-09-09 12:57:09 +0200696#if defined(EXITFREE)
697 && !entered_free_all_mem
698#endif
699 )
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200700 end_visual_mode();
701
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100702 // Free all things allocated for this buffer.
703 // Also calls the "BufDelete" autocommands when del_buf is TRUE.
704 //
Bram Moolenaarc667da52019-11-30 20:52:27 +0100705 // Remember if we are closing the current buffer. Restore the number of
706 // windows, so that autocommands in buf_freeall() don't get confused.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 is_curbuf = (buf == curbuf);
708 buf->b_nwindows = nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100710 buf_freeall(buf, (del_buf ? BFA_DEL : 0)
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100711 + (wipe_buf ? BFA_WIPE : 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100712 + (ignore_abort ? BFA_IGNORE_ABORT : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713
Bram Moolenaarc667da52019-11-30 20:52:27 +0100714 // Autocommands may have deleted the buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200715 if (!bufref_valid(&bufref))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100716 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100717#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100718 // autocmds may abort script processing
719 if (!ignore_abort && aborting())
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100720 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100723 // It's possible that autocommands change curbuf to the one being deleted.
724 // This might cause the previous curbuf to be deleted unexpectedly. But
725 // in some cases it's OK to delete the curbuf, because a new one is
726 // obtained anyway. Therefore only return if curbuf changed to the
727 // deleted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 if (buf == curbuf && !is_curbuf)
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100729 return FALSE;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200730
Bram Moolenaar4033c552017-09-16 20:54:51 +0200731 if (win_valid_any_tab(win) && win->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100732 win->w_buffer = NULL; // make sure we don't use the buffer now
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200733
Bram Moolenaarc667da52019-11-30 20:52:27 +0100734 // Autocommands may have opened or closed windows for this buffer.
735 // Decrement the count for the close we do here.
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200736 if (buf->b_nwindows > 0)
737 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 /*
740 * Remove the buffer from the list.
741 */
742 if (wipe_buf)
743 {
Bram Moolenaar347538f2022-03-26 16:28:06 +0000744 // Do not wipe out the buffer if it is used in a window.
745 if (buf->b_nwindows > 0)
746 return FALSE;
747
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200748 if (action == DOBUF_WIPE_REUSE)
749 {
750 // we can re-use this buffer number, store it
751 if (buf_reuse.ga_itemsize == 0)
752 ga_init2(&buf_reuse, sizeof(int), 50);
753 if (ga_grow(&buf_reuse, 1) == OK)
754 ((int *)buf_reuse.ga_data)[buf_reuse.ga_len++] = buf->b_fnum;
755 }
Bram Moolenaar3d6014f2018-10-11 19:27:47 +0200756 if (buf->b_sfname != buf->b_ffname)
757 VIM_CLEAR(buf->b_sfname);
758 else
759 buf->b_sfname = NULL;
760 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 if (buf->b_prev == NULL)
762 firstbuf = buf->b_next;
763 else
764 buf->b_prev->b_next = buf->b_next;
765 if (buf->b_next == NULL)
766 lastbuf = buf->b_prev;
767 else
768 buf->b_next->b_prev = buf->b_prev;
769 free_buffer(buf);
770 }
771 else
772 {
773 if (del_buf)
774 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100775 // Free all internal variables and reset option values, to make
776 // ":bdel" compatible with Vim 5.7.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 free_buffer_stuff(buf, TRUE);
778
Bram Moolenaarc667da52019-11-30 20:52:27 +0100779 // Make it look like a new buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
781
Bram Moolenaarc667da52019-11-30 20:52:27 +0100782 // Init the options when loaded again.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 buf->b_p_initialized = FALSE;
784 }
785 buf_clear_file(buf);
786 if (del_buf)
787 buf->b_p_bl = FALSE;
788 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100789 // NOTE: at this point "curbuf" may be invalid!
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100790 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791}
792
793/*
794 * Make buffer not contain a file.
795 */
796 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100797buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798{
799 buf->b_ml.ml_line_count = 1;
Bram Moolenaarc024b462019-06-08 18:07:21 +0200800 unchanged(buf, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 buf->b_shortname = FALSE;
Bram Moolenaar15775372022-10-29 20:01:52 +0100802 buf->b_p_eof = FALSE;
803 buf->b_start_eof = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 buf->b_p_eol = TRUE;
805 buf->b_start_eol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000807 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 buf->b_ml.ml_mfp = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +0100809 buf->b_ml.ml_flags = ML_EMPTY; // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810#ifdef FEAT_NETBEANS_INTG
811 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812#endif
813}
814
815/*
816 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200817 * the file. Careful: get here with "curwin" NULL when exiting.
818 * flags:
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100819 * BFA_DEL buffer is going to be deleted
820 * BFA_WIPE buffer is going to be wiped out
821 * BFA_KEEP_UNDO do not free undo information
822 * BFA_IGNORE_ABORT don't abort even when aborting() returns TRUE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100825buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200828 bufref_T bufref;
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200829 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaar5a497892016-09-03 16:29:04 +0200830 win_T *the_curwin = curwin;
831 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832
Bram Moolenaarc667da52019-11-30 20:52:27 +0100833 // Make sure the buffer isn't closed by autocommands.
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200834 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100835 ++buf->b_locked_split;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200836 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200837 if (buf->b_ml.ml_mfp != NULL)
838 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200839 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
840 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200841 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100842 // autocommands deleted the buffer
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200843 return;
844 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200845 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200847 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
848 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200849 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100850 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 return;
852 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200853 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200855 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
856 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200857 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100858 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 return;
860 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200861 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100862 --buf->b_locked_split;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200863
Bram Moolenaarc667da52019-11-30 20:52:27 +0100864 // If the buffer was in curwin and the window has changed, go back to that
865 // window, if it still exists. This avoids that ":edit x" triggering a
866 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaar5a497892016-09-03 16:29:04 +0200867 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
868 {
869 block_autocmds();
870 goto_tabpage_win(the_curtab, the_curwin);
871 unblock_autocmds();
872 }
Bram Moolenaar5a497892016-09-03 16:29:04 +0200873
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100874#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100875 // autocmds may abort script processing
876 if ((flags & BFA_IGNORE_ABORT) == 0 && aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100880 // It's possible that autocommands change curbuf to the one being deleted.
881 // This might cause curbuf to be deleted unexpectedly. But in some cases
882 // it's OK to delete the curbuf, because a new one is obtained anyway.
883 // Therefore only return if curbuf changed to the deleted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 if (buf == curbuf && !is_curbuf)
885 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886#ifdef FEAT_DIFF
Bram Moolenaarc667da52019-11-30 20:52:27 +0100887 diff_buf_delete(buf); // Can't use 'diff' for unloaded buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200889#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100890 // Remove any ownsyntax, unless exiting.
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200891 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaar89c71222011-11-30 15:40:56 +0100892 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200893#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000894
895#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +0100896 // No folds in an empty buffer.
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000897 {
898 win_T *win;
899 tabpage_T *tp;
900
901 FOR_ALL_TAB_WINDOWS(tp, win)
902 if (win->w_buffer == buf)
903 clearFolding(win);
904 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000905#endif
906
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907#ifdef FEAT_TCL
908 tcl_buffer_free(buf);
909#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100910 ml_close(buf, TRUE); // close and delete the memline/memfile
911 buf->b_ml.ml_line_count = 0; // no lines in buffer
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200912 if ((flags & BFA_KEEP_UNDO) == 0)
913 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100914 u_blockfree(buf); // free the memory allocated for undo
915 u_clearall(buf); // reset all undo information
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100918 syntax_clear(&buf->b_s); // reset syntax info
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100920#ifdef FEAT_PROP_POPUP
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100921 clear_buf_prop_types(buf);
922#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100923 buf->b_flags &= ~BF_READERR; // a read error is no longer relevant
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924}
925
926/*
927 * Free a buffer structure and the things it contains related to the buffer
928 * itself (not the file, that must have been done already).
929 */
930 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100931free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200933 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200935#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100936 // b:changedtick uses an item in buf_T, remove it now
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000937 dictitem_remove(buf->b_vars, (dictitem_T *)&buf->b_ct_di, "free buffer");
Bram Moolenaar429fa852013-04-15 12:27:36 +0200938 unref_var_dict(buf->b_vars);
Bram Moolenaar86173482019-10-01 17:02:16 +0200939 remove_listeners(buf);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200940#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200941#ifdef FEAT_LUA
942 lua_buffer_free(buf);
943#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000944#ifdef FEAT_MZSCHEME
945 mzscheme_buffer_free(buf);
946#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947#ifdef FEAT_PERL
948 perl_buf_free(buf);
949#endif
950#ifdef FEAT_PYTHON
951 python_buffer_free(buf);
952#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200953#ifdef FEAT_PYTHON3
954 python3_buffer_free(buf);
955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956#ifdef FEAT_RUBY
957 ruby_buffer_free(buf);
958#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200959#ifdef FEAT_JOB_CHANNEL
960 channel_buffer_free(buf);
961#endif
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200962#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +0200963 free_terminal(buf);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200964#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +0200965#ifdef FEAT_JOB_CHANNEL
966 vim_free(buf->b_prompt_text);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200967 free_callback(&buf->b_prompt_callback);
Bram Moolenaar86173482019-10-01 17:02:16 +0200968 free_callback(&buf->b_prompt_interrupt);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200969#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200970
971 buf_hashtab_remove(buf);
972
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200973 aubuflocal_remove(buf);
974
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200975 if (autocmd_busy)
976 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100977 // Do not free the buffer structure while autocommands are executing,
978 // it's still needed. Free it when autocmd_busy is reset.
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200979 buf->b_next = au_pending_free_buf;
980 au_pending_free_buf = buf;
981 }
982 else
Bram Moolenaarcee52202020-03-11 14:19:58 +0100983 {
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200984 vim_free(buf);
Bram Moolenaarcee52202020-03-11 14:19:58 +0100985 if (curbuf == buf)
986 curbuf = NULL; // make clear it's not to be used
987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988}
989
990/*
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100991 * Initializes b:changedtick.
Bram Moolenaar79518e22017-02-17 16:31:35 +0100992 */
993 static void
994init_changedtick(buf_T *buf)
995{
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100996 dictitem_T *di = (dictitem_T *)&buf->b_ct_di;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100997
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100998 di->di_flags = DI_FLAGS_FIX | DI_FLAGS_RO;
999 di->di_tv.v_type = VAR_NUMBER;
1000 di->di_tv.v_lock = VAR_FIXED;
1001 di->di_tv.vval.v_number = 0;
1002
Bram Moolenaar92769c32017-02-25 15:41:37 +01001003#ifdef FEAT_EVAL
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001004 STRCPY(buf->b_ct_di.di_key, "changedtick");
1005 (void)dict_add(buf->b_vars, di);
Bram Moolenaar92769c32017-02-25 15:41:37 +01001006#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01001007}
1008
1009/*
Bram Moolenaarc3126192022-08-26 12:58:17 +01001010 * Free the b_wininfo list for buffer "buf".
1011 */
1012 static void
1013clear_wininfo(buf_T *buf)
1014{
1015 wininfo_T *wip;
1016
1017 while (buf->b_wininfo != NULL)
1018 {
1019 wip = buf->b_wininfo;
1020 buf->b_wininfo = wip->wi_next;
1021 free_wininfo(wip);
1022 }
1023}
1024
1025/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
1027 */
1028 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001029free_buffer_stuff(
1030 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001031 int free_options) // free options as well
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032{
1033 if (free_options)
1034 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001035 clear_wininfo(buf); // including window-local options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +02001037#ifdef FEAT_SPELL
1038 ga_clear(&buf->b_s.b_langp);
1039#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 }
1041#ifdef FEAT_EVAL
Bram Moolenaar79518e22017-02-17 16:31:35 +01001042 {
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001043 varnumber_T tick = CHANGEDTICK(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001044
Bram Moolenaarc667da52019-11-30 20:52:27 +01001045 vars_clear(&buf->b_vars->dv_hashtab); // free all buffer variables
Bram Moolenaar79518e22017-02-17 16:31:35 +01001046 hash_init(&buf->b_vars->dv_hashtab);
1047 init_changedtick(buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001048 CHANGEDTICK(buf) = tick;
Bram Moolenaarf10997a2020-01-03 21:00:02 +01001049 remove_listeners(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051#endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001052 uc_clear(&buf->b_ucmds); // clear local user commands
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053#ifdef FEAT_SIGNS
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001054 buf_delete_signs(buf, (char_u *)"*"); // delete any signs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00001056#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001057 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00001058#endif
Bram Moolenaar0c740e72022-07-25 19:07:04 +01001059#ifdef FEAT_PROP_POPUP
1060 ga_clear_strings(&buf->b_textprop_text);
1061#endif
zeertzjqc207fd22022-06-29 10:37:40 +01001062 map_clear_mode(buf, MAP_ALL_MODES, TRUE, FALSE); // clear local mappings
1063 map_clear_mode(buf, MAP_ALL_MODES, TRUE, TRUE); // clear local abbrevs
Bram Moolenaard23a8232018-02-10 18:45:26 +01001064 VIM_CLEAR(buf->b_start_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065}
1066
1067/*
Bram Moolenaar4882d982020-10-25 17:55:09 +01001068 * Free one wininfo_T.
1069 */
1070 void
1071free_wininfo(wininfo_T *wip)
1072{
1073 if (wip->wi_optset)
1074 {
1075 clear_winopt(&wip->wi_opt);
1076#ifdef FEAT_FOLDING
1077 deleteFoldRecurse(&wip->wi_folds);
1078#endif
1079 }
1080 vim_free(wip);
1081}
1082
1083/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 * Go to another buffer. Handles the result of the ATTENTION dialog.
1085 */
1086 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001087goto_buffer(
1088 exarg_T *eap,
1089 int start,
1090 int dir,
1091 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001093 bufref_T old_curbuf;
Bram Moolenaar188639d2022-04-04 16:57:21 +01001094 int save_sea = swap_exists_action;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001095
1096 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097
Bram Moolenaar188639d2022-04-04 16:57:21 +01001098 if (swap_exists_action == SEA_NONE)
1099 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
1101 start, dir, count, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
1103 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001104#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001105 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001106
Bram Moolenaarc667da52019-11-30 20:52:27 +01001107 // Reset the error/interrupt/exception state here so that
1108 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001109 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001110#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001111
Bram Moolenaarc667da52019-11-30 20:52:27 +01001112 // Quitting means closing the split window, nothing else.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 win_close(curwin, TRUE);
Bram Moolenaar188639d2022-04-04 16:57:21 +01001114 swap_exists_action = save_sea;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001115 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001116
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001117#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001118 // Restore the error/interrupt/exception state if not discarded by a
1119 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001120 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001121#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 }
1123 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001124 handle_swap_exists(&old_curbuf);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001125}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127/*
1128 * Handle the situation of swap_exists_action being set.
1129 * It is allowed for "old_curbuf" to be NULL or invalid.
1130 */
1131 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001132handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133{
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001134#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001135 cleanup_T cs;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001136#endif
1137#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001138 long old_tw = curbuf->b_p_tw;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001139#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001140 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001141
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 if (swap_exists_action == SEA_QUIT)
1143 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001144#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001145 // Reset the error/interrupt/exception state here so that
1146 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001147 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001148#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001149
Bram Moolenaarc667da52019-11-30 20:52:27 +01001150 // User selected Quit at ATTENTION prompt. Go back to previous
1151 // buffer. If that buffer is gone or the same as the current one,
1152 // open a new, empty buffer.
1153 swap_exists_action = SEA_NONE; // don't want it again
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001154 swap_exists_did_quit = TRUE;
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001155 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001156 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1157 || old_curbuf->br_buf == curbuf)
Bram Moolenaar1d97efc2021-07-04 13:27:11 +02001158 {
1159 // Block autocommands here because curwin->w_buffer is NULL.
1160 block_autocmds();
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001161 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar1d97efc2021-07-04 13:27:11 +02001162 unblock_autocmds();
1163 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001164 else
1165 buf = old_curbuf->br_buf;
1166 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001167 {
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001168 int old_msg_silent = msg_silent;
1169
1170 if (shortmess(SHM_FILEINFO))
1171 msg_silent = 1; // prevent fileinfo message
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001172 enter_buffer(buf);
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001173 // restore msg_silent, so that the command line will be shown
1174 msg_silent = old_msg_silent;
1175
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001176#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001177 if (old_tw != curbuf->b_p_tw)
1178 check_colorcolumn(curwin);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001179#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001180 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001181 // If "old_curbuf" is NULL we are in big trouble here...
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001182
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001183#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001184 // Restore the error/interrupt/exception state if not discarded by a
1185 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001186 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 }
1189 else if (swap_exists_action == SEA_RECOVER)
1190 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001191#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001192 // Reset the error/interrupt/exception state here so that
1193 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001194 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001195#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001196
Bram Moolenaarc667da52019-11-30 20:52:27 +01001197 // User selected Recover at ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 msg_scroll = TRUE;
Bram Moolenaar99499b12019-05-23 21:35:48 +02001199 ml_recover(FALSE);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001200 msg_puts("\n"); // don't overwrite the last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001202 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001203
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001204#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001205 // Restore the error/interrupt/exception state if not discarded by a
1206 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001207 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001208#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 }
1210 swap_exists_action = SEA_NONE;
1211}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213/*
Bram Moolenaara02471e2014-01-10 16:43:14 +01001214 * Make the current buffer empty.
1215 * Used when it is wiped out and it's the last buffer.
1216 */
1217 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001218empty_curbuf(
1219 int close_others,
1220 int forceit,
1221 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001222{
1223 int retval;
1224 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001225 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001226
1227 if (action == DOBUF_UNLOAD)
1228 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001229 emsg(_(e_cannot_unload_last_buffer));
Bram Moolenaara02471e2014-01-10 16:43:14 +01001230 return FAIL;
1231 }
1232
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001233 set_bufref(&bufref, buf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001234 if (close_others)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001235 // Close any other windows on this buffer, then make it empty.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001236 close_windows(buf, TRUE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001237
1238 setpcmark();
1239 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1240 forceit ? ECMD_FORCEIT : 0, curwin);
1241
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001242 // do_ecmd() may create a new buffer, then we have to delete
1243 // the old one. But do_ecmd() may have done that already, check
1244 // if the buffer still exists.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001245 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001246 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001247 if (!close_others)
1248 need_fileinfo = FALSE;
1249 return retval;
1250}
Bram Moolenaar94f01952018-09-01 15:30:03 +02001251
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252/*
1253 * Implementation of the commands for the buffer list.
1254 *
1255 * action == DOBUF_GOTO go to specified buffer
1256 * action == DOBUF_SPLIT split window and go to specified buffer
1257 * action == DOBUF_UNLOAD unload specified buffer(s)
1258 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1259 * action == DOBUF_WIPE delete specified buffer(s) really
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001260 * action == DOBUF_WIPE_REUSE idem, and add number to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 *
1262 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1263 * start == DOBUF_FIRST go to "count" buffer from first buffer
1264 * start == DOBUF_LAST go to "count" buffer from last buffer
1265 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1266 *
1267 * Return FAIL or OK.
1268 */
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001269 static int
1270do_buffer_ext(
Bram Moolenaar7454a062016-01-30 15:14:10 +01001271 int action,
1272 int start,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001273 int dir, // FORWARD or BACKWARD
1274 int count, // buffer number or number of buffers
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001275 int flags) // DOBUF_FORCEIT etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276{
1277 buf_T *buf;
1278 buf_T *bp;
1279 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001280 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281
1282 switch (start)
1283 {
1284 case DOBUF_FIRST: buf = firstbuf; break;
1285 case DOBUF_LAST: buf = lastbuf; break;
1286 default: buf = curbuf; break;
1287 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001288 if (start == DOBUF_MOD) // find next modified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 {
1290 while (count-- > 0)
1291 {
1292 do
1293 {
1294 buf = buf->b_next;
1295 if (buf == NULL)
1296 buf = firstbuf;
1297 }
1298 while (buf != curbuf && !bufIsChanged(buf));
1299 }
1300 if (!bufIsChanged(buf))
1301 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001302 emsg(_(e_no_modified_buffer_found));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 return FAIL;
1304 }
1305 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001306 else if (start == DOBUF_FIRST && count) // find specified buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 {
1308 while (buf != NULL && buf->b_fnum != count)
1309 buf = buf->b_next;
1310 }
1311 else
1312 {
1313 bp = NULL;
1314 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1315 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001316 // remember the buffer where we start, we come back there when all
1317 // buffers are unlisted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 if (bp == NULL)
1319 bp = buf;
1320 if (dir == FORWARD)
1321 {
1322 buf = buf->b_next;
1323 if (buf == NULL)
1324 buf = firstbuf;
1325 }
1326 else
1327 {
1328 buf = buf->b_prev;
1329 if (buf == NULL)
1330 buf = lastbuf;
1331 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001332 // don't count unlisted buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 if (unload || buf->b_p_bl)
1334 {
1335 --count;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001336 bp = NULL; // use this buffer as new starting point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 }
1338 if (bp == buf)
1339 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001340 // back where we started, didn't find anything.
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001341 emsg(_(e_there_is_no_listed_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 return FAIL;
1343 }
1344 }
1345 }
1346
Bram Moolenaarc667da52019-11-30 20:52:27 +01001347 if (buf == NULL) // could not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 {
1349 if (start == DOBUF_FIRST)
1350 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001351 // don't warn when deleting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 if (!unload)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001353 semsg(_(e_buffer_nr_does_not_exist), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 }
1355 else if (dir == FORWARD)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001356 emsg(_(e_cannot_go_beyond_last_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 else
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001358 emsg(_(e_cannot_go_before_first_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 return FAIL;
1360 }
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001361#ifdef FEAT_PROP_POPUP
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001362 if ((flags & DOBUF_NOPOPUP) && bt_popup(buf) && !bt_terminal(buf))
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001363 return OK;
1364#endif
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01001365 if ((action == DOBUF_GOTO || action == DOBUF_SPLIT)
1366 && (buf->b_flags & BF_DUMMY))
1367 {
1368 // disallow navigating to the dummy buffer
1369 semsg(_(e_buffer_nr_does_not_exist), count);
1370 return FAIL;
1371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372
1373#ifdef FEAT_GUI
1374 need_mouse_correct = TRUE;
1375#endif
1376
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001378 * delete buffer "buf" from memory and/or the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 */
1380 if (unload)
1381 {
1382 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001383 bufref_T bufref;
1384
Bram Moolenaar94f01952018-09-01 15:30:03 +02001385 if (!can_unload_buffer(buf))
Bram Moolenaara997b452018-04-17 23:24:06 +02001386 return FAIL;
Bram Moolenaara997b452018-04-17 23:24:06 +02001387
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001388 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389
Bram Moolenaarc667da52019-11-30 20:52:27 +01001390 // When unloading or deleting a buffer that's already unloaded and
1391 // unlisted: fail silently.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001392 if (action != DOBUF_WIPE && action != DOBUF_WIPE_REUSE
1393 && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 return FAIL;
1395
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001396 if ((flags & DOBUF_FORCEIT) == 0 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001398#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001399 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001401# ifdef FEAT_TERMINAL
1402 if (term_job_running(buf->b_term))
1403 {
1404 if (term_confirm_stop(buf) == FAIL)
1405 return FAIL;
1406 }
1407 else
1408# endif
1409 {
1410 dialog_changed(buf, FALSE);
1411 if (!bufref_valid(&bufref))
1412 // Autocommand deleted buffer, oops! It's not changed
1413 // now.
1414 return FAIL;
1415 // If it's still changed fail silently, the dialog already
1416 // mentioned why it fails.
1417 if (bufIsChanged(buf))
1418 return FAIL;
1419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001421 else
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001424 no_write_message_buf(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 return FAIL;
1426 }
1427 }
1428
Bram Moolenaarc667da52019-11-30 20:52:27 +01001429 // When closing the current buffer stop Visual mode.
Bram Moolenaar4930a762016-09-11 14:39:53 +02001430 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001431 end_visual_mode();
1432
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001433 // If deleting the last (listed) buffer, make it empty.
1434 // The last (listed) buffer cannot be unloaded.
Bram Moolenaar29323592016-07-24 22:04:11 +02001435 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 if (bp->b_p_bl && bp != buf)
1437 break;
1438 if (bp == NULL && buf == curbuf)
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001439 return empty_curbuf(TRUE, (flags & DOBUF_FORCEIT), action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001441 // If the deleted buffer is the current one, close the current window
1442 // (unless it's the only window). Repeat this so long as we end up in
1443 // a window with this buffer.
Bram Moolenaarf740b292006-02-16 22:11:02 +00001444 while (buf == curbuf
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001445 && !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
Bram Moolenaar459ca562016-11-10 18:16:33 +01001446 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001447 {
1448 if (win_close(curwin, FALSE) == FAIL)
1449 break;
1450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001452 // If the buffer to be deleted is not the current one, delete it here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 if (buf != curbuf)
1454 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001455 close_windows(buf, FALSE);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001456 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows <= 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001457 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 return OK;
1459 }
1460
1461 /*
1462 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001463 * There should be another, otherwise it would have been handled
1464 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001465 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 * Then prefer the buffer we most recently visited.
1467 * Else try to find one that is loaded, after the current buffer,
1468 * then before the current buffer.
1469 * Finally use any buffer.
1470 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001471 buf = NULL; // selected buffer
1472 bp = NULL; // used when no loaded buffer found
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001473 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1474 buf = au_new_curbuf.br_buf;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001475 else if (curwin->w_jumplistlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 {
1477 int jumpidx;
1478
1479 jumpidx = curwin->w_jumplistidx - 1;
1480 if (jumpidx < 0)
1481 jumpidx = curwin->w_jumplistlen - 1;
1482
1483 forward = jumpidx;
1484 while (jumpidx != curwin->w_jumplistidx)
1485 {
1486 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1487 if (buf != NULL)
1488 {
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001489 // Skip current and unlisted bufs. Also skip a quickfix
1490 // buffer, it might be deleted soon.
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001491 if (buf == curbuf || !buf->b_p_bl || bt_quickfix(buf))
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001492 buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 else if (buf->b_ml.ml_mfp == NULL)
1494 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001495 // skip unloaded buf, but may keep it for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 if (bp == NULL)
1497 bp = buf;
1498 buf = NULL;
1499 }
1500 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001501 if (buf != NULL) // found a valid buffer: stop searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001503 // advance to older entry in jump list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1505 break;
1506 if (--jumpidx < 0)
1507 jumpidx = curwin->w_jumplistlen - 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001508 if (jumpidx == forward) // List exhausted for sure
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 break;
1510 }
1511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512
Bram Moolenaarc667da52019-11-30 20:52:27 +01001513 if (buf == NULL) // No previous buffer, Try 2'nd approach
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 {
1515 forward = TRUE;
1516 buf = curbuf->b_next;
1517 for (;;)
1518 {
1519 if (buf == NULL)
1520 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001521 if (!forward) // tried both directions
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 break;
1523 buf = curbuf->b_prev;
1524 forward = FALSE;
1525 continue;
1526 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001527 // in non-help buffer, try to skip help buffers, and vv
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001528 if (buf->b_help == curbuf->b_help && buf->b_p_bl
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001529 && !bt_quickfix(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001531 if (buf->b_ml.ml_mfp != NULL) // found loaded buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001533 if (bp == NULL) // remember unloaded buf for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 bp = buf;
1535 }
1536 if (forward)
1537 buf = buf->b_next;
1538 else
1539 buf = buf->b_prev;
1540 }
1541 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001542 if (buf == NULL) // No loaded buffer, use unloaded one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 buf = bp;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001544 if (buf == NULL) // No loaded buffer, find listed one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001546 FOR_ALL_BUFFERS(buf)
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001547 if (buf->b_p_bl && buf != curbuf && !bt_quickfix(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 break;
1549 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001550 if (buf == NULL) // Still no buffer, just take one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 {
1552 if (curbuf->b_next != NULL)
1553 buf = curbuf->b_next;
1554 else
1555 buf = curbuf->b_prev;
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001556 if (bt_quickfix(buf))
1557 buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 }
1559 }
1560
Bram Moolenaara02471e2014-01-10 16:43:14 +01001561 if (buf == NULL)
1562 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001563 // Autocommands must have wiped out all other buffers. Only option
1564 // now is to make the current buffer empty.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001565 return empty_curbuf(FALSE, (flags & DOBUF_FORCEIT), action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001566 }
1567
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001569 * make "buf" the current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001571 if (action == DOBUF_SPLIT) // split window first
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001573 // If 'switchbuf' contains "useopen": jump to first window containing
1574 // "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00001575 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001576 return OK;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001577 // If 'switchbuf' contains "usetab": jump to first window in any tab
1578 // page containing "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00001579 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 return OK;
1581 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 return FAIL;
1583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584
Bram Moolenaarc667da52019-11-30 20:52:27 +01001585 // go to current buffer - nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 if (buf == curbuf)
1587 return OK;
1588
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001589 // Check if the current buffer may be abandoned.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001590 if (action == DOBUF_GOTO && !can_abandon(curbuf, (flags & DOBUF_FORCEIT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 {
1592#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001593 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001595# ifdef FEAT_TERMINAL
1596 if (term_job_running(curbuf->b_term))
1597 {
1598 if (term_confirm_stop(curbuf) == FAIL)
1599 return FAIL;
1600 // Manually kill the terminal here because this command will
1601 // hide it otherwise.
1602 free_terminal(curbuf);
1603 }
1604 else
1605# endif
1606 {
1607 bufref_T bufref;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001608
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001609 set_bufref(&bufref, buf);
1610 dialog_changed(curbuf, FALSE);
1611 if (!bufref_valid(&bufref))
1612 // Autocommand deleted buffer, oops!
1613 return FAIL;
1614
1615 if (bufIsChanged(curbuf))
1616 {
1617 no_write_message();
1618 return FAIL;
1619 }
1620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 }
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001622 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623#endif
1624 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001625 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 return FAIL;
1627 }
1628 }
1629
Bram Moolenaarc667da52019-11-30 20:52:27 +01001630 // Go to the other buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 set_curbuf(buf, action);
1632
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 if (action == DOBUF_SPLIT)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001634 RESET_BINDING(curwin); // reset 'scrollbind' and 'cursorbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001636#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001637 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 return FAIL;
1639#endif
1640
1641 return OK;
1642}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001644 int
1645do_buffer(
1646 int action,
1647 int start,
1648 int dir, // FORWARD or BACKWARD
1649 int count, // buffer number or number of buffers
1650 int forceit) // TRUE when using !
1651{
1652 return do_buffer_ext(action, start, dir, count,
1653 forceit ? DOBUF_FORCEIT : 0);
1654}
1655
1656/*
1657 * do_bufdel() - delete or unload buffer(s)
1658 *
1659 * addr_count == 0: ":bdel" - delete current buffer
1660 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1661 * buffer "end_bnr", then any other arguments.
1662 * addr_count == 2: ":N,N bdel" - delete buffers in range
1663 *
1664 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1665 * DOBUF_DEL (":bdel")
1666 *
1667 * Returns error message or NULL
1668 */
1669 char *
1670do_bufdel(
1671 int command,
1672 char_u *arg, // pointer to extra arguments
1673 int addr_count,
1674 int start_bnr, // first buffer number in a range
1675 int end_bnr, // buffer nr or last buffer nr in a range
1676 int forceit)
1677{
1678 int do_current = 0; // delete current buffer?
1679 int deleted = 0; // number of buffers deleted
1680 char *errormsg = NULL; // return value
1681 int bnr; // buffer number
1682 char_u *p;
1683
1684 if (addr_count == 0)
1685 {
1686 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1687 }
1688 else
1689 {
1690 if (addr_count == 2)
1691 {
1692 if (*arg) // both range and argument is not allowed
Bram Moolenaar74409f62022-01-01 15:58:22 +00001693 return ex_errmsg(e_trailing_characters_str, arg);
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001694 bnr = start_bnr;
1695 }
1696 else // addr_count == 1
1697 bnr = end_bnr;
1698
1699 for ( ;!got_int; ui_breakcheck())
1700 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001701 // Delete the current buffer last, otherwise when the
1702 // current buffer is deleted, the next buffer becomes
1703 // the current one and will be loaded, which may then
1704 // also be deleted, etc.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001705 if (bnr == curbuf->b_fnum)
1706 do_current = bnr;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00001707 else if (do_buffer_ext(command, DOBUF_FIRST, FORWARD, bnr,
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001708 DOBUF_NOPOPUP | (forceit ? DOBUF_FORCEIT : 0)) == OK)
1709 ++deleted;
1710
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001711 // find next buffer number to delete/unload
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001712 if (addr_count == 2)
1713 {
1714 if (++bnr > end_bnr)
1715 break;
1716 }
1717 else // addr_count == 1
1718 {
1719 arg = skipwhite(arg);
1720 if (*arg == NUL)
1721 break;
1722 if (!VIM_ISDIGIT(*arg))
1723 {
1724 p = skiptowhite_esc(arg);
1725 bnr = buflist_findpat(arg, p,
1726 command == DOBUF_WIPE || command == DOBUF_WIPE_REUSE,
1727 FALSE, FALSE);
1728 if (bnr < 0) // failed
1729 break;
1730 arg = p;
1731 }
1732 else
1733 bnr = getdigits(&arg);
1734 }
1735 }
1736 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1737 FORWARD, do_current, forceit) == OK)
1738 ++deleted;
1739
1740 if (deleted == 0)
1741 {
1742 if (command == DOBUF_UNLOAD)
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001743 STRCPY(IObuff, _(e_no_buffers_were_unloaded));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001744 else if (command == DOBUF_DEL)
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001745 STRCPY(IObuff, _(e_no_buffers_were_deleted));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001746 else
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001747 STRCPY(IObuff, _(e_no_buffers_were_wiped_out));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001748 errormsg = (char *)IObuff;
1749 }
1750 else if (deleted >= p_report)
1751 {
1752 if (command == DOBUF_UNLOAD)
1753 smsg(NGETTEXT("%d buffer unloaded",
1754 "%d buffers unloaded", deleted), deleted);
1755 else if (command == DOBUF_DEL)
1756 smsg(NGETTEXT("%d buffer deleted",
1757 "%d buffers deleted", deleted), deleted);
1758 else
1759 smsg(NGETTEXT("%d buffer wiped out",
1760 "%d buffers wiped out", deleted), deleted);
1761 }
1762 }
1763
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001764 return errormsg;
1765}
1766
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767/*
1768 * Set current buffer to "buf". Executes autocommands and closes current
1769 * buffer. "action" tells how to close the current buffer:
1770 * DOBUF_GOTO free or hide it
1771 * DOBUF_SPLIT nothing
1772 * DOBUF_UNLOAD unload it
1773 * DOBUF_DEL delete it
1774 * DOBUF_WIPE wipe it out
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001775 * DOBUF_WIPE_REUSE wipe it out and add to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 */
1777 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001778set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779{
1780 buf_T *prevbuf;
1781 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001782 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001783#ifdef FEAT_SYN_HL
1784 long old_tw = curbuf->b_p_tw;
1785#endif
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001786 bufref_T newbufref;
1787 bufref_T prevbufref;
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001788 int valid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789
1790 setpcmark();
Bram Moolenaare1004402020-10-24 20:49:43 +02001791 if ((cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001792 curwin->w_alt_fnum = curbuf->b_fnum; // remember alternate file
1793 buflist_altfpos(curwin); // remember curpos
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794
Bram Moolenaarc667da52019-11-30 20:52:27 +01001795 // Don't restart Select mode after switching to another buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797
Bram Moolenaarc667da52019-11-30 20:52:27 +01001798 // close_windows() or apply_autocmds() may change curbuf and wipe out "buf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 prevbuf = curbuf;
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001800 set_bufref(&prevbufref, prevbuf);
1801 set_bufref(&newbufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802
Bram Moolenaar983d83f2021-02-07 12:12:43 +01001803 // Autocommands may delete the current buffer and/or the buffer we want to
1804 // go to. In those cases don't close the buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001805 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001806 || (bufref_valid(&prevbufref)
1807 && bufref_valid(&newbufref)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001808#ifdef FEAT_EVAL
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001809 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001811 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001813#ifdef FEAT_SYN_HL
1814 if (prevbuf == curwin->w_buffer)
1815 reset_synblock(curwin);
1816#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001818 close_windows(prevbuf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001819#if defined(FEAT_EVAL)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001820 if (bufref_valid(&prevbufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821#else
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001822 if (bufref_valid(&prevbufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001824 {
Bram Moolenaare25865a2012-07-06 16:22:02 +02001825 win_T *previouswin = curwin;
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001826
Bram Moolenaare615db02022-01-20 21:00:54 +00001827 // Do not sync when in Insert mode and the buffer is open in
1828 // another window, might be a timer doing something in another
1829 // window.
1830 if (prevbuf == curbuf
Bram Moolenaar24959102022-05-07 20:01:16 +01001831 && ((State & MODE_INSERT) == 0 || curbuf->b_nwindows <= 1))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001832 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1834 unload ? action : (action == DOBUF_GOTO
Bram Moolenaareb44a682017-08-03 22:44:55 +02001835 && !buf_hide(prevbuf)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001836 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0,
1837 FALSE, FALSE);
Bram Moolenaare25865a2012-07-06 16:22:02 +02001838 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001839 // autocommands changed curwin, Grr!
Bram Moolenaare25865a2012-07-06 16:22:02 +02001840 curwin = previouswin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001843 // An autocommand may have deleted "buf", already entered it (e.g., when
1844 // it did ":bunload") or aborted the script processing.
1845 // If curwin->w_buffer is null, enter_buffer() will make it valid again
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001846 valid = buf_valid(buf);
1847 if ((valid && buf != curbuf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001848#ifdef FEAT_EVAL
Bram Moolenaar4033c552017-09-16 20:54:51 +02001849 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001851 ) || curwin->w_buffer == NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001852 {
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001853 // If the buffer is not valid but curwin->w_buffer is NULL we must
1854 // enter some buffer. Using the last one is hopefully OK.
1855 if (!valid)
1856 enter_buffer(lastbuf);
1857 else
1858 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001859#ifdef FEAT_SYN_HL
1860 if (old_tw != curbuf->b_p_tw)
1861 check_colorcolumn(curwin);
1862#endif
1863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864}
1865
1866/*
1867 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001868 * Old curbuf must have been abandoned already! This also means "curbuf" may
1869 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001872enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873{
Bram Moolenaarcfeb8a52022-08-13 14:09:44 +01001874 // when closing the current buffer stop Visual mode
1875 if (VIsual_active
1876#if defined(EXITFREE)
1877 && !entered_free_all_mem
1878#endif
1879 )
1880 end_visual_mode();
1881
Bram Moolenaar010ee962019-09-25 20:37:36 +02001882 // Get the buffer in the current window.
1883 curwin->w_buffer = buf;
1884 curbuf = buf;
1885 ++curbuf->b_nwindows;
1886
1887 // Copy buffer and window local option values. Not for a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1889 if (!buf->b_help)
1890 get_winopts(buf);
1891#ifdef FEAT_FOLDING
1892 else
Bram Moolenaar010ee962019-09-25 20:37:36 +02001893 // Remove all folds in the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 clearFolding(curwin);
Bram Moolenaar010ee962019-09-25 20:37:36 +02001895 foldUpdateAll(curwin); // update folds (later).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896#endif
1897
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001899 if (curwin->w_p_diff)
1900 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901#endif
1902
Bram Moolenaara971b822011-09-14 14:43:25 +02001903#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001904 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001905#endif
1906
Bram Moolenaarc667da52019-11-30 20:52:27 +01001907 // Cursor on first line by default.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 curwin->w_cursor.lnum = 1;
1909 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001912 curwin->w_topline_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913
Bram Moolenaarc667da52019-11-30 20:52:27 +01001914 // mark cursor position as being invalid
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001915 curwin->w_valid = 0;
1916
Bram Moolenaar9cea87c2018-09-21 16:59:45 +02001917 buflist_setfpos(curbuf, curwin, curbuf->b_last_cursor.lnum,
1918 curbuf->b_last_cursor.col, TRUE);
1919
Bram Moolenaarc667da52019-11-30 20:52:27 +01001920 // Make sure the buffer is loaded.
1921 if (curbuf->b_ml.ml_mfp == NULL) // need to load the file
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001922 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001923 // If there is no filetype, allow for detecting one. Esp. useful for
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001924 // ":ball" used in an autocommand. If there already is a filetype we
Bram Moolenaarc667da52019-11-30 20:52:27 +01001925 // might prefer to keep it.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001926 if (*curbuf->b_p_ft == NUL)
1927 did_filetype = FALSE;
1928
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001929 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 else
1932 {
Bram Moolenaareda65222019-05-16 20:29:44 +02001933 if (!msg_silent && !shortmess(SHM_FILEINFO))
1934 need_fileinfo = TRUE; // display file info after redraw
1935
1936 // check if file changed
1937 (void)buf_check_timestamp(curbuf, FALSE);
1938
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001940#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001942#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1944 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 }
1946
Bram Moolenaarc667da52019-11-30 20:52:27 +01001947 // If autocommands did not change the cursor position, restore cursor lnum
1948 // and possibly cursor col.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 if (curwin->w_cursor.lnum == 1 && inindent(0))
1950 buflist_getfpos();
1951
Bram Moolenaarc667da52019-11-30 20:52:27 +01001952 check_arg_idx(curwin); // check for valid arg_idx
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 maketitle();
Bram Moolenaarc667da52019-11-30 20:52:27 +01001954 // when autocmds didn't change it
Bram Moolenaard4153d42008-11-15 15:06:17 +00001955 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00001956 scroll_cursor_halfway(FALSE, FALSE); // redisplay at correct position
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957
Bram Moolenaar009b2592004-10-24 19:18:58 +00001958#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarc667da52019-11-30 20:52:27 +01001959 // Send fileOpened event because we've changed buffers.
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001960 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001961#endif
1962
Bram Moolenaarc667da52019-11-30 20:52:27 +01001963 // Change directories when the 'acd' option is set.
Bram Moolenaar6f470022018-04-10 18:47:20 +02001964 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965
1966#ifdef FEAT_KEYMAP
1967 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001968 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001970#ifdef FEAT_SPELL
Bram Moolenaarc667da52019-11-30 20:52:27 +01001971 // May need to set the spell language. Can only do this after the buffer
1972 // has been properly setup.
Bram Moolenaar860cae12010-06-05 23:22:07 +02001973 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001974 (void)parse_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001975#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001976#ifdef FEAT_VIMINFO
1977 curbuf->b_last_used = vim_time();
1978#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001979
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001980 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981}
1982
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001983#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1984/*
1985 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001986 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001987 */
1988 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001989do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001990{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001991 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001992 && curbuf->b_ffname != NULL
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001993 && vim_chdirfile(curbuf->b_ffname, "auto") == OK)
Bram Moolenaar05268152021-11-18 18:53:45 +00001994 {
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001995 shorten_fnames(TRUE);
Bram Moolenaar05268152021-11-18 18:53:45 +00001996 last_chdir_reason = "autochdir";
1997 }
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001998}
1999#endif
2000
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01002001 static void
2002no_write_message_buf(buf_T *buf UNUSED)
2003{
2004#ifdef FEAT_TERMINAL
2005 if (term_job_running(buf->b_term))
2006 emsg(_(e_job_still_running_add_bang_to_end_the_job));
2007 else
2008#endif
2009 semsg(_(e_no_write_since_last_change_for_buffer_nr_add_bang_to_override),
2010 buf->b_fnum);
2011}
2012
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002013 void
2014no_write_message(void)
2015{
2016#ifdef FEAT_TERMINAL
2017 if (term_job_running(curbuf->b_term))
Bram Moolenaarf1474d82021-12-31 19:59:55 +00002018 emsg(_(e_job_still_running_add_bang_to_end_the_job));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002019 else
2020#endif
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002021 emsg(_(e_no_write_since_last_change_add_bang_to_override));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002022}
2023
2024 void
Bram Moolenaar7a760922018-02-19 23:10:02 +01002025no_write_message_nobang(buf_T *buf UNUSED)
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002026{
2027#ifdef FEAT_TERMINAL
Bram Moolenaar7a760922018-02-19 23:10:02 +01002028 if (term_job_running(buf->b_term))
Bram Moolenaarf1474d82021-12-31 19:59:55 +00002029 emsg(_(e_job_still_running));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002030 else
2031#endif
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002032 emsg(_(e_no_write_since_last_change));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002033}
2034
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035/*
2036 * functions for dealing with the buffer list
2037 */
2038
2039/*
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002040 * Return TRUE if the current buffer is empty, unnamed, unmodified and used in
2041 * only one window. That means it can be re-used.
2042 */
2043 int
2044curbuf_reusable(void)
2045{
2046 return (curbuf != NULL
2047 && curbuf->b_ffname == NULL
2048 && curbuf->b_nwindows <= 1
2049 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY())
Bram Moolenaar39803d82019-04-07 12:04:51 +02002050 && !bt_quickfix(curbuf)
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002051 && !curbufIsChanged());
2052}
2053
2054/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 * Add a file name to the buffer list. Return a pointer to the buffer.
2056 * If the same file name already exists return a pointer to that buffer.
2057 * If it does not exist, or if fname == NULL, a new entry is created.
2058 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
2059 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
2060 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02002061 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02002062 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
2063 * if the buffer already exists.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002064 * If (flags & BLN_REUSE) is TRUE, may use buffer number from "buf_reuse".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 * This is the ONLY way to create a new buffer.
2066 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002068buflist_new(
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002069 char_u *ffname_arg, // full path of fname or relative
2070 char_u *sfname_arg, // short fname or NULL
2071 linenr_T lnum, // preferred cursor line
2072 int flags) // BLN_ defines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002074 char_u *ffname = ffname_arg;
2075 char_u *sfname = sfname_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 buf_T *buf;
2077#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002078 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079#endif
2080
Bram Moolenaar480778b2016-07-14 22:09:39 +02002081 if (top_file_num == 1)
2082 hash_init(&buf_hashtab);
2083
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002084 fname_expand(curbuf, &ffname, &sfname); // will allocate ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085
2086 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002087 * If the file name already exists in the list, update the entry.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 */
2089#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002090 // On Unix we can use inode numbers when the file exists. Works better
2091 // for hard links.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
2093 st.st_dev = (dev_T)-1;
2094#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02002095 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096#ifdef UNIX
2097 buflist_findname_stat(ffname, &st)
2098#else
2099 buflist_findname(ffname)
2100#endif
2101 ) != NULL)
2102 {
2103 vim_free(ffname);
2104 if (lnum != 0)
Bram Moolenaar89b693e2020-10-25 17:09:50 +01002105 buflist_setfpos(buf, (flags & BLN_NOCURWIN) ? NULL : curwin,
2106 lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02002107
2108 if ((flags & BLN_NOOPT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002109 // copy the options now, if 'cpo' doesn't have 's' and not done
2110 // already
Bram Moolenaar82404332016-07-10 17:00:38 +02002111 buf_copy_options(buf, 0);
2112
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 if ((flags & BLN_LISTED) && !buf->b_p_bl)
2114 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002115 bufref_T bufref;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002116
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 buf->b_p_bl = TRUE;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002118 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002120 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002121 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002122 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002123 return NULL;
2124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 }
2126 return buf;
2127 }
2128
2129 /*
2130 * If the current buffer has no name and no contents, use the current
2131 * buffer. Otherwise: Need to allocate a new buffer structure.
2132 *
2133 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00002134 * (A spell file buffer is allocated in spell.c, but that's not a normal
2135 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 */
2137 buf = NULL;
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002138 if ((flags & BLN_CURBUF) && curbuf_reusable())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 {
2140 buf = curbuf;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002141 // It's like this buffer is deleted. Watch out for autocommands that
2142 // change curbuf! If that happens, allocate a new buffer anyway.
Charlie Grovesfef44852022-04-19 16:24:12 +01002143 buf_freeall(buf, BFA_WIPE | BFA_DEL);
2144 if (buf != curbuf) // autocommands deleted the buffer!
2145 return NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002146#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002147 if (aborting()) // autocmds may abort script processing
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002148 {
2149 vim_free(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 return NULL;
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 }
2154 if (buf != curbuf || curbuf == NULL)
2155 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002156 buf = ALLOC_CLEAR_ONE(buf_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 if (buf == NULL)
2158 {
2159 vim_free(ffname);
2160 return NULL;
2161 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02002162#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002163 // init b: variables
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01002164 buf->b_vars = dict_alloc_id(aid_newbuf_bvars);
Bram Moolenaar429fa852013-04-15 12:27:36 +02002165 if (buf->b_vars == NULL)
2166 {
2167 vim_free(ffname);
2168 vim_free(buf);
2169 return NULL;
2170 }
2171 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
2172#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01002173 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 }
2175
2176 if (ffname != NULL)
2177 {
2178 buf->b_ffname = ffname;
2179 buf->b_sfname = vim_strsave(sfname);
2180 }
2181
2182 clear_wininfo(buf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002183 buf->b_wininfo = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184
2185 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
2186 || buf->b_wininfo == NULL)
2187 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002188 if (buf->b_sfname != buf->b_ffname)
2189 VIM_CLEAR(buf->b_sfname);
2190 else
2191 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01002192 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 if (buf != curbuf)
2194 free_buffer(buf);
2195 return NULL;
2196 }
2197
2198 if (buf == curbuf)
2199 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002200 free_buffer_stuff(buf, FALSE); // delete local variables et al.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002201
Bram Moolenaarc667da52019-11-30 20:52:27 +01002202 // Init the options.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002203 buf->b_p_initialized = FALSE;
2204 buf_copy_options(buf, BCO_ENTER);
2205
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206#ifdef FEAT_KEYMAP
Bram Moolenaarc667da52019-11-30 20:52:27 +01002207 // need to reload lmaps and set b:keymap_name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 curbuf->b_kmap_state |= KEYMAP_INIT;
2209#endif
2210 }
2211 else
2212 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002213 // put the new buffer at the end of the buffer list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 buf->b_next = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002215 if (firstbuf == NULL) // buffer list is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 {
2217 buf->b_prev = NULL;
2218 firstbuf = buf;
2219 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002220 else // append new buffer at end of list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 {
2222 lastbuf->b_next = buf;
2223 buf->b_prev = lastbuf;
2224 }
2225 lastbuf = buf;
2226
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002227 if ((flags & BLN_REUSE) && buf_reuse.ga_len > 0)
2228 {
2229 // Recycle a previously used buffer number. Used for buffers which
2230 // are normally hidden, e.g. in a popup window. Avoids that the
2231 // buffer number grows rapidly.
2232 --buf_reuse.ga_len;
2233 buf->b_fnum = ((int *)buf_reuse.ga_data)[buf_reuse.ga_len];
Bram Moolenaar99ebf222019-12-10 23:44:48 +01002234
2235 // Move buffer to the right place in the buffer list.
2236 while (buf->b_prev != NULL && buf->b_fnum < buf->b_prev->b_fnum)
2237 {
2238 buf_T *prev = buf->b_prev;
2239
2240 prev->b_next = buf->b_next;
2241 if (prev->b_next != NULL)
2242 prev->b_next->b_prev = prev;
2243 buf->b_next = prev;
2244 buf->b_prev = prev->b_prev;
2245 if (buf->b_prev != NULL)
2246 buf->b_prev->b_next = buf;
2247 prev->b_prev = buf;
2248 if (lastbuf == buf)
2249 lastbuf = prev;
2250 if (firstbuf == prev)
2251 firstbuf = buf;
2252 }
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002253 }
2254 else
2255 buf->b_fnum = top_file_num++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002256 if (top_file_num < 0) // wrap around (may cause duplicates)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002258 emsg(_("W14: Warning: List of file names overflow"));
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002259 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 {
2261 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002262 ui_delay(3001L, TRUE); // make sure it is noticed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 }
2264 top_file_num = 1;
2265 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002266 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002268 // Always copy the options from the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 buf_copy_options(buf, BCO_ALWAYS);
2270 }
2271
2272 buf->b_wininfo->wi_fpos.lnum = lnum;
2273 buf->b_wininfo->wi_win = curwin;
2274
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002275#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002276 hash_init(&buf->b_s.b_keywtab);
2277 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278#endif
2279
2280 buf->b_fname = buf->b_sfname;
2281#ifdef UNIX
2282 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002283 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 else
2285 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002286 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 buf->b_dev = st.st_dev;
2288 buf->b_ino = st.st_ino;
2289 }
2290#endif
2291 buf->b_u_synced = TRUE;
2292 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002293 if (flags & BLN_DUMMY)
2294 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 buf_clear_file(buf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002296 clrallmarks(buf); // clear marks
2297 fmarks_check_names(buf); // check file marks for this file
2298 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; // init 'buflisted'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 if (!(flags & BLN_DUMMY))
2300 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002301 bufref_T bufref;
2302
Bram Moolenaarc667da52019-11-30 20:52:27 +01002303 // Tricky: these autocommands may change the buffer list. They could
2304 // also split the window with re-using the one empty buffer. This may
2305 // result in unexpectedly losing the empty buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002306 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002307 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002308 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002309 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002311 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002312 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002313 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002314 return NULL;
2315 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002316#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002317 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321
2322 return buf;
2323}
2324
2325/*
2326 * Free the memory for the options of a buffer.
2327 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2328 * 'fileencoding'.
2329 */
2330 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002331free_buf_options(
2332 buf_T *buf,
2333 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334{
2335 if (free_p_ff)
2336 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 clear_string_option(&buf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 clear_string_option(&buf->b_p_bh);
2340 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 }
2342#ifdef FEAT_FIND_ID
2343 clear_string_option(&buf->b_p_def);
2344 clear_string_option(&buf->b_p_inc);
2345# ifdef FEAT_EVAL
2346 clear_string_option(&buf->b_p_inex);
2347# endif
2348#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002349#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 clear_string_option(&buf->b_p_inde);
2351 clear_string_option(&buf->b_p_indk);
2352#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002353#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2354 clear_string_option(&buf->b_p_bexpr);
2355#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002356#if defined(FEAT_CRYPT)
2357 clear_string_option(&buf->b_p_cm);
2358#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002359 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002360#if defined(FEAT_EVAL)
2361 clear_string_option(&buf->b_p_fex);
2362#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363#ifdef FEAT_CRYPT
Bram Moolenaar131530a2021-07-29 20:37:49 +02002364# ifdef FEAT_SODIUM
K.Takata1a8825d2022-01-19 13:32:57 +00002365 if ((buf->b_p_key != NULL) && (*buf->b_p_key != NUL) &&
2366 (crypt_get_method_nr(buf) == CRYPT_M_SOD))
2367 crypt_sodium_munlock(buf->b_p_key, STRLEN(buf->b_p_key));
Bram Moolenaar131530a2021-07-29 20:37:49 +02002368# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 clear_string_option(&buf->b_p_key);
2370#endif
2371 clear_string_option(&buf->b_p_kp);
2372 clear_string_option(&buf->b_p_mps);
2373 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002374 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 clear_string_option(&buf->b_p_isk);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002376#ifdef FEAT_VARTABS
2377 clear_string_option(&buf->b_p_vsts);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00002378 VIM_CLEAR(buf->b_p_vsts_nopaste);
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00002379 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002380 clear_string_option(&buf->b_p_vts);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002381 VIM_CLEAR(buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383#ifdef FEAT_KEYMAP
2384 clear_string_option(&buf->b_p_keymap);
Bram Moolenaar50138322018-01-28 17:05:16 +01002385 keymap_clear(&buf->b_kmap_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 ga_clear(&buf->b_kmap_ga);
2387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 clear_string_option(&buf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389#ifdef FEAT_FOLDING
2390 clear_string_option(&buf->b_p_cms);
2391#endif
2392 clear_string_option(&buf->b_p_nf);
2393#ifdef FEAT_SYN_HL
2394 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002395 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002396#endif
2397#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002398 clear_string_option(&buf->b_s.b_p_spc);
2399 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002400 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002401 buf->b_s.b_cap_prog = NULL;
2402 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02002403 clear_string_option(&buf->b_s.b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 clear_string_option(&buf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 clear_string_option(&buf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 clear_string_option(&buf->b_p_cink);
2408 clear_string_option(&buf->b_p_cino);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002409 clear_string_option(&buf->b_p_lop);
Tom Praschan3506cf32022-04-07 12:39:08 +01002410 clear_string_option(&buf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 clear_string_option(&buf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 clear_string_option(&buf->b_p_cpt);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002413#ifdef FEAT_COMPL_FUNC
2414 clear_string_option(&buf->b_p_cfu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002415 free_callback(&buf->b_cfu_cb);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002416 clear_string_option(&buf->b_p_ofu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002417 free_callback(&buf->b_ofu_cb);
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +01002418 clear_string_option(&buf->b_p_tsrfu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002419 free_callback(&buf->b_tsrfu_cb);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421#ifdef FEAT_QUICKFIX
2422 clear_string_option(&buf->b_p_gp);
2423 clear_string_option(&buf->b_p_mp);
2424 clear_string_option(&buf->b_p_efm);
2425#endif
2426 clear_string_option(&buf->b_p_ep);
2427 clear_string_option(&buf->b_p_path);
2428 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002429 clear_string_option(&buf->b_p_tc);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002430#ifdef FEAT_EVAL
2431 clear_string_option(&buf->b_p_tfu);
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +00002432 free_callback(&buf->b_tfu_cb);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002433#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 clear_string_option(&buf->b_p_dict);
2435 clear_string_option(&buf->b_p_tsr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002436 clear_string_option(&buf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002438 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002439 clear_string_option(&buf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002440 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002441 clear_string_option(&buf->b_p_menc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442}
2443
2444/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002445 * Get alternate file "n".
2446 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2447 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 * if (options & GETF_SETMARK) call setpcmark()
2449 * if (options & GETF_ALT) we are jumping to an alternate file.
2450 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2451 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002452 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 */
2454 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002455buflist_getfile(
2456 int n,
2457 linenr_T lnum,
2458 int options,
2459 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460{
2461 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 pos_T *fpos;
2464 colnr_T col;
2465
2466 buf = buflist_findnr(n);
2467 if (buf == NULL)
2468 {
2469 if ((options & GETF_ALT) && n == 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02002470 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 else
Bram Moolenaare1242042021-12-16 20:56:57 +00002472 semsg(_(e_buffer_nr_not_found), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 return FAIL;
2474 }
2475
Bram Moolenaarc667da52019-11-30 20:52:27 +01002476 // if alternate file is the current buffer, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 if (buf == curbuf)
2478 return OK;
2479
Bram Moolenaar71223e22022-05-30 15:23:09 +01002480 if (text_or_buf_locked())
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002481 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482
Bram Moolenaarc667da52019-11-30 20:52:27 +01002483 // altfpos may be changed by getfile(), get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 if (lnum == 0)
2485 {
2486 fpos = buflist_findfpos(buf);
2487 lnum = fpos->lnum;
2488 col = fpos->col;
2489 }
2490 else
2491 col = 0;
2492
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 if (options & GETF_SWITCH)
2494 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002495 // If 'switchbuf' contains "useopen": jump to first window containing
2496 // "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00002497 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002499
Bram Moolenaarc667da52019-11-30 20:52:27 +01002500 // If 'switchbuf' contains "usetab": jump to first window in any tab
2501 // page containing "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00002502 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002503 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002504
Bram Moolenaarc667da52019-11-30 20:52:27 +01002505 // If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2506 // current buffer isn't empty: open new tab or window
Bram Moolenaara594d772015-06-19 14:41:49 +02002507 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002508 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002510 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002511 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002512 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2513 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002515 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 }
2517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518
2519 ++RedrawingDisabled;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002520 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2521 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 {
2523 --RedrawingDisabled;
2524
Bram Moolenaarc667da52019-11-30 20:52:27 +01002525 // cursor is at to BOL and w_cursor.lnum is checked due to getfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 if (!p_sol && col != 0)
2527 {
2528 curwin->w_cursor.col = col;
2529 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 curwin->w_set_curswant = TRUE;
2532 }
2533 return OK;
2534 }
2535 --RedrawingDisabled;
2536 return FAIL;
2537}
2538
2539/*
2540 * go to the last know line number for the current buffer
2541 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002543buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544{
2545 pos_T *fpos;
2546
2547 fpos = buflist_findfpos(curbuf);
2548
2549 curwin->w_cursor.lnum = fpos->lnum;
2550 check_cursor_lnum();
2551
2552 if (p_sol)
2553 curwin->w_cursor.col = 0;
2554 else
2555 {
2556 curwin->w_cursor.col = fpos->col;
2557 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 curwin->w_set_curswant = TRUE;
2560 }
2561}
2562
Martin Tournoijba43e762022-10-13 22:12:15 +01002563#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar81695252004-12-29 20:58:21 +00002564/*
2565 * Find file in buffer list by name (it has to be for the current window).
2566 * Returns NULL if not found.
2567 */
2568 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002569buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002570{
2571 char_u *ffname;
2572 buf_T *buf = NULL;
2573
Bram Moolenaarc667da52019-11-30 20:52:27 +01002574 // First make the name into a full path name
Bram Moolenaar81695252004-12-29 20:58:21 +00002575 ffname = FullName_save(fname,
2576#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002577 TRUE // force expansion, get rid of symbolic links
Bram Moolenaar81695252004-12-29 20:58:21 +00002578#else
2579 FALSE
2580#endif
2581 );
2582 if (ffname != NULL)
2583 {
2584 buf = buflist_findname(ffname);
2585 vim_free(ffname);
2586 }
2587 return buf;
2588}
2589#endif
2590
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591/*
2592 * Find file in buffer list by name (it has to be for the current window).
2593 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002594 * Skips dummy buffers.
2595 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 */
2597 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002598buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599{
2600#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002601 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602
2603 if (mch_stat((char *)ffname, &st) < 0)
2604 st.st_dev = (dev_T)-1;
2605 return buflist_findname_stat(ffname, &st);
2606}
2607
2608/*
2609 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2610 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002611 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 */
2613 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002614buflist_findname_stat(
2615 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002616 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617{
2618#endif
2619 buf_T *buf;
2620
Bram Moolenaarc667da52019-11-30 20:52:27 +01002621 // Start at the last buffer, expect to find a match sooner.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002622 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaar81695252004-12-29 20:58:21 +00002623 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624#ifdef UNIX
2625 , stp
2626#endif
2627 ))
2628 return buf;
2629 return NULL;
2630}
2631
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632/*
2633 * Find file in buffer list by a regexp pattern.
2634 * Return fnum of the found buffer.
2635 * Return < 0 for error.
2636 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002638buflist_findpat(
2639 char_u *pattern,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002640 char_u *pattern_end, // pointer to first char after pattern
2641 int unlisted, // find unlisted buffers
2642 int diffmode UNUSED, // find diff-mode buffers only
2643 int curtab_only) // find buffers in current tab only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644{
2645 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 int match = -1;
2647 int find_listed;
2648 char_u *pat;
2649 char_u *patend;
2650 int attempt;
2651 char_u *p;
2652 int toggledollar;
2653
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002654 // "%" is current file, "%%" or "#" is alternate file
2655 if ((pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2656 || (in_vim9script() && pattern_end == pattern + 2
2657 && pattern[0] == '%' && pattern[1] == '%'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 {
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002659 if (*pattern == '#' || pattern_end == pattern + 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 match = curwin->w_alt_fnum;
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002661 else
2662 match = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663#ifdef FEAT_DIFF
2664 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2665 match = -1;
2666#endif
2667 }
2668
2669 /*
2670 * Try four ways of matching a listed buffer:
2671 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002672 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 * attempt == 2: with '$' at end (only match at end)
2674 * attempt == 3: with '^' at start and '$' at end (only full match)
2675 * Repeat this for finding an unlisted buffer if there was no matching
2676 * listed buffer.
2677 */
2678 else
2679 {
2680 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2681 if (pat == NULL)
2682 return -1;
2683 patend = pat + STRLEN(pat) - 1;
2684 toggledollar = (patend > pat && *patend == '$');
2685
Bram Moolenaarc667da52019-11-30 20:52:27 +01002686 // First try finding a listed buffer. If not found and "unlisted"
2687 // is TRUE, try finding an unlisted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 find_listed = TRUE;
2689 for (;;)
2690 {
2691 for (attempt = 0; attempt <= 3; ++attempt)
2692 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002693 regmatch_T regmatch;
2694
Bram Moolenaarc667da52019-11-30 20:52:27 +01002695 // may add '^' and '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 if (toggledollar)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002697 *patend = (attempt < 2) ? NUL : '$'; // add/remove '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 p = pat;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002699 if (*p == '^' && !(attempt & 1)) // add/remove '^'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 ++p;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01002701 regmatch.regprog = vim_regcomp(p, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002703 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002704 {
2705 if (regmatch.regprog == NULL)
2706 {
2707 // invalid pattern, possibly after switching engine
2708 vim_free(pat);
2709 return -1;
2710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 if (buf->b_p_bl == find_listed
2712#ifdef FEAT_DIFF
2713 && (!diffmode || diff_mode_buf(buf))
2714#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002715 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002717 if (curtab_only)
2718 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002719 // Ignore the match if the buffer is not open in
2720 // the current tab.
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002721 win_T *wp;
2722
Bram Moolenaar29323592016-07-24 22:04:11 +02002723 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002724 if (wp->w_buffer == buf)
2725 break;
2726 if (wp == NULL)
2727 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002728 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002729 if (match >= 0) // already found a match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 {
2731 match = -2;
2732 break;
2733 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002734 match = buf->b_fnum; // remember first match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 }
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002738 vim_regfree(regmatch.regprog);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002739 if (match >= 0) // found one match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 break;
2741 }
2742
Bram Moolenaarc667da52019-11-30 20:52:27 +01002743 // Only search for unlisted buffers if there was no match with
2744 // a listed buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 if (!unlisted || !find_listed || match != -1)
2746 break;
2747 find_listed = FALSE;
2748 }
2749
2750 vim_free(pat);
2751 }
2752
2753 if (match == -2)
Bram Moolenaare1242042021-12-16 20:56:57 +00002754 semsg(_(e_more_than_one_match_for_str), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 else if (match < 0)
Bram Moolenaare1242042021-12-16 20:56:57 +00002756 semsg(_(e_no_matching_buffer_for_str), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 return match;
2758}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759
Bram Moolenaar52410572019-10-27 05:12:45 +01002760#ifdef FEAT_VIMINFO
2761typedef struct {
2762 buf_T *buf;
2763 char_u *match;
2764} bufmatch_T;
2765#endif
2766
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767/*
2768 * Find all buffer names that match.
2769 * For command line expansion of ":buf" and ":sbuf".
2770 * Return OK if matches found, FAIL otherwise.
2771 */
2772 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002773ExpandBufnames(
2774 char_u *pat,
2775 int *num_file,
2776 char_u ***file,
2777 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778{
2779 int count = 0;
2780 buf_T *buf;
2781 int round;
2782 char_u *p;
2783 int attempt;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002784 char_u *patc = NULL;
Bram Moolenaar52410572019-10-27 05:12:45 +01002785#ifdef FEAT_VIMINFO
2786 bufmatch_T *matches = NULL;
2787#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002788 int fuzzy;
2789 fuzmatch_str_T *fuzmatch = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790
Bram Moolenaarc667da52019-11-30 20:52:27 +01002791 *num_file = 0; // return values in case of FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 *file = NULL;
2793
Bram Moolenaarefcc3292019-12-30 21:59:03 +01002794#ifdef FEAT_DIFF
2795 if ((options & BUF_DIFF_FILTER) && !curwin->w_p_diff)
2796 return FAIL;
2797#endif
2798
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002799 fuzzy = cmdline_fuzzy_complete(pat);
2800
2801 // Make a copy of "pat" and change "^" to "\(^\|[\/]\)" (if doing regular
2802 // expression matching)
2803 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002805 if (*pat == '^')
2806 {
2807 patc = alloc(STRLEN(pat) + 11);
2808 if (patc == NULL)
2809 return FAIL;
2810 STRCPY(patc, "\\(^\\|[\\/]\\)");
2811 STRCPY(patc + 11, pat + 1);
2812 }
2813 else
2814 patc = pat;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002815 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00002816
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002817 // attempt == 0: try match with '\<', match at start of word
2818 // attempt == 1: try match without '\<', match anywhere
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002819 for (attempt = 0; attempt <= (fuzzy ? 0 : 1); ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002820 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002821 regmatch_T regmatch;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002822 int score = 0;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002823
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002824 if (!fuzzy)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002825 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002826 if (attempt > 0 && patc == pat)
2827 break; // there was no anchor, no need to try again
2828 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002831 // round == 1: Count the matches.
2832 // round == 2: Build the array to keep the matches.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 for (round = 1; round <= 2; ++round)
2834 {
2835 count = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002836 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002838 if (!buf->b_p_bl) // skip unlisted buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 continue;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002840#ifdef FEAT_DIFF
2841 if (options & BUF_DIFF_FILTER)
2842 // Skip buffers not suitable for
2843 // :diffget or :diffput completion.
Bram Moolenaarefcc3292019-12-30 21:59:03 +01002844 if (buf == curbuf || !diff_mode_buf(buf))
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002845 continue;
2846#endif
2847
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002848 if (!fuzzy)
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002849 {
2850 if (regmatch.regprog == NULL)
2851 {
2852 // invalid pattern, possibly after recompiling
2853 if (patc != pat)
2854 vim_free(patc);
2855 return FAIL;
2856 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002857 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002858 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002859 else
2860 {
2861 p = NULL;
2862 // first try matching with the short file name
2863 if ((score = fuzzy_match_str(buf->b_sfname, pat)) != 0)
2864 p = buf->b_sfname;
2865 if (p == NULL)
2866 {
2867 // next try matching with the full path file name
2868 if ((score = fuzzy_match_str(buf->b_ffname, pat)) != 0)
2869 p = buf->b_ffname;
2870 }
2871 }
2872
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002873 if (p == NULL)
2874 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002875
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002876 if (round == 1)
2877 {
2878 ++count;
2879 continue;
2880 }
2881
2882 if (options & WILD_HOME_REPLACE)
2883 p = home_replace_save(buf, p);
2884 else
2885 p = vim_strsave(p);
2886
2887 if (!fuzzy)
2888 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002889#ifdef FEAT_VIMINFO
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002890 if (matches != NULL)
2891 {
2892 matches[count].buf = buf;
2893 matches[count].match = p;
2894 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002896 else
2897#endif
2898 (*file)[count++] = p;
2899 }
2900 else
2901 {
2902 fuzmatch[count].idx = count;
2903 fuzmatch[count].str = p;
2904 fuzmatch[count].score = score;
2905 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 }
2907 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002908 if (count == 0) // no match found, break here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 break;
2910 if (round == 1)
2911 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002912 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002914 *file = ALLOC_MULT(char_u *, count);
2915 if (*file == NULL)
2916 {
2917 vim_regfree(regmatch.regprog);
2918 if (patc != pat)
2919 vim_free(patc);
2920 return FAIL;
2921 }
Bram Moolenaar52410572019-10-27 05:12:45 +01002922#ifdef FEAT_VIMINFO
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002923 if (options & WILD_BUFLASTUSED)
2924 matches = ALLOC_MULT(bufmatch_T, count);
Bram Moolenaar52410572019-10-27 05:12:45 +01002925#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002926 }
2927 else
2928 {
2929 fuzmatch = ALLOC_MULT(fuzmatch_str_T, count);
2930 if (fuzmatch == NULL)
2931 {
2932 *num_file = 0;
2933 *file = NULL;
2934 return FAIL;
2935 }
2936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 }
2938 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002939
2940 if (!fuzzy)
2941 {
2942 vim_regfree(regmatch.regprog);
2943 if (count) // match(es) found, break here
2944 break;
2945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 }
2947
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002948 if (!fuzzy && patc != pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002949 vim_free(patc);
2950
Bram Moolenaar52410572019-10-27 05:12:45 +01002951#ifdef FEAT_VIMINFO
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002952 if (!fuzzy)
Bram Moolenaar52410572019-10-27 05:12:45 +01002953 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002954 if (matches != NULL)
Bram Moolenaar52410572019-10-27 05:12:45 +01002955 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002956 int i;
2957 if (count > 1)
2958 qsort(matches, count, sizeof(bufmatch_T), buf_compare);
2959 // if the current buffer is first in the list, place it at the end
2960 if (matches[0].buf == curbuf)
2961 {
2962 for (i = 1; i < count; i++)
2963 (*file)[i-1] = matches[i].match;
2964 (*file)[count-1] = matches[0].match;
2965 }
2966 else
2967 {
2968 for (i = 0; i < count; i++)
2969 (*file)[i] = matches[i].match;
2970 }
2971 vim_free(matches);
Bram Moolenaar52410572019-10-27 05:12:45 +01002972 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002973 }
2974 else
2975 {
2976 if (fuzzymatches_to_strmatches(fuzmatch, file, count, FALSE) == FAIL)
2977 return FAIL;
Bram Moolenaar52410572019-10-27 05:12:45 +01002978 }
2979#endif
2980
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 *num_file = count;
2982 return (count == 0 ? FAIL : OK);
2983}
2984
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985/*
2986 * Check for a match on the file name for buffer "buf" with regprog "prog".
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002987 * Note that rmp->regprog may become NULL when switching regexp engine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 */
2989 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002990buflist_match(
2991 regmatch_T *rmp,
2992 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002993 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994{
2995 char_u *match;
2996
Bram Moolenaarc667da52019-11-30 20:52:27 +01002997 // First try the short file name, then the long file name.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002998 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaara59f2df2022-05-11 11:42:28 +01002999 if (match == NULL && rmp->regprog != NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01003000 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001
3002 return match;
3003}
3004
3005/*
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003006 * Try matching the regexp in "rmp->regprog" with file name "name".
3007 * Note that rmp->regprog may become NULL when switching regexp engine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 * Return "name" when there is a match, NULL when not.
3009 */
3010 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003011fname_match(
3012 regmatch_T *rmp,
3013 char_u *name,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003014 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015{
3016 char_u *match = NULL;
3017 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003019 // extra check for valid arguments
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003020 if (name == NULL || rmp->regprog == NULL)
3021 return NULL;
3022
3023 // Ignore case when 'fileignorecase' or the argument is set.
3024 rmp->rm_ic = p_fic || ignore_case;
3025 if (vim_regexec(rmp, name, (colnr_T)0))
3026 match = name;
3027 else if (rmp->regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003029 // Replace $(HOME) with '~' and try matching again.
3030 p = home_replace_save(NULL, name);
3031 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 match = name;
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003033 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 }
3035
3036 return match;
3037}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038
3039/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02003040 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 */
3042 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003043buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044{
Bram Moolenaar480778b2016-07-14 22:09:39 +02003045 char_u key[VIM_SIZEOF_INT * 2 + 1];
3046 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047
3048 if (nr == 0)
3049 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02003050 sprintf((char *)key, "%x", nr);
3051 hi = hash_find(&buf_hashtab, key);
3052
3053 if (!HASHITEM_EMPTY(hi))
3054 return (buf_T *)(hi->hi_key
3055 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 return NULL;
3057}
3058
3059/*
3060 * Get name of file 'n' in the buffer list.
3061 * When the file has no name an empty string is returned.
3062 * home_replace() is used to shorten the file name (used for marks).
3063 * Returns a pointer to allocated memory, of NULL when failed.
3064 */
3065 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003066buflist_nr2name(
3067 int n,
3068 int fullname,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003069 int helptail) // for help buffers return tail only
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070{
3071 buf_T *buf;
3072
3073 buf = buflist_findnr(n);
3074 if (buf == NULL)
3075 return NULL;
3076 return home_replace_save(helptail ? buf : NULL,
3077 fullname ? buf->b_ffname : buf->b_fname);
3078}
3079
3080/*
3081 * Set the "lnum" and "col" for the buffer "buf" and the current window.
3082 * When "copy_options" is TRUE save the local window option values.
3083 * When "lnum" is 0 only do the options.
3084 */
Bram Moolenaardefa0672019-07-21 19:25:37 +02003085 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003086buflist_setfpos(
3087 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003088 win_T *win, // may be NULL when using :badd
Bram Moolenaar7454a062016-01-30 15:14:10 +01003089 linenr_T lnum,
3090 colnr_T col,
3091 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092{
3093 wininfo_T *wip;
3094
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003095 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 if (wip->wi_win == win)
3097 break;
3098 if (wip == NULL)
3099 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003100 // allocate a new entry
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003101 wip = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 if (wip == NULL)
3103 return;
3104 wip->wi_win = win;
Bram Moolenaarc667da52019-11-30 20:52:27 +01003105 if (lnum == 0) // set lnum even when it's 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 lnum = 1;
3107 }
3108 else
3109 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003110 // remove the entry from the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 if (wip->wi_prev)
3112 wip->wi_prev->wi_next = wip->wi_next;
3113 else
3114 buf->b_wininfo = wip->wi_next;
3115 if (wip->wi_next)
3116 wip->wi_next->wi_prev = wip->wi_prev;
3117 if (copy_options && wip->wi_optset)
3118 {
3119 clear_winopt(&wip->wi_opt);
3120#ifdef FEAT_FOLDING
3121 deleteFoldRecurse(&wip->wi_folds);
3122#endif
3123 }
3124 }
3125 if (lnum != 0)
3126 {
3127 wip->wi_fpos.lnum = lnum;
3128 wip->wi_fpos.col = col;
3129 }
LemonBoydb0ea7f2022-04-10 17:59:26 +01003130 if (win != NULL)
3131 wip->wi_changelistidx = win->w_changelistidx;
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003132 if (copy_options && win != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003134 // Save the window-specific option values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
3136#ifdef FEAT_FOLDING
3137 wip->wi_fold_manual = win->w_fold_manual;
3138 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
3139#endif
3140 wip->wi_optset = TRUE;
3141 }
3142
Bram Moolenaarc667da52019-11-30 20:52:27 +01003143 // insert the entry in front of the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 wip->wi_next = buf->b_wininfo;
3145 buf->b_wininfo = wip;
3146 wip->wi_prev = NULL;
3147 if (wip->wi_next)
3148 wip->wi_next->wi_prev = wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149}
3150
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003151#ifdef FEAT_DIFF
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003152/*
3153 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
3154 * page. That's because a diff is local to a tab page.
3155 */
3156 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003157wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003158{
3159 win_T *wp;
3160
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003161 if (!wip->wi_opt.wo_diff)
3162 return FALSE;
3163
3164 FOR_ALL_WINDOWS(wp)
3165 // return FALSE when it's a window in the current tab page, thus
3166 // the buffer was in diff mode here
3167 if (wip->wi_win == wp)
3168 return FALSE;
3169 return TRUE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003170}
3171#endif
3172
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173/*
3174 * Find info for the current window in buffer "buf".
3175 * If not found, return the info for the most recently used window.
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003176 * When "need_options" is TRUE skip entries where wi_optset is FALSE.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003177 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
3178 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 * Returns NULL when there isn't any info.
3180 */
3181 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003182find_wininfo(
3183 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003184 int need_options,
Bram Moolenaar7454a062016-01-30 15:14:10 +01003185 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186{
3187 wininfo_T *wip;
3188
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003189 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003190 if (wip->wi_win == curwin
3191#ifdef FEAT_DIFF
3192 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
3193#endif
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003194
3195 && (!need_options || wip->wi_optset))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003197
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003198 if (wip != NULL)
3199 return wip;
3200
Bram Moolenaarc667da52019-11-30 20:52:27 +01003201 // If no wininfo for curwin, use the first in the list (that doesn't have
3202 // 'diff' set and is in another tab page).
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003203 // If "need_options" is TRUE skip entries that don't have options set,
3204 // unless the window is editing "buf", so we can copy from the window
3205 // itself.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003206#ifdef FEAT_DIFF
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003207 if (skip_diff_buffer)
3208 {
3209 FOR_ALL_BUF_WININFO(buf, wip)
3210 if (!wininfo_other_tab_diff(wip)
3211 && (!need_options || wip->wi_optset
3212 || (wip->wi_win != NULL
3213 && wip->wi_win->w_buffer == buf)))
3214 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003215 }
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003216 else
3217#endif
3218 wip = buf->b_wininfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 return wip;
3220}
3221
3222/*
3223 * Reset the local window options to the values last used in this window.
3224 * If the buffer wasn't used in this window before, use the values from
3225 * the most recently used window. If the values were never set, use the
3226 * global values for the window.
3227 */
3228 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003229get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230{
3231 wininfo_T *wip;
3232
3233 clear_winopt(&curwin->w_onebuf_opt);
3234#ifdef FEAT_FOLDING
3235 clearFolding(curwin);
3236#endif
3237
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003238 wip = find_wininfo(buf, TRUE, TRUE);
Bram Moolenaar25782a72018-05-13 18:05:33 +02003239 if (wip != NULL && wip->wi_win != NULL
3240 && wip->wi_win != curwin && wip->wi_win->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003242 // The buffer is currently displayed in the window: use the actual
3243 // option values instead of the saved (possibly outdated) values.
Bram Moolenaar25782a72018-05-13 18:05:33 +02003244 win_T *wp = wip->wi_win;
3245
3246 copy_winopt(&wp->w_onebuf_opt, &curwin->w_onebuf_opt);
3247#ifdef FEAT_FOLDING
3248 curwin->w_fold_manual = wp->w_fold_manual;
3249 curwin->w_foldinvalid = TRUE;
3250 cloneFoldGrowArray(&wp->w_folds, &curwin->w_folds);
3251#endif
3252 }
3253 else if (wip != NULL && wip->wi_optset)
3254 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003255 // the buffer was displayed in the current window earlier
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
3257#ifdef FEAT_FOLDING
3258 curwin->w_fold_manual = wip->wi_fold_manual;
3259 curwin->w_foldinvalid = TRUE;
3260 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
3261#endif
3262 }
3263 else
3264 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
LemonBoydb0ea7f2022-04-10 17:59:26 +01003265 if (wip != NULL)
3266 curwin->w_changelistidx = wip->wi_changelistidx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267
3268#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +01003269 // Set 'foldlevel' to 'foldlevelstart' if it's not negative.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 if (p_fdls >= 0)
3271 curwin->w_p_fdl = p_fdls;
3272#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02003273 after_copy_winopt(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274}
3275
3276/*
3277 * Find the position (lnum and col) for the buffer 'buf' for the current
3278 * window.
3279 * Returns a pointer to no_position if no position is found.
3280 */
3281 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003282buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283{
3284 wininfo_T *wip;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003285 static pos_T no_position = {1, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003287 wip = find_wininfo(buf, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 if (wip != NULL)
3289 return &(wip->wi_fpos);
3290 else
3291 return &no_position;
3292}
3293
3294/*
3295 * Find the lnum for the buffer 'buf' for the current window.
3296 */
3297 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01003298buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299{
3300 return buflist_findfpos(buf)->lnum;
3301}
3302
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02003304 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003307buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308{
Bram Moolenaar52410572019-10-27 05:12:45 +01003309 buf_T *buf = firstbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 int len;
3311 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003312 int ro_char;
3313 int changed_char;
Bram Moolenaar0751f512018-03-29 16:37:16 +02003314#ifdef FEAT_TERMINAL
3315 int job_running;
3316 int job_none_open;
3317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318
Bram Moolenaar52410572019-10-27 05:12:45 +01003319#ifdef FEAT_VIMINFO
3320 garray_T buflist;
3321 buf_T **buflist_data = NULL, **p;
3322
3323 if (vim_strchr(eap->arg, 't'))
3324 {
3325 ga_init2(&buflist, sizeof(buf_T *), 50);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003326 FOR_ALL_BUFFERS(buf)
Bram Moolenaar52410572019-10-27 05:12:45 +01003327 {
3328 if (ga_grow(&buflist, 1) == OK)
3329 ((buf_T **)buflist.ga_data)[buflist.ga_len++] = buf;
3330 }
3331
3332 qsort(buflist.ga_data, (size_t)buflist.ga_len,
3333 sizeof(buf_T *), buf_compare);
3334
Bram Moolenaar3b991522019-11-06 23:26:20 +01003335 buflist_data = (buf_T **)buflist.ga_data;
3336 buf = *buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003337 }
Bram Moolenaar3b991522019-11-06 23:26:20 +01003338 p = buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003339
Bram Moolenaar3b991522019-11-06 23:26:20 +01003340 for (; buf != NULL && !got_int; buf = buflist_data != NULL
Bram Moolenaar52410572019-10-27 05:12:45 +01003341 ? (++p < buflist_data + buflist.ga_len ? *p : NULL)
3342 : buf->b_next)
3343#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
Bram Moolenaar52410572019-10-27 05:12:45 +01003345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 {
Bram Moolenaar0751f512018-03-29 16:37:16 +02003347#ifdef FEAT_TERMINAL
3348 job_running = term_job_running(buf->b_term);
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003349 job_none_open = term_none_open(buf->b_term);
Bram Moolenaar0751f512018-03-29 16:37:16 +02003350#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003351 // skip unlisted buffers, unless ! was used
Bram Moolenaard51cb702015-07-21 15:03:06 +02003352 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
3353 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
3354 || (vim_strchr(eap->arg, '+')
3355 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
3356 || (vim_strchr(eap->arg, 'a')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003357 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
Bram Moolenaard51cb702015-07-21 15:03:06 +02003358 || (vim_strchr(eap->arg, 'h')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003359 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3360#ifdef FEAT_TERMINAL
3361 || (vim_strchr(eap->arg, 'R')
3362 && (!job_running || (job_running && job_none_open)))
3363 || (vim_strchr(eap->arg, '?')
3364 && (!job_running || (job_running && !job_none_open)))
3365 || (vim_strchr(eap->arg, 'F')
3366 && (job_running || buf->b_term == NULL))
3367#endif
Bram Moolenaard51cb702015-07-21 15:03:06 +02003368 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3369 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3370 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3371 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3372 || (vim_strchr(eap->arg, '#')
3373 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003376 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 else
3378 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003379 if (message_filtered(NameBuff))
3380 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003382 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3383 : (bufIsChanged(buf) ? '+' : ' ');
3384#ifdef FEAT_TERMINAL
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003385 if (job_running)
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003386 {
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003387 if (job_none_open)
Bram Moolenaar4033c552017-09-16 20:54:51 +02003388 ro_char = '?';
3389 else
3390 ro_char = 'R';
Bram Moolenaarc667da52019-11-30 20:52:27 +01003391 changed_char = ' '; // bufIsChanged() returns TRUE to avoid
3392 // closing, but it's not actually changed.
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003393 }
3394 else if (buf->b_term != NULL)
3395 ro_char = 'F';
3396 else
3397#endif
3398 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3399
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003400 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00003401 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 buf->b_fnum,
3403 buf->b_p_bl ? ' ' : 'u',
3404 buf == curbuf ? '%' :
3405 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3406 buf->b_ml.ml_mfp == NULL ? ' ' :
3407 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003408 ro_char,
3409 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003410 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003411 if (len > IOSIZE - 20)
3412 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413
Bram Moolenaarc667da52019-11-30 20:52:27 +01003414 // put "line 999" in column 40 or after the file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 i = 40 - vim_strsize(IObuff);
3416 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 IObuff[len++] = ' ';
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003418 while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar52410572019-10-27 05:12:45 +01003419#ifdef FEAT_VIMINFO
3420 if (vim_strchr(eap->arg, 't') && buf->b_last_used)
3421 add_time(IObuff + len, (size_t)(IOSIZE - len), buf->b_last_used);
3422 else
3423#endif
3424 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3425 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003426 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 msg_outtrans(IObuff);
Bram Moolenaarc667da52019-11-30 20:52:27 +01003428 out_flush(); // output one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 ui_breakcheck();
3430 }
Bram Moolenaar52410572019-10-27 05:12:45 +01003431
3432#ifdef FEAT_VIMINFO
3433 if (buflist_data)
3434 ga_clear(&buflist);
3435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437
3438/*
3439 * Get file name and line number for file 'fnum'.
3440 * Used by DoOneCmd() for translating '%' and '#'.
3441 * Used by insert_reg() and cmdline_paste() for '#' register.
3442 * Return FAIL if not found, OK for success.
3443 */
3444 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003445buflist_name_nr(
3446 int fnum,
3447 char_u **fname,
3448 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449{
3450 buf_T *buf;
3451
3452 buf = buflist_findnr(fnum);
3453 if (buf == NULL || buf->b_fname == NULL)
3454 return FAIL;
3455
3456 *fname = buf->b_fname;
3457 *lnum = buflist_findlnum(buf);
3458
3459 return OK;
3460}
3461
3462/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003463 * Set the file name for "buf"' to "ffname_arg", short file name to
3464 * "sfname_arg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 * The file name with the full path is also remembered, for when :cd is used.
3466 * Returns FAIL for failure (file name already in use by other buffer)
3467 * OK otherwise.
3468 */
3469 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003470setfname(
3471 buf_T *buf,
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003472 char_u *ffname_arg,
3473 char_u *sfname_arg,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003474 int message) // give message when buffer already exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003476 char_u *ffname = ffname_arg;
3477 char_u *sfname = sfname_arg;
Bram Moolenaar81695252004-12-29 20:58:21 +00003478 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003480 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481#endif
3482
3483 if (ffname == NULL || *ffname == NUL)
3484 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003485 // Removing the name.
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003486 if (buf->b_sfname != buf->b_ffname)
3487 VIM_CLEAR(buf->b_sfname);
3488 else
3489 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003490 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491#ifdef UNIX
3492 st.st_dev = (dev_T)-1;
3493#endif
3494 }
3495 else
3496 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003497 fname_expand(buf, &ffname, &sfname); // will allocate ffname
3498 if (ffname == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 return FAIL;
3500
3501 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01003502 * If the file name is already used in another buffer:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 * - if the buffer is loaded, fail
3504 * - if the buffer is not loaded, delete it from the list
3505 */
3506#ifdef UNIX
3507 if (mch_stat((char *)ffname, &st) < 0)
3508 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003509#endif
3510 if (!(buf->b_flags & BF_DUMMY))
3511#ifdef UNIX
3512 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003514 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515#endif
3516 if (obuf != NULL && obuf != buf)
3517 {
Bram Moolenaard3710cf2021-10-04 23:13:13 +01003518 win_T *win;
3519 tabpage_T *tab;
3520 int in_use = FALSE;
3521
3522 // during startup a window may use a buffer that is not loaded yet
3523 FOR_ALL_TAB_WINDOWS(tab, win)
3524 if (win->w_buffer == obuf)
3525 in_use = TRUE;
3526
3527 // it's loaded or used in a window, fail
3528 if (obuf->b_ml.ml_mfp != NULL || in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 {
3530 if (message)
Bram Moolenaare1242042021-12-16 20:56:57 +00003531 emsg(_(e_buffer_with_this_name_already_exists));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 vim_free(ffname);
3533 return FAIL;
3534 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003535 // delete from the list
Bram Moolenaara6e8f882019-12-14 16:18:15 +01003536 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 }
3538 sfname = vim_strsave(sfname);
3539 if (ffname == NULL || sfname == NULL)
3540 {
3541 vim_free(sfname);
3542 vim_free(ffname);
3543 return FAIL;
3544 }
3545#ifdef USE_FNAME_CASE
Bram Moolenaarc667da52019-11-30 20:52:27 +01003546 fname_case(sfname, 0); // set correct case for short file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547#endif
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003548 if (buf->b_sfname != buf->b_ffname)
3549 vim_free(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 vim_free(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 buf->b_ffname = ffname;
3552 buf->b_sfname = sfname;
3553 }
3554 buf->b_fname = buf->b_sfname;
3555#ifdef UNIX
3556 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003557 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 else
3559 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003560 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 buf->b_dev = st.st_dev;
3562 buf->b_ino = st.st_ino;
3563 }
3564#endif
3565
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567
3568 buf_name_changed(buf);
3569 return OK;
3570}
3571
3572/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003573 * Crude way of changing the name of a buffer. Use with care!
3574 * The name should be relative to the current directory.
3575 */
3576 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003577buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003578{
3579 buf_T *buf;
3580
3581 buf = buflist_findnr(fnum);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00003582 if (buf == NULL)
3583 return;
3584
3585 if (buf->b_sfname != buf->b_ffname)
3586 vim_free(buf->b_sfname);
3587 vim_free(buf->b_ffname);
3588 buf->b_ffname = vim_strsave(name);
3589 buf->b_sfname = NULL;
3590 // Allocate ffname and expand into full path. Also resolves .lnk
3591 // files on Win32.
3592 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
3593 buf->b_fname = buf->b_sfname;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003594}
3595
3596/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 * Take care of what needs to be done when the name of buffer "buf" has
3598 * changed.
3599 */
3600 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003601buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602{
3603 /*
3604 * If the file name changed, also change the name of the swapfile
3605 */
3606 if (buf->b_ml.ml_mfp != NULL)
3607 ml_setname(buf);
3608
Bram Moolenaar3ad69532021-11-19 17:01:08 +00003609#ifdef FEAT_TERMINAL
3610 if (buf->b_term != NULL)
3611 term_clear_status_text(buf->b_term);
3612#endif
3613
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 if (curwin->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003615 check_arg_idx(curwin); // check file name for arg list
Bram Moolenaarc667da52019-11-30 20:52:27 +01003616 maketitle(); // set window title
Bram Moolenaarc667da52019-11-30 20:52:27 +01003617 status_redraw_all(); // status lines need to be redrawn
3618 fmarks_check_names(buf); // check named file marks
3619 ml_timestamp(buf); // reset timestamp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620}
3621
3622/*
3623 * set alternate file name for current window
3624 *
3625 * Used by do_one_cmd(), do_write() and do_ecmd().
3626 * Return the buffer.
3627 */
3628 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003629setaltfname(
3630 char_u *ffname,
3631 char_u *sfname,
3632 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633{
3634 buf_T *buf;
3635
Bram Moolenaarc667da52019-11-30 20:52:27 +01003636 // Create a buffer. 'buflisted' is not set if it's a new buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaare1004402020-10-24 20:49:43 +02003638 if (buf != NULL && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 curwin->w_alt_fnum = buf->b_fnum;
3640 return buf;
3641}
3642
3643/*
3644 * Get alternate file name for current window.
3645 * Return NULL if there isn't any, and give error message if requested.
3646 */
3647 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003648getaltfname(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003649 int errmsg) // give error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650{
3651 char_u *fname;
3652 linenr_T dummy;
3653
3654 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3655 {
3656 if (errmsg)
Bram Moolenaar108010a2021-06-27 22:03:33 +02003657 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 return NULL;
3659 }
3660 return fname;
3661}
3662
3663/*
3664 * Add a file name to the buflist and return its number.
3665 * Uses same flags as buflist_new(), except BLN_DUMMY.
3666 *
3667 * used by qf_init(), main() and doarglist()
3668 */
3669 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003670buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671{
3672 buf_T *buf;
3673
3674 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3675 if (buf != NULL)
3676 return buf->b_fnum;
3677 return 0;
3678}
3679
3680#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3681/*
3682 * Adjust slashes in file names. Called after 'shellslash' was set.
3683 */
3684 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003685buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686{
3687 buf_T *bp;
3688
Bram Moolenaar29323592016-07-24 22:04:11 +02003689 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 {
3691 if (bp->b_ffname != NULL)
3692 slash_adjust(bp->b_ffname);
3693 if (bp->b_sfname != NULL)
3694 slash_adjust(bp->b_sfname);
3695 }
3696}
3697#endif
3698
3699/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003700 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 * Also save the local window option values.
3702 */
3703 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003704buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003706 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707}
3708
3709/*
3710 * Return TRUE if 'ffname' is not the same file as current file.
3711 * Fname must have a full path (expanded by mch_FullName()).
3712 */
3713 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003714otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715{
3716 return otherfile_buf(curbuf, ffname
3717#ifdef UNIX
3718 , NULL
3719#endif
3720 );
3721}
3722
3723 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003724otherfile_buf(
3725 buf_T *buf,
3726 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003728 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003730 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731{
Bram Moolenaarc667da52019-11-30 20:52:27 +01003732 // no name is different
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3734 return TRUE;
3735 if (fnamecmp(ffname, buf->b_ffname) == 0)
3736 return FALSE;
3737#ifdef UNIX
3738 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003739 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740
Bram Moolenaarc667da52019-11-30 20:52:27 +01003741 // If no stat_T given, get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 if (stp == NULL)
3743 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003744 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 st.st_dev = (dev_T)-1;
3746 stp = &st;
3747 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003748 // Use dev/ino to check if the files are the same, even when the names
3749 // are different (possible with links). Still need to compare the
3750 // name above, for when the file doesn't exist yet.
3751 // Problem: The dev/ino changes when a file is deleted (and created
3752 // again) and remains the same when renamed/moved. We don't want to
3753 // mch_stat() each buffer each time, that would be too slow. Get the
3754 // dev/ino again when they appear to match, but not when they appear
3755 // to be different: Could skip a buffer when it's actually the same
3756 // file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 if (buf_same_ino(buf, stp))
3758 {
3759 buf_setino(buf);
3760 if (buf_same_ino(buf, stp))
3761 return FALSE;
3762 }
3763 }
3764#endif
3765 return TRUE;
3766}
3767
3768#if defined(UNIX) || defined(PROTO)
3769/*
3770 * Set inode and device number for a buffer.
3771 * Must always be called when b_fname is changed!.
3772 */
3773 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003774buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003776 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777
3778 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3779 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003780 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 buf->b_dev = st.st_dev;
3782 buf->b_ino = st.st_ino;
3783 }
3784 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003785 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786}
3787
3788/*
3789 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3790 */
3791 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003792buf_same_ino(
3793 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003794 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003796 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 && stp->st_dev == buf->b_dev
3798 && stp->st_ino == buf->b_ino);
3799}
3800#endif
3801
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003802/*
3803 * Print info about the current buffer.
3804 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003806fileinfo(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003807 int fullname, // when non-zero print full path
Bram Moolenaar7454a062016-01-30 15:14:10 +01003808 int shorthelp,
3809 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810{
3811 char_u *name;
3812 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003813 char *p;
3814 char *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003815 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003817 buffer = alloc(IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 if (buffer == NULL)
3819 return;
3820
Bram Moolenaarc667da52019-11-30 20:52:27 +01003821 if (fullname > 1) // 2 CTRL-G: include buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003823 vim_snprintf(buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 p = buffer + STRLEN(buffer);
3825 }
3826 else
3827 p = buffer;
3828
3829 *p++ = '"';
3830 if (buf_spname(curbuf) != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003831 vim_strncpy((char_u *)p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 else
3833 {
3834 if (!fullname && curbuf->b_fname != NULL)
3835 name = curbuf->b_fname;
3836 else
3837 name = curbuf->b_ffname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003838 home_replace(shorthelp ? curbuf : NULL, name, (char_u *)p,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 (int)(IOSIZE - (p - buffer)), TRUE);
3840 }
3841
Bram Moolenaar32526b32019-01-19 17:43:09 +01003842 vim_snprintf_add(buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 curbufIsChanged() ? (shortmess(SHM_MOD)
3844 ? " [+]" : _(" [Modified]")) : " ",
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01003845 (curbuf->b_flags & BF_NOTEDITED) && !bt_dontwrite(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 ? _("[Not edited]") : "",
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01003847 (curbuf->b_flags & BF_NEW) && !bt_dontwrite(curbuf)
Bram Moolenaar722e5052020-06-12 22:31:00 +02003848 ? new_file_message() : "",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003850 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 : _("[readonly]")) : "",
3852 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3853 || curbuf->b_p_ro) ?
3854 " " : "");
Bram Moolenaarc667da52019-11-30 20:52:27 +01003855 // With 32 bit longs and more than 21,474,836 lines multiplying by 100
3856 // causes an overflow, thus for large numbers divide instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 if (curwin->w_cursor.lnum > 1000000L)
3858 n = (int)(((long)curwin->w_cursor.lnum) /
3859 ((long)curbuf->b_ml.ml_line_count / 100L));
3860 else
3861 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3862 (long)curbuf->b_ml.ml_line_count);
3863 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003864 vim_snprintf_add(buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 else if (p_ru)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003866 // Current line and column are already on the screen -- webb
Bram Moolenaar32526b32019-01-19 17:43:09 +01003867 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003868 NGETTEXT("%ld line --%d%%--", "%ld lines --%d%%--",
3869 curbuf->b_ml.ml_line_count),
3870 (long)curbuf->b_ml.ml_line_count, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 else
3872 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003873 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 _("line %ld of %ld --%d%%-- col "),
3875 (long)curwin->w_cursor.lnum,
3876 (long)curbuf->b_ml.ml_line_count,
3877 n);
3878 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003879 len = STRLEN(buffer);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003880 col_print((char_u *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3882 }
3883
Bram Moolenaar32526b32019-01-19 17:43:09 +01003884 (void)append_arg_number(curwin, (char_u *)buffer, IOSIZE,
3885 !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886
3887 if (dont_truncate)
3888 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003889 // Temporarily set msg_scroll to avoid the message being truncated.
3890 // First call msg_start() to get the message in the right place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 msg_start();
3892 n = msg_scroll;
3893 msg_scroll = TRUE;
3894 msg(buffer);
3895 msg_scroll = n;
3896 }
3897 else
3898 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003899 p = msg_trunc_attr(buffer, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaarc667da52019-11-30 20:52:27 +01003901 // Need to repeat the message after redrawing when:
3902 // - When restart_edit is set (otherwise there will be a delay
3903 // before redrawing).
3904 // - When the screen was scrolled but there is no wait-return
3905 // prompt.
Bram Moolenaar32526b32019-01-19 17:43:09 +01003906 set_keep_msg((char_u *)p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 }
3908
3909 vim_free(buffer);
3910}
3911
3912 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003913col_print(
3914 char_u *buf,
3915 size_t buflen,
3916 int col,
3917 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918{
3919 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003920 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003922 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923}
3924
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925static char_u *lasttitle = NULL;
3926static char_u *lasticon = NULL;
3927
Bram Moolenaar84a93082018-06-16 22:58:15 +02003928/*
3929 * Put the file name in the title bar and icon of the window.
3930 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003932maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933{
3934 char_u *p;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003935 char_u *title_str = NULL;
3936 char_u *icon_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 int maxlen = 0;
3938 int len;
3939 int mustset;
3940 char_u buf[IOSIZE];
3941 int off;
3942
3943 if (!redrawing())
3944 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003945 // Postpone updating the title when 'lazyredraw' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 need_maketitle = TRUE;
3947 return;
3948 }
3949
3950 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003951 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003952 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953
3954 if (p_title)
3955 {
3956 if (p_titlelen > 0)
3957 {
3958 maxlen = p_titlelen * Columns / 100;
3959 if (maxlen < 10)
3960 maxlen = 10;
3961 }
3962
Bram Moolenaar84a93082018-06-16 22:58:15 +02003963 title_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 if (*p_titlestring != NUL)
3965 {
3966#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003967 if (stl_syntax & STL_IN_TITLE)
Luuk van Baal7b224fd2022-11-07 12:16:51 +00003968 build_stl_str_hl(curwin, title_str, sizeof(buf), p_titlestring,
3969 (char_u *)"titlestring", 0,
3970 0, maxlen, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 else
3972#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003973 title_str = p_titlestring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 }
3975 else
3976 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003977 // format: "fname + (path) (1 of 2) - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978
Bram Moolenaar2c666692012-09-05 13:30:40 +02003979#define SPACE_FOR_FNAME (IOSIZE - 100)
3980#define SPACE_FOR_DIR (IOSIZE - 20)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003981#define SPACE_FOR_ARGNR (IOSIZE - 10) // at least room for " - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003983 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar21554412017-07-24 21:44:43 +02003984#ifdef FEAT_TERMINAL
3985 else if (curbuf->b_term != NULL)
3986 {
3987 vim_strncpy(buf, term_get_status_text(curbuf->b_term),
3988 SPACE_FOR_FNAME);
3989 }
3990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 else
3992 {
3993 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003994 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 }
3997
Bram Moolenaar21554412017-07-24 21:44:43 +02003998#ifdef FEAT_TERMINAL
3999 if (curbuf->b_term == NULL)
4000#endif
4001 switch (bufIsChanged(curbuf)
4002 + (curbuf->b_p_ro * 2)
4003 + (!curbuf->b_p_ma * 4))
4004 {
4005 case 1: STRCAT(buf, " +"); break;
4006 case 2: STRCAT(buf, " ="); break;
4007 case 3: STRCAT(buf, " =+"); break;
4008 case 4:
4009 case 6: STRCAT(buf, " -"); break;
4010 case 5:
4011 case 7: STRCAT(buf, " -+"); break;
4012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013
Bram Moolenaar21554412017-07-24 21:44:43 +02004014 if (curbuf->b_fname != NULL
4015#ifdef FEAT_TERMINAL
4016 && curbuf->b_term == NULL
4017#endif
4018 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004020 // Get path of file, replace home dir with ~
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 off = (int)STRLEN(buf);
4022 buf[off++] = ' ';
4023 buf[off++] = '(';
4024 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02004025 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc667da52019-11-30 20:52:27 +01004027 // avoid "c:/name" to be reduced to "c"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 if (isalpha(buf[off]) && buf[off + 1] == ':')
4029 off += 2;
4030#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01004031 // remove the file name
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004032 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 if (p == buf + off)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02004034 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004035 // must be a help buffer
Bram Moolenaar21554412017-07-24 21:44:43 +02004036 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02004037 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02004038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004041
Bram Moolenaarc667da52019-11-30 20:52:27 +01004042 // Translate unprintable chars and concatenate. Keep some
4043 // room for the server name. When there is no room (very long
4044 // file name) use (...).
Bram Moolenaar2c666692012-09-05 13:30:40 +02004045 if (off < SPACE_FOR_DIR)
4046 {
4047 p = transstr(buf + off);
4048 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
4049 vim_free(p);
4050 }
4051 else
4052 {
4053 vim_strncpy(buf + off, (char_u *)"...",
4054 (size_t)(SPACE_FOR_ARGNR - off));
4055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 STRCAT(buf, ")");
4057 }
4058
Bram Moolenaar2c666692012-09-05 13:30:40 +02004059 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060
4061#if defined(FEAT_CLIENTSERVER)
4062 if (serverName != NULL)
4063 {
4064 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02004065 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 }
4067 else
4068#endif
4069 STRCAT(buf, " - VIM");
4070
4071 if (maxlen > 0)
4072 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004073 // make it shorter by removing a bit in the middle
Bram Moolenaarf31b7642012-01-20 20:44:43 +01004074 if (vim_strsize(buf) > maxlen)
4075 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 }
4077 }
4078 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02004079 mustset = value_changed(title_str, &lasttitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080
4081 if (p_icon)
4082 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02004083 icon_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 if (*p_iconstring != NUL)
4085 {
4086#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00004087 if (stl_syntax & STL_IN_ICON)
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004088 build_stl_str_hl(curwin, icon_str, sizeof(buf), p_iconstring,
4089 (char_u *)"iconstring", 0, 0, 0, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 else
4091#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02004092 icon_str = p_iconstring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 }
4094 else
4095 {
4096 if (buf_spname(curbuf) != NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004097 p = buf_spname(curbuf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004098 else // use file name only in icon
Bram Moolenaar84a93082018-06-16 22:58:15 +02004099 p = gettail(curbuf->b_ffname);
4100 *icon_str = NUL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004101 // Truncate name at 100 bytes.
Bram Moolenaar84a93082018-06-16 22:58:15 +02004102 len = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 if (len > 100)
4104 {
4105 len -= 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 if (has_mbyte)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004107 len += (*mb_tail_off)(p, p + len) + 1;
Bram Moolenaar84a93082018-06-16 22:58:15 +02004108 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02004110 STRCPY(icon_str, p);
4111 trans_characters(icon_str, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 }
4113 }
4114
Bram Moolenaar84a93082018-06-16 22:58:15 +02004115 mustset |= value_changed(icon_str, &lasticon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116
4117 if (mustset)
4118 resettitle();
4119}
4120
4121/*
4122 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
4123 * from "str" if it does.
Bram Moolenaar84a93082018-06-16 22:58:15 +02004124 * Return TRUE if resettitle() is to be called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 */
4126 static int
Bram Moolenaar84a93082018-06-16 22:58:15 +02004127value_changed(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128{
4129 if ((str == NULL) != (*last == NULL)
4130 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
4131 {
4132 vim_free(*last);
4133 if (str == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004134 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 *last = NULL;
Bram Moolenaar40385db2018-08-07 22:31:44 +02004136 mch_restore_title(
4137 last == &lasttitle ? SAVE_RESTORE_TITLE : SAVE_RESTORE_ICON);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 else
Bram Moolenaar84a93082018-06-16 22:58:15 +02004140 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 *last = vim_strsave(str);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004142 return TRUE;
4143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 }
4145 return FALSE;
4146}
4147
4148/*
4149 * Put current window title back (used after calling a shell)
4150 */
4151 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004152resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153{
4154 mch_settitle(lasttitle, lasticon);
4155}
Bram Moolenaarea408852005-06-25 22:49:46 +00004156
4157# if defined(EXITFREE) || defined(PROTO)
4158 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004159free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00004160{
4161 vim_free(lasttitle);
4162 vim_free(lasticon);
4163}
4164# endif
4165
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004167#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004168
4169/*
4170 * Used for building in the status line.
4171 */
4172typedef struct
4173{
4174 char_u *stl_start;
4175 int stl_minwid;
4176 int stl_maxwid;
4177 enum {
4178 Normal,
4179 Empty,
4180 Group,
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004181 Separate,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004182 Highlight,
4183 TabPage,
4184 Trunc
4185 } stl_type;
4186} stl_item_T;
4187
4188static size_t stl_items_len = 20; // Initial value, grows as needed.
4189static stl_item_T *stl_items = NULL;
4190static int *stl_groupitem = NULL;
4191static stl_hlrec_T *stl_hltab = NULL;
4192static stl_hlrec_T *stl_tabtab = NULL;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004193static int *stl_separator_locations = NULL;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004194
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004196 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 * Return length of string in screen cells.
4198 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004199 * Normally works for window "wp", except when working for 'tabline' then it
4200 * is "curwin".
4201 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 * Items are drawn interspersed with the text that surrounds it
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004203 * Specials: %-<wid>(xxx%) => group, %= => separation marker, %< => truncation
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
4205 *
4206 * If maxwidth is not zero, the string will be filled at any middle marker
4207 * or truncated if too long, fillchar is used for all whitespace.
4208 */
4209 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004210build_stl_str_hl(
4211 win_T *wp,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004212 char_u *out, // buffer to write into != NameBuff
4213 size_t outlen, // length of out[]
Bram Moolenaar7454a062016-01-30 15:14:10 +01004214 char_u *fmt,
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004215 char_u *opt_name, // option name corresponding to "fmt"
4216 int opt_scope, // scope for "opt_name"
Bram Moolenaar7454a062016-01-30 15:14:10 +01004217 int fillchar,
4218 int maxwidth,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004219 stl_hlrec_T **hltab, // return: HL attributes (can be NULL)
4220 stl_hlrec_T **tabtab) // return: tab page nrs (can be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221{
Bram Moolenaar10772302019-01-20 18:25:54 +01004222 linenr_T lnum;
4223 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 char_u *p;
4225 char_u *s;
4226 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004227 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228#ifdef FEAT_EVAL
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004229 int use_sandbox;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004230 win_T *save_curwin;
4231 buf_T *save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004232 int save_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233#endif
4234 int empty_line;
4235 colnr_T virtcol;
4236 long l;
4237 long n;
4238 int prevchar_isflag;
4239 int prevchar_isitem;
4240 int itemisflag;
4241 int fillable;
4242 char_u *str;
4243 long num;
4244 int width;
4245 int itemcnt;
4246 int curitem;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004247 int group_end_userhl;
4248 int group_start_userhl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 int groupdepth;
shadmansaleh30e3de22021-05-15 17:23:28 +02004250#ifdef FEAT_EVAL
4251 int evaldepth;
4252#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 int minwid;
4254 int maxwid;
4255 int zeropad;
4256 char_u base;
4257 char_u opt;
4258#define TMPLEN 70
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004259 char_u buf_tmp[TMPLEN];
4260 char_u win_tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004261 char_u *usefmt = fmt;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004262 stl_hlrec_T *sp;
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01004263 int save_redraw_not_allowed = redraw_not_allowed;
Bram Moolenaar0e1f36f2022-02-15 16:17:44 +00004264 int save_KeyTyped = KeyTyped;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004265 // TODO: find out why using called_emsg_before makes tests fail, does it
4266 // matter?
4267 // int called_emsg_before = called_emsg;
4268 int did_emsg_before = did_emsg;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004269
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01004270 // When inside update_screen() we do not want redrawing a statusline,
4271 // ruler, title, etc. to trigger another redraw, it may cause an endless
4272 // loop.
4273 if (updating_screen)
4274 redraw_not_allowed = TRUE;
4275
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004276 if (stl_items == NULL)
4277 {
4278 stl_items = ALLOC_MULT(stl_item_T, stl_items_len);
4279 stl_groupitem = ALLOC_MULT(int, stl_items_len);
Brandon Richardsona493b652022-02-19 11:45:03 +00004280
4281 // Allocate one more, because the last element is used to indicate the
4282 // end of the list.
4283 stl_hltab = ALLOC_MULT(stl_hlrec_T, stl_items_len + 1);
4284 stl_tabtab = ALLOC_MULT(stl_hlrec_T, stl_items_len + 1);
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004285
4286 stl_separator_locations = ALLOC_MULT(int, stl_items_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004287 }
4288
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004289#ifdef FEAT_EVAL
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004290 // if "fmt" was set insecurely it needs to be evaluated in the sandbox
4291 use_sandbox = was_set_insecurely(opt_name, opt_scope);
4292
4293 // When the format starts with "%!" then evaluate it as an expression and
4294 // use the result as the actual format string.
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004295 if (fmt[0] == '%' && fmt[1] == '!')
4296 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004297 typval_T tv;
4298
4299 tv.v_type = VAR_NUMBER;
4300 tv.vval.v_number = wp->w_id;
4301 set_var((char_u *)"g:statusline_winid", &tv, FALSE);
4302
Bram Moolenaara4e0b972022-10-01 19:43:52 +01004303 usefmt = eval_to_string_safe(fmt + 2, use_sandbox, FALSE, FALSE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004304 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00004305 usefmt = fmt;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004306
4307 do_unlet((char_u *)"g:statusline_winid", TRUE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004308 }
4309#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310
4311 if (fillchar == 0)
4312 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004313
Bram Moolenaar10772302019-01-20 18:25:54 +01004314 // The cursor in windows other than the current one isn't always
4315 // up-to-date, esp. because of autocommands and timers.
4316 lnum = wp->w_cursor.lnum;
4317 if (lnum > wp->w_buffer->b_ml.ml_line_count)
4318 {
4319 lnum = wp->w_buffer->b_ml.ml_line_count;
4320 wp->w_cursor.lnum = lnum;
4321 }
4322
4323 // Get line & check if empty (cursorpos will show "0-1"). Note that
4324 // p will become invalid when getting another buffer line.
4325 p = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar567199b2013-04-24 16:52:36 +02004326 empty_line = (*p == NUL);
4327
Bram Moolenaar10772302019-01-20 18:25:54 +01004328 // Get the byte value now, in case we need it below. This is more efficient
4329 // than making a copy of the line.
4330 len = STRLEN(p);
4331 if (wp->w_cursor.col > (colnr_T)len)
4332 {
4333 // Line may have changed since checking the cursor column, or the lnum
4334 // was adjusted above.
4335 wp->w_cursor.col = (colnr_T)len;
Bram Moolenaar10772302019-01-20 18:25:54 +01004336 wp->w_cursor.coladd = 0;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004337 byteval = 0;
Bram Moolenaar10772302019-01-20 18:25:54 +01004338 }
Bram Moolenaar567199b2013-04-24 16:52:36 +02004339 else
Bram Moolenaar567199b2013-04-24 16:52:36 +02004340 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341
4342 groupdepth = 0;
shadmansaleh30e3de22021-05-15 17:23:28 +02004343#ifdef FEAT_EVAL
4344 evaldepth = 0;
4345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 p = out;
4347 curitem = 0;
4348 prevchar_isflag = TRUE;
4349 prevchar_isitem = FALSE;
zeertzjq122dea72022-07-27 15:48:45 +01004350 for (s = usefmt; *s != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004352 if (curitem == (int)stl_items_len)
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004353 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004354 size_t new_len = stl_items_len * 3 / 2;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004355
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004356 stl_item_T *new_items =
4357 vim_realloc(stl_items, sizeof(stl_item_T) * new_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004358 if (new_items == NULL)
4359 break;
4360 stl_items = new_items;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004361
4362 int *new_groupitem =
4363 vim_realloc(stl_groupitem, sizeof(int) * new_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004364 if (new_groupitem == NULL)
4365 break;
4366 stl_groupitem = new_groupitem;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004367
4368 stl_hlrec_T *new_hlrec = vim_realloc(stl_hltab,
Brandon Richardsona493b652022-02-19 11:45:03 +00004369 sizeof(stl_hlrec_T) * (new_len + 1));
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004370 if (new_hlrec == NULL)
4371 break;
4372 stl_hltab = new_hlrec;
Brandon Richardsona493b652022-02-19 11:45:03 +00004373 new_hlrec = vim_realloc(stl_tabtab,
4374 sizeof(stl_hlrec_T) * (new_len + 1));
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004375 if (new_hlrec == NULL)
4376 break;
4377 stl_tabtab = new_hlrec;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004378
4379 int *new_separator_locs = vim_realloc(stl_separator_locations,
4380 sizeof(int) * new_len);
4381 if (new_separator_locs == NULL)
4382 break;
4383 stl_separator_locations = new_separator_locs;;
4384
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004385 stl_items_len = new_len;
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004386 }
4387
zeertzjq122dea72022-07-27 15:48:45 +01004388 if (*s != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 prevchar_isflag = prevchar_isitem = FALSE;
4390
4391 /*
4392 * Handle up to the next '%' or the end.
4393 */
4394 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
4395 *p++ = *s++;
4396 if (*s == NUL || p + 1 >= out + outlen)
4397 break;
4398
4399 /*
4400 * Handle one '%' item.
4401 */
4402 s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004403 if (*s == NUL) // ignore trailing %
Bram Moolenaar1d87f512011-02-01 21:55:01 +01004404 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 if (*s == '%')
4406 {
4407 if (p + 1 >= out + outlen)
4408 break;
4409 *p++ = *s++;
4410 prevchar_isflag = prevchar_isitem = FALSE;
4411 continue;
4412 }
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004413 // STL_SEPARATE: Separation between items, filled with white space.
4414 if (*s == STL_SEPARATE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 {
4416 s++;
4417 if (groupdepth > 0)
4418 continue;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004419 stl_items[curitem].stl_type = Separate;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004420 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 continue;
4422 }
4423 if (*s == STL_TRUNCMARK)
4424 {
4425 s++;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004426 stl_items[curitem].stl_type = Trunc;
4427 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 continue;
4429 }
4430 if (*s == ')')
4431 {
4432 s++;
4433 if (groupdepth < 1)
4434 continue;
4435 groupdepth--;
4436
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004437 t = stl_items[stl_groupitem[groupdepth]].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 *p = NUL;
4439 l = vim_strsize(t);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004440 if (curitem > stl_groupitem[groupdepth] + 1
4441 && stl_items[stl_groupitem[groupdepth]].stl_minwid == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004443 // remove group if all items are empty and highlight group
4444 // doesn't change
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004445 group_start_userhl = group_end_userhl = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004446 for (n = stl_groupitem[groupdepth] - 1; n >= 0; n--)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004447 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004448 if (stl_items[n].stl_type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004449 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004450 group_start_userhl = group_end_userhl =
4451 stl_items[n].stl_minwid;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004452 break;
4453 }
4454 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004455 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004456 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004457 if (stl_items[n].stl_type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 break;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004459 if (stl_items[n].stl_type == Highlight)
4460 group_end_userhl = stl_items[n].stl_minwid;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004461 }
4462 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 {
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004464 // empty group
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 p = t;
4466 l = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004467 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004468 {
4469 // do not use the highlighting from the removed group
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004470 if (stl_items[n].stl_type == Highlight)
4471 stl_items[n].stl_type = Empty;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004472 // adjust the start position of TabPage to the next
4473 // item position
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004474 if (stl_items[n].stl_type == TabPage)
4475 stl_items[n].stl_start = p;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 }
4478 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004479 if (l > stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004481 // truncate, remove n bytes of text at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 if (has_mbyte)
4483 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004484 // Find the first character that should be included.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 n = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004486 while (l >= stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 {
4488 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004489 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 }
4491 }
4492 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004493 n = (long)(p - t) - stl_items[stl_groupitem[groupdepth]]
4494 .stl_maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495
4496 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004497 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 p = p - n + 1;
Bram Moolenaar13505972019-01-24 15:04:48 +01004499
4500 // Fill up space left over by half a double-wide char.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004501 while (++l < stl_items[stl_groupitem[groupdepth]].stl_minwid)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004502 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503
Bram Moolenaarc667da52019-11-30 20:52:27 +01004504 // correct the start of the items for the truncation
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004505 for (l = stl_groupitem[groupdepth] + 1; l < curitem; l++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 {
LemonBoy57ff5262022-05-09 21:03:47 +01004507 // Minus one for the leading '<' added above.
4508 stl_items[l].stl_start -= n - 1;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004509 if (stl_items[l].stl_start < t)
4510 stl_items[l].stl_start = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 }
4512 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004513 else if (abs(stl_items[stl_groupitem[groupdepth]].stl_minwid) > l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004515 // fill
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004516 n = stl_items[stl_groupitem[groupdepth]].stl_minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 if (n < 0)
4518 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004519 // fill by appending characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 n = 0 - n;
4521 while (l++ < n && p + 1 < out + outlen)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004522 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 }
4524 else
4525 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004526 // fill by inserting characters
Bram Moolenaar008bff92021-03-04 21:55:58 +01004527 l = (n - l) * MB_CHAR2LEN(fillchar);
4528 mch_memmove(t + l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004530 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 p += l;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004532 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
4533 stl_items[n].stl_start += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 for ( ; l > 0; l--)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004535 MB_CHAR2BYTES(fillchar, t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 }
4537 }
4538 continue;
4539 }
4540 minwid = 0;
4541 maxwid = 9999;
4542 zeropad = FALSE;
4543 l = 1;
4544 if (*s == '0')
4545 {
4546 s++;
4547 zeropad = TRUE;
4548 }
4549 if (*s == '-')
4550 {
4551 s++;
4552 l = -1;
4553 }
4554 if (VIM_ISDIGIT(*s))
4555 {
4556 minwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004557 if (minwid < 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 minwid = 0;
4559 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004560 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004562 stl_items[curitem].stl_type = Highlight;
4563 stl_items[curitem].stl_start = p;
4564 stl_items[curitem].stl_minwid = minwid > 9 ? 1 : minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 s++;
4566 curitem++;
4567 continue;
4568 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004569 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4570 {
4571 if (*s == STL_TABCLOSENR)
4572 {
4573 if (minwid == 0)
4574 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004575 // %X ends the close label, go back to the previously
4576 // define tab label nr.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004577 for (n = curitem - 1; n >= 0; --n)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004578 if (stl_items[n].stl_type == TabPage
4579 && stl_items[n].stl_minwid >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004580 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004581 minwid = stl_items[n].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004582 break;
4583 }
4584 }
4585 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01004586 // close nrs are stored as negative values
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004587 minwid = - minwid;
4588 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004589 stl_items[curitem].stl_type = TabPage;
4590 stl_items[curitem].stl_start = p;
4591 stl_items[curitem].stl_minwid = minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004592 s++;
4593 curitem++;
4594 continue;
4595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 if (*s == '.')
4597 {
4598 s++;
4599 if (VIM_ISDIGIT(*s))
4600 {
4601 maxwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004602 if (maxwid <= 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 maxwid = 50;
4604 }
4605 }
4606 minwid = (minwid > 50 ? 50 : minwid) * l;
4607 if (*s == '(')
4608 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004609 stl_groupitem[groupdepth++] = curitem;
4610 stl_items[curitem].stl_type = Group;
4611 stl_items[curitem].stl_start = p;
4612 stl_items[curitem].stl_minwid = minwid;
4613 stl_items[curitem].stl_maxwid = maxwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 s++;
4615 curitem++;
4616 continue;
4617 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004618#ifdef FEAT_EVAL
4619 // Denotes end of expanded %{} block
4620 if (*s == '}' && evaldepth > 0)
4621 {
4622 s++;
4623 evaldepth--;
4624 continue;
4625 }
4626#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 if (vim_strchr(STL_ALL, *s) == NULL)
4628 {
Bram Moolenaar7b17eb42023-01-04 14:31:49 +00004629 if (*s == NUL) // can happen with "%0"
4630 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 s++;
4632 continue;
4633 }
4634 opt = *s++;
4635
Bram Moolenaarc667da52019-11-30 20:52:27 +01004636 // OK - now for the real work
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 base = 'D';
4638 itemisflag = FALSE;
4639 fillable = TRUE;
4640 num = -1;
4641 str = NULL;
4642 switch (opt)
4643 {
4644 case STL_FILEPATH:
4645 case STL_FULLPATH:
4646 case STL_FILENAME:
Bram Moolenaarc667da52019-11-30 20:52:27 +01004647 fillable = FALSE; // don't change ' ' to fillchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004649 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 else
4651 {
4652 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004653 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4655 }
4656 trans_characters(NameBuff, MAXPATHL);
4657 if (opt != STL_FILENAME)
4658 str = NameBuff;
4659 else
4660 str = gettail(NameBuff);
4661 break;
4662
Bram Moolenaarc667da52019-11-30 20:52:27 +01004663 case STL_VIM_EXPR: // '{'
shadmansaleh30e3de22021-05-15 17:23:28 +02004664 {
4665#ifdef FEAT_EVAL
4666 char_u *block_start = s - 1;
4667#endif
4668 int reevaluate = (*s == '%');
4669
4670 if (reevaluate)
4671 s++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 itemisflag = TRUE;
4673 t = p;
shadmansaleh30e3de22021-05-15 17:23:28 +02004674 while ((*s != '}' || (reevaluate && s[-1] != '%'))
4675 && *s != NUL && p + 1 < out + outlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 *p++ = *s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004677 if (*s != '}') // missing '}' or out of space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 break;
4679 s++;
shadmansaleh30e3de22021-05-15 17:23:28 +02004680 if (reevaluate)
4681 p[-1] = 0; // remove the % at the end of %{% expr %}
4682 else
4683 *p = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 p = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685#ifdef FEAT_EVAL
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004686 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp),
4687 "%d", curbuf->b_fnum);
4688 set_internal_string_var((char_u *)"g:actual_curbuf", buf_tmp);
4689 vim_snprintf((char *)win_tmp, sizeof(win_tmp), "%d", curwin->w_id);
4690 set_internal_string_var((char_u *)"g:actual_curwin", win_tmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004692 save_curbuf = curbuf;
4693 save_curwin = curwin;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004694 save_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 curwin = wp;
4696 curbuf = wp->w_buffer;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004697 // Visual mode is only valid in the current window.
4698 if (curwin != save_curwin)
4699 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700
Bram Moolenaara4e0b972022-10-01 19:43:52 +01004701 str = eval_to_string_safe(p, use_sandbox, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004703 curwin = save_curwin;
4704 curbuf = save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004705 VIsual_active = save_VIsual_active;
Bram Moolenaar01824652005-01-31 18:58:23 +00004706 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004707 do_unlet((char_u *)"g:actual_curwin", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708
4709 if (str != NULL && *str != 0)
4710 {
4711 if (*skipdigits(str) == NUL)
4712 {
4713 num = atoi((char *)str);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004714 VIM_CLEAR(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 itemisflag = FALSE;
4716 }
4717 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004718
4719 // If the output of the expression needs to be evaluated
4720 // replace the %{} block with the result of evaluation
4721 if (reevaluate && str != NULL && *str != 0
4722 && strchr((const char *)str, '%') != NULL
4723 && evaldepth < MAX_STL_EVAL_DEPTH)
4724 {
4725 size_t parsed_usefmt = (size_t)(block_start - usefmt);
4726 size_t str_length = strlen((const char *)str);
4727 size_t fmt_length = strlen((const char *)s);
4728 size_t new_fmt_len = parsed_usefmt
4729 + str_length + fmt_length + 3;
4730 char_u *new_fmt = (char_u *)alloc(new_fmt_len * sizeof(char_u));
4731 char_u *new_fmt_p = new_fmt;
4732
4733 new_fmt_p = (char_u *)memcpy(new_fmt_p, usefmt, parsed_usefmt)
4734 + parsed_usefmt;
4735 new_fmt_p = (char_u *)memcpy(new_fmt_p , str, str_length)
4736 + str_length;
4737 new_fmt_p = (char_u *)memcpy(new_fmt_p, "%}", 2) + 2;
4738 new_fmt_p = (char_u *)memcpy(new_fmt_p , s, fmt_length)
4739 + fmt_length;
4740 *new_fmt_p = 0;
4741 new_fmt_p = NULL;
4742
4743 if (usefmt != fmt)
4744 vim_free(usefmt);
4745 VIM_CLEAR(str);
4746 usefmt = new_fmt;
4747 s = usefmt + parsed_usefmt;
4748 evaldepth++;
4749 continue;
4750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751#endif
4752 break;
shadmansaleh30e3de22021-05-15 17:23:28 +02004753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 case STL_LINE:
4755 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4756 ? 0L : (long)(wp->w_cursor.lnum);
4757 break;
4758
4759 case STL_NUMLINES:
4760 num = wp->w_buffer->b_ml.ml_line_count;
4761 break;
4762
4763 case STL_COLUMN:
Bram Moolenaar24959102022-05-07 20:01:16 +01004764 num = (State & MODE_INSERT) == 0 && empty_line
4765 ? 0 : (int)wp->w_cursor.col + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 break;
4767
4768 case STL_VIRTCOL:
4769 case STL_VIRTCOL_ALT:
zeertzjq0f112052022-01-14 20:11:38 +00004770 virtcol = wp->w_virtcol + 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004771 // Don't display %V if it's the same as %c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 if (opt == STL_VIRTCOL_ALT
Bram Moolenaar24959102022-05-07 20:01:16 +01004773 && (virtcol == (colnr_T)((State & MODE_INSERT) == 0
4774 && empty_line ? 0 : (int)wp->w_cursor.col + 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 break;
4776 num = (long)virtcol;
4777 break;
4778
4779 case STL_PERCENTAGE:
4780 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4781 (long)wp->w_buffer->b_ml.ml_line_count);
4782 break;
4783
4784 case STL_ALTPERCENT:
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004785 str = buf_tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004786 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 break;
4788
Luuk van Baalba936f62022-12-15 13:15:39 +00004789 case STL_SHOWCMD:
4790 if (p_sc && STRCMP(opt_name, p_sloc) == 0)
4791 str = showcmd_buf;
4792 break;
4793
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 case STL_ARGLISTSTAT:
4795 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004796 buf_tmp[0] = 0;
4797 if (append_arg_number(wp, buf_tmp, (int)sizeof(buf_tmp), FALSE))
4798 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 break;
4800
4801 case STL_KEYMAP:
4802 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004803 if (get_keymap_str(wp, (char_u *)"<%s>", buf_tmp, TMPLEN))
4804 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 break;
4806 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004807#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004808 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809#else
4810 num = 0;
4811#endif
4812 break;
4813
4814 case STL_BUFNO:
4815 num = wp->w_buffer->b_fnum;
4816 break;
4817
4818 case STL_OFFSET_X:
4819 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004820 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 case STL_OFFSET:
4822#ifdef FEAT_BYTEOFF
4823 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
Bram Moolenaar24959102022-05-07 20:01:16 +01004824 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0
4825 ? 0L : l + 1 + ((State & MODE_INSERT) == 0 && empty_line
4826 ? 0 : (int)wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827#endif
4828 break;
4829
4830 case STL_BYTEVAL_X:
4831 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004832 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004834 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 if (num == NL)
4836 num = 0;
4837 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4838 num = NL;
4839 break;
4840
4841 case STL_ROFLAG:
4842 case STL_ROFLAG_ALT:
4843 itemisflag = TRUE;
4844 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004845 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 break;
4847
4848 case STL_HELPFLAG:
4849 case STL_HELPFLAG_ALT:
4850 itemisflag = TRUE;
4851 if (wp->w_buffer->b_help)
4852 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004853 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 break;
4855
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 case STL_FILETYPE:
4857 if (*wp->w_buffer->b_p_ft != NUL
4858 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4859 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004860 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), "[%s]",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004861 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004862 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 }
4864 break;
4865
4866 case STL_FILETYPE_ALT:
4867 itemisflag = TRUE;
4868 if (*wp->w_buffer->b_p_ft != NUL
4869 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4870 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004871 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), ",%s",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004872 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004873 for (t = buf_tmp; *t != 0; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 *t = TOUPPER_LOC(*t);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004875 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 }
4877 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878
Bram Moolenaar4033c552017-09-16 20:54:51 +02004879#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 case STL_PREVIEWFLAG:
4881 case STL_PREVIEWFLAG_ALT:
4882 itemisflag = TRUE;
4883 if (wp->w_p_pvw)
4884 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4885 : _("[Preview]"));
4886 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004887
4888 case STL_QUICKFIX:
4889 if (bt_quickfix(wp->w_buffer))
4890 str = (char_u *)(wp->w_llist_ref
4891 ? _(msg_loclist)
4892 : _(msg_qflist));
4893 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894#endif
4895
4896 case STL_MODIFIED:
4897 case STL_MODIFIED_ALT:
4898 itemisflag = TRUE;
4899 switch ((opt == STL_MODIFIED_ALT)
4900 + bufIsChanged(wp->w_buffer) * 2
4901 + (!wp->w_buffer->b_p_ma) * 4)
4902 {
4903 case 2: str = (char_u *)"[+]"; break;
4904 case 3: str = (char_u *)",+"; break;
4905 case 4: str = (char_u *)"[-]"; break;
4906 case 5: str = (char_u *)",-"; break;
4907 case 6: str = (char_u *)"[+-]"; break;
4908 case 7: str = (char_u *)",+-"; break;
4909 }
4910 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004911
4912 case STL_HIGHLIGHT:
4913 t = s;
4914 while (*s != '#' && *s != NUL)
4915 ++s;
4916 if (*s == '#')
4917 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004918 stl_items[curitem].stl_type = Highlight;
4919 stl_items[curitem].stl_start = p;
4920 stl_items[curitem].stl_minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004921 curitem++;
4922 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004923 if (*s != NUL)
4924 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004925 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 }
4927
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004928 stl_items[curitem].stl_start = p;
4929 stl_items[curitem].stl_type = Normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 if (str != NULL && *str)
4931 {
4932 t = str;
4933 if (itemisflag)
4934 {
4935 if ((t[0] && t[1])
4936 && ((!prevchar_isitem && *t == ',')
4937 || (prevchar_isflag && *t == ' ')))
4938 t++;
4939 prevchar_isflag = TRUE;
4940 }
4941 l = vim_strsize(t);
4942 if (l > 0)
4943 prevchar_isitem = TRUE;
4944 if (l > maxwid)
4945 {
4946 while (l >= maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 if (has_mbyte)
4948 {
4949 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004950 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 }
4952 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 l -= byte2cells(*t++);
4954 if (p + 1 >= out + outlen)
4955 break;
4956 *p++ = '<';
4957 }
4958 if (minwid > 0)
4959 {
4960 for (; l < minwid && p + 1 < out + outlen; l++)
4961 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004962 // Don't put a "-" in front of a digit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4964 *p++ = ' ';
4965 else
Bram Moolenaar008bff92021-03-04 21:55:58 +01004966 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 }
4968 minwid = 0;
4969 }
4970 else
4971 minwid *= -1;
Bram Moolenaar008bff92021-03-04 21:55:58 +01004972 for (; *t && p + 1 < out + outlen; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004974 // Change a space by fillchar, unless fillchar is '-' and a
4975 // digit follows.
Bram Moolenaar008bff92021-03-04 21:55:58 +01004976 if (fillable && *t == ' '
4977 && (!VIM_ISDIGIT(*(t + 1)) || fillchar != '-'))
4978 MB_CHAR2BYTES(fillchar, p);
4979 else
4980 *p++ = *t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 }
4982 for (; l < minwid && p + 1 < out + outlen; l++)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004983 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 }
4985 else if (num >= 0)
4986 {
4987 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4988 char_u nstr[20];
4989
4990 if (p + 20 >= out + outlen)
Bram Moolenaarc667da52019-11-30 20:52:27 +01004991 break; // not sufficient space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 prevchar_isitem = TRUE;
4993 t = nstr;
4994 if (opt == STL_VIRTCOL_ALT)
4995 {
4996 *t++ = '-';
4997 minwid--;
4998 }
4999 *t++ = '%';
5000 if (zeropad)
5001 *t++ = '0';
5002 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005003 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 *t = 0;
5005
5006 for (n = num, l = 1; n >= nbase; n /= nbase)
5007 l++;
5008 if (opt == STL_VIRTCOL_ALT)
5009 l++;
5010 if (l > maxwid)
5011 {
5012 l += 2;
5013 n = l - maxwid;
5014 while (l-- > maxwid)
5015 num /= nbase;
5016 *t++ = '>';
5017 *t++ = '%';
5018 *t = t[-3];
5019 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005020 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
5021 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 }
5023 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005024 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
5025 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 p += STRLEN(p);
5027 }
5028 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005029 stl_items[curitem].stl_type = Empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01005031 if (num >= 0 || (!itemisflag && str != NULL && *str != NUL))
5032 prevchar_isflag = FALSE; // Item not NULL, but not a flag
5033 //
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 if (opt == STL_VIM_EXPR)
5035 vim_free(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 curitem++;
5037 }
5038 *p = NUL;
5039 itemcnt = curitem;
5040
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005041#ifdef FEAT_EVAL
5042 if (usefmt != fmt)
5043 vim_free(usefmt);
5044#endif
5045
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 width = vim_strsize(out);
5047 if (maxwidth > 0 && width > maxwidth)
5048 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005049 // Result is too long, must truncate somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 l = 0;
5051 if (itemcnt == 0)
5052 s = out;
5053 else
5054 {
5055 for ( ; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005056 if (stl_items[l].stl_type == Trunc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005058 // Truncate at %< item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005059 s = stl_items[l].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 break;
5061 }
5062 if (l == itemcnt)
5063 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005064 // No %< item, truncate first item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005065 s = stl_items[0].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 l = 0;
5067 }
5068 }
5069
5070 if (width - vim_strsize(s) >= maxwidth)
5071 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005072 // Truncation mark is beyond max length
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 if (has_mbyte)
5074 {
5075 s = out;
5076 width = 0;
5077 for (;;)
5078 {
5079 width += ptr2cells(s);
5080 if (width >= maxwidth)
5081 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005082 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005084 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 while (++width < maxwidth)
Bram Moolenaar008bff92021-03-04 21:55:58 +01005086 MB_CHAR2BYTES(fillchar, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 }
5088 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 s = out + maxwidth - 1;
5090 for (l = 0; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005091 if (stl_items[l].stl_start > s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 break;
5093 itemcnt = l;
5094 *s++ = '>';
5095 *s = 0;
5096 }
5097 else
5098 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 if (has_mbyte)
5100 {
5101 n = 0;
5102 while (width >= maxwidth)
5103 {
5104 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005105 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 }
5107 }
5108 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 n = width - maxwidth + 1;
5110 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00005111 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 *s = '<';
5113
Bram Moolenaarc667da52019-11-30 20:52:27 +01005114 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 while (++width < maxwidth)
5116 {
5117 s = s + STRLEN(s);
Bram Moolenaar008bff92021-03-04 21:55:58 +01005118 MB_CHAR2BYTES(fillchar, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 *s = NUL;
5120 }
5121
Bram Moolenaarc667da52019-11-30 20:52:27 +01005122 --n; // count the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 for (; l < itemcnt; l++)
5124 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005125 if (stl_items[l].stl_start - n >= s)
5126 stl_items[l].stl_start -= n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005128 stl_items[l].stl_start = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 }
5130 }
5131 width = maxwidth;
5132 }
5133 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
5134 {
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00005135 // Find how many separators there are, which we will use when
5136 // figuring out how many groups there are.
5137 int num_separators = 0;
5138
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 for (l = 0; l < itemcnt; l++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 {
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00005141 if (stl_items[l].stl_type == Separate)
5142 {
5143 // Create an array of the start location for each separator
5144 // mark.
5145 stl_separator_locations[num_separators] = l;
5146 num_separators++;
5147 }
5148 }
5149
5150 // If we have separated groups, then we deal with it now
5151 if (num_separators)
5152 {
5153 int standard_spaces;
5154 int final_spaces;
5155
5156 standard_spaces = (maxwidth - width) / num_separators;
5157 final_spaces = (maxwidth - width) -
5158 standard_spaces * (num_separators - 1);
5159 for (l = 0; l < num_separators; l++)
5160 {
5161 int dislocation = (l == (num_separators - 1)) ?
5162 final_spaces : standard_spaces;
5163 dislocation *= MB_CHAR2LEN(fillchar);
5164 char_u *start = stl_items[stl_separator_locations[l]].stl_start;
5165 char_u *seploc = start + dislocation;
5166 STRMOVE(seploc, start);
5167 for (s = start; s < seploc;)
5168 MB_CHAR2BYTES(fillchar, s);
5169
5170 for (int i = stl_separator_locations[l] + 1; i < itemcnt; i++)
5171 stl_items[i].stl_start += dislocation;
5172 }
5173
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 width = maxwidth;
5175 }
5176 }
5177
Bram Moolenaarc667da52019-11-30 20:52:27 +01005178 // Store the info about highlighting.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005179 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005181 *hltab = stl_hltab;
5182 sp = stl_hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 for (l = 0; l < itemcnt; l++)
5184 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005185 if (stl_items[l].stl_type == Highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005187 sp->start = stl_items[l].stl_start;
5188 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005189 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 }
5191 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005192 sp->start = NULL;
5193 sp->userhl = 0;
5194 }
5195
Bram Moolenaarc667da52019-11-30 20:52:27 +01005196 // Store the info about tab pages labels.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005197 if (tabtab != NULL)
5198 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005199 *tabtab = stl_tabtab;
5200 sp = stl_tabtab;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005201 for (l = 0; l < itemcnt; l++)
5202 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005203 if (stl_items[l].stl_type == TabPage)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005204 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005205 sp->start = stl_items[l].stl_start;
5206 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005207 sp++;
5208 }
5209 }
5210 sp->start = NULL;
5211 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 }
5213
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01005214 redraw_not_allowed = save_redraw_not_allowed;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02005215
Bram Moolenaar0e1f36f2022-02-15 16:17:44 +00005216 // A user function may reset KeyTyped, restore it.
5217 KeyTyped = save_KeyTyped;
5218
Luuk van Baal7b224fd2022-11-07 12:16:51 +00005219 // Check for an error. If there is one the display will be messed up and
5220 // might loop redrawing. Avoid that by making the corresponding option
5221 // empty.
5222 // TODO: find out why using called_emsg_before makes tests fail, does it
5223 // matter?
5224 // if (called_emsg > called_emsg_before)
5225 if (did_emsg > did_emsg_before)
5226 set_string_option_direct(opt_name, -1, (char_u *)"",
5227 OPT_FREE | opt_scope, SID_ERROR);
5228
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 return width;
5230}
Bram Moolenaarc667da52019-11-30 20:52:27 +01005231#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005234 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
5235 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 */
5237 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005238get_rel_pos(
5239 win_T *wp,
5240 char_u *buf,
5241 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005243 long above; // number of lines above window
5244 long below; // number of lines below window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245
Bram Moolenaarc667da52019-11-30 20:52:27 +01005246 if (buflen < 3) // need at least 3 chars for writing
Bram Moolenaar0027c212015-01-07 13:31:52 +01005247 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 above = wp->w_topline - 1;
5249#ifdef FEAT_DIFF
5250 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02005251 if (wp->w_topline == 1 && wp->w_topfill >= 1)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005252 above = 0; // All buffer lines are displayed and there is an
5253 // indication of filler lines, that can be considered
5254 // seeing all lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255#endif
5256 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
5257 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005258 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
5259 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005261 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005263 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 ? (int)(above / ((above + below) / 100L))
5265 : (int)(above * 100L / (above + below)));
5266}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267
5268/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005269 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 * Return TRUE if it was appended.
5271 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005272 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005273append_arg_number(
5274 win_T *wp,
5275 char_u *buf,
5276 int buflen,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005277 int add_file) // Add "file" before the arg number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278{
5279 char_u *p;
5280
Bram Moolenaarc667da52019-11-30 20:52:27 +01005281 if (ARGCOUNT <= 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 return FALSE;
5283
Bram Moolenaarc667da52019-11-30 20:52:27 +01005284 p = buf + STRLEN(buf); // go to the end of the buffer
5285 if (p - buf + 35 >= buflen) // getting too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 return FALSE;
5287 *p++ = ' ';
5288 *p++ = '(';
5289 if (add_file)
5290 {
5291 STRCPY(p, "file ");
5292 p += 5;
5293 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005294 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
5295 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
5297 return TRUE;
5298}
5299
5300/*
5301 * If fname is not a full path, make it a full path.
5302 * Returns pointer to allocated memory (NULL for failure).
5303 */
5304 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005305fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306{
5307 /*
5308 * Force expanding the path always for Unix, because symbolic links may
5309 * mess up the full path name, even though it starts with a '/'.
5310 * Also expand when there is ".." in the file name, try to remove it,
5311 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00005312 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 * For MS-Windows also expand names like "longna~1" to "longname".
5314 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00005315#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 return FullName_save(fname, TRUE);
5317#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00005318 if (!vim_isAbsName(fname)
5319 || strstr((char *)fname, "..") != NULL
5320 || strstr((char *)fname, "//") != NULL
5321# ifdef BACKSLASH_IN_FILENAME
5322 || strstr((char *)fname, "\\\\") != NULL
5323# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005324# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00005326# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 )
5328 return FullName_save(fname, FALSE);
5329
5330 fname = vim_strsave(fname);
5331
Bram Moolenaar9b942202007-10-03 12:31:33 +00005332# ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01005333 if (fname != NULL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005334 fname_case(fname, 0); // set correct case for file name
Bram Moolenaar9b942202007-10-03 12:31:33 +00005335# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336
5337 return fname;
5338#endif
5339}
5340
5341/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005342 * Make "*ffname" a full file name, set "*sfname" to "*ffname" if not NULL.
5343 * "*ffname" becomes a pointer to allocated memory (or NULL).
5344 * When resolving a link both "*sfname" and "*ffname" will point to the same
5345 * allocated memory.
5346 * The "*ffname" and "*sfname" pointer values on call will not be freed.
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005347 * Note that the resulting "*ffname" pointer should be considered not allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005350fname_expand(
5351 buf_T *buf UNUSED,
5352 char_u **ffname,
5353 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005355 if (*ffname == NULL) // no file name given, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 return;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005357 if (*sfname == NULL) // no short file name given, use ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 *sfname = *ffname;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005359 *ffname = fix_fname(*ffname); // expand to full path
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360
5361#ifdef FEAT_SHORTCUT
5362 if (!buf->b_p_bin)
5363 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005364 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005366 // If the file name is a shortcut file, use the file it links to.
Bram Moolenaardce1e892019-02-10 23:18:53 +01005367 rfname = mch_resolve_path(*ffname, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005368 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 {
5370 vim_free(*ffname);
5371 *ffname = rfname;
5372 *sfname = rfname;
5373 }
5374 }
5375#endif
5376}
5377
5378/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 * Open a window for a number of buffers.
5380 */
5381 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005382ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383{
5384 buf_T *buf;
5385 win_T *wp, *wpnext;
5386 int split_ret = OK;
5387 int p_ea_save;
5388 int open_wins = 0;
5389 int r;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005390 int count; // Maximum number of windows to open.
5391 int all; // When TRUE also load inactive buffers.
Bram Moolenaare1004402020-10-24 20:49:43 +02005392 int had_tab = cmdmod.cmod_tab;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005393 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394
Bram Moolenaarc667da52019-11-30 20:52:27 +01005395 if (eap->addr_count == 0) // make as many windows as possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 count = 9999;
5397 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01005398 count = eap->line2; // make as many windows as specified
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5400 all = FALSE;
5401 else
5402 all = TRUE;
5403
Pavel Mayorove1121b12023-02-20 14:35:20 +00005404 // Stop Visual mode, the cursor and "VIsual" may very well be invalid after
5405 // switching to another buffer.
5406 reset_VIsual_and_resel();
5407
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 setpcmark();
5409
5410#ifdef FEAT_GUI
5411 need_mouse_correct = TRUE;
5412#endif
5413
5414 /*
5415 * Close superfluous windows (two windows for the same buffer).
5416 * Also close windows that are not full-width.
5417 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005418 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005419 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005420 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005422 tpnext = curtab->tp_next;
5423 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005425 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005426 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005427 || ((cmdmod.cmod_split & WSP_VERT)
5428 ? wp->w_height + wp->w_status_height < Rows - p_ch
5429 - tabline_height()
5430 : wp->w_width != Columns)
5431 || (had_tab > 0 && wp != firstwin))
5432 && !ONE_WINDOW
5433 && !(wp->w_closing || wp->w_buffer->b_locked > 0)
5434 && !win_unlisted(wp))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005435 {
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005436 if (win_close(wp, FALSE) == FAIL)
5437 break;
5438 // Just in case an autocommand does something strange with
5439 // windows: start all over...
5440 wpnext = firstwin;
5441 tpnext = first_tabpage;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005442 open_wins = 0;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005443 }
5444 else
5445 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005447
Bram Moolenaarc667da52019-11-30 20:52:27 +01005448 // Without the ":tab" modifier only do the current tab page.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005449 if (had_tab == 0 || tpnext == NULL)
5450 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005451 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 }
5453
5454 /*
5455 * Go through the buffer list. When a buffer doesn't have a window yet,
5456 * open one. Otherwise move the window to the right position.
5457 * Watch out for autocommands that delete buffers or windows!
5458 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01005459 // Don't execute Win/Buf Enter/Leave autocommands here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 ++autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 win_enter(lastwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5464 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005465 // Check if this buffer needs a window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5467 continue;
5468
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005469 if (had_tab != 0)
5470 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005471 // With the ":tab" modifier don't move the window.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005472 if (buf->b_nwindows > 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005473 wp = lastwin; // buffer has a window, skip it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005474 else
5475 wp = NULL;
5476 }
5477 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005478 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005479 // Check if this buffer already has a window
Bram Moolenaar29323592016-07-24 22:04:11 +02005480 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005481 if (wp->w_buffer == buf)
5482 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005483 // If the buffer already has a window, move it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005484 if (wp != NULL)
5485 win_move_after(wp, curwin);
5486 }
5487
5488 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005490 bufref_T bufref;
5491
5492 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005493
Bram Moolenaarc667da52019-11-30 20:52:27 +01005494 // Split the window and put the buffer in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 p_ea_save = p_ea;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005496 p_ea = TRUE; // use space from all windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5498 ++open_wins;
5499 p_ea = p_ea_save;
5500 if (split_ret == FAIL)
5501 continue;
5502
Bram Moolenaarc667da52019-11-30 20:52:27 +01005503 // Open the buffer in this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005506 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005508 // autocommands deleted the buffer!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 swap_exists_action = SEA_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 break;
5511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 if (swap_exists_action == SEA_QUIT)
5513 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005514#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005515 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005516
Bram Moolenaarc667da52019-11-30 20:52:27 +01005517 // Reset the error/interrupt/exception state here so that
5518 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005519 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005520#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005521
Bram Moolenaarc667da52019-11-30 20:52:27 +01005522 // User selected Quit at ATTENTION prompt; close this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 win_close(curwin, TRUE);
5524 --open_wins;
5525 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005526 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005527
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005528#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005529 // Restore the error/interrupt/exception state if not
5530 // discarded by a new aborting error, interrupt, or uncaught
5531 // exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005532 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005533#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 }
5535 else
5536 handle_swap_exists(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 }
5538
5539 ui_breakcheck();
5540 if (got_int)
5541 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005542 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 break;
5544 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005545#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01005546 // Autocommands deleted the buffer or aborted script processing!!!
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005547 if (aborting())
5548 break;
5549#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01005550 // When ":tab" was used open a new tab for a new window repeatedly.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005551 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
Bram Moolenaare1004402020-10-24 20:49:43 +02005552 cmdmod.cmod_tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 --autocmd_no_enter;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005555 win_enter(firstwin, FALSE); // back to first window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557
5558 /*
5559 * Close superfluous windows.
5560 */
5561 for (wp = lastwin; open_wins > count; )
5562 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005563 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 || autowrite(wp->w_buffer, FALSE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 if (!win_valid(wp))
5566 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005567 // BufWrite Autocommands made the window invalid, start over
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 wp = lastwin;
5569 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005570 else if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005572 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 --open_wins;
5574 wp = lastwin;
5575 }
5576 else
5577 {
5578 wp = wp->w_prev;
5579 if (wp == NULL)
5580 break;
5581 }
5582 }
5583}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005586static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005587
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588/*
5589 * do_modelines() - process mode lines for the current file
5590 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005591 * "flags" can be:
5592 * OPT_WINONLY only set options local to window
5593 * OPT_NOWIN don't set options local to window
5594 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 * Returns immediately if the "ml" option isn't set.
5596 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005598do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005600 linenr_T lnum;
5601 int nmlines;
5602 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603
5604 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5605 return;
5606
Bram Moolenaarc667da52019-11-30 20:52:27 +01005607 // Disallow recursive entry here. Can happen when executing a modeline
5608 // triggers an autocommand, which reloads modelines with a ":do".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 if (entered)
5610 return;
5611
5612 ++entered;
Hu Jialun9dcd3492021-08-28 20:42:50 +02005613 for (lnum = 1; curbuf->b_p_ml && lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005615 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 nmlines = 0;
5617
Hu Jialun9dcd3492021-08-28 20:42:50 +02005618 for (lnum = curbuf->b_ml.ml_line_count; curbuf->b_p_ml && lnum > 0 && lnum > nmlines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005620 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 nmlines = 0;
5622 --entered;
5623}
5624
Bram Moolenaarc667da52019-11-30 20:52:27 +01005625#include "version.h" // for version number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626
5627/*
5628 * chk_modeline() - check a single line for a mode string
5629 * Return FAIL if an error encountered.
5630 */
5631 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005632chk_modeline(
5633 linenr_T lnum,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005634 int flags) // Same as for do_modelines().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635{
5636 char_u *s;
5637 char_u *e;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005638 char_u *linecopy; // local copy of any modeline found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 int prev;
5640 int vers;
5641 int end;
5642 int retval = OK;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005643 sctx_T save_current_sctx;
ichizok7e5fe382023-04-15 13:17:50 +01005644 ESTACK_CHECK_DECLARATION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645
5646 prev = -1;
5647 for (s = ml_get(lnum); *s != NUL; ++s)
5648 {
5649 if (prev == -1 || vim_isspace(prev))
5650 {
5651 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5652 || STRNCMP(s, "vi:", (size_t)3) == 0)
5653 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005654 // Accept both "vim" and "Vim".
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005655 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 {
5657 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5658 e = s + 4;
5659 else
5660 e = s + 3;
5661 vers = getdigits(&e);
5662 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005663 && (s[0] != 'V'
5664 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 && (s[3] == ':'
5666 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5667 || (VIM_VERSION_100 < vers && s[3] == '<')
5668 || (VIM_VERSION_100 > vers && s[3] == '>')
5669 || (VIM_VERSION_100 == vers && s[3] == '=')))
5670 break;
5671 }
5672 }
5673 prev = *s;
5674 }
5675
5676 if (*s)
5677 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005678 do // skip over "ex:", "vi:" or "vim:"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 ++s;
5680 while (s[-1] != ':');
5681
Bram Moolenaarc667da52019-11-30 20:52:27 +01005682 s = linecopy = vim_strsave(s); // copy the line, it will change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 if (linecopy == NULL)
5684 return FAIL;
5685
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005686 // prepare for emsg()
5687 estack_push(ETYPE_MODELINE, (char_u *)"modelines", lnum);
ichizok7e5fe382023-04-15 13:17:50 +01005688 ESTACK_CHECK_SETUP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689
5690 end = FALSE;
5691 while (end == FALSE)
5692 {
5693 s = skipwhite(s);
5694 if (*s == NUL)
5695 break;
5696
5697 /*
5698 * Find end of set command: ':' or end of line.
5699 * Skip over "\:", replacing it with ":".
5700 */
5701 for (e = s; *e != ':' && *e != NUL; ++e)
5702 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005703 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 if (*e == NUL)
5705 end = TRUE;
5706
5707 /*
5708 * If there is a "set" command, require a terminating ':' and
5709 * ignore the stuff after the ':'.
5710 * "vi:set opt opt opt: foo" -- foo not interpreted
5711 * "vi:opt opt opt: foo" -- foo interpreted
5712 * Accept "se" for compatibility with Elvis.
5713 */
5714 if (STRNCMP(s, "set ", (size_t)4) == 0
5715 || STRNCMP(s, "se ", (size_t)3) == 0)
5716 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005717 if (*e != ':') // no terminating ':'?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 break;
5719 end = TRUE;
5720 s = vim_strchr(s, ' ') + 1;
5721 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005722 *e = NUL; // truncate the set command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723
Bram Moolenaarc667da52019-11-30 20:52:27 +01005724 if (*s != NUL) // skip over an empty "::"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 {
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005726 int secure_save = secure;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005727
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005728 save_current_sctx = current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005729 current_sctx.sc_version = 1;
5730#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005731 current_sctx.sc_sid = SID_MODELINE;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005732 current_sctx.sc_seq = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005733 current_sctx.sc_lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005735
Bram Moolenaar5958f952018-11-20 04:25:21 +01005736 // Make sure no risky things are executed as a side effect.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01005737 secure = 1;
Bram Moolenaar5958f952018-11-20 04:25:21 +01005738
Bram Moolenaara3227e22006-03-08 21:32:40 +00005739 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar5958f952018-11-20 04:25:21 +01005740
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005741 secure = secure_save;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005742 current_sctx = save_current_sctx;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005743 if (retval == FAIL) // stop if error found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 break;
5745 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005746 s = e + 1; // advance to next part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 }
5748
ichizok7e5fe382023-04-15 13:17:50 +01005749 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005750 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 vim_free(linecopy);
5752 }
5753 return retval;
5754}
5755
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005756/*
Bram Moolenaar91335e52018-08-01 17:53:12 +02005757 * Return TRUE if "buf" is a normal buffer, 'buftype' is empty.
5758 */
5759 int
5760bt_normal(buf_T *buf)
5761{
5762 return buf != NULL && buf->b_p_bt[0] == NUL;
5763}
5764
5765/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005766 * Return TRUE if "buf" is the quickfix buffer.
5767 */
5768 int
Bram Moolenaar340dafd2022-08-25 16:16:45 +01005769bt_quickfix(buf_T *buf UNUSED)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005770{
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005771#ifdef FEAT_QUICKFIX
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005772 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005773#else
5774 return FALSE;
Bram Moolenaar113e1072019-01-20 15:30:40 +01005775#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005776}
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005777
5778/*
5779 * Return TRUE if "buf" is a terminal buffer.
5780 */
5781 int
Bram Moolenaar340dafd2022-08-25 16:16:45 +01005782bt_terminal(buf_T *buf UNUSED)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005783{
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005784#if defined(FEAT_TERMINAL)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005785 return buf != NULL && buf->b_p_bt[0] == 't';
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005786#else
5787 return FALSE;
Bram Moolenaar113e1072019-01-20 15:30:40 +01005788#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005789}
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005790
5791/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005792 * Return TRUE if "buf" is a help buffer.
5793 */
5794 int
5795bt_help(buf_T *buf)
5796{
5797 return buf != NULL && buf->b_help;
5798}
5799
5800/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02005801 * Return TRUE if "buf" is a prompt buffer.
5802 */
5803 int
5804bt_prompt(buf_T *buf)
5805{
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005806 return buf != NULL && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'r';
5807}
5808
Dominique Pelle748b3082022-01-08 12:41:16 +00005809#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005810/*
5811 * Return TRUE if "buf" is a buffer for a popup window.
5812 */
5813 int
5814bt_popup(buf_T *buf)
5815{
5816 return buf != NULL && buf->b_p_bt != NULL
5817 && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'o';
Bram Moolenaarf2732452018-06-03 14:47:35 +02005818}
Dominique Pelle748b3082022-01-08 12:41:16 +00005819#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005820
5821/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005822 * Return TRUE if "buf" is a "nofile", "acwrite", "terminal" or "prompt"
Bram Moolenaarc3126192022-08-26 12:58:17 +01005823 * buffer. This means the buffer name may not be a file name, at least not for
5824 * writing the buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005825 */
5826 int
Bram Moolenaar26910de2019-06-15 19:37:15 +02005827bt_nofilename(buf_T *buf)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005828{
5829 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5830 || buf->b_p_bt[0] == 'a'
Bram Moolenaarf2732452018-06-03 14:47:35 +02005831 || buf->b_p_bt[0] == 't'
5832 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005833}
5834
Bram Moolenaarc3126192022-08-26 12:58:17 +01005835/*
5836 * Return TRUE if "buf" is a "nofile", "quickfix", "terminal" or "prompt"
5837 * buffer. This means the buffer is not to be read from a file.
5838 */
5839 static int
5840bt_nofileread(buf_T *buf)
5841{
5842 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5843 || buf->b_p_bt[0] == 't'
5844 || buf->b_p_bt[0] == 'q'
5845 || buf->b_p_bt[0] == 'p');
5846}
5847
Dominique Pelle748b3082022-01-08 12:41:16 +00005848#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005849/*
Bram Moolenaar26910de2019-06-15 19:37:15 +02005850 * Return TRUE if "buf" has 'buftype' set to "nofile".
5851 */
5852 int
5853bt_nofile(buf_T *buf)
5854{
5855 return buf != NULL && buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f';
5856}
Dominique Pelle748b3082022-01-08 12:41:16 +00005857#endif
Bram Moolenaar26910de2019-06-15 19:37:15 +02005858
5859/*
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01005860 * Return TRUE if "buf" is a "nowrite", "nofile", "terminal", "prompt", or
5861 * "popup" buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005862 */
5863 int
5864bt_dontwrite(buf_T *buf)
5865{
Bram Moolenaarf2732452018-06-03 14:47:35 +02005866 return buf != NULL && (buf->b_p_bt[0] == 'n'
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005867 || buf->b_p_bt[0] == 't'
5868 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005869}
5870
5871 int
5872bt_dontwrite_msg(buf_T *buf)
5873{
5874 if (bt_dontwrite(buf))
5875 {
Bram Moolenaarf1474d82021-12-31 19:59:55 +00005876 emsg(_(e_cannot_write_buftype_option_is_set));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005877 return TRUE;
5878 }
5879 return FALSE;
5880}
5881
5882/*
5883 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
5884 * and 'bufhidden'.
5885 */
5886 int
5887buf_hide(buf_T *buf)
5888{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005889 // 'bufhidden' overrules 'hidden' and ":hide", check it first
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005890 switch (buf->b_p_bh[0])
5891 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005892 case 'u': // "unload"
5893 case 'w': // "wipe"
5894 case 'd': return FALSE; // "delete"
5895 case 'h': return TRUE; // "hide"
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005896 }
Bram Moolenaare1004402020-10-24 20:49:43 +02005897 return (p_hid || (cmdmod.cmod_flags & CMOD_HIDE));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005898}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899
5900/*
5901 * Return special buffer name.
5902 * Returns NULL when the buffer has a normal file name.
5903 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005904 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005905buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906{
Bram Moolenaar4033c552017-09-16 20:54:51 +02005907#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005909 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005910 /*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005911 * Differentiate between the quickfix and location list buffers using
5912 * the buffer number stored in the global quickfix stack.
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005913 */
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005914 if (buf->b_fnum == qf_stack_get_bufnr())
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005915 return (char_u *)_(msg_qflist);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005916 else
5917 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02005920
Bram Moolenaarc667da52019-11-30 20:52:27 +01005921 // There is no _file_ when 'buftype' is "nofile", b_sfname
5922 // contains the name as specified by the user.
Bram Moolenaar26910de2019-06-15 19:37:15 +02005923 if (bt_nofilename(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 {
Bram Moolenaar21554412017-07-24 21:44:43 +02005925#ifdef FEAT_TERMINAL
5926 if (buf->b_term != NULL)
5927 return term_get_status_text(buf->b_term);
5928#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02005929 if (buf->b_fname != NULL)
5930 return buf->b_fname;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02005931#ifdef FEAT_JOB_CHANNEL
5932 if (bt_prompt(buf))
5933 return (char_u *)_("[Prompt]");
5934#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005935#ifdef FEAT_PROP_POPUP
Bram Moolenaarc6896e22019-05-30 22:32:34 +02005936 if (bt_popup(buf))
5937 return (char_u *)_("[Popup]");
5938#endif
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005939 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 }
Bram Moolenaar21554412017-07-24 21:44:43 +02005941
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 if (buf->b_fname == NULL)
Bram Moolenaar00806bc2020-11-05 19:36:38 +01005943 return buf_get_fname(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 return NULL;
5945}
5946
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947/*
Bram Moolenaar00806bc2020-11-05 19:36:38 +01005948 * Get "buf->b_fname", use "[No Name]" if it is NULL.
5949 */
5950 char_u *
5951buf_get_fname(buf_T *buf)
5952{
5953 if (buf->b_fname == NULL)
5954 return (char_u *)_("[No Name]");
5955 return buf->b_fname;
5956}
5957
5958/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5960 */
5961 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005962set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005964 if (on == curbuf->b_p_bl)
5965 return;
5966
5967 curbuf->b_p_bl = on;
5968 if (on)
5969 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5970 else
5971 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972}
5973
5974/*
5975 * Read the file for "buf" again and check if the contents changed.
5976 * Return TRUE if it changed or this could not be checked.
5977 */
5978 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005979buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980{
5981 buf_T *newbuf;
5982 int differ = TRUE;
5983 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 exarg_T ea;
5986
Bram Moolenaarc667da52019-11-30 20:52:27 +01005987 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5989 if (newbuf == NULL)
5990 return TRUE;
5991
Bram Moolenaarc667da52019-11-30 20:52:27 +01005992 // Force the 'fileencoding' and 'fileformat' to be equal.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 if (prep_exarg(&ea, buf) == FAIL)
5994 {
5995 wipe_buffer(newbuf, FALSE);
5996 return TRUE;
5997 }
5998
Bram Moolenaare76062c2022-11-28 18:51:43 +00005999 // Set curwin/curbuf to buf and save a few things.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00006001 if (curbuf != newbuf)
6002 {
6003 // Failed to find a window for "newbuf".
6004 wipe_buffer(newbuf, FALSE);
6005 return TRUE;
6006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007
Bram Moolenaar4770d092006-01-12 23:22:24 +00006008 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 && readfile(buf->b_ffname, buf->b_fname,
6010 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
6011 &ea, READ_NEW | READ_DUMMY) == OK)
6012 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01006013 // compare the two files line by line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
6015 {
6016 differ = FALSE;
6017 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6018 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
6019 {
6020 differ = TRUE;
6021 break;
6022 }
6023 }
6024 }
6025 vim_free(ea.cmd);
6026
Bram Moolenaarc667da52019-11-30 20:52:27 +01006027 // restore curwin/curbuf and a few other things
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029
Bram Moolenaarc667da52019-11-30 20:52:27 +01006030 if (curbuf != newbuf) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 wipe_buffer(newbuf, FALSE);
6032
6033 return differ;
6034}
6035
6036/*
6037 * Wipe out a buffer and decrement the last buffer number if it was used for
6038 * this buffer. Call this to wipe out a temp buffer that does not contain any
6039 * marks.
6040 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006042wipe_buffer(
6043 buf_T *buf,
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006044 int aucmd) // When TRUE trigger autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045{
6046 if (buf->b_fnum == top_file_num - 1)
6047 --top_file_num;
6048
Bram Moolenaarc667da52019-11-30 20:52:27 +01006049 if (!aucmd) // Don't trigger BufDelete autocommands here.
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006050 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006051
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006052 close_buffer(NULL, buf, DOBUF_WIPE, FALSE, TRUE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006053
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006055 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056}