blob: 93f9245f27f9d042a07da81e65c0f0e4d18f0b72 [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 Moolenaar3a2a60c2023-05-27 18:02:55 +0100221 // Do not sync this buffer yet, may first want to read the file.
222 if (curbuf->b_ml.ml_mfp != NULL)
223 curbuf->b_ml.ml_mfp->mf_dirty = MF_DIRTY_YES_NOSYNC;
224
Bram Moolenaarc667da52019-11-30 20:52:27 +0100225 // The autocommands in readfile() may change the buffer, but only AFTER
226 // reading the file.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200227 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 modified_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229
Bram Moolenaarc667da52019-11-30 20:52:27 +0100230 // mark cursor position as being invalid
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100231 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100233 // A buffer without an actual file should not use the buffer name to read a
234 // file.
Bram Moolenaarc3126192022-08-26 12:58:17 +0100235 if (bt_nofileread(curbuf))
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100236 flags |= READ_NOFILE;
237
Bram Moolenaar2eddbac2022-08-25 12:45:21 +0100238 // Read the file if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 if (curbuf->b_ffname != NULL
240#ifdef FEAT_NETBEANS_INTG
241 && netbeansReadFile
242#endif
243 )
244 {
Bram Moolenaar426dd022016-03-15 15:09:29 +0100245 int old_msg_silent = msg_silent;
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200246#ifdef UNIX
247 int save_bin = curbuf->b_p_bin;
248 int perm;
249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250#ifdef FEAT_NETBEANS_INTG
251 int oldFire = netbeansFireChanges;
252
253 netbeansFireChanges = 0;
254#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200255#ifdef UNIX
256 perm = mch_getperm(curbuf->b_ffname);
Bram Moolenaard569bb02018-08-11 13:57:20 +0200257 if (perm >= 0 && (S_ISFIFO(perm)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200258 || S_ISSOCK(perm)
Bram Moolenaarf04507d2016-08-20 15:05:39 +0200259# ifdef OPEN_CHR_FILES
260 || (S_ISCHR(perm) && is_dev_fd_file(curbuf->b_ffname))
261# endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200262 ))
263 read_fifo = TRUE;
264 if (read_fifo)
265 curbuf->b_p_bin = TRUE;
266#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100267 if (shortmess(SHM_FILEINFO))
268 msg_silent = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200270 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200271 flags | READ_NEW | (read_fifo ? READ_FIFO : 0));
272#ifdef UNIX
273 if (read_fifo)
274 {
275 curbuf->b_p_bin = save_bin;
276 if (retval == OK)
277 retval = read_buffer(FALSE, eap, flags);
278 }
279#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100280 msg_silent = old_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281#ifdef FEAT_NETBEANS_INTG
282 netbeansFireChanges = oldFire;
283#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100284 // Help buffer is filtered.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +0200285 if (bt_help(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286 fix_help_buffer();
287 }
288 else if (read_stdin)
289 {
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200290 int save_bin = curbuf->b_p_bin;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100292 // First read the text in binary mode into the buffer.
293 // Then read from that same buffer and append at the end. This makes
294 // it possible to retry when 'fileformat' or 'fileencoding' was
295 // guessed wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 curbuf->b_p_bin = TRUE;
297 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200298 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
299 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 curbuf->b_p_bin = save_bin;
301 if (retval == OK)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200302 retval = read_buffer(TRUE, eap, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303 }
304
Bram Moolenaar3a2a60c2023-05-27 18:02:55 +0100305 // Can now sync this buffer in ml_sync_all().
306 if (curbuf->b_ml.ml_mfp != NULL
307 && curbuf->b_ml.ml_mfp->mf_dirty == MF_DIRTY_YES_NOSYNC)
308 curbuf->b_ml.ml_mfp->mf_dirty = MF_DIRTY_YES;
309
Bram Moolenaarc667da52019-11-30 20:52:27 +0100310 // if first time loading this buffer, init b_chartab[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 if (curbuf->b_flags & BF_NEVERLOADED)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100312 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 (void)buf_init_chartab(curbuf, FALSE);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100314 parse_cino(curbuf);
315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100317 // Set/reset the Changed flag first, autocmds may change the buffer.
318 // Apply the automatic commands, before processing the modelines.
319 // So the modelines have priority over autocommands.
320 //
Bram Moolenaarc667da52019-11-30 20:52:27 +0100321 // When reading stdin, the buffer contents always needs writing, so set
322 // the changed flag. Unless in readonly mode: "ls | gview -".
323 // When interrupted and 'cpoptions' contains 'i' set changed flag.
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000324 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100325 || modified_was_set // ":set modified" used in autocmd
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100326#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000329 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100331 else if (retval == OK && !read_stdin && !read_fifo)
Bram Moolenaarc024b462019-06-08 18:07:21 +0200332 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaarc667da52019-11-30 20:52:27 +0100333 save_file_ff(curbuf); // keep this fileformat
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334
Bram Moolenaarc667da52019-11-30 20:52:27 +0100335 // Set last_changedtick to avoid triggering a TextChanged autocommand right
336 // after it was added.
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100337 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Christian Brabandtdb3b4462021-10-16 11:58:55 +0100338 curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100339 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100340
Bram Moolenaarc667da52019-11-30 20:52:27 +0100341 // require "!" to overwrite the file, because it wasn't read completely
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342#ifdef FEAT_EVAL
343 if (aborting())
344#else
345 if (got_int)
346#endif
347 curbuf->b_flags |= BF_READERR;
348
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000349#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +0100350 // Need to update automatic folding. Do this before the autocommands,
351 // they may use the fold info.
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000352 foldUpdateAll(curwin);
353#endif
354
Bram Moolenaarc667da52019-11-30 20:52:27 +0100355 // need to set w_topline, unless some autocommand already did that.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 if (!(curwin->w_valid & VALID_TOPLINE))
357 {
358 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100359#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100363#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100365#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367#endif
368
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000369 if (retval != OK)
370 return retval;
371
372 // The autocommands may have changed the current buffer. Apply the
373 // modelines to the correct buffer, if it still exists and is loaded.
374 if (bufref_valid(&old_curbuf) && old_curbuf.br_buf->b_ml.ml_mfp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000376 aco_save_T aco;
377
378 // Go to the buffer that was opened, make sure it is in a window.
379 // If not then skip it.
380 aucmd_prepbuf(&aco, old_curbuf.br_buf);
381 if (curbuf == old_curbuf.br_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000383 do_modelines(0);
384 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000386 if ((flags & READ_NOWINENTER) == 0)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100387#ifdef FEAT_EVAL
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000388 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL,
389 FALSE, curbuf, &retval);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100390#else
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000391 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL,
392 FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100393#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +0000395 // restore curwin/curbuf and a few other things
396 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 }
399
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 return retval;
401}
402
403/*
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200404 * Store "buf" in "bufref" and set the free count.
405 */
406 void
407set_bufref(bufref_T *bufref, buf_T *buf)
408{
409 bufref->br_buf = buf;
Bram Moolenaarfadacf02017-06-19 20:35:32 +0200410 bufref->br_fnum = buf == NULL ? 0 : buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200411 bufref->br_buf_free_count = buf_free_count;
412}
413
414/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200415 * Return TRUE if "bufref->br_buf" points to the same buffer as when
416 * set_bufref() was called and it is a valid buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200417 * Only goes through the buffer list if buf_free_count changed.
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200418 * Also checks if b_fnum is still the same, a :bwipe followed by :new might get
419 * the same allocated memory, but it's a different buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200420 */
421 int
422bufref_valid(bufref_T *bufref)
423{
424 return bufref->br_buf_free_count == buf_free_count
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200425 ? TRUE : buf_valid(bufref->br_buf)
426 && bufref->br_fnum == bufref->br_buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200427}
428
429/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200431 * This can be slow if there are many buffers, prefer using bufref_valid().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432 */
433 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100434buf_valid(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435{
436 buf_T *bp;
437
Bram Moolenaarc667da52019-11-30 20:52:27 +0100438 // Assume that we more often have a recent buffer, start with the last
439 // one.
Bram Moolenaar00d253e2020-04-06 22:13:01 +0200440 FOR_ALL_BUFS_FROM_LAST(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 if (bp == buf)
442 return TRUE;
443 return FALSE;
444}
445
446/*
Bram Moolenaar480778b2016-07-14 22:09:39 +0200447 * A hash table used to quickly lookup a buffer by its number.
448 */
449static hashtab_T buf_hashtab;
450
451 static void
452buf_hashtab_add(buf_T *buf)
453{
454 sprintf((char *)buf->b_key, "%x", buf->b_fnum);
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000455 if (hash_add(&buf_hashtab, buf->b_key, "create buffer") == FAIL)
Bram Moolenaarf1474d82021-12-31 19:59:55 +0000456 emsg(_(e_buffer_cannot_be_registered));
Bram Moolenaar480778b2016-07-14 22:09:39 +0200457}
458
459 static void
460buf_hashtab_remove(buf_T *buf)
461{
462 hashitem_T *hi = hash_find(&buf_hashtab, buf->b_key);
463
464 if (!HASHITEM_EMPTY(hi))
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000465 hash_remove(&buf_hashtab, hi, "close buffer");
Bram Moolenaar480778b2016-07-14 22:09:39 +0200466}
467
Bram Moolenaar94f01952018-09-01 15:30:03 +0200468/*
469 * Return TRUE when buffer "buf" can be unloaded.
470 * Give an error message and return FALSE when the buffer is locked or the
471 * screen is being redrawn and the buffer is in a window.
472 */
473 static int
474can_unload_buffer(buf_T *buf)
475{
476 int can_unload = !buf->b_locked;
477
478 if (can_unload && updating_screen)
479 {
480 win_T *wp;
481
482 FOR_ALL_WINDOWS(wp)
483 if (wp->w_buffer == buf)
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200484 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200485 can_unload = FALSE;
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200486 break;
487 }
Bram Moolenaar94f01952018-09-01 15:30:03 +0200488 }
489 if (!can_unload)
Bram Moolenaaref976322022-09-28 11:48:30 +0100490 {
491 char_u *fname = buf->b_fname != NULL ? buf->b_fname : buf->b_ffname;
492
493 semsg(_(e_attempt_to_delete_buffer_that_is_in_use_str),
494 fname != NULL ? fname : (char_u *)"[No Name]");
495 }
Bram Moolenaar94f01952018-09-01 15:30:03 +0200496 return can_unload;
497}
Bram Moolenaara997b452018-04-17 23:24:06 +0200498
Bram Moolenaar480778b2016-07-14 22:09:39 +0200499/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 * Close the link to a buffer.
501 * "action" is used when there is no longer a window for the buffer.
502 * It can be:
503 * 0 buffer becomes hidden
504 * DOBUF_UNLOAD buffer is unloaded
zeertzjqd392a742023-07-01 20:24:40 +0100505 * DOBUF_DEL buffer is unloaded and removed from buffer list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 * DOBUF_WIPE buffer is unloaded and really deleted
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200507 * DOBUF_WIPE_REUSE idem, and add to buf_reuse list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 * When doing all but the first one on the current buffer, the caller should
509 * get a new buffer very soon!
510 *
511 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100512 *
513 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
514 * cause there to be only one window with this buffer. e.g. when ":quit" is
515 * supposed to close the window but autocommands close all other windows.
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100516 *
517 * When "ignore_abort" is TRUE don't abort even when aborting() returns TRUE.
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100518 *
519 * Return TRUE when we got to the end and b_nwindows was decremented.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 */
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100521 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100522close_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +0100523 win_T *win, // if not NULL, set b_last_cursor
Bram Moolenaar7454a062016-01-30 15:14:10 +0100524 buf_T *buf,
525 int action,
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100526 int abort_if_last,
527 int ignore_abort)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100530 int nwindows;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200531 bufref_T bufref;
Bram Moolenaar3a117e12016-10-30 21:57:52 +0100532 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200533 win_T *the_curwin = curwin;
534 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 int unload_buf = (action != 0);
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200536 int wipe_buf = (action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
537 int del_buf = (action == DOBUF_DEL || wipe_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538
Bram Moolenaarcee52202020-03-11 14:19:58 +0100539 CHECK_CURBUF;
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100540
541 // Force unloading or deleting when 'bufhidden' says so.
542 // The caller must take care of NOT deleting/freeing when 'bufhidden' is
543 // "hide" (otherwise we could never free or delete a buffer).
Bram Moolenaarc667da52019-11-30 20:52:27 +0100544 if (buf->b_p_bh[0] == 'd') // 'bufhidden' == "delete"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200545 {
546 del_buf = TRUE;
547 unload_buf = TRUE;
548 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100549 else if (buf->b_p_bh[0] == 'w') // 'bufhidden' == "wipe"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200550 {
551 del_buf = TRUE;
552 unload_buf = TRUE;
553 wipe_buf = TRUE;
554 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100555 else if (buf->b_p_bh[0] == 'u') // 'bufhidden' == "unload"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200556 unload_buf = TRUE;
557
Bram Moolenaar94053a52017-08-01 21:44:33 +0200558#ifdef FEAT_TERMINAL
Yee Cheng Chin42826332022-10-10 11:46:16 +0100559 // depending on how we get here b_nwindows may already be zero
560 if (bt_terminal(buf) && (buf->b_nwindows <= 1 || del_buf))
Bram Moolenaar94053a52017-08-01 21:44:33 +0200561 {
Bram Moolenaarcee52202020-03-11 14:19:58 +0100562 CHECK_CURBUF;
Bram Moolenaar94053a52017-08-01 21:44:33 +0200563 if (term_job_running(buf->b_term))
564 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200565 if (wipe_buf || unload_buf)
Bram Moolenaara997b452018-04-17 23:24:06 +0200566 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200567 if (!can_unload_buffer(buf))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100568 return FALSE;
Bram Moolenaar94f01952018-09-01 15:30:03 +0200569
Bram Moolenaarc667da52019-11-30 20:52:27 +0100570 // Wiping out or unloading a terminal buffer kills the job.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200571 free_terminal(buf);
Yee Cheng Chin42826332022-10-10 11:46:16 +0100572
573 // A terminal buffer is wiped out when job has finished.
574 del_buf = TRUE;
575 unload_buf = TRUE;
576 wipe_buf = TRUE;
Bram Moolenaara997b452018-04-17 23:24:06 +0200577 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200578 else
579 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100580 // The job keeps running, hide the buffer.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200581 del_buf = FALSE;
582 unload_buf = FALSE;
583 }
584 }
Bram Moolenaarc9f8b842020-11-24 19:36:16 +0100585 else if (buf->b_p_bh[0] == 'h' && !del_buf)
586 {
587 // Hide a terminal buffer.
588 unload_buf = FALSE;
589 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200590 else
591 {
Yee Cheng Chin42826332022-10-10 11:46:16 +0100592 if (del_buf || unload_buf)
593 {
594 // A terminal buffer is wiped out if the job has finished.
595 // We only do this when there's an intention to unload the
596 // buffer. This way, :hide and other similar commands won't
597 // wipe the buffer.
598 del_buf = TRUE;
599 unload_buf = TRUE;
600 wipe_buf = TRUE;
601 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200602 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100603 CHECK_CURBUF;
Bram Moolenaar94053a52017-08-01 21:44:33 +0200604 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606
Bram Moolenaarc667da52019-11-30 20:52:27 +0100607 // Disallow deleting the buffer when it is locked (already being closed or
608 // halfway a command that relies on it). Unloading is allowed.
Bram Moolenaar94f01952018-09-01 15:30:03 +0200609 if ((del_buf || wipe_buf) && !can_unload_buffer(buf))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100610 return FALSE;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200611
Bram Moolenaarc667da52019-11-30 20:52:27 +0100612 // check no autocommands closed the window
Bram Moolenaar4033c552017-09-16 20:54:51 +0200613 if (win != NULL && win_valid_any_tab(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100615 // Set b_last_cursor when closing the last window for the buffer.
616 // Remember the last cursor position and window options of the buffer.
617 // This used to be only for the current window, but then options like
618 // 'foldmethod' may be lost with a ":only" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 if (buf->b_nwindows == 1)
620 set_last_cursor(win);
621 buflist_setfpos(buf, win,
622 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
623 win->w_cursor.col, TRUE);
624 }
625
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200626 set_bufref(&bufref, buf);
627
Bram Moolenaarc667da52019-11-30 20:52:27 +0100628 // When the buffer is no longer in a window, trigger BufWinLeave
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 if (buf->b_nwindows == 1)
630 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200631 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100632 ++buf->b_locked_split;
Bram Moolenaar82404332016-07-10 17:00:38 +0200633 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
634 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200635 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100636 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100637 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200638aucmd_abort:
Bram Moolenaarf1474d82021-12-31 19:59:55 +0000639 emsg(_(e_autocommands_caused_command_to_abort));
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100640 return FALSE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100641 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200642 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100643 --buf->b_locked_split;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200644 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100645 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200646 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647
Bram Moolenaarc667da52019-11-30 20:52:27 +0100648 // When the buffer becomes hidden, but is not unloaded, trigger
649 // BufHidden
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 if (!unload_buf)
651 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200652 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100653 ++buf->b_locked_split;
Bram Moolenaar82404332016-07-10 17:00:38 +0200654 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
655 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200656 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100657 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200658 goto aucmd_abort;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200659 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100660 --buf->b_locked_split;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200661 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100662 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200663 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100665#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100666 // autocmds may abort script processing
667 if (!ignore_abort && aborting())
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100668 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200671
Bram Moolenaarc667da52019-11-30 20:52:27 +0100672 // If the buffer was in curwin and the window has changed, go back to that
673 // window, if it still exists. This avoids that ":edit x" triggering a
674 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200675 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
676 {
677 block_autocmds();
678 goto_tabpage_win(the_curtab, the_curwin);
679 unblock_autocmds();
680 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200681
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 nwindows = buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683
Bram Moolenaarc667da52019-11-30 20:52:27 +0100684 // decrease the link count from windows (unless not in any window)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 if (buf->b_nwindows > 0)
686 --buf->b_nwindows;
687
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100688#ifdef FEAT_DIFF
689 if (diffopt_hiddenoff() && !unload_buf && buf->b_nwindows == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100690 diff_buf_delete(buf); // Clear 'diff' for hidden buffer.
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100691#endif
692
Bram Moolenaarc667da52019-11-30 20:52:27 +0100693 // Return when a window is displaying the buffer or when it's not
694 // unloaded.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100696 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697
Bram Moolenaarc667da52019-11-30 20:52:27 +0100698 // Always remove the buffer when there is no file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 if (buf->b_ffname == NULL)
700 del_buf = TRUE;
701
Bram Moolenaarc667da52019-11-30 20:52:27 +0100702 // When closing the current buffer stop Visual mode before freeing
703 // anything.
Bram Moolenaar4930a762016-09-11 14:39:53 +0200704 if (buf == curbuf && VIsual_active
Bram Moolenaar9a27c7f2016-09-09 12:57:09 +0200705#if defined(EXITFREE)
706 && !entered_free_all_mem
707#endif
708 )
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200709 end_visual_mode();
710
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100711 // Free all things allocated for this buffer.
712 // Also calls the "BufDelete" autocommands when del_buf is TRUE.
713 //
Bram Moolenaarc667da52019-11-30 20:52:27 +0100714 // Remember if we are closing the current buffer. Restore the number of
715 // windows, so that autocommands in buf_freeall() don't get confused.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 is_curbuf = (buf == curbuf);
717 buf->b_nwindows = nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100719 buf_freeall(buf, (del_buf ? BFA_DEL : 0)
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100720 + (wipe_buf ? BFA_WIPE : 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100721 + (ignore_abort ? BFA_IGNORE_ABORT : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722
Bram Moolenaarc667da52019-11-30 20:52:27 +0100723 // Autocommands may have deleted the buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200724 if (!bufref_valid(&bufref))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100725 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100726#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100727 // autocmds may abort script processing
728 if (!ignore_abort && aborting())
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100729 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100732 // It's possible that autocommands change curbuf to the one being deleted.
733 // This might cause the previous curbuf to be deleted unexpectedly. But
734 // in some cases it's OK to delete the curbuf, because a new one is
735 // obtained anyway. Therefore only return if curbuf changed to the
736 // deleted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 if (buf == curbuf && !is_curbuf)
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100738 return FALSE;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200739
Bram Moolenaar4033c552017-09-16 20:54:51 +0200740 if (win_valid_any_tab(win) && win->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100741 win->w_buffer = NULL; // make sure we don't use the buffer now
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200742
Bram Moolenaarc667da52019-11-30 20:52:27 +0100743 // Autocommands may have opened or closed windows for this buffer.
744 // Decrement the count for the close we do here.
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200745 if (buf->b_nwindows > 0)
746 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 /*
749 * Remove the buffer from the list.
750 */
751 if (wipe_buf)
752 {
Bram Moolenaar347538f2022-03-26 16:28:06 +0000753 // Do not wipe out the buffer if it is used in a window.
754 if (buf->b_nwindows > 0)
755 return FALSE;
756
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200757 if (action == DOBUF_WIPE_REUSE)
758 {
759 // we can re-use this buffer number, store it
760 if (buf_reuse.ga_itemsize == 0)
761 ga_init2(&buf_reuse, sizeof(int), 50);
762 if (ga_grow(&buf_reuse, 1) == OK)
763 ((int *)buf_reuse.ga_data)[buf_reuse.ga_len++] = buf->b_fnum;
764 }
Bram Moolenaar3d6014f2018-10-11 19:27:47 +0200765 if (buf->b_sfname != buf->b_ffname)
766 VIM_CLEAR(buf->b_sfname);
767 else
768 buf->b_sfname = NULL;
769 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 if (buf->b_prev == NULL)
771 firstbuf = buf->b_next;
772 else
773 buf->b_prev->b_next = buf->b_next;
774 if (buf->b_next == NULL)
775 lastbuf = buf->b_prev;
776 else
777 buf->b_next->b_prev = buf->b_prev;
778 free_buffer(buf);
779 }
780 else
781 {
782 if (del_buf)
783 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100784 // Free all internal variables and reset option values, to make
785 // ":bdel" compatible with Vim 5.7.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 free_buffer_stuff(buf, TRUE);
787
Bram Moolenaarc667da52019-11-30 20:52:27 +0100788 // Make it look like a new buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
790
Bram Moolenaarc667da52019-11-30 20:52:27 +0100791 // Init the options when loaded again.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 buf->b_p_initialized = FALSE;
793 }
794 buf_clear_file(buf);
795 if (del_buf)
796 buf->b_p_bl = FALSE;
797 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100798 // NOTE: at this point "curbuf" may be invalid!
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100799 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800}
801
802/*
803 * Make buffer not contain a file.
804 */
805 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100806buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807{
808 buf->b_ml.ml_line_count = 1;
Bram Moolenaarc024b462019-06-08 18:07:21 +0200809 unchanged(buf, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 buf->b_shortname = FALSE;
Bram Moolenaar15775372022-10-29 20:01:52 +0100811 buf->b_p_eof = FALSE;
812 buf->b_start_eof = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 buf->b_p_eol = TRUE;
814 buf->b_start_eol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000816 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 buf->b_ml.ml_mfp = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +0100818 buf->b_ml.ml_flags = ML_EMPTY; // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819#ifdef FEAT_NETBEANS_INTG
820 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821#endif
822}
823
824/*
825 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200826 * the file. Careful: get here with "curwin" NULL when exiting.
827 * flags:
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100828 * BFA_DEL buffer is going to be deleted
829 * BFA_WIPE buffer is going to be wiped out
830 * BFA_KEEP_UNDO do not free undo information
831 * BFA_IGNORE_ABORT don't abort even when aborting() returns TRUE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100834buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200837 bufref_T bufref;
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200838 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaar5a497892016-09-03 16:29:04 +0200839 win_T *the_curwin = curwin;
840 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841
Bram Moolenaarc667da52019-11-30 20:52:27 +0100842 // Make sure the buffer isn't closed by autocommands.
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200843 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100844 ++buf->b_locked_split;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200845 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200846 if (buf->b_ml.ml_mfp != NULL)
847 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200848 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
849 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200850 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100851 // autocommands deleted the buffer
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200852 return;
853 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200854 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200856 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
857 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200858 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100859 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 return;
861 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200862 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200864 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
865 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200866 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100867 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 return;
869 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200870 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100871 --buf->b_locked_split;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200872
Bram Moolenaarc667da52019-11-30 20:52:27 +0100873 // If the buffer was in curwin and the window has changed, go back to that
874 // window, if it still exists. This avoids that ":edit x" triggering a
875 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaar5a497892016-09-03 16:29:04 +0200876 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
877 {
878 block_autocmds();
879 goto_tabpage_win(the_curtab, the_curwin);
880 unblock_autocmds();
881 }
Bram Moolenaar5a497892016-09-03 16:29:04 +0200882
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100883#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100884 // autocmds may abort script processing
885 if ((flags & BFA_IGNORE_ABORT) == 0 && aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100889 // It's possible that autocommands change curbuf to the one being deleted.
890 // This might cause curbuf to be deleted unexpectedly. But in some cases
891 // it's OK to delete the curbuf, because a new one is obtained anyway.
892 // Therefore only return if curbuf changed to the deleted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 if (buf == curbuf && !is_curbuf)
894 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895#ifdef FEAT_DIFF
Bram Moolenaarc667da52019-11-30 20:52:27 +0100896 diff_buf_delete(buf); // Can't use 'diff' for unloaded buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200898#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100899 // Remove any ownsyntax, unless exiting.
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200900 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaar89c71222011-11-30 15:40:56 +0100901 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200902#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000903
904#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +0100905 // No folds in an empty buffer.
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000906 {
907 win_T *win;
908 tabpage_T *tp;
909
910 FOR_ALL_TAB_WINDOWS(tp, win)
911 if (win->w_buffer == buf)
912 clearFolding(win);
913 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000914#endif
915
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916#ifdef FEAT_TCL
917 tcl_buffer_free(buf);
918#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100919 ml_close(buf, TRUE); // close and delete the memline/memfile
920 buf->b_ml.ml_line_count = 0; // no lines in buffer
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200921 if ((flags & BFA_KEEP_UNDO) == 0)
922 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100923 u_blockfree(buf); // free the memory allocated for undo
924 u_clearall(buf); // reset all undo information
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100927 syntax_clear(&buf->b_s); // reset syntax info
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100929#ifdef FEAT_PROP_POPUP
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100930 clear_buf_prop_types(buf);
931#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100932 buf->b_flags &= ~BF_READERR; // a read error is no longer relevant
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933}
934
935/*
936 * Free a buffer structure and the things it contains related to the buffer
937 * itself (not the file, that must have been done already).
938 */
939 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100940free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200942 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200944#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100945 // b:changedtick uses an item in buf_T, remove it now
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000946 dictitem_remove(buf->b_vars, (dictitem_T *)&buf->b_ct_di, "free buffer");
Bram Moolenaar429fa852013-04-15 12:27:36 +0200947 unref_var_dict(buf->b_vars);
Bram Moolenaar86173482019-10-01 17:02:16 +0200948 remove_listeners(buf);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200949#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200950#ifdef FEAT_LUA
951 lua_buffer_free(buf);
952#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000953#ifdef FEAT_MZSCHEME
954 mzscheme_buffer_free(buf);
955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956#ifdef FEAT_PERL
957 perl_buf_free(buf);
958#endif
959#ifdef FEAT_PYTHON
960 python_buffer_free(buf);
961#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200962#ifdef FEAT_PYTHON3
963 python3_buffer_free(buf);
964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965#ifdef FEAT_RUBY
966 ruby_buffer_free(buf);
967#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200968#ifdef FEAT_JOB_CHANNEL
969 channel_buffer_free(buf);
970#endif
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200971#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +0200972 free_terminal(buf);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200973#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +0200974#ifdef FEAT_JOB_CHANNEL
975 vim_free(buf->b_prompt_text);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200976 free_callback(&buf->b_prompt_callback);
Bram Moolenaar86173482019-10-01 17:02:16 +0200977 free_callback(&buf->b_prompt_interrupt);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200978#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200979
980 buf_hashtab_remove(buf);
981
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200982 aubuflocal_remove(buf);
983
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200984 if (autocmd_busy)
985 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100986 // Do not free the buffer structure while autocommands are executing,
987 // it's still needed. Free it when autocmd_busy is reset.
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200988 buf->b_next = au_pending_free_buf;
989 au_pending_free_buf = buf;
990 }
991 else
Bram Moolenaarcee52202020-03-11 14:19:58 +0100992 {
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200993 vim_free(buf);
Bram Moolenaarcee52202020-03-11 14:19:58 +0100994 if (curbuf == buf)
995 curbuf = NULL; // make clear it's not to be used
996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997}
998
999/*
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001000 * Initializes b:changedtick.
Bram Moolenaar79518e22017-02-17 16:31:35 +01001001 */
1002 static void
1003init_changedtick(buf_T *buf)
1004{
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001005 dictitem_T *di = (dictitem_T *)&buf->b_ct_di;
Bram Moolenaar79518e22017-02-17 16:31:35 +01001006
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001007 di->di_flags = DI_FLAGS_FIX | DI_FLAGS_RO;
1008 di->di_tv.v_type = VAR_NUMBER;
1009 di->di_tv.v_lock = VAR_FIXED;
1010 di->di_tv.vval.v_number = 0;
1011
Bram Moolenaar92769c32017-02-25 15:41:37 +01001012#ifdef FEAT_EVAL
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001013 STRCPY(buf->b_ct_di.di_key, "changedtick");
1014 (void)dict_add(buf->b_vars, di);
Bram Moolenaar92769c32017-02-25 15:41:37 +01001015#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01001016}
1017
1018/*
Bram Moolenaarc3126192022-08-26 12:58:17 +01001019 * Free the b_wininfo list for buffer "buf".
1020 */
1021 static void
1022clear_wininfo(buf_T *buf)
1023{
1024 wininfo_T *wip;
1025
1026 while (buf->b_wininfo != NULL)
1027 {
1028 wip = buf->b_wininfo;
1029 buf->b_wininfo = wip->wi_next;
1030 free_wininfo(wip);
1031 }
1032}
1033
1034/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
1036 */
1037 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001038free_buffer_stuff(
1039 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001040 int free_options) // free options as well
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041{
1042 if (free_options)
1043 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001044 clear_wininfo(buf); // including window-local options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +02001046#ifdef FEAT_SPELL
1047 ga_clear(&buf->b_s.b_langp);
1048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 }
1050#ifdef FEAT_EVAL
Bram Moolenaar79518e22017-02-17 16:31:35 +01001051 {
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001052 varnumber_T tick = CHANGEDTICK(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001053
Bram Moolenaarc667da52019-11-30 20:52:27 +01001054 vars_clear(&buf->b_vars->dv_hashtab); // free all buffer variables
Bram Moolenaar79518e22017-02-17 16:31:35 +01001055 hash_init(&buf->b_vars->dv_hashtab);
1056 init_changedtick(buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001057 CHANGEDTICK(buf) = tick;
Bram Moolenaarf10997a2020-01-03 21:00:02 +01001058 remove_listeners(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060#endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001061 uc_clear(&buf->b_ucmds); // clear local user commands
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062#ifdef FEAT_SIGNS
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001063 buf_delete_signs(buf, (char_u *)"*"); // delete any signs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00001065#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001066 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00001067#endif
Bram Moolenaar0c740e72022-07-25 19:07:04 +01001068#ifdef FEAT_PROP_POPUP
1069 ga_clear_strings(&buf->b_textprop_text);
1070#endif
zeertzjqc207fd22022-06-29 10:37:40 +01001071 map_clear_mode(buf, MAP_ALL_MODES, TRUE, FALSE); // clear local mappings
1072 map_clear_mode(buf, MAP_ALL_MODES, TRUE, TRUE); // clear local abbrevs
Bram Moolenaard23a8232018-02-10 18:45:26 +01001073 VIM_CLEAR(buf->b_start_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074}
1075
1076/*
Bram Moolenaar4882d982020-10-25 17:55:09 +01001077 * Free one wininfo_T.
1078 */
1079 void
1080free_wininfo(wininfo_T *wip)
1081{
1082 if (wip->wi_optset)
1083 {
1084 clear_winopt(&wip->wi_opt);
1085#ifdef FEAT_FOLDING
1086 deleteFoldRecurse(&wip->wi_folds);
1087#endif
1088 }
1089 vim_free(wip);
1090}
1091
1092/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 * Go to another buffer. Handles the result of the ATTENTION dialog.
1094 */
1095 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001096goto_buffer(
1097 exarg_T *eap,
1098 int start,
1099 int dir,
1100 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001102 bufref_T old_curbuf;
Bram Moolenaar188639d2022-04-04 16:57:21 +01001103 int save_sea = swap_exists_action;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001104
1105 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106
Bram Moolenaar188639d2022-04-04 16:57:21 +01001107 if (swap_exists_action == SEA_NONE)
1108 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
1110 start, dir, count, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
1112 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001113#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001114 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001115
Bram Moolenaarc667da52019-11-30 20:52:27 +01001116 // Reset the error/interrupt/exception state here so that
1117 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001118 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001119#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001120
Bram Moolenaarc667da52019-11-30 20:52:27 +01001121 // Quitting means closing the split window, nothing else.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 win_close(curwin, TRUE);
Bram Moolenaar188639d2022-04-04 16:57:21 +01001123 swap_exists_action = save_sea;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001124 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001125
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001126#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001127 // Restore the error/interrupt/exception state if not discarded by a
1128 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001129 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 }
1132 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001133 handle_swap_exists(&old_curbuf);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001134}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136/*
1137 * Handle the situation of swap_exists_action being set.
1138 * It is allowed for "old_curbuf" to be NULL or invalid.
1139 */
1140 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001141handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142{
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001143#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001144 cleanup_T cs;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001145#endif
1146#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001147 long old_tw = curbuf->b_p_tw;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001148#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001149 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001150
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 if (swap_exists_action == SEA_QUIT)
1152 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001153#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001154 // Reset the error/interrupt/exception state here so that
1155 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001156 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001157#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001158
Bram Moolenaarc667da52019-11-30 20:52:27 +01001159 // User selected Quit at ATTENTION prompt. Go back to previous
1160 // buffer. If that buffer is gone or the same as the current one,
1161 // open a new, empty buffer.
1162 swap_exists_action = SEA_NONE; // don't want it again
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001163 swap_exists_did_quit = TRUE;
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001164 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001165 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1166 || old_curbuf->br_buf == curbuf)
Bram Moolenaar1d97efc2021-07-04 13:27:11 +02001167 {
1168 // Block autocommands here because curwin->w_buffer is NULL.
1169 block_autocmds();
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001170 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar1d97efc2021-07-04 13:27:11 +02001171 unblock_autocmds();
1172 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001173 else
1174 buf = old_curbuf->br_buf;
1175 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001176 {
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001177 int old_msg_silent = msg_silent;
1178
1179 if (shortmess(SHM_FILEINFO))
1180 msg_silent = 1; // prevent fileinfo message
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001181 enter_buffer(buf);
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001182 // restore msg_silent, so that the command line will be shown
1183 msg_silent = old_msg_silent;
1184
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001185#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001186 if (old_tw != curbuf->b_p_tw)
1187 check_colorcolumn(curwin);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001188#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001189 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001190 // If "old_curbuf" is NULL we are in big trouble here...
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001191
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001192#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001193 // Restore the error/interrupt/exception state if not discarded by a
1194 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001195 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 }
1198 else if (swap_exists_action == SEA_RECOVER)
1199 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001200#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001201 // Reset the error/interrupt/exception state here so that
1202 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001203 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001204#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001205
Bram Moolenaarc667da52019-11-30 20:52:27 +01001206 // User selected Recover at ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 msg_scroll = TRUE;
Bram Moolenaar99499b12019-05-23 21:35:48 +02001208 ml_recover(FALSE);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001209 msg_puts("\n"); // don't overwrite the last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001211 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001212
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001213#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001214 // Restore the error/interrupt/exception state if not discarded by a
1215 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001216 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001217#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 }
1219 swap_exists_action = SEA_NONE;
1220}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222/*
Bram Moolenaara02471e2014-01-10 16:43:14 +01001223 * Make the current buffer empty.
1224 * Used when it is wiped out and it's the last buffer.
1225 */
1226 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001227empty_curbuf(
1228 int close_others,
1229 int forceit,
1230 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001231{
1232 int retval;
1233 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001234 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001235
1236 if (action == DOBUF_UNLOAD)
1237 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001238 emsg(_(e_cannot_unload_last_buffer));
Bram Moolenaara02471e2014-01-10 16:43:14 +01001239 return FAIL;
1240 }
1241
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001242 set_bufref(&bufref, buf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001243 if (close_others)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001244 // Close any other windows on this buffer, then make it empty.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001245 close_windows(buf, TRUE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001246
1247 setpcmark();
1248 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1249 forceit ? ECMD_FORCEIT : 0, curwin);
1250
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001251 // do_ecmd() may create a new buffer, then we have to delete
1252 // the old one. But do_ecmd() may have done that already, check
1253 // if the buffer still exists.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001254 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001255 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001256 if (!close_others)
1257 need_fileinfo = FALSE;
1258 return retval;
1259}
Bram Moolenaar94f01952018-09-01 15:30:03 +02001260
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261/*
1262 * Implementation of the commands for the buffer list.
1263 *
1264 * action == DOBUF_GOTO go to specified buffer
1265 * action == DOBUF_SPLIT split window and go to specified buffer
1266 * action == DOBUF_UNLOAD unload specified buffer(s)
1267 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1268 * action == DOBUF_WIPE delete specified buffer(s) really
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001269 * action == DOBUF_WIPE_REUSE idem, and add number to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 *
1271 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1272 * start == DOBUF_FIRST go to "count" buffer from first buffer
1273 * start == DOBUF_LAST go to "count" buffer from last buffer
1274 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1275 *
1276 * Return FAIL or OK.
1277 */
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001278 static int
1279do_buffer_ext(
Bram Moolenaar7454a062016-01-30 15:14:10 +01001280 int action,
1281 int start,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001282 int dir, // FORWARD or BACKWARD
1283 int count, // buffer number or number of buffers
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001284 int flags) // DOBUF_FORCEIT etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285{
1286 buf_T *buf;
1287 buf_T *bp;
1288 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001289 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290
1291 switch (start)
1292 {
1293 case DOBUF_FIRST: buf = firstbuf; break;
1294 case DOBUF_LAST: buf = lastbuf; break;
1295 default: buf = curbuf; break;
1296 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001297 if (start == DOBUF_MOD) // find next modified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 {
1299 while (count-- > 0)
1300 {
1301 do
1302 {
1303 buf = buf->b_next;
1304 if (buf == NULL)
1305 buf = firstbuf;
1306 }
1307 while (buf != curbuf && !bufIsChanged(buf));
1308 }
1309 if (!bufIsChanged(buf))
1310 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001311 emsg(_(e_no_modified_buffer_found));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 return FAIL;
1313 }
1314 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001315 else if (start == DOBUF_FIRST && count) // find specified buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 {
1317 while (buf != NULL && buf->b_fnum != count)
1318 buf = buf->b_next;
1319 }
1320 else
1321 {
1322 bp = NULL;
1323 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1324 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001325 // remember the buffer where we start, we come back there when all
1326 // buffers are unlisted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 if (bp == NULL)
1328 bp = buf;
1329 if (dir == FORWARD)
1330 {
1331 buf = buf->b_next;
1332 if (buf == NULL)
1333 buf = firstbuf;
1334 }
1335 else
1336 {
1337 buf = buf->b_prev;
1338 if (buf == NULL)
1339 buf = lastbuf;
1340 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001341 // don't count unlisted buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 if (unload || buf->b_p_bl)
1343 {
1344 --count;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001345 bp = NULL; // use this buffer as new starting point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 }
1347 if (bp == buf)
1348 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001349 // back where we started, didn't find anything.
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001350 emsg(_(e_there_is_no_listed_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 return FAIL;
1352 }
1353 }
1354 }
1355
Bram Moolenaarc667da52019-11-30 20:52:27 +01001356 if (buf == NULL) // could not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 {
1358 if (start == DOBUF_FIRST)
1359 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001360 // don't warn when deleting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 if (!unload)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001362 semsg(_(e_buffer_nr_does_not_exist), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 }
1364 else if (dir == FORWARD)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001365 emsg(_(e_cannot_go_beyond_last_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 else
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001367 emsg(_(e_cannot_go_before_first_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 return FAIL;
1369 }
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001370#ifdef FEAT_PROP_POPUP
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001371 if ((flags & DOBUF_NOPOPUP) && bt_popup(buf) && !bt_terminal(buf))
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001372 return OK;
1373#endif
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01001374 if ((action == DOBUF_GOTO || action == DOBUF_SPLIT)
1375 && (buf->b_flags & BF_DUMMY))
1376 {
1377 // disallow navigating to the dummy buffer
1378 semsg(_(e_buffer_nr_does_not_exist), count);
1379 return FAIL;
1380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381
1382#ifdef FEAT_GUI
1383 need_mouse_correct = TRUE;
1384#endif
1385
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001387 * delete buffer "buf" from memory and/or the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 */
1389 if (unload)
1390 {
1391 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001392 bufref_T bufref;
1393
Bram Moolenaar94f01952018-09-01 15:30:03 +02001394 if (!can_unload_buffer(buf))
Bram Moolenaara997b452018-04-17 23:24:06 +02001395 return FAIL;
Bram Moolenaara997b452018-04-17 23:24:06 +02001396
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001397 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398
Bram Moolenaarc667da52019-11-30 20:52:27 +01001399 // When unloading or deleting a buffer that's already unloaded and
1400 // unlisted: fail silently.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001401 if (action != DOBUF_WIPE && action != DOBUF_WIPE_REUSE
1402 && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 return FAIL;
1404
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001405 if ((flags & DOBUF_FORCEIT) == 0 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001407#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001408 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001410# ifdef FEAT_TERMINAL
1411 if (term_job_running(buf->b_term))
1412 {
1413 if (term_confirm_stop(buf) == FAIL)
1414 return FAIL;
1415 }
1416 else
1417# endif
1418 {
1419 dialog_changed(buf, FALSE);
1420 if (!bufref_valid(&bufref))
1421 // Autocommand deleted buffer, oops! It's not changed
1422 // now.
1423 return FAIL;
1424 // If it's still changed fail silently, the dialog already
1425 // mentioned why it fails.
1426 if (bufIsChanged(buf))
1427 return FAIL;
1428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001430 else
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001431#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001433 no_write_message_buf(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 return FAIL;
1435 }
1436 }
1437
Bram Moolenaarc667da52019-11-30 20:52:27 +01001438 // When closing the current buffer stop Visual mode.
Bram Moolenaar4930a762016-09-11 14:39:53 +02001439 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001440 end_visual_mode();
1441
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001442 // If deleting the last (listed) buffer, make it empty.
1443 // The last (listed) buffer cannot be unloaded.
Bram Moolenaar29323592016-07-24 22:04:11 +02001444 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 if (bp->b_p_bl && bp != buf)
1446 break;
1447 if (bp == NULL && buf == curbuf)
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001448 return empty_curbuf(TRUE, (flags & DOBUF_FORCEIT), action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001450 // If the deleted buffer is the current one, close the current window
1451 // (unless it's the only window). Repeat this so long as we end up in
1452 // a window with this buffer.
Bram Moolenaarf740b292006-02-16 22:11:02 +00001453 while (buf == curbuf
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001454 && !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
Bram Moolenaar459ca562016-11-10 18:16:33 +01001455 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001456 {
1457 if (win_close(curwin, FALSE) == FAIL)
1458 break;
1459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001461 // If the buffer to be deleted is not the current one, delete it here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 if (buf != curbuf)
1463 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001464 close_windows(buf, FALSE);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001465 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows <= 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001466 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 return OK;
1468 }
1469
1470 /*
1471 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001472 * There should be another, otherwise it would have been handled
1473 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001474 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 * Then prefer the buffer we most recently visited.
1476 * Else try to find one that is loaded, after the current buffer,
1477 * then before the current buffer.
1478 * Finally use any buffer.
1479 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001480 buf = NULL; // selected buffer
1481 bp = NULL; // used when no loaded buffer found
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001482 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1483 buf = au_new_curbuf.br_buf;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001484 else if (curwin->w_jumplistlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 {
1486 int jumpidx;
1487
1488 jumpidx = curwin->w_jumplistidx - 1;
1489 if (jumpidx < 0)
1490 jumpidx = curwin->w_jumplistlen - 1;
1491
1492 forward = jumpidx;
1493 while (jumpidx != curwin->w_jumplistidx)
1494 {
1495 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1496 if (buf != NULL)
1497 {
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001498 // Skip current and unlisted bufs. Also skip a quickfix
1499 // buffer, it might be deleted soon.
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001500 if (buf == curbuf || !buf->b_p_bl || bt_quickfix(buf))
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001501 buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 else if (buf->b_ml.ml_mfp == NULL)
1503 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001504 // skip unloaded buf, but may keep it for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 if (bp == NULL)
1506 bp = buf;
1507 buf = NULL;
1508 }
1509 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001510 if (buf != NULL) // found a valid buffer: stop searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001512 // advance to older entry in jump list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1514 break;
1515 if (--jumpidx < 0)
1516 jumpidx = curwin->w_jumplistlen - 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001517 if (jumpidx == forward) // List exhausted for sure
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 break;
1519 }
1520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521
Bram Moolenaarc667da52019-11-30 20:52:27 +01001522 if (buf == NULL) // No previous buffer, Try 2'nd approach
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 {
1524 forward = TRUE;
1525 buf = curbuf->b_next;
1526 for (;;)
1527 {
1528 if (buf == NULL)
1529 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001530 if (!forward) // tried both directions
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 break;
1532 buf = curbuf->b_prev;
1533 forward = FALSE;
1534 continue;
1535 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001536 // in non-help buffer, try to skip help buffers, and vv
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001537 if (buf->b_help == curbuf->b_help && buf->b_p_bl
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001538 && !bt_quickfix(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001540 if (buf->b_ml.ml_mfp != NULL) // found loaded buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001542 if (bp == NULL) // remember unloaded buf for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 bp = buf;
1544 }
1545 if (forward)
1546 buf = buf->b_next;
1547 else
1548 buf = buf->b_prev;
1549 }
1550 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001551 if (buf == NULL) // No loaded buffer, use unloaded one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 buf = bp;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001553 if (buf == NULL) // No loaded buffer, find listed one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001555 FOR_ALL_BUFFERS(buf)
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001556 if (buf->b_p_bl && buf != curbuf && !bt_quickfix(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 break;
1558 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001559 if (buf == NULL) // Still no buffer, just take one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 {
1561 if (curbuf->b_next != NULL)
1562 buf = curbuf->b_next;
1563 else
1564 buf = curbuf->b_prev;
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001565 if (bt_quickfix(buf))
1566 buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 }
1568 }
1569
Bram Moolenaara02471e2014-01-10 16:43:14 +01001570 if (buf == NULL)
1571 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001572 // Autocommands must have wiped out all other buffers. Only option
1573 // now is to make the current buffer empty.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001574 return empty_curbuf(FALSE, (flags & DOBUF_FORCEIT), action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001575 }
1576
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001578 * make "buf" the current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001580 if (action == DOBUF_SPLIT) // split window first
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 {
Yegappan Lakshmanane42c27d2023-05-14 17:24:22 +01001582 // If 'switchbuf' is set jump to the window containing "buf".
1583 if (swbuf_goto_win_with_buf(buf) != NULL)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001584 return OK;
Yegappan Lakshmanane42c27d2023-05-14 17:24:22 +01001585
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 return FAIL;
1588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589
Bram Moolenaarc667da52019-11-30 20:52:27 +01001590 // go to current buffer - nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 if (buf == curbuf)
1592 return OK;
1593
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001594 // Check if the current buffer may be abandoned.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001595 if (action == DOBUF_GOTO && !can_abandon(curbuf, (flags & DOBUF_FORCEIT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 {
1597#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001598 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001600# ifdef FEAT_TERMINAL
1601 if (term_job_running(curbuf->b_term))
1602 {
1603 if (term_confirm_stop(curbuf) == FAIL)
1604 return FAIL;
1605 // Manually kill the terminal here because this command will
1606 // hide it otherwise.
1607 free_terminal(curbuf);
1608 }
1609 else
1610# endif
1611 {
1612 bufref_T bufref;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001613
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001614 set_bufref(&bufref, buf);
1615 dialog_changed(curbuf, FALSE);
1616 if (!bufref_valid(&bufref))
1617 // Autocommand deleted buffer, oops!
1618 return FAIL;
1619
1620 if (bufIsChanged(curbuf))
1621 {
1622 no_write_message();
1623 return FAIL;
1624 }
1625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 }
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001627 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628#endif
1629 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001630 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 return FAIL;
1632 }
1633 }
1634
Bram Moolenaarc667da52019-11-30 20:52:27 +01001635 // Go to the other buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 set_curbuf(buf, action);
1637
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 if (action == DOBUF_SPLIT)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001639 RESET_BINDING(curwin); // reset 'scrollbind' and 'cursorbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001641#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001642 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 return FAIL;
1644#endif
1645
1646 return OK;
1647}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001649 int
1650do_buffer(
1651 int action,
1652 int start,
1653 int dir, // FORWARD or BACKWARD
1654 int count, // buffer number or number of buffers
1655 int forceit) // TRUE when using !
1656{
1657 return do_buffer_ext(action, start, dir, count,
1658 forceit ? DOBUF_FORCEIT : 0);
1659}
1660
1661/*
1662 * do_bufdel() - delete or unload buffer(s)
1663 *
1664 * addr_count == 0: ":bdel" - delete current buffer
1665 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1666 * buffer "end_bnr", then any other arguments.
1667 * addr_count == 2: ":N,N bdel" - delete buffers in range
1668 *
1669 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1670 * DOBUF_DEL (":bdel")
1671 *
1672 * Returns error message or NULL
1673 */
1674 char *
1675do_bufdel(
1676 int command,
1677 char_u *arg, // pointer to extra arguments
1678 int addr_count,
1679 int start_bnr, // first buffer number in a range
1680 int end_bnr, // buffer nr or last buffer nr in a range
1681 int forceit)
1682{
1683 int do_current = 0; // delete current buffer?
1684 int deleted = 0; // number of buffers deleted
1685 char *errormsg = NULL; // return value
1686 int bnr; // buffer number
1687 char_u *p;
1688
1689 if (addr_count == 0)
1690 {
1691 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1692 }
1693 else
1694 {
1695 if (addr_count == 2)
1696 {
1697 if (*arg) // both range and argument is not allowed
Bram Moolenaar74409f62022-01-01 15:58:22 +00001698 return ex_errmsg(e_trailing_characters_str, arg);
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001699 bnr = start_bnr;
1700 }
1701 else // addr_count == 1
1702 bnr = end_bnr;
1703
1704 for ( ;!got_int; ui_breakcheck())
1705 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001706 // Delete the current buffer last, otherwise when the
1707 // current buffer is deleted, the next buffer becomes
1708 // the current one and will be loaded, which may then
1709 // also be deleted, etc.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001710 if (bnr == curbuf->b_fnum)
1711 do_current = bnr;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00001712 else if (do_buffer_ext(command, DOBUF_FIRST, FORWARD, bnr,
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001713 DOBUF_NOPOPUP | (forceit ? DOBUF_FORCEIT : 0)) == OK)
1714 ++deleted;
1715
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001716 // find next buffer number to delete/unload
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001717 if (addr_count == 2)
1718 {
1719 if (++bnr > end_bnr)
1720 break;
1721 }
1722 else // addr_count == 1
1723 {
1724 arg = skipwhite(arg);
1725 if (*arg == NUL)
1726 break;
1727 if (!VIM_ISDIGIT(*arg))
1728 {
1729 p = skiptowhite_esc(arg);
1730 bnr = buflist_findpat(arg, p,
1731 command == DOBUF_WIPE || command == DOBUF_WIPE_REUSE,
1732 FALSE, FALSE);
1733 if (bnr < 0) // failed
1734 break;
1735 arg = p;
1736 }
1737 else
1738 bnr = getdigits(&arg);
1739 }
1740 }
1741 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1742 FORWARD, do_current, forceit) == OK)
1743 ++deleted;
1744
1745 if (deleted == 0)
1746 {
1747 if (command == DOBUF_UNLOAD)
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001748 STRCPY(IObuff, _(e_no_buffers_were_unloaded));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001749 else if (command == DOBUF_DEL)
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001750 STRCPY(IObuff, _(e_no_buffers_were_deleted));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001751 else
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001752 STRCPY(IObuff, _(e_no_buffers_were_wiped_out));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001753 errormsg = (char *)IObuff;
1754 }
1755 else if (deleted >= p_report)
1756 {
1757 if (command == DOBUF_UNLOAD)
1758 smsg(NGETTEXT("%d buffer unloaded",
1759 "%d buffers unloaded", deleted), deleted);
1760 else if (command == DOBUF_DEL)
1761 smsg(NGETTEXT("%d buffer deleted",
1762 "%d buffers deleted", deleted), deleted);
1763 else
1764 smsg(NGETTEXT("%d buffer wiped out",
1765 "%d buffers wiped out", deleted), deleted);
1766 }
1767 }
1768
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001769 return errormsg;
1770}
1771
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772/*
1773 * Set current buffer to "buf". Executes autocommands and closes current
1774 * buffer. "action" tells how to close the current buffer:
1775 * DOBUF_GOTO free or hide it
1776 * DOBUF_SPLIT nothing
1777 * DOBUF_UNLOAD unload it
1778 * DOBUF_DEL delete it
1779 * DOBUF_WIPE wipe it out
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001780 * DOBUF_WIPE_REUSE wipe it out and add to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 */
1782 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001783set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784{
1785 buf_T *prevbuf;
1786 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001787 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001788#ifdef FEAT_SYN_HL
1789 long old_tw = curbuf->b_p_tw;
1790#endif
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001791 bufref_T newbufref;
1792 bufref_T prevbufref;
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001793 int valid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794
1795 setpcmark();
Bram Moolenaare1004402020-10-24 20:49:43 +02001796 if ((cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001797 curwin->w_alt_fnum = curbuf->b_fnum; // remember alternate file
1798 buflist_altfpos(curwin); // remember curpos
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799
Bram Moolenaarc667da52019-11-30 20:52:27 +01001800 // Don't restart Select mode after switching to another buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802
Bram Moolenaarc667da52019-11-30 20:52:27 +01001803 // close_windows() or apply_autocmds() may change curbuf and wipe out "buf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 prevbuf = curbuf;
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001805 set_bufref(&prevbufref, prevbuf);
1806 set_bufref(&newbufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807
Bram Moolenaar983d83f2021-02-07 12:12:43 +01001808 // Autocommands may delete the current buffer and/or the buffer we want to
1809 // go to. In those cases don't close the buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001810 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001811 || (bufref_valid(&prevbufref)
1812 && bufref_valid(&newbufref)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001813#ifdef FEAT_EVAL
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001814 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001816 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001818#ifdef FEAT_SYN_HL
1819 if (prevbuf == curwin->w_buffer)
1820 reset_synblock(curwin);
1821#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001823 close_windows(prevbuf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001824#if defined(FEAT_EVAL)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001825 if (bufref_valid(&prevbufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826#else
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001827 if (bufref_valid(&prevbufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001829 {
Bram Moolenaare25865a2012-07-06 16:22:02 +02001830 win_T *previouswin = curwin;
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001831
Bram Moolenaare615db02022-01-20 21:00:54 +00001832 // Do not sync when in Insert mode and the buffer is open in
1833 // another window, might be a timer doing something in another
1834 // window.
1835 if (prevbuf == curbuf
Bram Moolenaar24959102022-05-07 20:01:16 +01001836 && ((State & MODE_INSERT) == 0 || curbuf->b_nwindows <= 1))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001837 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1839 unload ? action : (action == DOBUF_GOTO
Bram Moolenaareb44a682017-08-03 22:44:55 +02001840 && !buf_hide(prevbuf)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001841 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0,
1842 FALSE, FALSE);
Bram Moolenaare25865a2012-07-06 16:22:02 +02001843 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001844 // autocommands changed curwin, Grr!
Bram Moolenaare25865a2012-07-06 16:22:02 +02001845 curwin = previouswin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001848 // An autocommand may have deleted "buf", already entered it (e.g., when
1849 // it did ":bunload") or aborted the script processing.
1850 // If curwin->w_buffer is null, enter_buffer() will make it valid again
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001851 valid = buf_valid(buf);
1852 if ((valid && buf != curbuf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001853#ifdef FEAT_EVAL
Bram Moolenaar4033c552017-09-16 20:54:51 +02001854 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001856 ) || curwin->w_buffer == NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001857 {
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001858 // If the buffer is not valid but curwin->w_buffer is NULL we must
1859 // enter some buffer. Using the last one is hopefully OK.
1860 if (!valid)
1861 enter_buffer(lastbuf);
1862 else
1863 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001864#ifdef FEAT_SYN_HL
1865 if (old_tw != curbuf->b_p_tw)
1866 check_colorcolumn(curwin);
1867#endif
1868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869}
1870
1871/*
1872 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001873 * Old curbuf must have been abandoned already! This also means "curbuf" may
1874 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001876 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001877enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878{
Bram Moolenaarcfeb8a52022-08-13 14:09:44 +01001879 // when closing the current buffer stop Visual mode
1880 if (VIsual_active
1881#if defined(EXITFREE)
1882 && !entered_free_all_mem
1883#endif
1884 )
1885 end_visual_mode();
1886
Bram Moolenaar010ee962019-09-25 20:37:36 +02001887 // Get the buffer in the current window.
1888 curwin->w_buffer = buf;
1889 curbuf = buf;
1890 ++curbuf->b_nwindows;
1891
1892 // Copy buffer and window local option values. Not for a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1894 if (!buf->b_help)
1895 get_winopts(buf);
1896#ifdef FEAT_FOLDING
1897 else
Bram Moolenaar010ee962019-09-25 20:37:36 +02001898 // Remove all folds in the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 clearFolding(curwin);
Bram Moolenaar010ee962019-09-25 20:37:36 +02001900 foldUpdateAll(curwin); // update folds (later).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901#endif
1902
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001904 if (curwin->w_p_diff)
1905 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906#endif
1907
Bram Moolenaara971b822011-09-14 14:43:25 +02001908#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001909 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001910#endif
1911
Bram Moolenaarc667da52019-11-30 20:52:27 +01001912 // Cursor on first line by default.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 curwin->w_cursor.lnum = 1;
1914 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001917 curwin->w_topline_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918
Bram Moolenaarc667da52019-11-30 20:52:27 +01001919 // mark cursor position as being invalid
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001920 curwin->w_valid = 0;
1921
Bram Moolenaar9cea87c2018-09-21 16:59:45 +02001922 buflist_setfpos(curbuf, curwin, curbuf->b_last_cursor.lnum,
1923 curbuf->b_last_cursor.col, TRUE);
1924
Bram Moolenaarc667da52019-11-30 20:52:27 +01001925 // Make sure the buffer is loaded.
1926 if (curbuf->b_ml.ml_mfp == NULL) // need to load the file
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001927 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001928 // If there is no filetype, allow for detecting one. Esp. useful for
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001929 // ":ball" used in an autocommand. If there already is a filetype we
Bram Moolenaarc667da52019-11-30 20:52:27 +01001930 // might prefer to keep it.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001931 if (*curbuf->b_p_ft == NUL)
1932 did_filetype = FALSE;
1933
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001934 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 else
1937 {
Bram Moolenaareda65222019-05-16 20:29:44 +02001938 if (!msg_silent && !shortmess(SHM_FILEINFO))
1939 need_fileinfo = TRUE; // display file info after redraw
1940
1941 // check if file changed
1942 (void)buf_check_timestamp(curbuf, FALSE);
1943
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001945#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1949 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 }
1951
Bram Moolenaarc667da52019-11-30 20:52:27 +01001952 // If autocommands did not change the cursor position, restore cursor lnum
1953 // and possibly cursor col.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 if (curwin->w_cursor.lnum == 1 && inindent(0))
1955 buflist_getfpos();
1956
Bram Moolenaarc667da52019-11-30 20:52:27 +01001957 check_arg_idx(curwin); // check for valid arg_idx
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 maketitle();
Bram Moolenaarc667da52019-11-30 20:52:27 +01001959 // when autocmds didn't change it
Bram Moolenaard4153d42008-11-15 15:06:17 +00001960 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00001961 scroll_cursor_halfway(FALSE, FALSE); // redisplay at correct position
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962
Bram Moolenaar009b2592004-10-24 19:18:58 +00001963#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarc667da52019-11-30 20:52:27 +01001964 // Send fileOpened event because we've changed buffers.
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001965 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001966#endif
1967
Bram Moolenaarc667da52019-11-30 20:52:27 +01001968 // Change directories when the 'acd' option is set.
Bram Moolenaar6f470022018-04-10 18:47:20 +02001969 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970
1971#ifdef FEAT_KEYMAP
1972 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001973 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001975#ifdef FEAT_SPELL
Bram Moolenaarc667da52019-11-30 20:52:27 +01001976 // May need to set the spell language. Can only do this after the buffer
1977 // has been properly setup.
Bram Moolenaar860cae12010-06-05 23:22:07 +02001978 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001979 (void)parse_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001980#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001981#ifdef FEAT_VIMINFO
1982 curbuf->b_last_used = vim_time();
1983#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001984
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001985 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986}
1987
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001988#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1989/*
1990 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001991 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001992 */
1993 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001994do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001995{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001996 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001997 && curbuf->b_ffname != NULL
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001998 && vim_chdirfile(curbuf->b_ffname, "auto") == OK)
Bram Moolenaar05268152021-11-18 18:53:45 +00001999 {
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002000 shorten_fnames(TRUE);
Bram Moolenaar05268152021-11-18 18:53:45 +00002001 last_chdir_reason = "autochdir";
2002 }
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002003}
2004#endif
2005
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01002006 static void
2007no_write_message_buf(buf_T *buf UNUSED)
2008{
2009#ifdef FEAT_TERMINAL
2010 if (term_job_running(buf->b_term))
2011 emsg(_(e_job_still_running_add_bang_to_end_the_job));
2012 else
2013#endif
2014 semsg(_(e_no_write_since_last_change_for_buffer_nr_add_bang_to_override),
2015 buf->b_fnum);
2016}
2017
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002018 void
2019no_write_message(void)
2020{
2021#ifdef FEAT_TERMINAL
2022 if (term_job_running(curbuf->b_term))
Bram Moolenaarf1474d82021-12-31 19:59:55 +00002023 emsg(_(e_job_still_running_add_bang_to_end_the_job));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002024 else
2025#endif
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002026 emsg(_(e_no_write_since_last_change_add_bang_to_override));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002027}
2028
2029 void
Bram Moolenaar7a760922018-02-19 23:10:02 +01002030no_write_message_nobang(buf_T *buf UNUSED)
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002031{
2032#ifdef FEAT_TERMINAL
Bram Moolenaar7a760922018-02-19 23:10:02 +01002033 if (term_job_running(buf->b_term))
Bram Moolenaarf1474d82021-12-31 19:59:55 +00002034 emsg(_(e_job_still_running));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002035 else
2036#endif
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002037 emsg(_(e_no_write_since_last_change));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002038}
2039
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040/*
2041 * functions for dealing with the buffer list
2042 */
2043
2044/*
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002045 * Return TRUE if the current buffer is empty, unnamed, unmodified and used in
2046 * only one window. That means it can be re-used.
2047 */
2048 int
2049curbuf_reusable(void)
2050{
2051 return (curbuf != NULL
2052 && curbuf->b_ffname == NULL
2053 && curbuf->b_nwindows <= 1
2054 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY())
Bram Moolenaar39803d82019-04-07 12:04:51 +02002055 && !bt_quickfix(curbuf)
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002056 && !curbufIsChanged());
2057}
2058
2059/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 * Add a file name to the buffer list. Return a pointer to the buffer.
2061 * If the same file name already exists return a pointer to that buffer.
2062 * If it does not exist, or if fname == NULL, a new entry is created.
2063 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
2064 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
2065 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02002066 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02002067 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
2068 * if the buffer already exists.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002069 * If (flags & BLN_REUSE) is TRUE, may use buffer number from "buf_reuse".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 * This is the ONLY way to create a new buffer.
2071 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002073buflist_new(
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002074 char_u *ffname_arg, // full path of fname or relative
2075 char_u *sfname_arg, // short fname or NULL
2076 linenr_T lnum, // preferred cursor line
2077 int flags) // BLN_ defines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002079 char_u *ffname = ffname_arg;
2080 char_u *sfname = sfname_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 buf_T *buf;
2082#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002083 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084#endif
2085
Bram Moolenaar480778b2016-07-14 22:09:39 +02002086 if (top_file_num == 1)
2087 hash_init(&buf_hashtab);
2088
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002089 fname_expand(curbuf, &ffname, &sfname); // will allocate ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090
2091 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002092 * If the file name already exists in the list, update the entry.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 */
2094#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002095 // On Unix we can use inode numbers when the file exists. Works better
2096 // for hard links.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
2098 st.st_dev = (dev_T)-1;
2099#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02002100 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101#ifdef UNIX
2102 buflist_findname_stat(ffname, &st)
2103#else
2104 buflist_findname(ffname)
2105#endif
2106 ) != NULL)
2107 {
2108 vim_free(ffname);
2109 if (lnum != 0)
Bram Moolenaar89b693e2020-10-25 17:09:50 +01002110 buflist_setfpos(buf, (flags & BLN_NOCURWIN) ? NULL : curwin,
2111 lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02002112
2113 if ((flags & BLN_NOOPT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002114 // copy the options now, if 'cpo' doesn't have 's' and not done
2115 // already
Bram Moolenaar82404332016-07-10 17:00:38 +02002116 buf_copy_options(buf, 0);
2117
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 if ((flags & BLN_LISTED) && !buf->b_p_bl)
2119 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002120 bufref_T bufref;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002121
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 buf->b_p_bl = TRUE;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002123 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002125 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002126 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002127 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002128 return NULL;
2129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 }
2131 return buf;
2132 }
2133
2134 /*
2135 * If the current buffer has no name and no contents, use the current
2136 * buffer. Otherwise: Need to allocate a new buffer structure.
2137 *
2138 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00002139 * (A spell file buffer is allocated in spell.c, but that's not a normal
2140 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 */
2142 buf = NULL;
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002143 if ((flags & BLN_CURBUF) && curbuf_reusable())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 {
2145 buf = curbuf;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002146 // It's like this buffer is deleted. Watch out for autocommands that
2147 // change curbuf! If that happens, allocate a new buffer anyway.
Charlie Grovesfef44852022-04-19 16:24:12 +01002148 buf_freeall(buf, BFA_WIPE | BFA_DEL);
2149 if (buf != curbuf) // autocommands deleted the buffer!
2150 return NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002151#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002152 if (aborting()) // autocmds may abort script processing
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002153 {
2154 vim_free(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 return NULL;
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 }
2159 if (buf != curbuf || curbuf == NULL)
2160 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002161 buf = ALLOC_CLEAR_ONE(buf_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 if (buf == NULL)
2163 {
2164 vim_free(ffname);
2165 return NULL;
2166 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02002167#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002168 // init b: variables
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01002169 buf->b_vars = dict_alloc_id(aid_newbuf_bvars);
Bram Moolenaar429fa852013-04-15 12:27:36 +02002170 if (buf->b_vars == NULL)
2171 {
2172 vim_free(ffname);
2173 vim_free(buf);
2174 return NULL;
2175 }
2176 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
2177#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01002178 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 }
2180
2181 if (ffname != NULL)
2182 {
2183 buf->b_ffname = ffname;
2184 buf->b_sfname = vim_strsave(sfname);
2185 }
2186
2187 clear_wininfo(buf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002188 buf->b_wininfo = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189
2190 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
2191 || buf->b_wininfo == NULL)
2192 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002193 if (buf->b_sfname != buf->b_ffname)
2194 VIM_CLEAR(buf->b_sfname);
2195 else
2196 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01002197 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 if (buf != curbuf)
2199 free_buffer(buf);
2200 return NULL;
2201 }
2202
2203 if (buf == curbuf)
2204 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002205 free_buffer_stuff(buf, FALSE); // delete local variables et al.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002206
Bram Moolenaarc667da52019-11-30 20:52:27 +01002207 // Init the options.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002208 buf->b_p_initialized = FALSE;
2209 buf_copy_options(buf, BCO_ENTER);
2210
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211#ifdef FEAT_KEYMAP
Bram Moolenaarc667da52019-11-30 20:52:27 +01002212 // need to reload lmaps and set b:keymap_name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 curbuf->b_kmap_state |= KEYMAP_INIT;
2214#endif
2215 }
2216 else
2217 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002218 // put the new buffer at the end of the buffer list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 buf->b_next = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002220 if (firstbuf == NULL) // buffer list is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 {
2222 buf->b_prev = NULL;
2223 firstbuf = buf;
2224 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002225 else // append new buffer at end of list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 {
2227 lastbuf->b_next = buf;
2228 buf->b_prev = lastbuf;
2229 }
2230 lastbuf = buf;
2231
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002232 if ((flags & BLN_REUSE) && buf_reuse.ga_len > 0)
2233 {
2234 // Recycle a previously used buffer number. Used for buffers which
2235 // are normally hidden, e.g. in a popup window. Avoids that the
2236 // buffer number grows rapidly.
2237 --buf_reuse.ga_len;
2238 buf->b_fnum = ((int *)buf_reuse.ga_data)[buf_reuse.ga_len];
Bram Moolenaar99ebf222019-12-10 23:44:48 +01002239
2240 // Move buffer to the right place in the buffer list.
2241 while (buf->b_prev != NULL && buf->b_fnum < buf->b_prev->b_fnum)
2242 {
2243 buf_T *prev = buf->b_prev;
2244
2245 prev->b_next = buf->b_next;
2246 if (prev->b_next != NULL)
2247 prev->b_next->b_prev = prev;
2248 buf->b_next = prev;
2249 buf->b_prev = prev->b_prev;
2250 if (buf->b_prev != NULL)
2251 buf->b_prev->b_next = buf;
2252 prev->b_prev = buf;
2253 if (lastbuf == buf)
2254 lastbuf = prev;
2255 if (firstbuf == prev)
2256 firstbuf = buf;
2257 }
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002258 }
2259 else
2260 buf->b_fnum = top_file_num++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002261 if (top_file_num < 0) // wrap around (may cause duplicates)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002263 emsg(_("W14: Warning: List of file names overflow"));
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002264 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 {
2266 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002267 ui_delay(3001L, TRUE); // make sure it is noticed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 }
2269 top_file_num = 1;
2270 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002271 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002273 // Always copy the options from the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 buf_copy_options(buf, BCO_ALWAYS);
2275 }
2276
2277 buf->b_wininfo->wi_fpos.lnum = lnum;
2278 buf->b_wininfo->wi_win = curwin;
2279
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002280#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002281 hash_init(&buf->b_s.b_keywtab);
2282 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283#endif
2284
2285 buf->b_fname = buf->b_sfname;
2286#ifdef UNIX
2287 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002288 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 else
2290 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002291 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 buf->b_dev = st.st_dev;
2293 buf->b_ino = st.st_ino;
2294 }
2295#endif
2296 buf->b_u_synced = TRUE;
2297 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002298 if (flags & BLN_DUMMY)
2299 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 buf_clear_file(buf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002301 clrallmarks(buf); // clear marks
2302 fmarks_check_names(buf); // check file marks for this file
2303 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; // init 'buflisted'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 if (!(flags & BLN_DUMMY))
2305 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002306 bufref_T bufref;
2307
Bram Moolenaarc667da52019-11-30 20:52:27 +01002308 // Tricky: these autocommands may change the buffer list. They could
2309 // also split the window with re-using the one empty buffer. This may
2310 // result in unexpectedly losing the empty buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002311 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002312 if (apply_autocmds(EVENT_BUFNEW, 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;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002316 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002317 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002318 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002319 return NULL;
2320 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002321#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002322 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326
2327 return buf;
2328}
2329
2330/*
2331 * Free the memory for the options of a buffer.
2332 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2333 * 'fileencoding'.
2334 */
2335 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002336free_buf_options(
2337 buf_T *buf,
2338 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339{
2340 if (free_p_ff)
2341 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 clear_string_option(&buf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 clear_string_option(&buf->b_p_bh);
2345 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 }
2347#ifdef FEAT_FIND_ID
2348 clear_string_option(&buf->b_p_def);
2349 clear_string_option(&buf->b_p_inc);
2350# ifdef FEAT_EVAL
2351 clear_string_option(&buf->b_p_inex);
2352# endif
2353#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002354#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 clear_string_option(&buf->b_p_inde);
2356 clear_string_option(&buf->b_p_indk);
2357#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002358#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2359 clear_string_option(&buf->b_p_bexpr);
2360#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002361#if defined(FEAT_CRYPT)
2362 clear_string_option(&buf->b_p_cm);
2363#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002364 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002365#if defined(FEAT_EVAL)
2366 clear_string_option(&buf->b_p_fex);
2367#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368#ifdef FEAT_CRYPT
Bram Moolenaar131530a2021-07-29 20:37:49 +02002369# ifdef FEAT_SODIUM
Christian Brabandtaae58342023-04-23 17:50:22 +01002370 if (buf->b_p_key != NULL && *buf->b_p_key != NUL
2371 && crypt_method_is_sodium(crypt_get_method_nr(buf)))
K.Takata1a8825d2022-01-19 13:32:57 +00002372 crypt_sodium_munlock(buf->b_p_key, STRLEN(buf->b_p_key));
Bram Moolenaar131530a2021-07-29 20:37:49 +02002373# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 clear_string_option(&buf->b_p_key);
2375#endif
2376 clear_string_option(&buf->b_p_kp);
2377 clear_string_option(&buf->b_p_mps);
2378 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002379 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 clear_string_option(&buf->b_p_isk);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002381#ifdef FEAT_VARTABS
2382 clear_string_option(&buf->b_p_vsts);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00002383 VIM_CLEAR(buf->b_p_vsts_nopaste);
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00002384 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002385 clear_string_option(&buf->b_p_vts);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002386 VIM_CLEAR(buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388#ifdef FEAT_KEYMAP
2389 clear_string_option(&buf->b_p_keymap);
Bram Moolenaar50138322018-01-28 17:05:16 +01002390 keymap_clear(&buf->b_kmap_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 ga_clear(&buf->b_kmap_ga);
2392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 clear_string_option(&buf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394#ifdef FEAT_FOLDING
2395 clear_string_option(&buf->b_p_cms);
2396#endif
2397 clear_string_option(&buf->b_p_nf);
2398#ifdef FEAT_SYN_HL
2399 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002400 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002401#endif
2402#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002403 clear_string_option(&buf->b_s.b_p_spc);
2404 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002405 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002406 buf->b_s.b_cap_prog = NULL;
2407 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02002408 clear_string_option(&buf->b_s.b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 clear_string_option(&buf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 clear_string_option(&buf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 clear_string_option(&buf->b_p_cink);
2413 clear_string_option(&buf->b_p_cino);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002414 clear_string_option(&buf->b_p_lop);
Tom Praschan3506cf32022-04-07 12:39:08 +01002415 clear_string_option(&buf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 clear_string_option(&buf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 clear_string_option(&buf->b_p_cpt);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002418#ifdef FEAT_COMPL_FUNC
2419 clear_string_option(&buf->b_p_cfu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002420 free_callback(&buf->b_cfu_cb);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002421 clear_string_option(&buf->b_p_ofu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002422 free_callback(&buf->b_ofu_cb);
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +01002423 clear_string_option(&buf->b_p_tsrfu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002424 free_callback(&buf->b_tsrfu_cb);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426#ifdef FEAT_QUICKFIX
2427 clear_string_option(&buf->b_p_gp);
2428 clear_string_option(&buf->b_p_mp);
2429 clear_string_option(&buf->b_p_efm);
2430#endif
2431 clear_string_option(&buf->b_p_ep);
2432 clear_string_option(&buf->b_p_path);
2433 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002434 clear_string_option(&buf->b_p_tc);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002435#ifdef FEAT_EVAL
2436 clear_string_option(&buf->b_p_tfu);
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +00002437 free_callback(&buf->b_tfu_cb);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002438#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 clear_string_option(&buf->b_p_dict);
2440 clear_string_option(&buf->b_p_tsr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002441 clear_string_option(&buf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002443 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002444 clear_string_option(&buf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002445 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002446 clear_string_option(&buf->b_p_menc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447}
2448
2449/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002450 * Get alternate file "n".
2451 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2452 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 * if (options & GETF_SETMARK) call setpcmark()
2454 * if (options & GETF_ALT) we are jumping to an alternate file.
2455 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2456 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002457 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 */
2459 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002460buflist_getfile(
2461 int n,
2462 linenr_T lnum,
2463 int options,
2464 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465{
2466 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 pos_T *fpos;
2469 colnr_T col;
2470
2471 buf = buflist_findnr(n);
2472 if (buf == NULL)
2473 {
2474 if ((options & GETF_ALT) && n == 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02002475 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 else
Bram Moolenaare1242042021-12-16 20:56:57 +00002477 semsg(_(e_buffer_nr_not_found), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 return FAIL;
2479 }
2480
Bram Moolenaarc667da52019-11-30 20:52:27 +01002481 // if alternate file is the current buffer, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 if (buf == curbuf)
2483 return OK;
2484
Bram Moolenaar71223e22022-05-30 15:23:09 +01002485 if (text_or_buf_locked())
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002486 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487
Bram Moolenaarc667da52019-11-30 20:52:27 +01002488 // altfpos may be changed by getfile(), get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 if (lnum == 0)
2490 {
2491 fpos = buflist_findfpos(buf);
2492 lnum = fpos->lnum;
2493 col = fpos->col;
2494 }
2495 else
2496 col = 0;
2497
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 if (options & GETF_SWITCH)
2499 {
Yegappan Lakshmanane42c27d2023-05-14 17:24:22 +01002500 // If 'switchbuf' is set jump to the window containing "buf".
2501 wp = swbuf_goto_win_with_buf(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002502
Bram Moolenaarc667da52019-11-30 20:52:27 +01002503 // If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2504 // current buffer isn't empty: open new tab or window
Bram Moolenaara594d772015-06-19 14:41:49 +02002505 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002506 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002508 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002509 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002510 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2511 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002513 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 }
2515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516
2517 ++RedrawingDisabled;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002518 int retval = FAIL;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002519 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2520 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002522 // cursor is at to BOL and w_cursor.lnum is checked due to getfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 if (!p_sol && col != 0)
2524 {
2525 curwin->w_cursor.col = col;
2526 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 curwin->w_set_curswant = TRUE;
2529 }
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002530 retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 }
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002532
2533 if (RedrawingDisabled > 0)
2534 --RedrawingDisabled;
2535 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536}
2537
2538/*
2539 * go to the last know line number for the current buffer
2540 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002541 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002542buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543{
2544 pos_T *fpos;
2545
2546 fpos = buflist_findfpos(curbuf);
2547
2548 curwin->w_cursor.lnum = fpos->lnum;
2549 check_cursor_lnum();
2550
2551 if (p_sol)
2552 curwin->w_cursor.col = 0;
2553 else
2554 {
2555 curwin->w_cursor.col = fpos->col;
2556 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 curwin->w_set_curswant = TRUE;
2559 }
2560}
2561
Bram Moolenaar81695252004-12-29 20:58:21 +00002562/*
2563 * Find file in buffer list by name (it has to be for the current window).
2564 * Returns NULL if not found.
2565 */
2566 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002567buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002568{
2569 char_u *ffname;
2570 buf_T *buf = NULL;
2571
Bram Moolenaarc667da52019-11-30 20:52:27 +01002572 // First make the name into a full path name
Bram Moolenaar81695252004-12-29 20:58:21 +00002573 ffname = FullName_save(fname,
2574#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002575 TRUE // force expansion, get rid of symbolic links
Bram Moolenaar81695252004-12-29 20:58:21 +00002576#else
2577 FALSE
2578#endif
2579 );
2580 if (ffname != NULL)
2581 {
2582 buf = buflist_findname(ffname);
2583 vim_free(ffname);
2584 }
2585 return buf;
2586}
Bram Moolenaar81695252004-12-29 20:58:21 +00002587
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588/*
2589 * Find file in buffer list by name (it has to be for the current window).
2590 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002591 * Skips dummy buffers.
2592 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 */
2594 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002595buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596{
2597#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002598 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599
2600 if (mch_stat((char *)ffname, &st) < 0)
2601 st.st_dev = (dev_T)-1;
2602 return buflist_findname_stat(ffname, &st);
2603}
2604
2605/*
2606 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2607 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002608 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 */
2610 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002611buflist_findname_stat(
2612 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002613 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614{
2615#endif
2616 buf_T *buf;
2617
Bram Moolenaarc667da52019-11-30 20:52:27 +01002618 // Start at the last buffer, expect to find a match sooner.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002619 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaar81695252004-12-29 20:58:21 +00002620 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621#ifdef UNIX
2622 , stp
2623#endif
2624 ))
2625 return buf;
2626 return NULL;
2627}
2628
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629/*
2630 * Find file in buffer list by a regexp pattern.
2631 * Return fnum of the found buffer.
2632 * Return < 0 for error.
2633 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002635buflist_findpat(
2636 char_u *pattern,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002637 char_u *pattern_end, // pointer to first char after pattern
2638 int unlisted, // find unlisted buffers
2639 int diffmode UNUSED, // find diff-mode buffers only
2640 int curtab_only) // find buffers in current tab only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641{
2642 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 int match = -1;
2644 int find_listed;
2645 char_u *pat;
2646 char_u *patend;
2647 int attempt;
2648 char_u *p;
2649 int toggledollar;
2650
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002651 // "%" is current file, "%%" or "#" is alternate file
2652 if ((pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2653 || (in_vim9script() && pattern_end == pattern + 2
2654 && pattern[0] == '%' && pattern[1] == '%'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 {
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002656 if (*pattern == '#' || pattern_end == pattern + 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 match = curwin->w_alt_fnum;
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002658 else
2659 match = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660#ifdef FEAT_DIFF
2661 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2662 match = -1;
2663#endif
2664 }
2665
2666 /*
2667 * Try four ways of matching a listed buffer:
2668 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002669 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 * attempt == 2: with '$' at end (only match at end)
2671 * attempt == 3: with '^' at start and '$' at end (only full match)
2672 * Repeat this for finding an unlisted buffer if there was no matching
2673 * listed buffer.
2674 */
2675 else
2676 {
2677 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2678 if (pat == NULL)
2679 return -1;
2680 patend = pat + STRLEN(pat) - 1;
2681 toggledollar = (patend > pat && *patend == '$');
2682
Bram Moolenaarc667da52019-11-30 20:52:27 +01002683 // First try finding a listed buffer. If not found and "unlisted"
2684 // is TRUE, try finding an unlisted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 find_listed = TRUE;
2686 for (;;)
2687 {
2688 for (attempt = 0; attempt <= 3; ++attempt)
2689 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002690 regmatch_T regmatch;
2691
Bram Moolenaarc667da52019-11-30 20:52:27 +01002692 // may add '^' and '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 if (toggledollar)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002694 *patend = (attempt < 2) ? NUL : '$'; // add/remove '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 p = pat;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002696 if (*p == '^' && !(attempt & 1)) // add/remove '^'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 ++p;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01002698 regmatch.regprog = vim_regcomp(p, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002700 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002701 {
2702 if (regmatch.regprog == NULL)
2703 {
2704 // invalid pattern, possibly after switching engine
2705 vim_free(pat);
2706 return -1;
2707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 if (buf->b_p_bl == find_listed
2709#ifdef FEAT_DIFF
2710 && (!diffmode || diff_mode_buf(buf))
2711#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002712 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002714 if (curtab_only)
2715 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002716 // Ignore the match if the buffer is not open in
2717 // the current tab.
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002718 win_T *wp;
2719
Bram Moolenaar29323592016-07-24 22:04:11 +02002720 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002721 if (wp->w_buffer == buf)
2722 break;
2723 if (wp == NULL)
2724 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002725 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002726 if (match >= 0) // already found a match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 {
2728 match = -2;
2729 break;
2730 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002731 match = buf->b_fnum; // remember first match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 }
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002735 vim_regfree(regmatch.regprog);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002736 if (match >= 0) // found one match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 break;
2738 }
2739
Bram Moolenaarc667da52019-11-30 20:52:27 +01002740 // Only search for unlisted buffers if there was no match with
2741 // a listed buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 if (!unlisted || !find_listed || match != -1)
2743 break;
2744 find_listed = FALSE;
2745 }
2746
2747 vim_free(pat);
2748 }
2749
2750 if (match == -2)
Bram Moolenaare1242042021-12-16 20:56:57 +00002751 semsg(_(e_more_than_one_match_for_str), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 else if (match < 0)
Bram Moolenaare1242042021-12-16 20:56:57 +00002753 semsg(_(e_no_matching_buffer_for_str), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 return match;
2755}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756
Bram Moolenaar52410572019-10-27 05:12:45 +01002757#ifdef FEAT_VIMINFO
2758typedef struct {
2759 buf_T *buf;
2760 char_u *match;
2761} bufmatch_T;
2762#endif
2763
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764/*
2765 * Find all buffer names that match.
2766 * For command line expansion of ":buf" and ":sbuf".
2767 * Return OK if matches found, FAIL otherwise.
2768 */
2769 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002770ExpandBufnames(
2771 char_u *pat,
2772 int *num_file,
2773 char_u ***file,
2774 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775{
2776 int count = 0;
2777 buf_T *buf;
2778 int round;
2779 char_u *p;
2780 int attempt;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002781 char_u *patc = NULL;
Bram Moolenaar52410572019-10-27 05:12:45 +01002782#ifdef FEAT_VIMINFO
2783 bufmatch_T *matches = NULL;
2784#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002785 int fuzzy;
2786 fuzmatch_str_T *fuzmatch = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787
Bram Moolenaarc667da52019-11-30 20:52:27 +01002788 *num_file = 0; // return values in case of FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 *file = NULL;
2790
Bram Moolenaarefcc3292019-12-30 21:59:03 +01002791#ifdef FEAT_DIFF
2792 if ((options & BUF_DIFF_FILTER) && !curwin->w_p_diff)
2793 return FAIL;
2794#endif
2795
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002796 fuzzy = cmdline_fuzzy_complete(pat);
2797
2798 // Make a copy of "pat" and change "^" to "\(^\|[\/]\)" (if doing regular
2799 // expression matching)
2800 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002802 if (*pat == '^')
2803 {
2804 patc = alloc(STRLEN(pat) + 11);
2805 if (patc == NULL)
2806 return FAIL;
2807 STRCPY(patc, "\\(^\\|[\\/]\\)");
2808 STRCPY(patc + 11, pat + 1);
2809 }
2810 else
2811 patc = pat;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002812 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00002813
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002814 // attempt == 0: try match with '\<', match at start of word
2815 // attempt == 1: try match without '\<', match anywhere
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002816 for (attempt = 0; attempt <= (fuzzy ? 0 : 1); ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002817 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002818 regmatch_T regmatch;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002819 int score = 0;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002820
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002821 if (!fuzzy)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002822 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002823 if (attempt > 0 && patc == pat)
2824 break; // there was no anchor, no need to try again
2825 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002828 // round == 1: Count the matches.
2829 // round == 2: Build the array to keep the matches.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 for (round = 1; round <= 2; ++round)
2831 {
2832 count = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002833 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002835 if (!buf->b_p_bl) // skip unlisted buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 continue;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002837#ifdef FEAT_DIFF
2838 if (options & BUF_DIFF_FILTER)
2839 // Skip buffers not suitable for
2840 // :diffget or :diffput completion.
Bram Moolenaarefcc3292019-12-30 21:59:03 +01002841 if (buf == curbuf || !diff_mode_buf(buf))
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002842 continue;
2843#endif
2844
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002845 if (!fuzzy)
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002846 {
2847 if (regmatch.regprog == NULL)
2848 {
2849 // invalid pattern, possibly after recompiling
2850 if (patc != pat)
2851 vim_free(patc);
2852 return FAIL;
2853 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002854 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002855 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002856 else
2857 {
2858 p = NULL;
2859 // first try matching with the short file name
2860 if ((score = fuzzy_match_str(buf->b_sfname, pat)) != 0)
2861 p = buf->b_sfname;
2862 if (p == NULL)
2863 {
2864 // next try matching with the full path file name
2865 if ((score = fuzzy_match_str(buf->b_ffname, pat)) != 0)
2866 p = buf->b_ffname;
2867 }
2868 }
2869
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002870 if (p == NULL)
2871 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002872
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002873 if (round == 1)
2874 {
2875 ++count;
2876 continue;
2877 }
2878
2879 if (options & WILD_HOME_REPLACE)
2880 p = home_replace_save(buf, p);
2881 else
2882 p = vim_strsave(p);
2883
2884 if (!fuzzy)
2885 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002886#ifdef FEAT_VIMINFO
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002887 if (matches != NULL)
2888 {
2889 matches[count].buf = buf;
2890 matches[count].match = p;
2891 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002893 else
2894#endif
2895 (*file)[count++] = p;
2896 }
2897 else
2898 {
2899 fuzmatch[count].idx = count;
2900 fuzmatch[count].str = p;
2901 fuzmatch[count].score = score;
2902 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 }
2904 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002905 if (count == 0) // no match found, break here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 break;
2907 if (round == 1)
2908 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002909 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002911 *file = ALLOC_MULT(char_u *, count);
2912 if (*file == NULL)
2913 {
2914 vim_regfree(regmatch.regprog);
2915 if (patc != pat)
2916 vim_free(patc);
2917 return FAIL;
2918 }
Bram Moolenaar52410572019-10-27 05:12:45 +01002919#ifdef FEAT_VIMINFO
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002920 if (options & WILD_BUFLASTUSED)
2921 matches = ALLOC_MULT(bufmatch_T, count);
Bram Moolenaar52410572019-10-27 05:12:45 +01002922#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002923 }
2924 else
2925 {
2926 fuzmatch = ALLOC_MULT(fuzmatch_str_T, count);
2927 if (fuzmatch == NULL)
2928 {
2929 *num_file = 0;
2930 *file = NULL;
2931 return FAIL;
2932 }
2933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 }
2935 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002936
2937 if (!fuzzy)
2938 {
2939 vim_regfree(regmatch.regprog);
2940 if (count) // match(es) found, break here
2941 break;
2942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 }
2944
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002945 if (!fuzzy && patc != pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002946 vim_free(patc);
2947
Bram Moolenaar52410572019-10-27 05:12:45 +01002948#ifdef FEAT_VIMINFO
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002949 if (!fuzzy)
Bram Moolenaar52410572019-10-27 05:12:45 +01002950 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002951 if (matches != NULL)
Bram Moolenaar52410572019-10-27 05:12:45 +01002952 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002953 int i;
2954 if (count > 1)
2955 qsort(matches, count, sizeof(bufmatch_T), buf_compare);
2956 // if the current buffer is first in the list, place it at the end
2957 if (matches[0].buf == curbuf)
2958 {
2959 for (i = 1; i < count; i++)
2960 (*file)[i-1] = matches[i].match;
2961 (*file)[count-1] = matches[0].match;
2962 }
2963 else
2964 {
2965 for (i = 0; i < count; i++)
2966 (*file)[i] = matches[i].match;
2967 }
2968 vim_free(matches);
Bram Moolenaar52410572019-10-27 05:12:45 +01002969 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002970 }
2971 else
2972 {
2973 if (fuzzymatches_to_strmatches(fuzmatch, file, count, FALSE) == FAIL)
2974 return FAIL;
Bram Moolenaar52410572019-10-27 05:12:45 +01002975 }
2976#endif
2977
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 *num_file = count;
2979 return (count == 0 ? FAIL : OK);
2980}
2981
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982/*
2983 * Check for a match on the file name for buffer "buf" with regprog "prog".
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002984 * Note that rmp->regprog may become NULL when switching regexp engine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 */
2986 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002987buflist_match(
2988 regmatch_T *rmp,
2989 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002990 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991{
2992 char_u *match;
2993
Bram Moolenaarc667da52019-11-30 20:52:27 +01002994 // First try the short file name, then the long file name.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002995 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaara59f2df2022-05-11 11:42:28 +01002996 if (match == NULL && rmp->regprog != NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002997 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998
2999 return match;
3000}
3001
3002/*
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003003 * Try matching the regexp in "rmp->regprog" with file name "name".
3004 * Note that rmp->regprog may become NULL when switching regexp engine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 * Return "name" when there is a match, NULL when not.
3006 */
3007 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003008fname_match(
3009 regmatch_T *rmp,
3010 char_u *name,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003011 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012{
3013 char_u *match = NULL;
3014 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003016 // extra check for valid arguments
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003017 if (name == NULL || rmp->regprog == NULL)
3018 return NULL;
3019
3020 // Ignore case when 'fileignorecase' or the argument is set.
3021 rmp->rm_ic = p_fic || ignore_case;
3022 if (vim_regexec(rmp, name, (colnr_T)0))
3023 match = name;
3024 else if (rmp->regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003026 // Replace $(HOME) with '~' and try matching again.
3027 p = home_replace_save(NULL, name);
3028 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 match = name;
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003030 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 }
3032
3033 return match;
3034}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035
3036/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02003037 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 */
3039 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003040buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041{
Bram Moolenaar480778b2016-07-14 22:09:39 +02003042 char_u key[VIM_SIZEOF_INT * 2 + 1];
3043 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044
3045 if (nr == 0)
3046 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02003047 sprintf((char *)key, "%x", nr);
3048 hi = hash_find(&buf_hashtab, key);
3049
3050 if (!HASHITEM_EMPTY(hi))
3051 return (buf_T *)(hi->hi_key
3052 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 return NULL;
3054}
3055
3056/*
3057 * Get name of file 'n' in the buffer list.
3058 * When the file has no name an empty string is returned.
3059 * home_replace() is used to shorten the file name (used for marks).
3060 * Returns a pointer to allocated memory, of NULL when failed.
3061 */
3062 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003063buflist_nr2name(
3064 int n,
3065 int fullname,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003066 int helptail) // for help buffers return tail only
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067{
3068 buf_T *buf;
3069
3070 buf = buflist_findnr(n);
3071 if (buf == NULL)
3072 return NULL;
3073 return home_replace_save(helptail ? buf : NULL,
3074 fullname ? buf->b_ffname : buf->b_fname);
3075}
3076
3077/*
3078 * Set the "lnum" and "col" for the buffer "buf" and the current window.
3079 * When "copy_options" is TRUE save the local window option values.
3080 * When "lnum" is 0 only do the options.
3081 */
Bram Moolenaardefa0672019-07-21 19:25:37 +02003082 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003083buflist_setfpos(
3084 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003085 win_T *win, // may be NULL when using :badd
Bram Moolenaar7454a062016-01-30 15:14:10 +01003086 linenr_T lnum,
3087 colnr_T col,
3088 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089{
3090 wininfo_T *wip;
3091
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003092 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 if (wip->wi_win == win)
3094 break;
3095 if (wip == NULL)
3096 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003097 // allocate a new entry
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003098 wip = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 if (wip == NULL)
3100 return;
3101 wip->wi_win = win;
Bram Moolenaarc667da52019-11-30 20:52:27 +01003102 if (lnum == 0) // set lnum even when it's 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 lnum = 1;
3104 }
3105 else
3106 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003107 // remove the entry from the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 if (wip->wi_prev)
3109 wip->wi_prev->wi_next = wip->wi_next;
3110 else
3111 buf->b_wininfo = wip->wi_next;
3112 if (wip->wi_next)
3113 wip->wi_next->wi_prev = wip->wi_prev;
3114 if (copy_options && wip->wi_optset)
3115 {
3116 clear_winopt(&wip->wi_opt);
3117#ifdef FEAT_FOLDING
3118 deleteFoldRecurse(&wip->wi_folds);
3119#endif
3120 }
3121 }
3122 if (lnum != 0)
3123 {
3124 wip->wi_fpos.lnum = lnum;
3125 wip->wi_fpos.col = col;
3126 }
LemonBoydb0ea7f2022-04-10 17:59:26 +01003127 if (win != NULL)
3128 wip->wi_changelistidx = win->w_changelistidx;
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003129 if (copy_options && win != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003131 // Save the window-specific option values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
3133#ifdef FEAT_FOLDING
3134 wip->wi_fold_manual = win->w_fold_manual;
3135 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
3136#endif
3137 wip->wi_optset = TRUE;
3138 }
3139
Bram Moolenaarc667da52019-11-30 20:52:27 +01003140 // insert the entry in front of the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 wip->wi_next = buf->b_wininfo;
3142 buf->b_wininfo = wip;
3143 wip->wi_prev = NULL;
3144 if (wip->wi_next)
3145 wip->wi_next->wi_prev = wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146}
3147
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003148#ifdef FEAT_DIFF
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003149/*
3150 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
3151 * page. That's because a diff is local to a tab page.
3152 */
3153 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003154wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003155{
3156 win_T *wp;
3157
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003158 if (!wip->wi_opt.wo_diff)
3159 return FALSE;
3160
3161 FOR_ALL_WINDOWS(wp)
3162 // return FALSE when it's a window in the current tab page, thus
3163 // the buffer was in diff mode here
3164 if (wip->wi_win == wp)
3165 return FALSE;
3166 return TRUE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003167}
3168#endif
3169
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170/*
3171 * Find info for the current window in buffer "buf".
3172 * If not found, return the info for the most recently used window.
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003173 * When "need_options" is TRUE skip entries where wi_optset is FALSE.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003174 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
3175 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 * Returns NULL when there isn't any info.
3177 */
3178 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003179find_wininfo(
3180 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003181 int need_options,
Bram Moolenaar7454a062016-01-30 15:14:10 +01003182 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183{
3184 wininfo_T *wip;
3185
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003186 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003187 if (wip->wi_win == curwin
3188#ifdef FEAT_DIFF
3189 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
3190#endif
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003191
3192 && (!need_options || wip->wi_optset))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003194
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003195 if (wip != NULL)
3196 return wip;
3197
Bram Moolenaarc667da52019-11-30 20:52:27 +01003198 // If no wininfo for curwin, use the first in the list (that doesn't have
3199 // 'diff' set and is in another tab page).
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003200 // If "need_options" is TRUE skip entries that don't have options set,
3201 // unless the window is editing "buf", so we can copy from the window
3202 // itself.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003203#ifdef FEAT_DIFF
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003204 if (skip_diff_buffer)
3205 {
3206 FOR_ALL_BUF_WININFO(buf, wip)
3207 if (!wininfo_other_tab_diff(wip)
3208 && (!need_options || wip->wi_optset
3209 || (wip->wi_win != NULL
3210 && wip->wi_win->w_buffer == buf)))
3211 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003212 }
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003213 else
3214#endif
3215 wip = buf->b_wininfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 return wip;
3217}
3218
3219/*
3220 * Reset the local window options to the values last used in this window.
3221 * If the buffer wasn't used in this window before, use the values from
3222 * the most recently used window. If the values were never set, use the
3223 * global values for the window.
3224 */
3225 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003226get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227{
3228 wininfo_T *wip;
3229
3230 clear_winopt(&curwin->w_onebuf_opt);
3231#ifdef FEAT_FOLDING
3232 clearFolding(curwin);
3233#endif
3234
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003235 wip = find_wininfo(buf, TRUE, TRUE);
Bram Moolenaar25782a72018-05-13 18:05:33 +02003236 if (wip != NULL && wip->wi_win != NULL
3237 && wip->wi_win != curwin && wip->wi_win->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003239 // The buffer is currently displayed in the window: use the actual
3240 // option values instead of the saved (possibly outdated) values.
Bram Moolenaar25782a72018-05-13 18:05:33 +02003241 win_T *wp = wip->wi_win;
3242
3243 copy_winopt(&wp->w_onebuf_opt, &curwin->w_onebuf_opt);
3244#ifdef FEAT_FOLDING
3245 curwin->w_fold_manual = wp->w_fold_manual;
3246 curwin->w_foldinvalid = TRUE;
3247 cloneFoldGrowArray(&wp->w_folds, &curwin->w_folds);
3248#endif
3249 }
3250 else if (wip != NULL && wip->wi_optset)
3251 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003252 // the buffer was displayed in the current window earlier
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
3254#ifdef FEAT_FOLDING
3255 curwin->w_fold_manual = wip->wi_fold_manual;
3256 curwin->w_foldinvalid = TRUE;
3257 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
3258#endif
3259 }
3260 else
3261 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
LemonBoydb0ea7f2022-04-10 17:59:26 +01003262 if (wip != NULL)
3263 curwin->w_changelistidx = wip->wi_changelistidx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264
3265#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +01003266 // Set 'foldlevel' to 'foldlevelstart' if it's not negative.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 if (p_fdls >= 0)
3268 curwin->w_p_fdl = p_fdls;
3269#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02003270 after_copy_winopt(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271}
3272
3273/*
3274 * Find the position (lnum and col) for the buffer 'buf' for the current
3275 * window.
3276 * Returns a pointer to no_position if no position is found.
3277 */
3278 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003279buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280{
3281 wininfo_T *wip;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003282 static pos_T no_position = {1, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003284 wip = find_wininfo(buf, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 if (wip != NULL)
3286 return &(wip->wi_fpos);
3287 else
3288 return &no_position;
3289}
3290
3291/*
3292 * Find the lnum for the buffer 'buf' for the current window.
3293 */
3294 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01003295buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296{
3297 return buflist_findfpos(buf)->lnum;
3298}
3299
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02003301 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003304buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305{
Bram Moolenaar52410572019-10-27 05:12:45 +01003306 buf_T *buf = firstbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 int len;
3308 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003309 int ro_char;
3310 int changed_char;
Bram Moolenaar0751f512018-03-29 16:37:16 +02003311#ifdef FEAT_TERMINAL
3312 int job_running;
3313 int job_none_open;
3314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315
Bram Moolenaar52410572019-10-27 05:12:45 +01003316#ifdef FEAT_VIMINFO
3317 garray_T buflist;
3318 buf_T **buflist_data = NULL, **p;
3319
3320 if (vim_strchr(eap->arg, 't'))
3321 {
3322 ga_init2(&buflist, sizeof(buf_T *), 50);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003323 FOR_ALL_BUFFERS(buf)
Bram Moolenaar52410572019-10-27 05:12:45 +01003324 {
3325 if (ga_grow(&buflist, 1) == OK)
3326 ((buf_T **)buflist.ga_data)[buflist.ga_len++] = buf;
3327 }
3328
3329 qsort(buflist.ga_data, (size_t)buflist.ga_len,
3330 sizeof(buf_T *), buf_compare);
3331
Bram Moolenaar3b991522019-11-06 23:26:20 +01003332 buflist_data = (buf_T **)buflist.ga_data;
3333 buf = *buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003334 }
Bram Moolenaar3b991522019-11-06 23:26:20 +01003335 p = buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003336
Bram Moolenaar3b991522019-11-06 23:26:20 +01003337 for (; buf != NULL && !got_int; buf = buflist_data != NULL
Bram Moolenaar52410572019-10-27 05:12:45 +01003338 ? (++p < buflist_data + buflist.ga_len ? *p : NULL)
3339 : buf->b_next)
3340#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
Bram Moolenaar52410572019-10-27 05:12:45 +01003342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 {
Bram Moolenaar0751f512018-03-29 16:37:16 +02003344#ifdef FEAT_TERMINAL
3345 job_running = term_job_running(buf->b_term);
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003346 job_none_open = term_none_open(buf->b_term);
Bram Moolenaar0751f512018-03-29 16:37:16 +02003347#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003348 // skip unlisted buffers, unless ! was used
Bram Moolenaard51cb702015-07-21 15:03:06 +02003349 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
3350 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
3351 || (vim_strchr(eap->arg, '+')
3352 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
3353 || (vim_strchr(eap->arg, 'a')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003354 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
Bram Moolenaard51cb702015-07-21 15:03:06 +02003355 || (vim_strchr(eap->arg, 'h')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003356 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3357#ifdef FEAT_TERMINAL
3358 || (vim_strchr(eap->arg, 'R')
3359 && (!job_running || (job_running && job_none_open)))
3360 || (vim_strchr(eap->arg, '?')
3361 && (!job_running || (job_running && !job_none_open)))
3362 || (vim_strchr(eap->arg, 'F')
3363 && (job_running || buf->b_term == NULL))
3364#endif
Bram Moolenaard51cb702015-07-21 15:03:06 +02003365 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3366 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3367 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3368 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3369 || (vim_strchr(eap->arg, '#')
3370 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003373 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 else
3375 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003376 if (message_filtered(NameBuff))
3377 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003379 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3380 : (bufIsChanged(buf) ? '+' : ' ');
3381#ifdef FEAT_TERMINAL
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003382 if (job_running)
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003383 {
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003384 if (job_none_open)
Bram Moolenaar4033c552017-09-16 20:54:51 +02003385 ro_char = '?';
3386 else
3387 ro_char = 'R';
Bram Moolenaarc667da52019-11-30 20:52:27 +01003388 changed_char = ' '; // bufIsChanged() returns TRUE to avoid
3389 // closing, but it's not actually changed.
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003390 }
3391 else if (buf->b_term != NULL)
3392 ro_char = 'F';
3393 else
3394#endif
3395 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3396
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003397 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00003398 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 buf->b_fnum,
3400 buf->b_p_bl ? ' ' : 'u',
3401 buf == curbuf ? '%' :
3402 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3403 buf->b_ml.ml_mfp == NULL ? ' ' :
3404 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003405 ro_char,
3406 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003407 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003408 if (len > IOSIZE - 20)
3409 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410
Bram Moolenaarc667da52019-11-30 20:52:27 +01003411 // put "line 999" in column 40 or after the file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 i = 40 - vim_strsize(IObuff);
3413 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 IObuff[len++] = ' ';
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003415 while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar52410572019-10-27 05:12:45 +01003416#ifdef FEAT_VIMINFO
3417 if (vim_strchr(eap->arg, 't') && buf->b_last_used)
3418 add_time(IObuff + len, (size_t)(IOSIZE - len), buf->b_last_used);
3419 else
3420#endif
3421 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3422 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003423 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 msg_outtrans(IObuff);
Bram Moolenaarc667da52019-11-30 20:52:27 +01003425 out_flush(); // output one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 ui_breakcheck();
3427 }
Bram Moolenaar52410572019-10-27 05:12:45 +01003428
3429#ifdef FEAT_VIMINFO
3430 if (buflist_data)
3431 ga_clear(&buflist);
3432#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434
3435/*
3436 * Get file name and line number for file 'fnum'.
3437 * Used by DoOneCmd() for translating '%' and '#'.
3438 * Used by insert_reg() and cmdline_paste() for '#' register.
3439 * Return FAIL if not found, OK for success.
3440 */
3441 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003442buflist_name_nr(
3443 int fnum,
3444 char_u **fname,
3445 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446{
3447 buf_T *buf;
3448
3449 buf = buflist_findnr(fnum);
3450 if (buf == NULL || buf->b_fname == NULL)
3451 return FAIL;
3452
3453 *fname = buf->b_fname;
3454 *lnum = buflist_findlnum(buf);
3455
3456 return OK;
3457}
3458
3459/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003460 * Set the file name for "buf"' to "ffname_arg", short file name to
3461 * "sfname_arg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 * The file name with the full path is also remembered, for when :cd is used.
3463 * Returns FAIL for failure (file name already in use by other buffer)
3464 * OK otherwise.
3465 */
3466 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003467setfname(
3468 buf_T *buf,
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003469 char_u *ffname_arg,
3470 char_u *sfname_arg,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003471 int message) // give message when buffer already exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003473 char_u *ffname = ffname_arg;
3474 char_u *sfname = sfname_arg;
Bram Moolenaar81695252004-12-29 20:58:21 +00003475 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003477 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478#endif
3479
3480 if (ffname == NULL || *ffname == NUL)
3481 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003482 // Removing the name.
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003483 if (buf->b_sfname != buf->b_ffname)
3484 VIM_CLEAR(buf->b_sfname);
3485 else
3486 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003487 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488#ifdef UNIX
3489 st.st_dev = (dev_T)-1;
3490#endif
3491 }
3492 else
3493 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003494 fname_expand(buf, &ffname, &sfname); // will allocate ffname
3495 if (ffname == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 return FAIL;
3497
3498 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01003499 * If the file name is already used in another buffer:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 * - if the buffer is loaded, fail
3501 * - if the buffer is not loaded, delete it from the list
3502 */
3503#ifdef UNIX
3504 if (mch_stat((char *)ffname, &st) < 0)
3505 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003506#endif
3507 if (!(buf->b_flags & BF_DUMMY))
3508#ifdef UNIX
3509 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003511 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512#endif
3513 if (obuf != NULL && obuf != buf)
3514 {
Bram Moolenaard3710cf2021-10-04 23:13:13 +01003515 win_T *win;
3516 tabpage_T *tab;
3517 int in_use = FALSE;
3518
3519 // during startup a window may use a buffer that is not loaded yet
3520 FOR_ALL_TAB_WINDOWS(tab, win)
3521 if (win->w_buffer == obuf)
3522 in_use = TRUE;
3523
3524 // it's loaded or used in a window, fail
3525 if (obuf->b_ml.ml_mfp != NULL || in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 {
3527 if (message)
Bram Moolenaare1242042021-12-16 20:56:57 +00003528 emsg(_(e_buffer_with_this_name_already_exists));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 vim_free(ffname);
3530 return FAIL;
3531 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003532 // delete from the list
Bram Moolenaara6e8f882019-12-14 16:18:15 +01003533 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 }
3535 sfname = vim_strsave(sfname);
3536 if (ffname == NULL || sfname == NULL)
3537 {
3538 vim_free(sfname);
3539 vim_free(ffname);
3540 return FAIL;
3541 }
3542#ifdef USE_FNAME_CASE
Bram Moolenaarc667da52019-11-30 20:52:27 +01003543 fname_case(sfname, 0); // set correct case for short file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544#endif
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003545 if (buf->b_sfname != buf->b_ffname)
3546 vim_free(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 vim_free(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 buf->b_ffname = ffname;
3549 buf->b_sfname = sfname;
3550 }
3551 buf->b_fname = buf->b_sfname;
3552#ifdef UNIX
3553 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003554 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 else
3556 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003557 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 buf->b_dev = st.st_dev;
3559 buf->b_ino = st.st_ino;
3560 }
3561#endif
3562
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564
3565 buf_name_changed(buf);
3566 return OK;
3567}
3568
3569/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003570 * Crude way of changing the name of a buffer. Use with care!
3571 * The name should be relative to the current directory.
3572 */
3573 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003574buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003575{
3576 buf_T *buf;
3577
3578 buf = buflist_findnr(fnum);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00003579 if (buf == NULL)
3580 return;
3581
3582 if (buf->b_sfname != buf->b_ffname)
3583 vim_free(buf->b_sfname);
3584 vim_free(buf->b_ffname);
3585 buf->b_ffname = vim_strsave(name);
3586 buf->b_sfname = NULL;
3587 // Allocate ffname and expand into full path. Also resolves .lnk
3588 // files on Win32.
3589 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
3590 buf->b_fname = buf->b_sfname;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003591}
3592
3593/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 * Take care of what needs to be done when the name of buffer "buf" has
3595 * changed.
3596 */
3597 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003598buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599{
3600 /*
3601 * If the file name changed, also change the name of the swapfile
3602 */
3603 if (buf->b_ml.ml_mfp != NULL)
3604 ml_setname(buf);
3605
Bram Moolenaar3ad69532021-11-19 17:01:08 +00003606#ifdef FEAT_TERMINAL
3607 if (buf->b_term != NULL)
3608 term_clear_status_text(buf->b_term);
3609#endif
3610
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 if (curwin->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003612 check_arg_idx(curwin); // check file name for arg list
Bram Moolenaarc667da52019-11-30 20:52:27 +01003613 maketitle(); // set window title
Bram Moolenaarc667da52019-11-30 20:52:27 +01003614 status_redraw_all(); // status lines need to be redrawn
3615 fmarks_check_names(buf); // check named file marks
3616 ml_timestamp(buf); // reset timestamp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617}
3618
3619/*
3620 * set alternate file name for current window
3621 *
3622 * Used by do_one_cmd(), do_write() and do_ecmd().
3623 * Return the buffer.
3624 */
3625 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003626setaltfname(
3627 char_u *ffname,
3628 char_u *sfname,
3629 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630{
3631 buf_T *buf;
3632
Bram Moolenaarc667da52019-11-30 20:52:27 +01003633 // Create a buffer. 'buflisted' is not set if it's a new buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaare1004402020-10-24 20:49:43 +02003635 if (buf != NULL && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 curwin->w_alt_fnum = buf->b_fnum;
3637 return buf;
3638}
3639
3640/*
3641 * Get alternate file name for current window.
3642 * Return NULL if there isn't any, and give error message if requested.
3643 */
3644 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003645getaltfname(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003646 int errmsg) // give error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647{
3648 char_u *fname;
3649 linenr_T dummy;
3650
3651 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3652 {
3653 if (errmsg)
Bram Moolenaar108010a2021-06-27 22:03:33 +02003654 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 return NULL;
3656 }
3657 return fname;
3658}
3659
3660/*
3661 * Add a file name to the buflist and return its number.
3662 * Uses same flags as buflist_new(), except BLN_DUMMY.
3663 *
3664 * used by qf_init(), main() and doarglist()
3665 */
3666 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003667buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668{
3669 buf_T *buf;
3670
3671 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3672 if (buf != NULL)
3673 return buf->b_fnum;
3674 return 0;
3675}
3676
3677#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3678/*
3679 * Adjust slashes in file names. Called after 'shellslash' was set.
3680 */
3681 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003682buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683{
3684 buf_T *bp;
3685
Bram Moolenaar29323592016-07-24 22:04:11 +02003686 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 {
3688 if (bp->b_ffname != NULL)
3689 slash_adjust(bp->b_ffname);
3690 if (bp->b_sfname != NULL)
3691 slash_adjust(bp->b_sfname);
3692 }
3693}
3694#endif
3695
3696/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003697 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 * Also save the local window option values.
3699 */
3700 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003701buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003703 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704}
3705
3706/*
3707 * Return TRUE if 'ffname' is not the same file as current file.
3708 * Fname must have a full path (expanded by mch_FullName()).
3709 */
3710 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003711otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712{
3713 return otherfile_buf(curbuf, ffname
3714#ifdef UNIX
3715 , NULL
3716#endif
3717 );
3718}
3719
3720 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003721otherfile_buf(
3722 buf_T *buf,
3723 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003725 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003727 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728{
Bram Moolenaarc667da52019-11-30 20:52:27 +01003729 // no name is different
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3731 return TRUE;
3732 if (fnamecmp(ffname, buf->b_ffname) == 0)
3733 return FALSE;
3734#ifdef UNIX
3735 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003736 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737
Bram Moolenaarc667da52019-11-30 20:52:27 +01003738 // If no stat_T given, get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 if (stp == NULL)
3740 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003741 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 st.st_dev = (dev_T)-1;
3743 stp = &st;
3744 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003745 // Use dev/ino to check if the files are the same, even when the names
3746 // are different (possible with links). Still need to compare the
3747 // name above, for when the file doesn't exist yet.
3748 // Problem: The dev/ino changes when a file is deleted (and created
3749 // again) and remains the same when renamed/moved. We don't want to
3750 // mch_stat() each buffer each time, that would be too slow. Get the
3751 // dev/ino again when they appear to match, but not when they appear
3752 // to be different: Could skip a buffer when it's actually the same
3753 // file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 if (buf_same_ino(buf, stp))
3755 {
3756 buf_setino(buf);
3757 if (buf_same_ino(buf, stp))
3758 return FALSE;
3759 }
3760 }
3761#endif
3762 return TRUE;
3763}
3764
3765#if defined(UNIX) || defined(PROTO)
3766/*
3767 * Set inode and device number for a buffer.
3768 * Must always be called when b_fname is changed!.
3769 */
3770 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003771buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003773 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774
3775 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3776 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003777 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 buf->b_dev = st.st_dev;
3779 buf->b_ino = st.st_ino;
3780 }
3781 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003782 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783}
3784
3785/*
3786 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3787 */
3788 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003789buf_same_ino(
3790 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003791 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003793 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 && stp->st_dev == buf->b_dev
3795 && stp->st_ino == buf->b_ino);
3796}
3797#endif
3798
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003799/*
3800 * Print info about the current buffer.
3801 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003803fileinfo(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003804 int fullname, // when non-zero print full path
Bram Moolenaar7454a062016-01-30 15:14:10 +01003805 int shorthelp,
3806 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807{
3808 char_u *name;
3809 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003810 char *p;
3811 char *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003812 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003814 buffer = alloc(IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 if (buffer == NULL)
3816 return;
3817
Bram Moolenaarc667da52019-11-30 20:52:27 +01003818 if (fullname > 1) // 2 CTRL-G: include buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003820 vim_snprintf(buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 p = buffer + STRLEN(buffer);
3822 }
3823 else
3824 p = buffer;
3825
3826 *p++ = '"';
3827 if (buf_spname(curbuf) != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003828 vim_strncpy((char_u *)p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 else
3830 {
3831 if (!fullname && curbuf->b_fname != NULL)
3832 name = curbuf->b_fname;
3833 else
3834 name = curbuf->b_ffname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003835 home_replace(shorthelp ? curbuf : NULL, name, (char_u *)p,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 (int)(IOSIZE - (p - buffer)), TRUE);
3837 }
3838
Bram Moolenaar32526b32019-01-19 17:43:09 +01003839 vim_snprintf_add(buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 curbufIsChanged() ? (shortmess(SHM_MOD)
3841 ? " [+]" : _(" [Modified]")) : " ",
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01003842 (curbuf->b_flags & BF_NOTEDITED) && !bt_dontwrite(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 ? _("[Not edited]") : "",
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01003844 (curbuf->b_flags & BF_NEW) && !bt_dontwrite(curbuf)
Bram Moolenaar722e5052020-06-12 22:31:00 +02003845 ? new_file_message() : "",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003847 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 : _("[readonly]")) : "",
3849 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3850 || curbuf->b_p_ro) ?
3851 " " : "");
Bram Moolenaarc667da52019-11-30 20:52:27 +01003852 // With 32 bit longs and more than 21,474,836 lines multiplying by 100
3853 // causes an overflow, thus for large numbers divide instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 if (curwin->w_cursor.lnum > 1000000L)
3855 n = (int)(((long)curwin->w_cursor.lnum) /
3856 ((long)curbuf->b_ml.ml_line_count / 100L));
3857 else
3858 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3859 (long)curbuf->b_ml.ml_line_count);
3860 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003861 vim_snprintf_add(buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 else if (p_ru)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003863 // Current line and column are already on the screen -- webb
Bram Moolenaar32526b32019-01-19 17:43:09 +01003864 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003865 NGETTEXT("%ld line --%d%%--", "%ld lines --%d%%--",
3866 curbuf->b_ml.ml_line_count),
3867 (long)curbuf->b_ml.ml_line_count, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 else
3869 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003870 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 _("line %ld of %ld --%d%%-- col "),
3872 (long)curwin->w_cursor.lnum,
3873 (long)curbuf->b_ml.ml_line_count,
3874 n);
3875 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003876 len = STRLEN(buffer);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003877 col_print((char_u *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3879 }
3880
Bram Moolenaar32526b32019-01-19 17:43:09 +01003881 (void)append_arg_number(curwin, (char_u *)buffer, IOSIZE,
3882 !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883
3884 if (dont_truncate)
3885 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003886 // Temporarily set msg_scroll to avoid the message being truncated.
3887 // First call msg_start() to get the message in the right place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 msg_start();
3889 n = msg_scroll;
3890 msg_scroll = TRUE;
3891 msg(buffer);
3892 msg_scroll = n;
3893 }
3894 else
3895 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003896 p = msg_trunc_attr(buffer, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaarc667da52019-11-30 20:52:27 +01003898 // Need to repeat the message after redrawing when:
3899 // - When restart_edit is set (otherwise there will be a delay
3900 // before redrawing).
3901 // - When the screen was scrolled but there is no wait-return
3902 // prompt.
Bram Moolenaar32526b32019-01-19 17:43:09 +01003903 set_keep_msg((char_u *)p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 }
3905
3906 vim_free(buffer);
3907}
3908
3909 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003910col_print(
3911 char_u *buf,
3912 size_t buflen,
3913 int col,
3914 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915{
3916 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003917 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003919 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920}
3921
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922static char_u *lasttitle = NULL;
3923static char_u *lasticon = NULL;
3924
Bram Moolenaar84a93082018-06-16 22:58:15 +02003925/*
3926 * Put the file name in the title bar and icon of the window.
3927 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003929maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930{
3931 char_u *p;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003932 char_u *title_str = NULL;
3933 char_u *icon_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 int maxlen = 0;
3935 int len;
3936 int mustset;
3937 char_u buf[IOSIZE];
3938 int off;
3939
3940 if (!redrawing())
3941 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003942 // Postpone updating the title when 'lazyredraw' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 need_maketitle = TRUE;
3944 return;
3945 }
3946
3947 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003948 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003949 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950
3951 if (p_title)
3952 {
3953 if (p_titlelen > 0)
3954 {
3955 maxlen = p_titlelen * Columns / 100;
3956 if (maxlen < 10)
3957 maxlen = 10;
3958 }
3959
Bram Moolenaar84a93082018-06-16 22:58:15 +02003960 title_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 if (*p_titlestring != NUL)
3962 {
3963#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003964 if (stl_syntax & STL_IN_TITLE)
Luuk van Baal7b224fd2022-11-07 12:16:51 +00003965 build_stl_str_hl(curwin, title_str, sizeof(buf), p_titlestring,
3966 (char_u *)"titlestring", 0,
3967 0, maxlen, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 else
3969#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003970 title_str = p_titlestring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 }
3972 else
3973 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003974 // format: "fname + (path) (1 of 2) - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975
Bram Moolenaar2c666692012-09-05 13:30:40 +02003976#define SPACE_FOR_FNAME (IOSIZE - 100)
3977#define SPACE_FOR_DIR (IOSIZE - 20)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003978#define SPACE_FOR_ARGNR (IOSIZE - 10) // at least room for " - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003980 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar21554412017-07-24 21:44:43 +02003981#ifdef FEAT_TERMINAL
3982 else if (curbuf->b_term != NULL)
3983 {
3984 vim_strncpy(buf, term_get_status_text(curbuf->b_term),
3985 SPACE_FOR_FNAME);
3986 }
3987#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 else
3989 {
3990 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003991 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 }
3994
Bram Moolenaar21554412017-07-24 21:44:43 +02003995#ifdef FEAT_TERMINAL
3996 if (curbuf->b_term == NULL)
3997#endif
3998 switch (bufIsChanged(curbuf)
3999 + (curbuf->b_p_ro * 2)
4000 + (!curbuf->b_p_ma * 4))
4001 {
4002 case 1: STRCAT(buf, " +"); break;
4003 case 2: STRCAT(buf, " ="); break;
4004 case 3: STRCAT(buf, " =+"); break;
4005 case 4:
4006 case 6: STRCAT(buf, " -"); break;
4007 case 5:
4008 case 7: STRCAT(buf, " -+"); break;
4009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010
Bram Moolenaar21554412017-07-24 21:44:43 +02004011 if (curbuf->b_fname != NULL
4012#ifdef FEAT_TERMINAL
4013 && curbuf->b_term == NULL
4014#endif
4015 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004017 // Get path of file, replace home dir with ~
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 off = (int)STRLEN(buf);
4019 buf[off++] = ' ';
4020 buf[off++] = '(';
4021 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02004022 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc667da52019-11-30 20:52:27 +01004024 // avoid "c:/name" to be reduced to "c"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 if (isalpha(buf[off]) && buf[off + 1] == ':')
4026 off += 2;
4027#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01004028 // remove the file name
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004029 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 if (p == buf + off)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02004031 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004032 // must be a help buffer
Bram Moolenaar21554412017-07-24 21:44:43 +02004033 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02004034 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02004035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004038
Bram Moolenaarc667da52019-11-30 20:52:27 +01004039 // Translate unprintable chars and concatenate. Keep some
4040 // room for the server name. When there is no room (very long
4041 // file name) use (...).
Bram Moolenaar2c666692012-09-05 13:30:40 +02004042 if (off < SPACE_FOR_DIR)
4043 {
4044 p = transstr(buf + off);
4045 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
4046 vim_free(p);
4047 }
4048 else
4049 {
4050 vim_strncpy(buf + off, (char_u *)"...",
4051 (size_t)(SPACE_FOR_ARGNR - off));
4052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 STRCAT(buf, ")");
4054 }
4055
Bram Moolenaar2c666692012-09-05 13:30:40 +02004056 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057
4058#if defined(FEAT_CLIENTSERVER)
4059 if (serverName != NULL)
4060 {
4061 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02004062 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 }
4064 else
4065#endif
4066 STRCAT(buf, " - VIM");
4067
4068 if (maxlen > 0)
4069 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004070 // make it shorter by removing a bit in the middle
Bram Moolenaarf31b7642012-01-20 20:44:43 +01004071 if (vim_strsize(buf) > maxlen)
4072 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 }
4074 }
4075 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02004076 mustset = value_changed(title_str, &lasttitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077
4078 if (p_icon)
4079 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02004080 icon_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 if (*p_iconstring != NUL)
4082 {
4083#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00004084 if (stl_syntax & STL_IN_ICON)
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004085 build_stl_str_hl(curwin, icon_str, sizeof(buf), p_iconstring,
4086 (char_u *)"iconstring", 0, 0, 0, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 else
4088#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02004089 icon_str = p_iconstring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 }
4091 else
4092 {
4093 if (buf_spname(curbuf) != NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004094 p = buf_spname(curbuf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004095 else // use file name only in icon
Bram Moolenaar84a93082018-06-16 22:58:15 +02004096 p = gettail(curbuf->b_ffname);
4097 *icon_str = NUL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004098 // Truncate name at 100 bytes.
Bram Moolenaar84a93082018-06-16 22:58:15 +02004099 len = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 if (len > 100)
4101 {
4102 len -= 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 if (has_mbyte)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004104 len += (*mb_tail_off)(p, p + len) + 1;
Bram Moolenaar84a93082018-06-16 22:58:15 +02004105 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02004107 STRCPY(icon_str, p);
4108 trans_characters(icon_str, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 }
4110 }
4111
Bram Moolenaar84a93082018-06-16 22:58:15 +02004112 mustset |= value_changed(icon_str, &lasticon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113
4114 if (mustset)
4115 resettitle();
4116}
4117
4118/*
4119 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
4120 * from "str" if it does.
Bram Moolenaar84a93082018-06-16 22:58:15 +02004121 * Return TRUE if resettitle() is to be called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 */
4123 static int
Bram Moolenaar84a93082018-06-16 22:58:15 +02004124value_changed(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125{
4126 if ((str == NULL) != (*last == NULL)
4127 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
4128 {
4129 vim_free(*last);
4130 if (str == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004131 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 *last = NULL;
Bram Moolenaar40385db2018-08-07 22:31:44 +02004133 mch_restore_title(
4134 last == &lasttitle ? SAVE_RESTORE_TITLE : SAVE_RESTORE_ICON);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 else
Bram Moolenaar84a93082018-06-16 22:58:15 +02004137 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 *last = vim_strsave(str);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004139 return TRUE;
4140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 }
4142 return FALSE;
4143}
4144
4145/*
4146 * Put current window title back (used after calling a shell)
4147 */
4148 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004149resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150{
4151 mch_settitle(lasttitle, lasticon);
4152}
Bram Moolenaarea408852005-06-25 22:49:46 +00004153
4154# if defined(EXITFREE) || defined(PROTO)
4155 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004156free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00004157{
4158 vim_free(lasttitle);
4159 vim_free(lasticon);
4160}
4161# endif
4162
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004164#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004165
4166/*
4167 * Used for building in the status line.
4168 */
4169typedef struct
4170{
4171 char_u *stl_start;
4172 int stl_minwid;
4173 int stl_maxwid;
4174 enum {
4175 Normal,
4176 Empty,
4177 Group,
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004178 Separate,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004179 Highlight,
4180 TabPage,
4181 Trunc
4182 } stl_type;
4183} stl_item_T;
4184
4185static size_t stl_items_len = 20; // Initial value, grows as needed.
4186static stl_item_T *stl_items = NULL;
4187static int *stl_groupitem = NULL;
4188static stl_hlrec_T *stl_hltab = NULL;
4189static stl_hlrec_T *stl_tabtab = NULL;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004190static int *stl_separator_locations = NULL;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004191
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004193 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 * Return length of string in screen cells.
4195 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004196 * Normally works for window "wp", except when working for 'tabline' then it
4197 * is "curwin".
4198 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 * Items are drawn interspersed with the text that surrounds it
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004200 * Specials: %-<wid>(xxx%) => group, %= => separation marker, %< => truncation
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
4202 *
4203 * If maxwidth is not zero, the string will be filled at any middle marker
4204 * or truncated if too long, fillchar is used for all whitespace.
4205 */
4206 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004207build_stl_str_hl(
4208 win_T *wp,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004209 char_u *out, // buffer to write into != NameBuff
4210 size_t outlen, // length of out[]
Bram Moolenaar7454a062016-01-30 15:14:10 +01004211 char_u *fmt,
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004212 char_u *opt_name, // option name corresponding to "fmt"
4213 int opt_scope, // scope for "opt_name"
Bram Moolenaar7454a062016-01-30 15:14:10 +01004214 int fillchar,
4215 int maxwidth,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004216 stl_hlrec_T **hltab, // return: HL attributes (can be NULL)
4217 stl_hlrec_T **tabtab) // return: tab page nrs (can be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218{
Bram Moolenaar10772302019-01-20 18:25:54 +01004219 linenr_T lnum;
4220 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 char_u *p;
4222 char_u *s;
4223 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004224 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225#ifdef FEAT_EVAL
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004226 int use_sandbox;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004227 win_T *save_curwin;
4228 buf_T *save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004229 int save_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230#endif
4231 int empty_line;
4232 colnr_T virtcol;
4233 long l;
4234 long n;
4235 int prevchar_isflag;
4236 int prevchar_isitem;
4237 int itemisflag;
4238 int fillable;
4239 char_u *str;
4240 long num;
4241 int width;
4242 int itemcnt;
4243 int curitem;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004244 int group_end_userhl;
4245 int group_start_userhl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 int groupdepth;
shadmansaleh30e3de22021-05-15 17:23:28 +02004247#ifdef FEAT_EVAL
4248 int evaldepth;
4249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 int minwid;
4251 int maxwid;
4252 int zeropad;
4253 char_u base;
4254 char_u opt;
4255#define TMPLEN 70
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004256 char_u buf_tmp[TMPLEN];
4257 char_u win_tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004258 char_u *usefmt = fmt;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004259 stl_hlrec_T *sp;
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01004260 int save_redraw_not_allowed = redraw_not_allowed;
Bram Moolenaar0e1f36f2022-02-15 16:17:44 +00004261 int save_KeyTyped = KeyTyped;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004262 // TODO: find out why using called_emsg_before makes tests fail, does it
4263 // matter?
4264 // int called_emsg_before = called_emsg;
4265 int did_emsg_before = did_emsg;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004266
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01004267 // When inside update_screen() we do not want redrawing a statusline,
4268 // ruler, title, etc. to trigger another redraw, it may cause an endless
4269 // loop.
4270 if (updating_screen)
4271 redraw_not_allowed = TRUE;
4272
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004273 if (stl_items == NULL)
4274 {
4275 stl_items = ALLOC_MULT(stl_item_T, stl_items_len);
4276 stl_groupitem = ALLOC_MULT(int, stl_items_len);
Brandon Richardsona493b652022-02-19 11:45:03 +00004277
4278 // Allocate one more, because the last element is used to indicate the
4279 // end of the list.
4280 stl_hltab = ALLOC_MULT(stl_hlrec_T, stl_items_len + 1);
4281 stl_tabtab = ALLOC_MULT(stl_hlrec_T, stl_items_len + 1);
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004282
4283 stl_separator_locations = ALLOC_MULT(int, stl_items_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004284 }
4285
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004286#ifdef FEAT_EVAL
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004287 // if "fmt" was set insecurely it needs to be evaluated in the sandbox
4288 use_sandbox = was_set_insecurely(opt_name, opt_scope);
4289
4290 // When the format starts with "%!" then evaluate it as an expression and
4291 // use the result as the actual format string.
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004292 if (fmt[0] == '%' && fmt[1] == '!')
4293 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004294 typval_T tv;
4295
4296 tv.v_type = VAR_NUMBER;
4297 tv.vval.v_number = wp->w_id;
4298 set_var((char_u *)"g:statusline_winid", &tv, FALSE);
4299
Bram Moolenaara4e0b972022-10-01 19:43:52 +01004300 usefmt = eval_to_string_safe(fmt + 2, use_sandbox, FALSE, FALSE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004301 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00004302 usefmt = fmt;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004303
4304 do_unlet((char_u *)"g:statusline_winid", TRUE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004305 }
4306#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307
4308 if (fillchar == 0)
4309 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004310
Bram Moolenaar10772302019-01-20 18:25:54 +01004311 // The cursor in windows other than the current one isn't always
4312 // up-to-date, esp. because of autocommands and timers.
4313 lnum = wp->w_cursor.lnum;
4314 if (lnum > wp->w_buffer->b_ml.ml_line_count)
4315 {
4316 lnum = wp->w_buffer->b_ml.ml_line_count;
4317 wp->w_cursor.lnum = lnum;
4318 }
4319
4320 // Get line & check if empty (cursorpos will show "0-1"). Note that
4321 // p will become invalid when getting another buffer line.
4322 p = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar567199b2013-04-24 16:52:36 +02004323 empty_line = (*p == NUL);
4324
Bram Moolenaar10772302019-01-20 18:25:54 +01004325 // Get the byte value now, in case we need it below. This is more efficient
4326 // than making a copy of the line.
4327 len = STRLEN(p);
4328 if (wp->w_cursor.col > (colnr_T)len)
4329 {
4330 // Line may have changed since checking the cursor column, or the lnum
4331 // was adjusted above.
4332 wp->w_cursor.col = (colnr_T)len;
Bram Moolenaar10772302019-01-20 18:25:54 +01004333 wp->w_cursor.coladd = 0;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004334 byteval = 0;
Bram Moolenaar10772302019-01-20 18:25:54 +01004335 }
Bram Moolenaar567199b2013-04-24 16:52:36 +02004336 else
Bram Moolenaar567199b2013-04-24 16:52:36 +02004337 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338
4339 groupdepth = 0;
shadmansaleh30e3de22021-05-15 17:23:28 +02004340#ifdef FEAT_EVAL
4341 evaldepth = 0;
4342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 p = out;
4344 curitem = 0;
4345 prevchar_isflag = TRUE;
4346 prevchar_isitem = FALSE;
zeertzjq122dea72022-07-27 15:48:45 +01004347 for (s = usefmt; *s != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004349 if (curitem == (int)stl_items_len)
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004350 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004351 size_t new_len = stl_items_len * 3 / 2;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004352
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004353 stl_item_T *new_items =
4354 vim_realloc(stl_items, sizeof(stl_item_T) * new_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004355 if (new_items == NULL)
4356 break;
4357 stl_items = new_items;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004358
4359 int *new_groupitem =
4360 vim_realloc(stl_groupitem, sizeof(int) * new_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004361 if (new_groupitem == NULL)
4362 break;
4363 stl_groupitem = new_groupitem;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004364
4365 stl_hlrec_T *new_hlrec = vim_realloc(stl_hltab,
Brandon Richardsona493b652022-02-19 11:45:03 +00004366 sizeof(stl_hlrec_T) * (new_len + 1));
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004367 if (new_hlrec == NULL)
4368 break;
4369 stl_hltab = new_hlrec;
Brandon Richardsona493b652022-02-19 11:45:03 +00004370 new_hlrec = vim_realloc(stl_tabtab,
4371 sizeof(stl_hlrec_T) * (new_len + 1));
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004372 if (new_hlrec == NULL)
4373 break;
4374 stl_tabtab = new_hlrec;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004375
4376 int *new_separator_locs = vim_realloc(stl_separator_locations,
4377 sizeof(int) * new_len);
4378 if (new_separator_locs == NULL)
4379 break;
4380 stl_separator_locations = new_separator_locs;;
4381
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004382 stl_items_len = new_len;
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004383 }
4384
zeertzjq122dea72022-07-27 15:48:45 +01004385 if (*s != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 prevchar_isflag = prevchar_isitem = FALSE;
4387
4388 /*
4389 * Handle up to the next '%' or the end.
4390 */
4391 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
4392 *p++ = *s++;
4393 if (*s == NUL || p + 1 >= out + outlen)
4394 break;
4395
4396 /*
4397 * Handle one '%' item.
4398 */
4399 s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004400 if (*s == NUL) // ignore trailing %
Bram Moolenaar1d87f512011-02-01 21:55:01 +01004401 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 if (*s == '%')
4403 {
4404 if (p + 1 >= out + outlen)
4405 break;
4406 *p++ = *s++;
4407 prevchar_isflag = prevchar_isitem = FALSE;
4408 continue;
4409 }
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004410 // STL_SEPARATE: Separation between items, filled with white space.
4411 if (*s == STL_SEPARATE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 {
4413 s++;
4414 if (groupdepth > 0)
4415 continue;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004416 stl_items[curitem].stl_type = Separate;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004417 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 continue;
4419 }
4420 if (*s == STL_TRUNCMARK)
4421 {
4422 s++;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004423 stl_items[curitem].stl_type = Trunc;
4424 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 continue;
4426 }
4427 if (*s == ')')
4428 {
4429 s++;
4430 if (groupdepth < 1)
4431 continue;
4432 groupdepth--;
4433
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004434 t = stl_items[stl_groupitem[groupdepth]].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 *p = NUL;
4436 l = vim_strsize(t);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004437 if (curitem > stl_groupitem[groupdepth] + 1
4438 && stl_items[stl_groupitem[groupdepth]].stl_minwid == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004440 // remove group if all items are empty and highlight group
4441 // doesn't change
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004442 group_start_userhl = group_end_userhl = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004443 for (n = stl_groupitem[groupdepth] - 1; n >= 0; n--)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004444 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004445 if (stl_items[n].stl_type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004446 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004447 group_start_userhl = group_end_userhl =
4448 stl_items[n].stl_minwid;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004449 break;
4450 }
4451 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004452 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004453 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004454 if (stl_items[n].stl_type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 break;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004456 if (stl_items[n].stl_type == Highlight)
4457 group_end_userhl = stl_items[n].stl_minwid;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004458 }
4459 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 {
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004461 // empty group
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 p = t;
4463 l = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004464 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004465 {
4466 // do not use the highlighting from the removed group
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004467 if (stl_items[n].stl_type == Highlight)
4468 stl_items[n].stl_type = Empty;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004469 // adjust the start position of TabPage to the next
4470 // item position
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004471 if (stl_items[n].stl_type == TabPage)
4472 stl_items[n].stl_start = p;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 }
4475 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004476 if (l > stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004478 // truncate, remove n bytes of text at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 if (has_mbyte)
4480 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004481 // Find the first character that should be included.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 n = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004483 while (l >= stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 {
4485 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004486 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 }
4488 }
4489 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004490 n = (long)(p - t) - stl_items[stl_groupitem[groupdepth]]
4491 .stl_maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492
4493 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004494 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 p = p - n + 1;
Bram Moolenaar13505972019-01-24 15:04:48 +01004496
4497 // Fill up space left over by half a double-wide char.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004498 while (++l < stl_items[stl_groupitem[groupdepth]].stl_minwid)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004499 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500
Bram Moolenaarc667da52019-11-30 20:52:27 +01004501 // correct the start of the items for the truncation
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004502 for (l = stl_groupitem[groupdepth] + 1; l < curitem; l++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 {
LemonBoy57ff5262022-05-09 21:03:47 +01004504 // Minus one for the leading '<' added above.
4505 stl_items[l].stl_start -= n - 1;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004506 if (stl_items[l].stl_start < t)
4507 stl_items[l].stl_start = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 }
4509 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004510 else if (abs(stl_items[stl_groupitem[groupdepth]].stl_minwid) > l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004512 // fill
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004513 n = stl_items[stl_groupitem[groupdepth]].stl_minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 if (n < 0)
4515 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004516 // fill by appending characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 n = 0 - n;
4518 while (l++ < n && p + 1 < out + outlen)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004519 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 }
4521 else
4522 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004523 // fill by inserting characters
Bram Moolenaar008bff92021-03-04 21:55:58 +01004524 l = (n - l) * MB_CHAR2LEN(fillchar);
4525 mch_memmove(t + l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004527 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 p += l;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004529 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
4530 stl_items[n].stl_start += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 for ( ; l > 0; l--)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004532 MB_CHAR2BYTES(fillchar, t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 }
4534 }
4535 continue;
4536 }
4537 minwid = 0;
4538 maxwid = 9999;
4539 zeropad = FALSE;
4540 l = 1;
4541 if (*s == '0')
4542 {
4543 s++;
4544 zeropad = TRUE;
4545 }
4546 if (*s == '-')
4547 {
4548 s++;
4549 l = -1;
4550 }
4551 if (VIM_ISDIGIT(*s))
4552 {
4553 minwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004554 if (minwid < 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 minwid = 0;
4556 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004557 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004559 stl_items[curitem].stl_type = Highlight;
4560 stl_items[curitem].stl_start = p;
4561 stl_items[curitem].stl_minwid = minwid > 9 ? 1 : minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 s++;
4563 curitem++;
4564 continue;
4565 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004566 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4567 {
4568 if (*s == STL_TABCLOSENR)
4569 {
4570 if (minwid == 0)
4571 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004572 // %X ends the close label, go back to the previously
4573 // define tab label nr.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004574 for (n = curitem - 1; n >= 0; --n)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004575 if (stl_items[n].stl_type == TabPage
4576 && stl_items[n].stl_minwid >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004577 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004578 minwid = stl_items[n].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004579 break;
4580 }
4581 }
4582 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01004583 // close nrs are stored as negative values
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004584 minwid = - minwid;
4585 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004586 stl_items[curitem].stl_type = TabPage;
4587 stl_items[curitem].stl_start = p;
4588 stl_items[curitem].stl_minwid = minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004589 s++;
4590 curitem++;
4591 continue;
4592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 if (*s == '.')
4594 {
4595 s++;
4596 if (VIM_ISDIGIT(*s))
4597 {
4598 maxwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004599 if (maxwid <= 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 maxwid = 50;
4601 }
4602 }
4603 minwid = (minwid > 50 ? 50 : minwid) * l;
4604 if (*s == '(')
4605 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004606 stl_groupitem[groupdepth++] = curitem;
4607 stl_items[curitem].stl_type = Group;
4608 stl_items[curitem].stl_start = p;
4609 stl_items[curitem].stl_minwid = minwid;
4610 stl_items[curitem].stl_maxwid = maxwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 s++;
4612 curitem++;
4613 continue;
4614 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004615#ifdef FEAT_EVAL
4616 // Denotes end of expanded %{} block
4617 if (*s == '}' && evaldepth > 0)
4618 {
4619 s++;
4620 evaldepth--;
4621 continue;
4622 }
4623#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 if (vim_strchr(STL_ALL, *s) == NULL)
4625 {
Bram Moolenaar7b17eb42023-01-04 14:31:49 +00004626 if (*s == NUL) // can happen with "%0"
4627 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 s++;
4629 continue;
4630 }
4631 opt = *s++;
4632
Bram Moolenaarc667da52019-11-30 20:52:27 +01004633 // OK - now for the real work
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 base = 'D';
4635 itemisflag = FALSE;
4636 fillable = TRUE;
4637 num = -1;
4638 str = NULL;
4639 switch (opt)
4640 {
4641 case STL_FILEPATH:
4642 case STL_FULLPATH:
4643 case STL_FILENAME:
Bram Moolenaarc667da52019-11-30 20:52:27 +01004644 fillable = FALSE; // don't change ' ' to fillchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004646 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 else
4648 {
4649 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004650 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4652 }
4653 trans_characters(NameBuff, MAXPATHL);
4654 if (opt != STL_FILENAME)
4655 str = NameBuff;
4656 else
4657 str = gettail(NameBuff);
4658 break;
4659
Bram Moolenaarc667da52019-11-30 20:52:27 +01004660 case STL_VIM_EXPR: // '{'
shadmansaleh30e3de22021-05-15 17:23:28 +02004661 {
4662#ifdef FEAT_EVAL
4663 char_u *block_start = s - 1;
4664#endif
4665 int reevaluate = (*s == '%');
4666
4667 if (reevaluate)
4668 s++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 itemisflag = TRUE;
4670 t = p;
shadmansaleh30e3de22021-05-15 17:23:28 +02004671 while ((*s != '}' || (reevaluate && s[-1] != '%'))
4672 && *s != NUL && p + 1 < out + outlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 *p++ = *s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004674 if (*s != '}') // missing '}' or out of space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 break;
4676 s++;
shadmansaleh30e3de22021-05-15 17:23:28 +02004677 if (reevaluate)
4678 p[-1] = 0; // remove the % at the end of %{% expr %}
4679 else
4680 *p = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 p = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682#ifdef FEAT_EVAL
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004683 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp),
4684 "%d", curbuf->b_fnum);
4685 set_internal_string_var((char_u *)"g:actual_curbuf", buf_tmp);
4686 vim_snprintf((char *)win_tmp, sizeof(win_tmp), "%d", curwin->w_id);
4687 set_internal_string_var((char_u *)"g:actual_curwin", win_tmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004689 save_curbuf = curbuf;
4690 save_curwin = curwin;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004691 save_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 curwin = wp;
4693 curbuf = wp->w_buffer;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004694 // Visual mode is only valid in the current window.
4695 if (curwin != save_curwin)
4696 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697
Bram Moolenaara4e0b972022-10-01 19:43:52 +01004698 str = eval_to_string_safe(p, use_sandbox, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004700 curwin = save_curwin;
4701 curbuf = save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004702 VIsual_active = save_VIsual_active;
Bram Moolenaar01824652005-01-31 18:58:23 +00004703 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004704 do_unlet((char_u *)"g:actual_curwin", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705
4706 if (str != NULL && *str != 0)
4707 {
4708 if (*skipdigits(str) == NUL)
4709 {
4710 num = atoi((char *)str);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004711 VIM_CLEAR(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 itemisflag = FALSE;
4713 }
4714 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004715
4716 // If the output of the expression needs to be evaluated
4717 // replace the %{} block with the result of evaluation
4718 if (reevaluate && str != NULL && *str != 0
4719 && strchr((const char *)str, '%') != NULL
4720 && evaldepth < MAX_STL_EVAL_DEPTH)
4721 {
4722 size_t parsed_usefmt = (size_t)(block_start - usefmt);
4723 size_t str_length = strlen((const char *)str);
4724 size_t fmt_length = strlen((const char *)s);
4725 size_t new_fmt_len = parsed_usefmt
4726 + str_length + fmt_length + 3;
4727 char_u *new_fmt = (char_u *)alloc(new_fmt_len * sizeof(char_u));
4728 char_u *new_fmt_p = new_fmt;
4729
4730 new_fmt_p = (char_u *)memcpy(new_fmt_p, usefmt, parsed_usefmt)
4731 + parsed_usefmt;
4732 new_fmt_p = (char_u *)memcpy(new_fmt_p , str, str_length)
4733 + str_length;
4734 new_fmt_p = (char_u *)memcpy(new_fmt_p, "%}", 2) + 2;
4735 new_fmt_p = (char_u *)memcpy(new_fmt_p , s, fmt_length)
4736 + fmt_length;
4737 *new_fmt_p = 0;
4738 new_fmt_p = NULL;
4739
4740 if (usefmt != fmt)
4741 vim_free(usefmt);
4742 VIM_CLEAR(str);
4743 usefmt = new_fmt;
4744 s = usefmt + parsed_usefmt;
4745 evaldepth++;
4746 continue;
4747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748#endif
4749 break;
shadmansaleh30e3de22021-05-15 17:23:28 +02004750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 case STL_LINE:
4752 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4753 ? 0L : (long)(wp->w_cursor.lnum);
4754 break;
4755
4756 case STL_NUMLINES:
4757 num = wp->w_buffer->b_ml.ml_line_count;
4758 break;
4759
4760 case STL_COLUMN:
Bram Moolenaar24959102022-05-07 20:01:16 +01004761 num = (State & MODE_INSERT) == 0 && empty_line
4762 ? 0 : (int)wp->w_cursor.col + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 break;
4764
4765 case STL_VIRTCOL:
4766 case STL_VIRTCOL_ALT:
zeertzjq0f112052022-01-14 20:11:38 +00004767 virtcol = wp->w_virtcol + 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004768 // Don't display %V if it's the same as %c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 if (opt == STL_VIRTCOL_ALT
Bram Moolenaar24959102022-05-07 20:01:16 +01004770 && (virtcol == (colnr_T)((State & MODE_INSERT) == 0
4771 && empty_line ? 0 : (int)wp->w_cursor.col + 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 break;
4773 num = (long)virtcol;
4774 break;
4775
4776 case STL_PERCENTAGE:
4777 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4778 (long)wp->w_buffer->b_ml.ml_line_count);
4779 break;
4780
4781 case STL_ALTPERCENT:
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004782 str = buf_tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004783 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 break;
4785
Luuk van Baalba936f62022-12-15 13:15:39 +00004786 case STL_SHOWCMD:
4787 if (p_sc && STRCMP(opt_name, p_sloc) == 0)
4788 str = showcmd_buf;
4789 break;
4790
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 case STL_ARGLISTSTAT:
4792 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004793 buf_tmp[0] = 0;
4794 if (append_arg_number(wp, buf_tmp, (int)sizeof(buf_tmp), FALSE))
4795 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 break;
4797
4798 case STL_KEYMAP:
4799 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004800 if (get_keymap_str(wp, (char_u *)"<%s>", buf_tmp, TMPLEN))
4801 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 break;
4803 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004804#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004805 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806#else
4807 num = 0;
4808#endif
4809 break;
4810
4811 case STL_BUFNO:
4812 num = wp->w_buffer->b_fnum;
4813 break;
4814
4815 case STL_OFFSET_X:
4816 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004817 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 case STL_OFFSET:
4819#ifdef FEAT_BYTEOFF
4820 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
Bram Moolenaar24959102022-05-07 20:01:16 +01004821 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0
4822 ? 0L : l + 1 + ((State & MODE_INSERT) == 0 && empty_line
4823 ? 0 : (int)wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824#endif
4825 break;
4826
4827 case STL_BYTEVAL_X:
4828 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004829 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004831 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 if (num == NL)
4833 num = 0;
4834 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4835 num = NL;
4836 break;
4837
4838 case STL_ROFLAG:
4839 case STL_ROFLAG_ALT:
4840 itemisflag = TRUE;
4841 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004842 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 break;
4844
4845 case STL_HELPFLAG:
4846 case STL_HELPFLAG_ALT:
4847 itemisflag = TRUE;
4848 if (wp->w_buffer->b_help)
4849 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004850 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 break;
4852
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 case STL_FILETYPE:
4854 if (*wp->w_buffer->b_p_ft != NUL
4855 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4856 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004857 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), "[%s]",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004858 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004859 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 }
4861 break;
4862
4863 case STL_FILETYPE_ALT:
4864 itemisflag = TRUE;
4865 if (*wp->w_buffer->b_p_ft != NUL
4866 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4867 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004868 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), ",%s",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004869 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004870 for (t = buf_tmp; *t != 0; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 *t = TOUPPER_LOC(*t);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004872 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 }
4874 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875
Bram Moolenaar4033c552017-09-16 20:54:51 +02004876#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 case STL_PREVIEWFLAG:
4878 case STL_PREVIEWFLAG_ALT:
4879 itemisflag = TRUE;
4880 if (wp->w_p_pvw)
4881 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4882 : _("[Preview]"));
4883 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004884
4885 case STL_QUICKFIX:
4886 if (bt_quickfix(wp->w_buffer))
4887 str = (char_u *)(wp->w_llist_ref
4888 ? _(msg_loclist)
4889 : _(msg_qflist));
4890 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891#endif
4892
4893 case STL_MODIFIED:
4894 case STL_MODIFIED_ALT:
4895 itemisflag = TRUE;
4896 switch ((opt == STL_MODIFIED_ALT)
4897 + bufIsChanged(wp->w_buffer) * 2
4898 + (!wp->w_buffer->b_p_ma) * 4)
4899 {
4900 case 2: str = (char_u *)"[+]"; break;
4901 case 3: str = (char_u *)",+"; break;
4902 case 4: str = (char_u *)"[-]"; break;
4903 case 5: str = (char_u *)",-"; break;
4904 case 6: str = (char_u *)"[+-]"; break;
4905 case 7: str = (char_u *)",+-"; break;
4906 }
4907 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004908
4909 case STL_HIGHLIGHT:
4910 t = s;
4911 while (*s != '#' && *s != NUL)
4912 ++s;
4913 if (*s == '#')
4914 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004915 stl_items[curitem].stl_type = Highlight;
4916 stl_items[curitem].stl_start = p;
4917 stl_items[curitem].stl_minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004918 curitem++;
4919 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004920 if (*s != NUL)
4921 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004922 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 }
4924
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004925 stl_items[curitem].stl_start = p;
4926 stl_items[curitem].stl_type = Normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 if (str != NULL && *str)
4928 {
4929 t = str;
4930 if (itemisflag)
4931 {
4932 if ((t[0] && t[1])
4933 && ((!prevchar_isitem && *t == ',')
4934 || (prevchar_isflag && *t == ' ')))
4935 t++;
4936 prevchar_isflag = TRUE;
4937 }
4938 l = vim_strsize(t);
4939 if (l > 0)
4940 prevchar_isitem = TRUE;
4941 if (l > maxwid)
4942 {
4943 while (l >= maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 if (has_mbyte)
4945 {
4946 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004947 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 }
4949 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 l -= byte2cells(*t++);
4951 if (p + 1 >= out + outlen)
4952 break;
4953 *p++ = '<';
4954 }
4955 if (minwid > 0)
4956 {
4957 for (; l < minwid && p + 1 < out + outlen; l++)
4958 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004959 // Don't put a "-" in front of a digit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4961 *p++ = ' ';
4962 else
Bram Moolenaar008bff92021-03-04 21:55:58 +01004963 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 }
4965 minwid = 0;
4966 }
4967 else
4968 minwid *= -1;
Bram Moolenaar008bff92021-03-04 21:55:58 +01004969 for (; *t && p + 1 < out + outlen; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004971 // Change a space by fillchar, unless fillchar is '-' and a
4972 // digit follows.
Bram Moolenaar008bff92021-03-04 21:55:58 +01004973 if (fillable && *t == ' '
4974 && (!VIM_ISDIGIT(*(t + 1)) || fillchar != '-'))
4975 MB_CHAR2BYTES(fillchar, p);
4976 else
4977 *p++ = *t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 }
4979 for (; l < minwid && p + 1 < out + outlen; l++)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004980 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 }
4982 else if (num >= 0)
4983 {
4984 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4985 char_u nstr[20];
4986
4987 if (p + 20 >= out + outlen)
Bram Moolenaarc667da52019-11-30 20:52:27 +01004988 break; // not sufficient space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 prevchar_isitem = TRUE;
4990 t = nstr;
4991 if (opt == STL_VIRTCOL_ALT)
4992 {
4993 *t++ = '-';
4994 minwid--;
4995 }
4996 *t++ = '%';
4997 if (zeropad)
4998 *t++ = '0';
4999 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005000 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 *t = 0;
5002
5003 for (n = num, l = 1; n >= nbase; n /= nbase)
5004 l++;
5005 if (opt == STL_VIRTCOL_ALT)
5006 l++;
5007 if (l > maxwid)
5008 {
5009 l += 2;
5010 n = l - maxwid;
5011 while (l-- > maxwid)
5012 num /= nbase;
5013 *t++ = '>';
5014 *t++ = '%';
5015 *t = t[-3];
5016 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005017 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
5018 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 }
5020 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005021 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
5022 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 p += STRLEN(p);
5024 }
5025 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005026 stl_items[curitem].stl_type = Empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01005028 if (num >= 0 || (!itemisflag && str != NULL && *str != NUL))
5029 prevchar_isflag = FALSE; // Item not NULL, but not a flag
5030 //
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 if (opt == STL_VIM_EXPR)
5032 vim_free(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 curitem++;
5034 }
5035 *p = NUL;
5036 itemcnt = curitem;
5037
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005038#ifdef FEAT_EVAL
5039 if (usefmt != fmt)
5040 vim_free(usefmt);
5041#endif
5042
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 width = vim_strsize(out);
5044 if (maxwidth > 0 && width > maxwidth)
5045 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005046 // Result is too long, must truncate somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 l = 0;
5048 if (itemcnt == 0)
5049 s = out;
5050 else
5051 {
5052 for ( ; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005053 if (stl_items[l].stl_type == Trunc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005055 // Truncate at %< item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005056 s = stl_items[l].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 break;
5058 }
5059 if (l == itemcnt)
5060 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005061 // No %< item, truncate first item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005062 s = stl_items[0].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 l = 0;
5064 }
5065 }
5066
5067 if (width - vim_strsize(s) >= maxwidth)
5068 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005069 // Truncation mark is beyond max length
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 if (has_mbyte)
5071 {
5072 s = out;
5073 width = 0;
5074 for (;;)
5075 {
5076 width += ptr2cells(s);
5077 if (width >= maxwidth)
5078 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005079 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005081 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 while (++width < maxwidth)
Bram Moolenaar008bff92021-03-04 21:55:58 +01005083 MB_CHAR2BYTES(fillchar, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 }
5085 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 s = out + maxwidth - 1;
5087 for (l = 0; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005088 if (stl_items[l].stl_start > s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 break;
5090 itemcnt = l;
5091 *s++ = '>';
5092 *s = 0;
5093 }
5094 else
5095 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 if (has_mbyte)
5097 {
5098 n = 0;
5099 while (width >= maxwidth)
5100 {
5101 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005102 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 }
5104 }
5105 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 n = width - maxwidth + 1;
5107 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00005108 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 *s = '<';
5110
Bram Moolenaarc667da52019-11-30 20:52:27 +01005111 --n; // count the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 for (; l < itemcnt; l++)
5113 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005114 if (stl_items[l].stl_start - n >= s)
5115 stl_items[l].stl_start -= n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005117 stl_items[l].stl_start = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 }
zeertzjqd392a742023-07-01 20:24:40 +01005119
5120 // Fill up for half a double-wide character.
5121 while (++width < maxwidth)
5122 {
5123 s = s + STRLEN(s);
5124 MB_CHAR2BYTES(fillchar, s);
5125 *s = NUL;
5126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 }
5128 width = maxwidth;
5129 }
5130 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
5131 {
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00005132 // Find how many separators there are, which we will use when
5133 // figuring out how many groups there are.
5134 int num_separators = 0;
5135
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 for (l = 0; l < itemcnt; l++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 {
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00005138 if (stl_items[l].stl_type == Separate)
5139 {
5140 // Create an array of the start location for each separator
5141 // mark.
5142 stl_separator_locations[num_separators] = l;
5143 num_separators++;
5144 }
5145 }
5146
5147 // If we have separated groups, then we deal with it now
5148 if (num_separators)
5149 {
5150 int standard_spaces;
5151 int final_spaces;
5152
5153 standard_spaces = (maxwidth - width) / num_separators;
5154 final_spaces = (maxwidth - width) -
5155 standard_spaces * (num_separators - 1);
5156 for (l = 0; l < num_separators; l++)
5157 {
5158 int dislocation = (l == (num_separators - 1)) ?
5159 final_spaces : standard_spaces;
5160 dislocation *= MB_CHAR2LEN(fillchar);
5161 char_u *start = stl_items[stl_separator_locations[l]].stl_start;
5162 char_u *seploc = start + dislocation;
5163 STRMOVE(seploc, start);
5164 for (s = start; s < seploc;)
5165 MB_CHAR2BYTES(fillchar, s);
5166
5167 for (int i = stl_separator_locations[l] + 1; i < itemcnt; i++)
5168 stl_items[i].stl_start += dislocation;
5169 }
5170
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 width = maxwidth;
5172 }
5173 }
5174
Bram Moolenaarc667da52019-11-30 20:52:27 +01005175 // Store the info about highlighting.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005176 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005178 *hltab = stl_hltab;
5179 sp = stl_hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 for (l = 0; l < itemcnt; l++)
5181 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005182 if (stl_items[l].stl_type == Highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005184 sp->start = stl_items[l].stl_start;
5185 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005186 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 }
5188 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005189 sp->start = NULL;
5190 sp->userhl = 0;
5191 }
5192
Bram Moolenaarc667da52019-11-30 20:52:27 +01005193 // Store the info about tab pages labels.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005194 if (tabtab != NULL)
5195 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005196 *tabtab = stl_tabtab;
5197 sp = stl_tabtab;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005198 for (l = 0; l < itemcnt; l++)
5199 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005200 if (stl_items[l].stl_type == TabPage)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005201 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005202 sp->start = stl_items[l].stl_start;
5203 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005204 sp++;
5205 }
5206 }
5207 sp->start = NULL;
5208 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 }
5210
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01005211 redraw_not_allowed = save_redraw_not_allowed;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02005212
Bram Moolenaar0e1f36f2022-02-15 16:17:44 +00005213 // A user function may reset KeyTyped, restore it.
5214 KeyTyped = save_KeyTyped;
5215
Luuk van Baal7b224fd2022-11-07 12:16:51 +00005216 // Check for an error. If there is one the display will be messed up and
5217 // might loop redrawing. Avoid that by making the corresponding option
5218 // empty.
5219 // TODO: find out why using called_emsg_before makes tests fail, does it
5220 // matter?
5221 // if (called_emsg > called_emsg_before)
5222 if (did_emsg > did_emsg_before)
5223 set_string_option_direct(opt_name, -1, (char_u *)"",
5224 OPT_FREE | opt_scope, SID_ERROR);
5225
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 return width;
5227}
Bram Moolenaarc667da52019-11-30 20:52:27 +01005228#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230/*
Emir SARI971cd2b2023-04-29 12:09:53 +01005231 * Get relative cursor position in window into "buf[buflen]", in the localized
5232 * percentage form like %99, 99%; using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 */
5234 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005235get_rel_pos(
5236 win_T *wp,
5237 char_u *buf,
5238 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005240 long above; // number of lines above window
5241 long below; // number of lines below window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242
Bram Moolenaarc667da52019-11-30 20:52:27 +01005243 if (buflen < 3) // need at least 3 chars for writing
Bram Moolenaar0027c212015-01-07 13:31:52 +01005244 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 above = wp->w_topline - 1;
5246#ifdef FEAT_DIFF
5247 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02005248 if (wp->w_topline == 1 && wp->w_topfill >= 1)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005249 above = 0; // All buffer lines are displayed and there is an
5250 // indication of filler lines, that can be considered
5251 // seeing all lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252#endif
5253 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
5254 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005255 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
Emir SARI971cd2b2023-04-29 12:09:53 +01005256 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005258 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 else
Emir SARI971cd2b2023-04-29 12:09:53 +01005260 {
5261 int perc = (above > 1000000L)
5262 ? (int)(above / ((above + below) / 100L))
5263 : (int)(above * 100L / (above + below));
5264
5265 char *p = (char *)buf;
5266 size_t l = buflen;
5267 if (perc < 10)
5268 {
5269 // prepend one space
5270 buf[0] = ' ';
5271 ++p;
5272 --l;
5273 }
5274 // localized percentage value
5275 vim_snprintf(p, l, _("%d%%"), perc);
5276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278
5279/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005280 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 * Return TRUE if it was appended.
5282 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005283 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005284append_arg_number(
5285 win_T *wp,
5286 char_u *buf,
5287 int buflen,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005288 int add_file) // Add "file" before the arg number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005290 if (ARGCOUNT <= 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 return FALSE;
5292
Bram Moolenaara8490a42023-05-23 18:00:58 +01005293 char *msg;
5294 switch ((wp->w_arg_idx_invalid ? 1 : 0) + (add_file ? 2 : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 {
Bram Moolenaara8490a42023-05-23 18:00:58 +01005296 case 0: msg = _(" (%d of %d)"); break;
5297 case 1: msg = _(" ((%d) of %d)"); break;
5298 case 2: msg = _(" (file %d of %d)"); break;
5299 case 3: msg = _(" (file (%d) of %d)"); break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 }
Bram Moolenaara8490a42023-05-23 18:00:58 +01005301
5302 char_u *p = buf + STRLEN(buf); // go to the end of the buffer
5303 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)), msg,
5304 wp->w_arg_idx + 1, ARGCOUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 return TRUE;
5306}
5307
5308/*
5309 * If fname is not a full path, make it a full path.
5310 * Returns pointer to allocated memory (NULL for failure).
5311 */
5312 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005313fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314{
5315 /*
5316 * Force expanding the path always for Unix, because symbolic links may
5317 * mess up the full path name, even though it starts with a '/'.
5318 * Also expand when there is ".." in the file name, try to remove it,
5319 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00005320 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 * For MS-Windows also expand names like "longna~1" to "longname".
5322 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00005323#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 return FullName_save(fname, TRUE);
5325#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00005326 if (!vim_isAbsName(fname)
5327 || strstr((char *)fname, "..") != NULL
5328 || strstr((char *)fname, "//") != NULL
5329# ifdef BACKSLASH_IN_FILENAME
5330 || strstr((char *)fname, "\\\\") != NULL
5331# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005332# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00005334# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 )
5336 return FullName_save(fname, FALSE);
5337
5338 fname = vim_strsave(fname);
5339
Bram Moolenaar9b942202007-10-03 12:31:33 +00005340# ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01005341 if (fname != NULL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005342 fname_case(fname, 0); // set correct case for file name
Bram Moolenaar9b942202007-10-03 12:31:33 +00005343# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344
5345 return fname;
5346#endif
5347}
5348
5349/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005350 * Make "*ffname" a full file name, set "*sfname" to "*ffname" if not NULL.
5351 * "*ffname" becomes a pointer to allocated memory (or NULL).
5352 * When resolving a link both "*sfname" and "*ffname" will point to the same
5353 * allocated memory.
5354 * The "*ffname" and "*sfname" pointer values on call will not be freed.
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005355 * Note that the resulting "*ffname" pointer should be considered not allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005358fname_expand(
5359 buf_T *buf UNUSED,
5360 char_u **ffname,
5361 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005363 if (*ffname == NULL) // no file name given, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 return;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005365 if (*sfname == NULL) // no short file name given, use ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 *sfname = *ffname;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005367 *ffname = fix_fname(*ffname); // expand to full path
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368
5369#ifdef FEAT_SHORTCUT
5370 if (!buf->b_p_bin)
5371 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005372 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005374 // If the file name is a shortcut file, use the file it links to.
Bram Moolenaardce1e892019-02-10 23:18:53 +01005375 rfname = mch_resolve_path(*ffname, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005376 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 {
5378 vim_free(*ffname);
5379 *ffname = rfname;
5380 *sfname = rfname;
5381 }
5382 }
5383#endif
5384}
5385
5386/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 * Open a window for a number of buffers.
5388 */
5389 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005390ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391{
5392 buf_T *buf;
5393 win_T *wp, *wpnext;
5394 int split_ret = OK;
5395 int p_ea_save;
5396 int open_wins = 0;
5397 int r;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005398 int count; // Maximum number of windows to open.
5399 int all; // When TRUE also load inactive buffers.
Bram Moolenaare1004402020-10-24 20:49:43 +02005400 int had_tab = cmdmod.cmod_tab;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005401 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402
Bram Moolenaarc667da52019-11-30 20:52:27 +01005403 if (eap->addr_count == 0) // make as many windows as possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 count = 9999;
5405 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01005406 count = eap->line2; // make as many windows as specified
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5408 all = FALSE;
5409 else
5410 all = TRUE;
5411
Pavel Mayorove1121b12023-02-20 14:35:20 +00005412 // Stop Visual mode, the cursor and "VIsual" may very well be invalid after
5413 // switching to another buffer.
5414 reset_VIsual_and_resel();
5415
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 setpcmark();
5417
5418#ifdef FEAT_GUI
5419 need_mouse_correct = TRUE;
5420#endif
5421
5422 /*
5423 * Close superfluous windows (two windows for the same buffer).
5424 * Also close windows that are not full-width.
5425 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005426 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005427 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005428 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005430 tpnext = curtab->tp_next;
5431 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005433 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005434 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005435 || ((cmdmod.cmod_split & WSP_VERT)
5436 ? wp->w_height + wp->w_status_height < Rows - p_ch
5437 - tabline_height()
5438 : wp->w_width != Columns)
5439 || (had_tab > 0 && wp != firstwin))
5440 && !ONE_WINDOW
5441 && !(wp->w_closing || wp->w_buffer->b_locked > 0)
5442 && !win_unlisted(wp))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005443 {
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005444 if (win_close(wp, FALSE) == FAIL)
5445 break;
5446 // Just in case an autocommand does something strange with
5447 // windows: start all over...
5448 wpnext = firstwin;
5449 tpnext = first_tabpage;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005450 open_wins = 0;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005451 }
5452 else
5453 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005455
Bram Moolenaarc667da52019-11-30 20:52:27 +01005456 // Without the ":tab" modifier only do the current tab page.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005457 if (had_tab == 0 || tpnext == NULL)
5458 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005459 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 }
5461
5462 /*
5463 * Go through the buffer list. When a buffer doesn't have a window yet,
5464 * open one. Otherwise move the window to the right position.
5465 * Watch out for autocommands that delete buffers or windows!
5466 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01005467 // Don't execute Win/Buf Enter/Leave autocommands here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 ++autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 win_enter(lastwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5472 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005473 // Check if this buffer needs a window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5475 continue;
5476
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005477 if (had_tab != 0)
5478 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005479 // With the ":tab" modifier don't move the window.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005480 if (buf->b_nwindows > 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005481 wp = lastwin; // buffer has a window, skip it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005482 else
5483 wp = NULL;
5484 }
5485 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005486 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005487 // Check if this buffer already has a window
Bram Moolenaar29323592016-07-24 22:04:11 +02005488 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005489 if (wp->w_buffer == buf)
5490 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005491 // If the buffer already has a window, move it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005492 if (wp != NULL)
5493 win_move_after(wp, curwin);
5494 }
5495
5496 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005498 bufref_T bufref;
5499
5500 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005501
Bram Moolenaarc667da52019-11-30 20:52:27 +01005502 // Split the window and put the buffer in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 p_ea_save = p_ea;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005504 p_ea = TRUE; // use space from all windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5506 ++open_wins;
5507 p_ea = p_ea_save;
5508 if (split_ret == FAIL)
5509 continue;
5510
Bram Moolenaarc667da52019-11-30 20:52:27 +01005511 // Open the buffer in this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005514 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005516 // autocommands deleted the buffer!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 swap_exists_action = SEA_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 break;
5519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 if (swap_exists_action == SEA_QUIT)
5521 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005522#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005523 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005524
Bram Moolenaarc667da52019-11-30 20:52:27 +01005525 // Reset the error/interrupt/exception state here so that
5526 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005527 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005528#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005529
Bram Moolenaarc667da52019-11-30 20:52:27 +01005530 // User selected Quit at ATTENTION prompt; close this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 win_close(curwin, TRUE);
5532 --open_wins;
5533 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005534 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005535
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005536#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005537 // Restore the error/interrupt/exception state if not
5538 // discarded by a new aborting error, interrupt, or uncaught
5539 // exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005540 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005541#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 }
5543 else
5544 handle_swap_exists(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 }
5546
5547 ui_breakcheck();
5548 if (got_int)
5549 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005550 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 break;
5552 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005553#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01005554 // Autocommands deleted the buffer or aborted script processing!!!
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005555 if (aborting())
5556 break;
5557#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01005558 // When ":tab" was used open a new tab for a new window repeatedly.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005559 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
Bram Moolenaare1004402020-10-24 20:49:43 +02005560 cmdmod.cmod_tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 --autocmd_no_enter;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005563 win_enter(firstwin, FALSE); // back to first window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565
5566 /*
5567 * Close superfluous windows.
5568 */
5569 for (wp = lastwin; open_wins > count; )
5570 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005571 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 || autowrite(wp->w_buffer, FALSE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 if (!win_valid(wp))
5574 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005575 // BufWrite Autocommands made the window invalid, start over
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 wp = lastwin;
5577 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005578 else if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005580 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 --open_wins;
5582 wp = lastwin;
5583 }
5584 else
5585 {
5586 wp = wp->w_prev;
5587 if (wp == NULL)
5588 break;
5589 }
5590 }
5591}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005594static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005595
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596/*
5597 * do_modelines() - process mode lines for the current file
5598 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005599 * "flags" can be:
5600 * OPT_WINONLY only set options local to window
5601 * OPT_NOWIN don't set options local to window
5602 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 * Returns immediately if the "ml" option isn't set.
5604 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005606do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005608 linenr_T lnum;
5609 int nmlines;
5610 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611
5612 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5613 return;
5614
Bram Moolenaarc667da52019-11-30 20:52:27 +01005615 // Disallow recursive entry here. Can happen when executing a modeline
5616 // triggers an autocommand, which reloads modelines with a ":do".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 if (entered)
5618 return;
5619
5620 ++entered;
Hu Jialun9dcd3492021-08-28 20:42:50 +02005621 for (lnum = 1; curbuf->b_p_ml && lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005623 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 nmlines = 0;
5625
Hu Jialun9dcd3492021-08-28 20:42:50 +02005626 for (lnum = curbuf->b_ml.ml_line_count; curbuf->b_p_ml && lnum > 0 && lnum > nmlines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005628 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 nmlines = 0;
5630 --entered;
5631}
5632
Bram Moolenaarc667da52019-11-30 20:52:27 +01005633#include "version.h" // for version number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634
5635/*
5636 * chk_modeline() - check a single line for a mode string
5637 * Return FAIL if an error encountered.
5638 */
5639 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005640chk_modeline(
5641 linenr_T lnum,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005642 int flags) // Same as for do_modelines().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643{
5644 char_u *s;
5645 char_u *e;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005646 char_u *linecopy; // local copy of any modeline found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 int prev;
5648 int vers;
5649 int end;
5650 int retval = OK;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005651 sctx_T save_current_sctx;
ichizok7e5fe382023-04-15 13:17:50 +01005652 ESTACK_CHECK_DECLARATION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653
5654 prev = -1;
5655 for (s = ml_get(lnum); *s != NUL; ++s)
5656 {
5657 if (prev == -1 || vim_isspace(prev))
5658 {
5659 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5660 || STRNCMP(s, "vi:", (size_t)3) == 0)
5661 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005662 // Accept both "vim" and "Vim".
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005663 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 {
5665 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5666 e = s + 4;
5667 else
5668 e = s + 3;
5669 vers = getdigits(&e);
5670 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005671 && (s[0] != 'V'
5672 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 && (s[3] == ':'
5674 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5675 || (VIM_VERSION_100 < vers && s[3] == '<')
5676 || (VIM_VERSION_100 > vers && s[3] == '>')
5677 || (VIM_VERSION_100 == vers && s[3] == '=')))
5678 break;
5679 }
5680 }
5681 prev = *s;
5682 }
5683
5684 if (*s)
5685 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005686 do // skip over "ex:", "vi:" or "vim:"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 ++s;
5688 while (s[-1] != ':');
5689
Bram Moolenaarc667da52019-11-30 20:52:27 +01005690 s = linecopy = vim_strsave(s); // copy the line, it will change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 if (linecopy == NULL)
5692 return FAIL;
5693
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005694 // prepare for emsg()
5695 estack_push(ETYPE_MODELINE, (char_u *)"modelines", lnum);
ichizok7e5fe382023-04-15 13:17:50 +01005696 ESTACK_CHECK_SETUP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697
5698 end = FALSE;
5699 while (end == FALSE)
5700 {
5701 s = skipwhite(s);
5702 if (*s == NUL)
5703 break;
5704
5705 /*
5706 * Find end of set command: ':' or end of line.
5707 * Skip over "\:", replacing it with ":".
5708 */
5709 for (e = s; *e != ':' && *e != NUL; ++e)
5710 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005711 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 if (*e == NUL)
5713 end = TRUE;
5714
5715 /*
5716 * If there is a "set" command, require a terminating ':' and
5717 * ignore the stuff after the ':'.
5718 * "vi:set opt opt opt: foo" -- foo not interpreted
5719 * "vi:opt opt opt: foo" -- foo interpreted
5720 * Accept "se" for compatibility with Elvis.
5721 */
5722 if (STRNCMP(s, "set ", (size_t)4) == 0
5723 || STRNCMP(s, "se ", (size_t)3) == 0)
5724 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005725 if (*e != ':') // no terminating ':'?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 break;
5727 end = TRUE;
5728 s = vim_strchr(s, ' ') + 1;
5729 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005730 *e = NUL; // truncate the set command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731
Bram Moolenaarc667da52019-11-30 20:52:27 +01005732 if (*s != NUL) // skip over an empty "::"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 {
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005734 int secure_save = secure;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005735
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005736 save_current_sctx = current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005737 current_sctx.sc_version = 1;
5738#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005739 current_sctx.sc_sid = SID_MODELINE;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005740 current_sctx.sc_seq = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005741 current_sctx.sc_lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005743
Bram Moolenaar5958f952018-11-20 04:25:21 +01005744 // Make sure no risky things are executed as a side effect.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01005745 secure = 1;
Bram Moolenaar5958f952018-11-20 04:25:21 +01005746
Bram Moolenaara3227e22006-03-08 21:32:40 +00005747 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar5958f952018-11-20 04:25:21 +01005748
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005749 secure = secure_save;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005750 current_sctx = save_current_sctx;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005751 if (retval == FAIL) // stop if error found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 break;
5753 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005754 s = e + 1; // advance to next part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 }
5756
ichizok7e5fe382023-04-15 13:17:50 +01005757 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005758 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 vim_free(linecopy);
5760 }
5761 return retval;
5762}
5763
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005764/*
Bram Moolenaar91335e52018-08-01 17:53:12 +02005765 * Return TRUE if "buf" is a normal buffer, 'buftype' is empty.
5766 */
5767 int
5768bt_normal(buf_T *buf)
5769{
5770 return buf != NULL && buf->b_p_bt[0] == NUL;
5771}
5772
5773/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005774 * Return TRUE if "buf" is the quickfix buffer.
5775 */
5776 int
Bram Moolenaar340dafd2022-08-25 16:16:45 +01005777bt_quickfix(buf_T *buf UNUSED)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005778{
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005779#ifdef FEAT_QUICKFIX
Christian Brabandt6e60cf42023-09-03 21:43:46 +02005780 return buf != NULL && buf_valid(buf) && buf->b_p_bt[0] == 'q';
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005781#else
5782 return FALSE;
Bram Moolenaar113e1072019-01-20 15:30:40 +01005783#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005784}
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005785
5786/*
5787 * Return TRUE if "buf" is a terminal buffer.
5788 */
5789 int
Bram Moolenaar340dafd2022-08-25 16:16:45 +01005790bt_terminal(buf_T *buf UNUSED)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005791{
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005792#if defined(FEAT_TERMINAL)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005793 return buf != NULL && buf->b_p_bt[0] == 't';
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005794#else
5795 return FALSE;
Bram Moolenaar113e1072019-01-20 15:30:40 +01005796#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005797}
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005798
5799/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005800 * Return TRUE if "buf" is a help buffer.
5801 */
5802 int
5803bt_help(buf_T *buf)
5804{
5805 return buf != NULL && buf->b_help;
5806}
5807
5808/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02005809 * Return TRUE if "buf" is a prompt buffer.
5810 */
5811 int
5812bt_prompt(buf_T *buf)
5813{
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005814 return buf != NULL && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'r';
5815}
5816
Dominique Pelle748b3082022-01-08 12:41:16 +00005817#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005818/*
5819 * Return TRUE if "buf" is a buffer for a popup window.
5820 */
5821 int
5822bt_popup(buf_T *buf)
5823{
5824 return buf != NULL && buf->b_p_bt != NULL
5825 && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'o';
Bram Moolenaarf2732452018-06-03 14:47:35 +02005826}
Dominique Pelle748b3082022-01-08 12:41:16 +00005827#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005828
5829/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005830 * Return TRUE if "buf" is a "nofile", "acwrite", "terminal" or "prompt"
Bram Moolenaarc3126192022-08-26 12:58:17 +01005831 * buffer. This means the buffer name may not be a file name, at least not for
5832 * writing the buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005833 */
5834 int
Bram Moolenaar26910de2019-06-15 19:37:15 +02005835bt_nofilename(buf_T *buf)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005836{
5837 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5838 || buf->b_p_bt[0] == 'a'
Bram Moolenaarf2732452018-06-03 14:47:35 +02005839 || buf->b_p_bt[0] == 't'
5840 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005841}
5842
Bram Moolenaarc3126192022-08-26 12:58:17 +01005843/*
5844 * Return TRUE if "buf" is a "nofile", "quickfix", "terminal" or "prompt"
5845 * buffer. This means the buffer is not to be read from a file.
5846 */
5847 static int
5848bt_nofileread(buf_T *buf)
5849{
5850 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5851 || buf->b_p_bt[0] == 't'
5852 || buf->b_p_bt[0] == 'q'
5853 || buf->b_p_bt[0] == 'p');
5854}
5855
Dominique Pelle748b3082022-01-08 12:41:16 +00005856#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005857/*
Bram Moolenaar26910de2019-06-15 19:37:15 +02005858 * Return TRUE if "buf" has 'buftype' set to "nofile".
5859 */
5860 int
5861bt_nofile(buf_T *buf)
5862{
5863 return buf != NULL && buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f';
5864}
Dominique Pelle748b3082022-01-08 12:41:16 +00005865#endif
Bram Moolenaar26910de2019-06-15 19:37:15 +02005866
5867/*
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01005868 * Return TRUE if "buf" is a "nowrite", "nofile", "terminal", "prompt", or
5869 * "popup" buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005870 */
5871 int
5872bt_dontwrite(buf_T *buf)
5873{
Bram Moolenaarf2732452018-06-03 14:47:35 +02005874 return buf != NULL && (buf->b_p_bt[0] == 'n'
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005875 || buf->b_p_bt[0] == 't'
5876 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005877}
5878
5879 int
5880bt_dontwrite_msg(buf_T *buf)
5881{
5882 if (bt_dontwrite(buf))
5883 {
Bram Moolenaarf1474d82021-12-31 19:59:55 +00005884 emsg(_(e_cannot_write_buftype_option_is_set));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005885 return TRUE;
5886 }
5887 return FALSE;
5888}
5889
5890/*
5891 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
5892 * and 'bufhidden'.
5893 */
5894 int
5895buf_hide(buf_T *buf)
5896{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005897 // 'bufhidden' overrules 'hidden' and ":hide", check it first
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005898 switch (buf->b_p_bh[0])
5899 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005900 case 'u': // "unload"
5901 case 'w': // "wipe"
5902 case 'd': return FALSE; // "delete"
5903 case 'h': return TRUE; // "hide"
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005904 }
Bram Moolenaare1004402020-10-24 20:49:43 +02005905 return (p_hid || (cmdmod.cmod_flags & CMOD_HIDE));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005906}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907
5908/*
5909 * Return special buffer name.
5910 * Returns NULL when the buffer has a normal file name.
5911 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005912 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005913buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914{
Bram Moolenaar4033c552017-09-16 20:54:51 +02005915#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005917 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005918 /*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005919 * Differentiate between the quickfix and location list buffers using
5920 * the buffer number stored in the global quickfix stack.
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005921 */
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005922 if (buf->b_fnum == qf_stack_get_bufnr())
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005923 return (char_u *)_(msg_qflist);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005924 else
5925 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02005928
Bram Moolenaarc667da52019-11-30 20:52:27 +01005929 // There is no _file_ when 'buftype' is "nofile", b_sfname
5930 // contains the name as specified by the user.
Bram Moolenaar26910de2019-06-15 19:37:15 +02005931 if (bt_nofilename(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 {
Bram Moolenaar21554412017-07-24 21:44:43 +02005933#ifdef FEAT_TERMINAL
5934 if (buf->b_term != NULL)
5935 return term_get_status_text(buf->b_term);
5936#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02005937 if (buf->b_fname != NULL)
5938 return buf->b_fname;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02005939#ifdef FEAT_JOB_CHANNEL
5940 if (bt_prompt(buf))
5941 return (char_u *)_("[Prompt]");
5942#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005943#ifdef FEAT_PROP_POPUP
Bram Moolenaarc6896e22019-05-30 22:32:34 +02005944 if (bt_popup(buf))
5945 return (char_u *)_("[Popup]");
5946#endif
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005947 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 }
Bram Moolenaar21554412017-07-24 21:44:43 +02005949
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 if (buf->b_fname == NULL)
Bram Moolenaar00806bc2020-11-05 19:36:38 +01005951 return buf_get_fname(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 return NULL;
5953}
5954
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955/*
Bram Moolenaar00806bc2020-11-05 19:36:38 +01005956 * Get "buf->b_fname", use "[No Name]" if it is NULL.
5957 */
5958 char_u *
5959buf_get_fname(buf_T *buf)
5960{
5961 if (buf->b_fname == NULL)
5962 return (char_u *)_("[No Name]");
5963 return buf->b_fname;
5964}
5965
5966/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5968 */
5969 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005970set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005972 if (on == curbuf->b_p_bl)
5973 return;
5974
5975 curbuf->b_p_bl = on;
5976 if (on)
5977 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5978 else
5979 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980}
5981
5982/*
5983 * Read the file for "buf" again and check if the contents changed.
5984 * Return TRUE if it changed or this could not be checked.
5985 */
5986 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005987buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988{
5989 buf_T *newbuf;
5990 int differ = TRUE;
5991 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 exarg_T ea;
5994
Bram Moolenaarc667da52019-11-30 20:52:27 +01005995 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5997 if (newbuf == NULL)
5998 return TRUE;
5999
Bram Moolenaarc667da52019-11-30 20:52:27 +01006000 // Force the 'fileencoding' and 'fileformat' to be equal.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 if (prep_exarg(&ea, buf) == FAIL)
6002 {
6003 wipe_buffer(newbuf, FALSE);
6004 return TRUE;
6005 }
6006
Bram Moolenaare76062c2022-11-28 18:51:43 +00006007 // Set curwin/curbuf to buf and save a few things.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00006009 if (curbuf != newbuf)
6010 {
6011 // Failed to find a window for "newbuf".
6012 wipe_buffer(newbuf, FALSE);
6013 return TRUE;
6014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015
Bram Moolenaar4770d092006-01-12 23:22:24 +00006016 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 && readfile(buf->b_ffname, buf->b_fname,
6018 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
6019 &ea, READ_NEW | READ_DUMMY) == OK)
6020 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01006021 // compare the two files line by line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
6023 {
6024 differ = FALSE;
6025 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6026 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
6027 {
6028 differ = TRUE;
6029 break;
6030 }
6031 }
6032 }
6033 vim_free(ea.cmd);
6034
Bram Moolenaarc667da52019-11-30 20:52:27 +01006035 // restore curwin/curbuf and a few other things
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037
Bram Moolenaarc667da52019-11-30 20:52:27 +01006038 if (curbuf != newbuf) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 wipe_buffer(newbuf, FALSE);
6040
6041 return differ;
6042}
6043
6044/*
6045 * Wipe out a buffer and decrement the last buffer number if it was used for
6046 * this buffer. Call this to wipe out a temp buffer that does not contain any
6047 * marks.
6048 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006050wipe_buffer(
6051 buf_T *buf,
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006052 int aucmd) // When TRUE trigger autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053{
6054 if (buf->b_fnum == top_file_num - 1)
6055 --top_file_num;
6056
Bram Moolenaarc667da52019-11-30 20:52:27 +01006057 if (!aucmd) // Don't trigger BufDelete autocommands here.
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006058 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006059
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006060 close_buffer(NULL, buf, DOBUF_WIPE, FALSE, TRUE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006061
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006063 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064}