blob: 4a39329c5c602617e62e524b99569b71390545e1 [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)
Christian Brabandt9071ed82024-02-15 20:17:37 +0100922 // free the memory allocated for undo
923 // and reset all undo information
924 u_clearallandblockfree(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100926 syntax_clear(&buf->b_s); // reset syntax info
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100928#ifdef FEAT_PROP_POPUP
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100929 clear_buf_prop_types(buf);
930#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100931 buf->b_flags &= ~BF_READERR; // a read error is no longer relevant
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932}
933
934/*
935 * Free a buffer structure and the things it contains related to the buffer
936 * itself (not the file, that must have been done already).
937 */
938 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100939free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200941 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200943#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100944 // b:changedtick uses an item in buf_T, remove it now
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000945 dictitem_remove(buf->b_vars, (dictitem_T *)&buf->b_ct_di, "free buffer");
Bram Moolenaar429fa852013-04-15 12:27:36 +0200946 unref_var_dict(buf->b_vars);
Bram Moolenaar86173482019-10-01 17:02:16 +0200947 remove_listeners(buf);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200948#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200949#ifdef FEAT_LUA
950 lua_buffer_free(buf);
951#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000952#ifdef FEAT_MZSCHEME
953 mzscheme_buffer_free(buf);
954#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955#ifdef FEAT_PERL
956 perl_buf_free(buf);
957#endif
958#ifdef FEAT_PYTHON
959 python_buffer_free(buf);
960#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200961#ifdef FEAT_PYTHON3
962 python3_buffer_free(buf);
963#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964#ifdef FEAT_RUBY
965 ruby_buffer_free(buf);
966#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200967#ifdef FEAT_JOB_CHANNEL
968 channel_buffer_free(buf);
969#endif
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200970#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +0200971 free_terminal(buf);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200972#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +0200973#ifdef FEAT_JOB_CHANNEL
974 vim_free(buf->b_prompt_text);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200975 free_callback(&buf->b_prompt_callback);
Bram Moolenaar86173482019-10-01 17:02:16 +0200976 free_callback(&buf->b_prompt_interrupt);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200977#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200978
979 buf_hashtab_remove(buf);
980
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200981 aubuflocal_remove(buf);
982
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200983 if (autocmd_busy)
984 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100985 // Do not free the buffer structure while autocommands are executing,
986 // it's still needed. Free it when autocmd_busy is reset.
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200987 buf->b_next = au_pending_free_buf;
988 au_pending_free_buf = buf;
989 }
990 else
Bram Moolenaarcee52202020-03-11 14:19:58 +0100991 {
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200992 vim_free(buf);
Bram Moolenaarcee52202020-03-11 14:19:58 +0100993 if (curbuf == buf)
994 curbuf = NULL; // make clear it's not to be used
995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996}
997
998/*
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100999 * Initializes b:changedtick.
Bram Moolenaar79518e22017-02-17 16:31:35 +01001000 */
1001 static void
1002init_changedtick(buf_T *buf)
1003{
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001004 dictitem_T *di = (dictitem_T *)&buf->b_ct_di;
Bram Moolenaar79518e22017-02-17 16:31:35 +01001005
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001006 di->di_flags = DI_FLAGS_FIX | DI_FLAGS_RO;
1007 di->di_tv.v_type = VAR_NUMBER;
1008 di->di_tv.v_lock = VAR_FIXED;
1009 di->di_tv.vval.v_number = 0;
1010
Bram Moolenaar92769c32017-02-25 15:41:37 +01001011#ifdef FEAT_EVAL
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001012 STRCPY(buf->b_ct_di.di_key, "changedtick");
1013 (void)dict_add(buf->b_vars, di);
Bram Moolenaar92769c32017-02-25 15:41:37 +01001014#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01001015}
1016
1017/*
Bram Moolenaarc3126192022-08-26 12:58:17 +01001018 * Free the b_wininfo list for buffer "buf".
1019 */
1020 static void
1021clear_wininfo(buf_T *buf)
1022{
1023 wininfo_T *wip;
1024
1025 while (buf->b_wininfo != NULL)
1026 {
1027 wip = buf->b_wininfo;
1028 buf->b_wininfo = wip->wi_next;
1029 free_wininfo(wip);
1030 }
1031}
1032
1033/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
1035 */
1036 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001037free_buffer_stuff(
1038 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001039 int free_options) // free options as well
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040{
1041 if (free_options)
1042 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001043 clear_wininfo(buf); // including window-local options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +02001045#ifdef FEAT_SPELL
1046 ga_clear(&buf->b_s.b_langp);
1047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 }
1049#ifdef FEAT_EVAL
Bram Moolenaar79518e22017-02-17 16:31:35 +01001050 {
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001051 varnumber_T tick = CHANGEDTICK(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001052
Bram Moolenaarc667da52019-11-30 20:52:27 +01001053 vars_clear(&buf->b_vars->dv_hashtab); // free all buffer variables
Bram Moolenaar79518e22017-02-17 16:31:35 +01001054 hash_init(&buf->b_vars->dv_hashtab);
1055 init_changedtick(buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001056 CHANGEDTICK(buf) = tick;
Bram Moolenaarf10997a2020-01-03 21:00:02 +01001057 remove_listeners(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059#endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001060 uc_clear(&buf->b_ucmds); // clear local user commands
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061#ifdef FEAT_SIGNS
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001062 buf_delete_signs(buf, (char_u *)"*"); // delete any signs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00001064#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001065 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00001066#endif
Bram Moolenaar0c740e72022-07-25 19:07:04 +01001067#ifdef FEAT_PROP_POPUP
1068 ga_clear_strings(&buf->b_textprop_text);
1069#endif
zeertzjqc207fd22022-06-29 10:37:40 +01001070 map_clear_mode(buf, MAP_ALL_MODES, TRUE, FALSE); // clear local mappings
1071 map_clear_mode(buf, MAP_ALL_MODES, TRUE, TRUE); // clear local abbrevs
Bram Moolenaard23a8232018-02-10 18:45:26 +01001072 VIM_CLEAR(buf->b_start_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073}
1074
1075/*
Bram Moolenaar4882d982020-10-25 17:55:09 +01001076 * Free one wininfo_T.
1077 */
1078 void
1079free_wininfo(wininfo_T *wip)
1080{
1081 if (wip->wi_optset)
1082 {
1083 clear_winopt(&wip->wi_opt);
1084#ifdef FEAT_FOLDING
1085 deleteFoldRecurse(&wip->wi_folds);
1086#endif
1087 }
1088 vim_free(wip);
1089}
1090
1091/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 * Go to another buffer. Handles the result of the ATTENTION dialog.
1093 */
1094 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001095goto_buffer(
1096 exarg_T *eap,
1097 int start,
1098 int dir,
1099 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001101 bufref_T old_curbuf;
Bram Moolenaar188639d2022-04-04 16:57:21 +01001102 int save_sea = swap_exists_action;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001103
1104 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105
Bram Moolenaar188639d2022-04-04 16:57:21 +01001106 if (swap_exists_action == SEA_NONE)
1107 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
1109 start, dir, count, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
1111 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001112#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001113 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001114
Bram Moolenaarc667da52019-11-30 20:52:27 +01001115 // Reset the error/interrupt/exception state here so that
1116 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001117 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001118#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001119
Bram Moolenaarc667da52019-11-30 20:52:27 +01001120 // Quitting means closing the split window, nothing else.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 win_close(curwin, TRUE);
Bram Moolenaar188639d2022-04-04 16:57:21 +01001122 swap_exists_action = save_sea;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001123 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001124
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001125#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001126 // Restore the error/interrupt/exception state if not discarded by a
1127 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001128 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001129#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 }
1131 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001132 handle_swap_exists(&old_curbuf);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001133}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135/*
1136 * Handle the situation of swap_exists_action being set.
1137 * It is allowed for "old_curbuf" to be NULL or invalid.
1138 */
1139 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001140handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141{
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001142#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001143 cleanup_T cs;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001144#endif
1145#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001146 long old_tw = curbuf->b_p_tw;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001147#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001148 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001149
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 if (swap_exists_action == SEA_QUIT)
1151 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001152#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001153 // Reset the error/interrupt/exception state here so that
1154 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001155 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001156#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001157
Bram Moolenaarc667da52019-11-30 20:52:27 +01001158 // User selected Quit at ATTENTION prompt. Go back to previous
1159 // buffer. If that buffer is gone or the same as the current one,
1160 // open a new, empty buffer.
1161 swap_exists_action = SEA_NONE; // don't want it again
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001162 swap_exists_did_quit = TRUE;
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001163 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001164 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1165 || old_curbuf->br_buf == curbuf)
Bram Moolenaar1d97efc2021-07-04 13:27:11 +02001166 {
1167 // Block autocommands here because curwin->w_buffer is NULL.
1168 block_autocmds();
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001169 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar1d97efc2021-07-04 13:27:11 +02001170 unblock_autocmds();
1171 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001172 else
1173 buf = old_curbuf->br_buf;
1174 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001175 {
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001176 int old_msg_silent = msg_silent;
1177
1178 if (shortmess(SHM_FILEINFO))
1179 msg_silent = 1; // prevent fileinfo message
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001180 enter_buffer(buf);
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001181 // restore msg_silent, so that the command line will be shown
1182 msg_silent = old_msg_silent;
1183
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001184#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001185 if (old_tw != curbuf->b_p_tw)
1186 check_colorcolumn(curwin);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001187#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001188 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001189 // If "old_curbuf" is NULL we are in big trouble here...
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001190
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001191#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001192 // Restore the error/interrupt/exception state if not discarded by a
1193 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001194 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 }
1197 else if (swap_exists_action == SEA_RECOVER)
1198 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001199#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001200 // Reset the error/interrupt/exception state here so that
1201 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001202 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001203#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001204
Bram Moolenaarc667da52019-11-30 20:52:27 +01001205 // User selected Recover at ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 msg_scroll = TRUE;
Bram Moolenaar99499b12019-05-23 21:35:48 +02001207 ml_recover(FALSE);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001208 msg_puts("\n"); // don't overwrite the last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001210 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001211
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001212#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001213 // Restore the error/interrupt/exception state if not discarded by a
1214 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001215 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 }
1218 swap_exists_action = SEA_NONE;
1219}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221/*
Bram Moolenaara02471e2014-01-10 16:43:14 +01001222 * Make the current buffer empty.
1223 * Used when it is wiped out and it's the last buffer.
1224 */
1225 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001226empty_curbuf(
1227 int close_others,
1228 int forceit,
1229 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001230{
1231 int retval;
1232 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001233 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001234
1235 if (action == DOBUF_UNLOAD)
1236 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001237 emsg(_(e_cannot_unload_last_buffer));
Bram Moolenaara02471e2014-01-10 16:43:14 +01001238 return FAIL;
1239 }
1240
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001241 set_bufref(&bufref, buf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001242 if (close_others)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001243 // Close any other windows on this buffer, then make it empty.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001244 close_windows(buf, TRUE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001245
1246 setpcmark();
1247 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1248 forceit ? ECMD_FORCEIT : 0, curwin);
1249
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001250 // do_ecmd() may create a new buffer, then we have to delete
1251 // the old one. But do_ecmd() may have done that already, check
1252 // if the buffer still exists.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001253 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001254 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001255 if (!close_others)
1256 need_fileinfo = FALSE;
1257 return retval;
1258}
Bram Moolenaar94f01952018-09-01 15:30:03 +02001259
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260/*
1261 * Implementation of the commands for the buffer list.
1262 *
1263 * action == DOBUF_GOTO go to specified buffer
1264 * action == DOBUF_SPLIT split window and go to specified buffer
1265 * action == DOBUF_UNLOAD unload specified buffer(s)
1266 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1267 * action == DOBUF_WIPE delete specified buffer(s) really
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001268 * action == DOBUF_WIPE_REUSE idem, and add number to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 *
1270 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1271 * start == DOBUF_FIRST go to "count" buffer from first buffer
1272 * start == DOBUF_LAST go to "count" buffer from last buffer
1273 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1274 *
1275 * Return FAIL or OK.
1276 */
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001277 static int
1278do_buffer_ext(
Bram Moolenaar7454a062016-01-30 15:14:10 +01001279 int action,
1280 int start,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001281 int dir, // FORWARD or BACKWARD
1282 int count, // buffer number or number of buffers
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001283 int flags) // DOBUF_FORCEIT etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284{
1285 buf_T *buf;
1286 buf_T *bp;
1287 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001288 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289
1290 switch (start)
1291 {
1292 case DOBUF_FIRST: buf = firstbuf; break;
1293 case DOBUF_LAST: buf = lastbuf; break;
1294 default: buf = curbuf; break;
1295 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001296 if (start == DOBUF_MOD) // find next modified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 {
1298 while (count-- > 0)
1299 {
1300 do
1301 {
1302 buf = buf->b_next;
1303 if (buf == NULL)
1304 buf = firstbuf;
1305 }
1306 while (buf != curbuf && !bufIsChanged(buf));
1307 }
1308 if (!bufIsChanged(buf))
1309 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001310 emsg(_(e_no_modified_buffer_found));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 return FAIL;
1312 }
1313 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001314 else if (start == DOBUF_FIRST && count) // find specified buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 {
1316 while (buf != NULL && buf->b_fnum != count)
1317 buf = buf->b_next;
1318 }
1319 else
1320 {
1321 bp = NULL;
1322 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1323 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001324 // remember the buffer where we start, we come back there when all
1325 // buffers are unlisted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 if (bp == NULL)
1327 bp = buf;
1328 if (dir == FORWARD)
1329 {
1330 buf = buf->b_next;
1331 if (buf == NULL)
1332 buf = firstbuf;
1333 }
1334 else
1335 {
1336 buf = buf->b_prev;
1337 if (buf == NULL)
1338 buf = lastbuf;
1339 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001340 // don't count unlisted buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 if (unload || buf->b_p_bl)
1342 {
1343 --count;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001344 bp = NULL; // use this buffer as new starting point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 }
1346 if (bp == buf)
1347 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001348 // back where we started, didn't find anything.
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001349 emsg(_(e_there_is_no_listed_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 return FAIL;
1351 }
1352 }
1353 }
1354
Bram Moolenaarc667da52019-11-30 20:52:27 +01001355 if (buf == NULL) // could not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 {
1357 if (start == DOBUF_FIRST)
1358 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001359 // don't warn when deleting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 if (!unload)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001361 semsg(_(e_buffer_nr_does_not_exist), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 }
1363 else if (dir == FORWARD)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001364 emsg(_(e_cannot_go_beyond_last_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 else
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001366 emsg(_(e_cannot_go_before_first_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 return FAIL;
1368 }
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001369#ifdef FEAT_PROP_POPUP
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001370 if ((flags & DOBUF_NOPOPUP) && bt_popup(buf) && !bt_terminal(buf))
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001371 return OK;
1372#endif
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01001373 if ((action == DOBUF_GOTO || action == DOBUF_SPLIT)
1374 && (buf->b_flags & BF_DUMMY))
1375 {
1376 // disallow navigating to the dummy buffer
1377 semsg(_(e_buffer_nr_does_not_exist), count);
1378 return FAIL;
1379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380
1381#ifdef FEAT_GUI
1382 need_mouse_correct = TRUE;
1383#endif
1384
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001386 * delete buffer "buf" from memory and/or the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 */
1388 if (unload)
1389 {
1390 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001391 bufref_T bufref;
1392
Bram Moolenaar94f01952018-09-01 15:30:03 +02001393 if (!can_unload_buffer(buf))
Bram Moolenaara997b452018-04-17 23:24:06 +02001394 return FAIL;
Bram Moolenaara997b452018-04-17 23:24:06 +02001395
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001396 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397
Bram Moolenaarc667da52019-11-30 20:52:27 +01001398 // When unloading or deleting a buffer that's already unloaded and
1399 // unlisted: fail silently.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001400 if (action != DOBUF_WIPE && action != DOBUF_WIPE_REUSE
1401 && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 return FAIL;
1403
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001404 if ((flags & DOBUF_FORCEIT) == 0 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001406#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001407 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001409# ifdef FEAT_TERMINAL
1410 if (term_job_running(buf->b_term))
1411 {
1412 if (term_confirm_stop(buf) == FAIL)
1413 return FAIL;
1414 }
1415 else
1416# endif
1417 {
1418 dialog_changed(buf, FALSE);
1419 if (!bufref_valid(&bufref))
1420 // Autocommand deleted buffer, oops! It's not changed
1421 // now.
1422 return FAIL;
1423 // If it's still changed fail silently, the dialog already
1424 // mentioned why it fails.
1425 if (bufIsChanged(buf))
1426 return FAIL;
1427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001429 else
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001430#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001432 no_write_message_buf(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 return FAIL;
1434 }
1435 }
1436
Bram Moolenaarc667da52019-11-30 20:52:27 +01001437 // When closing the current buffer stop Visual mode.
Bram Moolenaar4930a762016-09-11 14:39:53 +02001438 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001439 end_visual_mode();
1440
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001441 // If deleting the last (listed) buffer, make it empty.
1442 // The last (listed) buffer cannot be unloaded.
Bram Moolenaar29323592016-07-24 22:04:11 +02001443 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 if (bp->b_p_bl && bp != buf)
1445 break;
1446 if (bp == NULL && buf == curbuf)
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001447 return empty_curbuf(TRUE, (flags & DOBUF_FORCEIT), action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001449 // If the deleted buffer is the current one, close the current window
1450 // (unless it's the only window). Repeat this so long as we end up in
1451 // a window with this buffer.
Bram Moolenaarf740b292006-02-16 22:11:02 +00001452 while (buf == curbuf
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001453 && !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
Bram Moolenaar459ca562016-11-10 18:16:33 +01001454 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001455 {
1456 if (win_close(curwin, FALSE) == FAIL)
1457 break;
1458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001460 // If the buffer to be deleted is not the current one, delete it here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 if (buf != curbuf)
1462 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001463 close_windows(buf, FALSE);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001464 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows <= 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001465 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 return OK;
1467 }
1468
1469 /*
1470 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001471 * There should be another, otherwise it would have been handled
1472 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001473 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 * Then prefer the buffer we most recently visited.
1475 * Else try to find one that is loaded, after the current buffer,
1476 * then before the current buffer.
1477 * Finally use any buffer.
1478 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001479 buf = NULL; // selected buffer
1480 bp = NULL; // used when no loaded buffer found
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001481 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1482 buf = au_new_curbuf.br_buf;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001483 else if (curwin->w_jumplistlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 {
1485 int jumpidx;
1486
1487 jumpidx = curwin->w_jumplistidx - 1;
1488 if (jumpidx < 0)
1489 jumpidx = curwin->w_jumplistlen - 1;
1490
1491 forward = jumpidx;
1492 while (jumpidx != curwin->w_jumplistidx)
1493 {
1494 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1495 if (buf != NULL)
1496 {
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001497 // Skip current and unlisted bufs. Also skip a quickfix
1498 // buffer, it might be deleted soon.
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001499 if (buf == curbuf || !buf->b_p_bl || bt_quickfix(buf))
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001500 buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 else if (buf->b_ml.ml_mfp == NULL)
1502 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001503 // skip unloaded buf, but may keep it for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 if (bp == NULL)
1505 bp = buf;
1506 buf = NULL;
1507 }
1508 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001509 if (buf != NULL) // found a valid buffer: stop searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001511 // advance to older entry in jump list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1513 break;
1514 if (--jumpidx < 0)
1515 jumpidx = curwin->w_jumplistlen - 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001516 if (jumpidx == forward) // List exhausted for sure
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 break;
1518 }
1519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520
Bram Moolenaarc667da52019-11-30 20:52:27 +01001521 if (buf == NULL) // No previous buffer, Try 2'nd approach
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 {
1523 forward = TRUE;
1524 buf = curbuf->b_next;
1525 for (;;)
1526 {
1527 if (buf == NULL)
1528 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001529 if (!forward) // tried both directions
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 break;
1531 buf = curbuf->b_prev;
1532 forward = FALSE;
1533 continue;
1534 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001535 // in non-help buffer, try to skip help buffers, and vv
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001536 if (buf->b_help == curbuf->b_help && buf->b_p_bl
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001537 && !bt_quickfix(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001539 if (buf->b_ml.ml_mfp != NULL) // found loaded buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001541 if (bp == NULL) // remember unloaded buf for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 bp = buf;
1543 }
1544 if (forward)
1545 buf = buf->b_next;
1546 else
1547 buf = buf->b_prev;
1548 }
1549 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001550 if (buf == NULL) // No loaded buffer, use unloaded one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 buf = bp;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001552 if (buf == NULL) // No loaded buffer, find listed one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001554 FOR_ALL_BUFFERS(buf)
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001555 if (buf->b_p_bl && buf != curbuf && !bt_quickfix(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 break;
1557 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001558 if (buf == NULL) // Still no buffer, just take one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 {
1560 if (curbuf->b_next != NULL)
1561 buf = curbuf->b_next;
1562 else
1563 buf = curbuf->b_prev;
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001564 if (bt_quickfix(buf))
1565 buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 }
1567 }
1568
Bram Moolenaara02471e2014-01-10 16:43:14 +01001569 if (buf == NULL)
1570 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001571 // Autocommands must have wiped out all other buffers. Only option
1572 // now is to make the current buffer empty.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001573 return empty_curbuf(FALSE, (flags & DOBUF_FORCEIT), action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001574 }
1575
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001577 * make "buf" the current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001579 if (action == DOBUF_SPLIT) // split window first
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 {
Yegappan Lakshmanane42c27d2023-05-14 17:24:22 +01001581 // If 'switchbuf' is set jump to the window containing "buf".
1582 if (swbuf_goto_win_with_buf(buf) != NULL)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001583 return OK;
Yegappan Lakshmanane42c27d2023-05-14 17:24:22 +01001584
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 return FAIL;
1587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588
Bram Moolenaarc667da52019-11-30 20:52:27 +01001589 // go to current buffer - nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 if (buf == curbuf)
1591 return OK;
1592
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001593 // Check if the current buffer may be abandoned.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001594 if (action == DOBUF_GOTO && !can_abandon(curbuf, (flags & DOBUF_FORCEIT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 {
1596#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001597 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001599# ifdef FEAT_TERMINAL
1600 if (term_job_running(curbuf->b_term))
1601 {
1602 if (term_confirm_stop(curbuf) == FAIL)
1603 return FAIL;
1604 // Manually kill the terminal here because this command will
1605 // hide it otherwise.
1606 free_terminal(curbuf);
1607 }
1608 else
1609# endif
1610 {
1611 bufref_T bufref;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001612
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001613 set_bufref(&bufref, buf);
1614 dialog_changed(curbuf, FALSE);
1615 if (!bufref_valid(&bufref))
1616 // Autocommand deleted buffer, oops!
1617 return FAIL;
1618
1619 if (bufIsChanged(curbuf))
1620 {
1621 no_write_message();
1622 return FAIL;
1623 }
1624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 }
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001626 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627#endif
1628 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001629 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 return FAIL;
1631 }
1632 }
1633
Bram Moolenaarc667da52019-11-30 20:52:27 +01001634 // Go to the other buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 set_curbuf(buf, action);
1636
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 if (action == DOBUF_SPLIT)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001638 RESET_BINDING(curwin); // reset 'scrollbind' and 'cursorbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001640#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001641 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 return FAIL;
1643#endif
1644
1645 return OK;
1646}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001648 int
1649do_buffer(
1650 int action,
1651 int start,
1652 int dir, // FORWARD or BACKWARD
1653 int count, // buffer number or number of buffers
1654 int forceit) // TRUE when using !
1655{
1656 return do_buffer_ext(action, start, dir, count,
1657 forceit ? DOBUF_FORCEIT : 0);
1658}
1659
1660/*
1661 * do_bufdel() - delete or unload buffer(s)
1662 *
1663 * addr_count == 0: ":bdel" - delete current buffer
1664 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1665 * buffer "end_bnr", then any other arguments.
1666 * addr_count == 2: ":N,N bdel" - delete buffers in range
1667 *
1668 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1669 * DOBUF_DEL (":bdel")
1670 *
1671 * Returns error message or NULL
1672 */
1673 char *
1674do_bufdel(
1675 int command,
1676 char_u *arg, // pointer to extra arguments
1677 int addr_count,
1678 int start_bnr, // first buffer number in a range
1679 int end_bnr, // buffer nr or last buffer nr in a range
1680 int forceit)
1681{
1682 int do_current = 0; // delete current buffer?
1683 int deleted = 0; // number of buffers deleted
1684 char *errormsg = NULL; // return value
1685 int bnr; // buffer number
1686 char_u *p;
1687
1688 if (addr_count == 0)
1689 {
1690 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1691 }
1692 else
1693 {
1694 if (addr_count == 2)
1695 {
1696 if (*arg) // both range and argument is not allowed
Bram Moolenaar74409f62022-01-01 15:58:22 +00001697 return ex_errmsg(e_trailing_characters_str, arg);
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001698 bnr = start_bnr;
1699 }
1700 else // addr_count == 1
1701 bnr = end_bnr;
1702
1703 for ( ;!got_int; ui_breakcheck())
1704 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001705 // Delete the current buffer last, otherwise when the
1706 // current buffer is deleted, the next buffer becomes
1707 // the current one and will be loaded, which may then
1708 // also be deleted, etc.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001709 if (bnr == curbuf->b_fnum)
1710 do_current = bnr;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00001711 else if (do_buffer_ext(command, DOBUF_FIRST, FORWARD, bnr,
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001712 DOBUF_NOPOPUP | (forceit ? DOBUF_FORCEIT : 0)) == OK)
1713 ++deleted;
1714
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001715 // find next buffer number to delete/unload
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001716 if (addr_count == 2)
1717 {
1718 if (++bnr > end_bnr)
1719 break;
1720 }
1721 else // addr_count == 1
1722 {
1723 arg = skipwhite(arg);
1724 if (*arg == NUL)
1725 break;
1726 if (!VIM_ISDIGIT(*arg))
1727 {
1728 p = skiptowhite_esc(arg);
1729 bnr = buflist_findpat(arg, p,
1730 command == DOBUF_WIPE || command == DOBUF_WIPE_REUSE,
1731 FALSE, FALSE);
1732 if (bnr < 0) // failed
1733 break;
1734 arg = p;
1735 }
1736 else
1737 bnr = getdigits(&arg);
1738 }
1739 }
1740 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1741 FORWARD, do_current, forceit) == OK)
1742 ++deleted;
1743
1744 if (deleted == 0)
1745 {
1746 if (command == DOBUF_UNLOAD)
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001747 STRCPY(IObuff, _(e_no_buffers_were_unloaded));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001748 else if (command == DOBUF_DEL)
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001749 STRCPY(IObuff, _(e_no_buffers_were_deleted));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001750 else
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001751 STRCPY(IObuff, _(e_no_buffers_were_wiped_out));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001752 errormsg = (char *)IObuff;
1753 }
1754 else if (deleted >= p_report)
1755 {
1756 if (command == DOBUF_UNLOAD)
1757 smsg(NGETTEXT("%d buffer unloaded",
1758 "%d buffers unloaded", deleted), deleted);
1759 else if (command == DOBUF_DEL)
1760 smsg(NGETTEXT("%d buffer deleted",
1761 "%d buffers deleted", deleted), deleted);
1762 else
1763 smsg(NGETTEXT("%d buffer wiped out",
1764 "%d buffers wiped out", deleted), deleted);
1765 }
1766 }
1767
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001768 return errormsg;
1769}
1770
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771/*
1772 * Set current buffer to "buf". Executes autocommands and closes current
1773 * buffer. "action" tells how to close the current buffer:
1774 * DOBUF_GOTO free or hide it
1775 * DOBUF_SPLIT nothing
1776 * DOBUF_UNLOAD unload it
1777 * DOBUF_DEL delete it
1778 * DOBUF_WIPE wipe it out
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001779 * DOBUF_WIPE_REUSE wipe it out and add to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 */
1781 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001782set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783{
1784 buf_T *prevbuf;
1785 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001786 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001787#ifdef FEAT_SYN_HL
1788 long old_tw = curbuf->b_p_tw;
1789#endif
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001790 bufref_T newbufref;
1791 bufref_T prevbufref;
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001792 int valid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793
1794 setpcmark();
Bram Moolenaare1004402020-10-24 20:49:43 +02001795 if ((cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001796 curwin->w_alt_fnum = curbuf->b_fnum; // remember alternate file
1797 buflist_altfpos(curwin); // remember curpos
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798
Bram Moolenaarc667da52019-11-30 20:52:27 +01001799 // Don't restart Select mode after switching to another buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801
Bram Moolenaarc667da52019-11-30 20:52:27 +01001802 // close_windows() or apply_autocmds() may change curbuf and wipe out "buf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 prevbuf = curbuf;
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001804 set_bufref(&prevbufref, prevbuf);
1805 set_bufref(&newbufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806
Bram Moolenaar983d83f2021-02-07 12:12:43 +01001807 // Autocommands may delete the current buffer and/or the buffer we want to
1808 // go to. In those cases don't close the buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001809 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001810 || (bufref_valid(&prevbufref)
1811 && bufref_valid(&newbufref)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001812#ifdef FEAT_EVAL
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001813 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001815 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001817#ifdef FEAT_SYN_HL
1818 if (prevbuf == curwin->w_buffer)
1819 reset_synblock(curwin);
1820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001822 close_windows(prevbuf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001823#if defined(FEAT_EVAL)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001824 if (bufref_valid(&prevbufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825#else
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001826 if (bufref_valid(&prevbufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001828 {
Bram Moolenaare25865a2012-07-06 16:22:02 +02001829 win_T *previouswin = curwin;
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001830
Bram Moolenaare615db02022-01-20 21:00:54 +00001831 // Do not sync when in Insert mode and the buffer is open in
1832 // another window, might be a timer doing something in another
1833 // window.
1834 if (prevbuf == curbuf
Bram Moolenaar24959102022-05-07 20:01:16 +01001835 && ((State & MODE_INSERT) == 0 || curbuf->b_nwindows <= 1))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001836 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1838 unload ? action : (action == DOBUF_GOTO
Bram Moolenaareb44a682017-08-03 22:44:55 +02001839 && !buf_hide(prevbuf)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001840 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0,
1841 FALSE, FALSE);
Bram Moolenaare25865a2012-07-06 16:22:02 +02001842 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001843 // autocommands changed curwin, Grr!
Bram Moolenaare25865a2012-07-06 16:22:02 +02001844 curwin = previouswin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001847 // An autocommand may have deleted "buf", already entered it (e.g., when
1848 // it did ":bunload") or aborted the script processing.
1849 // If curwin->w_buffer is null, enter_buffer() will make it valid again
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001850 valid = buf_valid(buf);
1851 if ((valid && buf != curbuf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001852#ifdef FEAT_EVAL
Bram Moolenaar4033c552017-09-16 20:54:51 +02001853 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001855 ) || curwin->w_buffer == NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001856 {
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001857 // If the buffer is not valid but curwin->w_buffer is NULL we must
1858 // enter some buffer. Using the last one is hopefully OK.
1859 if (!valid)
1860 enter_buffer(lastbuf);
1861 else
1862 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001863#ifdef FEAT_SYN_HL
1864 if (old_tw != curbuf->b_p_tw)
1865 check_colorcolumn(curwin);
1866#endif
1867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868}
1869
1870/*
1871 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001872 * Old curbuf must have been abandoned already! This also means "curbuf" may
1873 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001875 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001876enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877{
Bram Moolenaarcfeb8a52022-08-13 14:09:44 +01001878 // when closing the current buffer stop Visual mode
1879 if (VIsual_active
1880#if defined(EXITFREE)
1881 && !entered_free_all_mem
1882#endif
1883 )
1884 end_visual_mode();
1885
Bram Moolenaar010ee962019-09-25 20:37:36 +02001886 // Get the buffer in the current window.
1887 curwin->w_buffer = buf;
1888 curbuf = buf;
1889 ++curbuf->b_nwindows;
1890
1891 // Copy buffer and window local option values. Not for a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1893 if (!buf->b_help)
1894 get_winopts(buf);
1895#ifdef FEAT_FOLDING
1896 else
Bram Moolenaar010ee962019-09-25 20:37:36 +02001897 // Remove all folds in the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 clearFolding(curwin);
Bram Moolenaar010ee962019-09-25 20:37:36 +02001899 foldUpdateAll(curwin); // update folds (later).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900#endif
1901
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001903 if (curwin->w_p_diff)
1904 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905#endif
1906
Bram Moolenaara971b822011-09-14 14:43:25 +02001907#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001908 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001909#endif
1910
Bram Moolenaarc667da52019-11-30 20:52:27 +01001911 // Cursor on first line by default.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 curwin->w_cursor.lnum = 1;
1913 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001916 curwin->w_topline_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917
Bram Moolenaarc667da52019-11-30 20:52:27 +01001918 // mark cursor position as being invalid
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001919 curwin->w_valid = 0;
1920
Bram Moolenaar9cea87c2018-09-21 16:59:45 +02001921 buflist_setfpos(curbuf, curwin, curbuf->b_last_cursor.lnum,
1922 curbuf->b_last_cursor.col, TRUE);
1923
Bram Moolenaarc667da52019-11-30 20:52:27 +01001924 // Make sure the buffer is loaded.
1925 if (curbuf->b_ml.ml_mfp == NULL) // need to load the file
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001926 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001927 // If there is no filetype, allow for detecting one. Esp. useful for
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001928 // ":ball" used in an autocommand. If there already is a filetype we
Bram Moolenaarc667da52019-11-30 20:52:27 +01001929 // might prefer to keep it.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001930 if (*curbuf->b_p_ft == NUL)
1931 did_filetype = FALSE;
1932
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001933 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 else
1936 {
Bram Moolenaareda65222019-05-16 20:29:44 +02001937 if (!msg_silent && !shortmess(SHM_FILEINFO))
1938 need_fileinfo = TRUE; // display file info after redraw
1939
1940 // check if file changed
1941 (void)buf_check_timestamp(curbuf, FALSE);
1942
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001944#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001946#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1948 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 }
1950
Bram Moolenaarc667da52019-11-30 20:52:27 +01001951 // If autocommands did not change the cursor position, restore cursor lnum
1952 // and possibly cursor col.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 if (curwin->w_cursor.lnum == 1 && inindent(0))
1954 buflist_getfpos();
1955
Bram Moolenaarc667da52019-11-30 20:52:27 +01001956 check_arg_idx(curwin); // check for valid arg_idx
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 maketitle();
Bram Moolenaarc667da52019-11-30 20:52:27 +01001958 // when autocmds didn't change it
Bram Moolenaard4153d42008-11-15 15:06:17 +00001959 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar1d6539c2023-02-14 17:41:20 +00001960 scroll_cursor_halfway(FALSE, FALSE); // redisplay at correct position
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961
Bram Moolenaar009b2592004-10-24 19:18:58 +00001962#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarc667da52019-11-30 20:52:27 +01001963 // Send fileOpened event because we've changed buffers.
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001964 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001965#endif
1966
Bram Moolenaarc667da52019-11-30 20:52:27 +01001967 // Change directories when the 'acd' option is set.
Bram Moolenaar6f470022018-04-10 18:47:20 +02001968 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969
1970#ifdef FEAT_KEYMAP
1971 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001972 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001974#ifdef FEAT_SPELL
Bram Moolenaarc667da52019-11-30 20:52:27 +01001975 // May need to set the spell language. Can only do this after the buffer
1976 // has been properly setup.
Bram Moolenaar860cae12010-06-05 23:22:07 +02001977 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Yegappan Lakshmananaf936912023-02-20 12:16:39 +00001978 (void)parse_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001979#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001980#ifdef FEAT_VIMINFO
1981 curbuf->b_last_used = vim_time();
1982#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001983
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001984 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985}
1986
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001987#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1988/*
1989 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001990 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001991 */
1992 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001993do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001994{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001995 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001996 && curbuf->b_ffname != NULL
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001997 && vim_chdirfile(curbuf->b_ffname, "auto") == OK)
Bram Moolenaar05268152021-11-18 18:53:45 +00001998 {
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001999 shorten_fnames(TRUE);
Bram Moolenaar05268152021-11-18 18:53:45 +00002000 last_chdir_reason = "autochdir";
2001 }
Bram Moolenaar498efdb2006-09-05 14:31:54 +00002002}
2003#endif
2004
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01002005 static void
2006no_write_message_buf(buf_T *buf UNUSED)
2007{
2008#ifdef FEAT_TERMINAL
2009 if (term_job_running(buf->b_term))
2010 emsg(_(e_job_still_running_add_bang_to_end_the_job));
2011 else
2012#endif
2013 semsg(_(e_no_write_since_last_change_for_buffer_nr_add_bang_to_override),
2014 buf->b_fnum);
2015}
2016
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002017 void
2018no_write_message(void)
2019{
2020#ifdef FEAT_TERMINAL
2021 if (term_job_running(curbuf->b_term))
Bram Moolenaarf1474d82021-12-31 19:59:55 +00002022 emsg(_(e_job_still_running_add_bang_to_end_the_job));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002023 else
2024#endif
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002025 emsg(_(e_no_write_since_last_change_add_bang_to_override));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002026}
2027
2028 void
Bram Moolenaar7a760922018-02-19 23:10:02 +01002029no_write_message_nobang(buf_T *buf UNUSED)
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002030{
2031#ifdef FEAT_TERMINAL
Bram Moolenaar7a760922018-02-19 23:10:02 +01002032 if (term_job_running(buf->b_term))
Bram Moolenaarf1474d82021-12-31 19:59:55 +00002033 emsg(_(e_job_still_running));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002034 else
2035#endif
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002036 emsg(_(e_no_write_since_last_change));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002037}
2038
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039/*
2040 * functions for dealing with the buffer list
2041 */
2042
2043/*
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002044 * Return TRUE if the current buffer is empty, unnamed, unmodified and used in
2045 * only one window. That means it can be re-used.
2046 */
2047 int
2048curbuf_reusable(void)
2049{
2050 return (curbuf != NULL
2051 && curbuf->b_ffname == NULL
2052 && curbuf->b_nwindows <= 1
2053 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY())
Bram Moolenaar39803d82019-04-07 12:04:51 +02002054 && !bt_quickfix(curbuf)
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002055 && !curbufIsChanged());
2056}
2057
2058/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 * Add a file name to the buffer list. Return a pointer to the buffer.
2060 * If the same file name already exists return a pointer to that buffer.
2061 * If it does not exist, or if fname == NULL, a new entry is created.
2062 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
2063 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
2064 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02002065 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02002066 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
2067 * if the buffer already exists.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002068 * If (flags & BLN_REUSE) is TRUE, may use buffer number from "buf_reuse".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 * This is the ONLY way to create a new buffer.
2070 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002072buflist_new(
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002073 char_u *ffname_arg, // full path of fname or relative
2074 char_u *sfname_arg, // short fname or NULL
2075 linenr_T lnum, // preferred cursor line
2076 int flags) // BLN_ defines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002078 char_u *ffname = ffname_arg;
2079 char_u *sfname = sfname_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 buf_T *buf;
2081#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002082 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083#endif
2084
Bram Moolenaar480778b2016-07-14 22:09:39 +02002085 if (top_file_num == 1)
2086 hash_init(&buf_hashtab);
2087
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002088 fname_expand(curbuf, &ffname, &sfname); // will allocate ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089
2090 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002091 * If the file name already exists in the list, update the entry.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 */
2093#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002094 // On Unix we can use inode numbers when the file exists. Works better
2095 // for hard links.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
2097 st.st_dev = (dev_T)-1;
2098#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02002099 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100#ifdef UNIX
2101 buflist_findname_stat(ffname, &st)
2102#else
2103 buflist_findname(ffname)
2104#endif
2105 ) != NULL)
2106 {
2107 vim_free(ffname);
2108 if (lnum != 0)
Bram Moolenaar89b693e2020-10-25 17:09:50 +01002109 buflist_setfpos(buf, (flags & BLN_NOCURWIN) ? NULL : curwin,
2110 lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02002111
2112 if ((flags & BLN_NOOPT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002113 // copy the options now, if 'cpo' doesn't have 's' and not done
2114 // already
Bram Moolenaar82404332016-07-10 17:00:38 +02002115 buf_copy_options(buf, 0);
2116
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 if ((flags & BLN_LISTED) && !buf->b_p_bl)
2118 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002119 bufref_T bufref;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002120
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 buf->b_p_bl = TRUE;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002122 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002124 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002125 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002126 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002127 return NULL;
2128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 }
2130 return buf;
2131 }
2132
2133 /*
2134 * If the current buffer has no name and no contents, use the current
2135 * buffer. Otherwise: Need to allocate a new buffer structure.
2136 *
2137 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00002138 * (A spell file buffer is allocated in spell.c, but that's not a normal
2139 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 */
2141 buf = NULL;
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002142 if ((flags & BLN_CURBUF) && curbuf_reusable())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 {
2144 buf = curbuf;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002145 // It's like this buffer is deleted. Watch out for autocommands that
2146 // change curbuf! If that happens, allocate a new buffer anyway.
Charlie Grovesfef44852022-04-19 16:24:12 +01002147 buf_freeall(buf, BFA_WIPE | BFA_DEL);
2148 if (buf != curbuf) // autocommands deleted the buffer!
2149 return NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002150#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002151 if (aborting()) // autocmds may abort script processing
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002152 {
2153 vim_free(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 return NULL;
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 }
2158 if (buf != curbuf || curbuf == NULL)
2159 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002160 buf = ALLOC_CLEAR_ONE(buf_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 if (buf == NULL)
2162 {
2163 vim_free(ffname);
2164 return NULL;
2165 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02002166#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002167 // init b: variables
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01002168 buf->b_vars = dict_alloc_id(aid_newbuf_bvars);
Bram Moolenaar429fa852013-04-15 12:27:36 +02002169 if (buf->b_vars == NULL)
2170 {
2171 vim_free(ffname);
2172 vim_free(buf);
2173 return NULL;
2174 }
2175 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
2176#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01002177 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 }
2179
2180 if (ffname != NULL)
2181 {
2182 buf->b_ffname = ffname;
2183 buf->b_sfname = vim_strsave(sfname);
2184 }
2185
2186 clear_wininfo(buf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002187 buf->b_wininfo = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188
2189 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
2190 || buf->b_wininfo == NULL)
2191 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002192 if (buf->b_sfname != buf->b_ffname)
2193 VIM_CLEAR(buf->b_sfname);
2194 else
2195 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01002196 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 if (buf != curbuf)
2198 free_buffer(buf);
2199 return NULL;
2200 }
2201
2202 if (buf == curbuf)
2203 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002204 free_buffer_stuff(buf, FALSE); // delete local variables et al.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002205
Bram Moolenaarc667da52019-11-30 20:52:27 +01002206 // Init the options.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002207 buf->b_p_initialized = FALSE;
2208 buf_copy_options(buf, BCO_ENTER);
2209
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210#ifdef FEAT_KEYMAP
Bram Moolenaarc667da52019-11-30 20:52:27 +01002211 // need to reload lmaps and set b:keymap_name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 curbuf->b_kmap_state |= KEYMAP_INIT;
2213#endif
2214 }
2215 else
2216 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002217 // put the new buffer at the end of the buffer list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 buf->b_next = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002219 if (firstbuf == NULL) // buffer list is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 {
2221 buf->b_prev = NULL;
2222 firstbuf = buf;
2223 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002224 else // append new buffer at end of list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 {
2226 lastbuf->b_next = buf;
2227 buf->b_prev = lastbuf;
2228 }
2229 lastbuf = buf;
2230
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002231 if ((flags & BLN_REUSE) && buf_reuse.ga_len > 0)
2232 {
2233 // Recycle a previously used buffer number. Used for buffers which
2234 // are normally hidden, e.g. in a popup window. Avoids that the
2235 // buffer number grows rapidly.
2236 --buf_reuse.ga_len;
2237 buf->b_fnum = ((int *)buf_reuse.ga_data)[buf_reuse.ga_len];
Bram Moolenaar99ebf222019-12-10 23:44:48 +01002238
2239 // Move buffer to the right place in the buffer list.
2240 while (buf->b_prev != NULL && buf->b_fnum < buf->b_prev->b_fnum)
2241 {
2242 buf_T *prev = buf->b_prev;
2243
2244 prev->b_next = buf->b_next;
2245 if (prev->b_next != NULL)
2246 prev->b_next->b_prev = prev;
2247 buf->b_next = prev;
2248 buf->b_prev = prev->b_prev;
2249 if (buf->b_prev != NULL)
2250 buf->b_prev->b_next = buf;
2251 prev->b_prev = buf;
2252 if (lastbuf == buf)
2253 lastbuf = prev;
2254 if (firstbuf == prev)
2255 firstbuf = buf;
2256 }
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002257 }
2258 else
2259 buf->b_fnum = top_file_num++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002260 if (top_file_num < 0) // wrap around (may cause duplicates)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002262 emsg(_("W14: Warning: List of file names overflow"));
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002263 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 {
2265 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002266 ui_delay(3001L, TRUE); // make sure it is noticed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 }
2268 top_file_num = 1;
2269 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002270 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002272 // Always copy the options from the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 buf_copy_options(buf, BCO_ALWAYS);
2274 }
2275
2276 buf->b_wininfo->wi_fpos.lnum = lnum;
2277 buf->b_wininfo->wi_win = curwin;
2278
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002279#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002280 hash_init(&buf->b_s.b_keywtab);
2281 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282#endif
2283
2284 buf->b_fname = buf->b_sfname;
2285#ifdef UNIX
2286 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002287 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 else
2289 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002290 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 buf->b_dev = st.st_dev;
2292 buf->b_ino = st.st_ino;
2293 }
2294#endif
2295 buf->b_u_synced = TRUE;
2296 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002297 if (flags & BLN_DUMMY)
2298 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 buf_clear_file(buf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002300 clrallmarks(buf); // clear marks
2301 fmarks_check_names(buf); // check file marks for this file
2302 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; // init 'buflisted'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 if (!(flags & BLN_DUMMY))
2304 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002305 bufref_T bufref;
2306
Bram Moolenaarc667da52019-11-30 20:52:27 +01002307 // Tricky: these autocommands may change the buffer list. They could
2308 // also split the window with re-using the one empty buffer. This may
2309 // result in unexpectedly losing the empty buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002310 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002311 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002312 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002313 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002315 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002316 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002317 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002318 return NULL;
2319 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002320#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002321 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325
2326 return buf;
2327}
2328
2329/*
2330 * Free the memory for the options of a buffer.
2331 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2332 * 'fileencoding'.
2333 */
2334 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002335free_buf_options(
2336 buf_T *buf,
2337 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338{
2339 if (free_p_ff)
2340 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 clear_string_option(&buf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 clear_string_option(&buf->b_p_bh);
2344 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 }
2346#ifdef FEAT_FIND_ID
2347 clear_string_option(&buf->b_p_def);
2348 clear_string_option(&buf->b_p_inc);
2349# ifdef FEAT_EVAL
2350 clear_string_option(&buf->b_p_inex);
2351# endif
2352#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002353#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 clear_string_option(&buf->b_p_inde);
2355 clear_string_option(&buf->b_p_indk);
2356#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002357#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2358 clear_string_option(&buf->b_p_bexpr);
2359#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002360#if defined(FEAT_CRYPT)
2361 clear_string_option(&buf->b_p_cm);
2362#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002363 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002364#if defined(FEAT_EVAL)
2365 clear_string_option(&buf->b_p_fex);
2366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367#ifdef FEAT_CRYPT
Bram Moolenaar131530a2021-07-29 20:37:49 +02002368# ifdef FEAT_SODIUM
Christian Brabandtaae58342023-04-23 17:50:22 +01002369 if (buf->b_p_key != NULL && *buf->b_p_key != NUL
2370 && crypt_method_is_sodium(crypt_get_method_nr(buf)))
K.Takata1a8825d2022-01-19 13:32:57 +00002371 crypt_sodium_munlock(buf->b_p_key, STRLEN(buf->b_p_key));
Bram Moolenaar131530a2021-07-29 20:37:49 +02002372# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 clear_string_option(&buf->b_p_key);
2374#endif
2375 clear_string_option(&buf->b_p_kp);
2376 clear_string_option(&buf->b_p_mps);
2377 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002378 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 clear_string_option(&buf->b_p_isk);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002380#ifdef FEAT_VARTABS
2381 clear_string_option(&buf->b_p_vsts);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00002382 VIM_CLEAR(buf->b_p_vsts_nopaste);
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00002383 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002384 clear_string_option(&buf->b_p_vts);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002385 VIM_CLEAR(buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387#ifdef FEAT_KEYMAP
2388 clear_string_option(&buf->b_p_keymap);
Bram Moolenaar50138322018-01-28 17:05:16 +01002389 keymap_clear(&buf->b_kmap_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 ga_clear(&buf->b_kmap_ga);
2391#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 clear_string_option(&buf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393#ifdef FEAT_FOLDING
2394 clear_string_option(&buf->b_p_cms);
2395#endif
2396 clear_string_option(&buf->b_p_nf);
2397#ifdef FEAT_SYN_HL
2398 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002399 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002400#endif
2401#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002402 clear_string_option(&buf->b_s.b_p_spc);
2403 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002404 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002405 buf->b_s.b_cap_prog = NULL;
2406 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02002407 clear_string_option(&buf->b_s.b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 clear_string_option(&buf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 clear_string_option(&buf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 clear_string_option(&buf->b_p_cink);
2412 clear_string_option(&buf->b_p_cino);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002413 clear_string_option(&buf->b_p_lop);
Tom Praschan3506cf32022-04-07 12:39:08 +01002414 clear_string_option(&buf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 clear_string_option(&buf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 clear_string_option(&buf->b_p_cpt);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002417#ifdef FEAT_COMPL_FUNC
2418 clear_string_option(&buf->b_p_cfu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002419 free_callback(&buf->b_cfu_cb);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002420 clear_string_option(&buf->b_p_ofu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002421 free_callback(&buf->b_ofu_cb);
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +01002422 clear_string_option(&buf->b_p_tsrfu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002423 free_callback(&buf->b_tsrfu_cb);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002424#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425#ifdef FEAT_QUICKFIX
2426 clear_string_option(&buf->b_p_gp);
2427 clear_string_option(&buf->b_p_mp);
2428 clear_string_option(&buf->b_p_efm);
2429#endif
2430 clear_string_option(&buf->b_p_ep);
2431 clear_string_option(&buf->b_p_path);
2432 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002433 clear_string_option(&buf->b_p_tc);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002434#ifdef FEAT_EVAL
2435 clear_string_option(&buf->b_p_tfu);
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +00002436 free_callback(&buf->b_tfu_cb);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002437#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 clear_string_option(&buf->b_p_dict);
2439 clear_string_option(&buf->b_p_tsr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002440 clear_string_option(&buf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002442 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002443 clear_string_option(&buf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002444 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002445 clear_string_option(&buf->b_p_menc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446}
2447
2448/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002449 * Get alternate file "n".
2450 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2451 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 * if (options & GETF_SETMARK) call setpcmark()
2453 * if (options & GETF_ALT) we are jumping to an alternate file.
2454 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2455 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002456 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 */
2458 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002459buflist_getfile(
2460 int n,
2461 linenr_T lnum,
2462 int options,
2463 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464{
2465 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 pos_T *fpos;
2468 colnr_T col;
2469
2470 buf = buflist_findnr(n);
2471 if (buf == NULL)
2472 {
2473 if ((options & GETF_ALT) && n == 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02002474 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 else
Bram Moolenaare1242042021-12-16 20:56:57 +00002476 semsg(_(e_buffer_nr_not_found), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 return FAIL;
2478 }
2479
Bram Moolenaarc667da52019-11-30 20:52:27 +01002480 // if alternate file is the current buffer, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 if (buf == curbuf)
2482 return OK;
2483
Bram Moolenaar71223e22022-05-30 15:23:09 +01002484 if (text_or_buf_locked())
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002485 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486
Bram Moolenaarc667da52019-11-30 20:52:27 +01002487 // altfpos may be changed by getfile(), get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 if (lnum == 0)
2489 {
2490 fpos = buflist_findfpos(buf);
2491 lnum = fpos->lnum;
2492 col = fpos->col;
2493 }
2494 else
2495 col = 0;
2496
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 if (options & GETF_SWITCH)
2498 {
Yegappan Lakshmanane42c27d2023-05-14 17:24:22 +01002499 // If 'switchbuf' is set jump to the window containing "buf".
2500 wp = swbuf_goto_win_with_buf(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002501
Bram Moolenaarc667da52019-11-30 20:52:27 +01002502 // If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2503 // current buffer isn't empty: open new tab or window
Bram Moolenaara594d772015-06-19 14:41:49 +02002504 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002505 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002507 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002508 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002509 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2510 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002512 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 }
2514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515
2516 ++RedrawingDisabled;
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002517 int retval = FAIL;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002518 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2519 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002521 // cursor is at to BOL and w_cursor.lnum is checked due to getfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 if (!p_sol && col != 0)
2523 {
2524 curwin->w_cursor.col = col;
2525 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 curwin->w_set_curswant = TRUE;
2528 }
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002529 retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 }
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002531
2532 if (RedrawingDisabled > 0)
2533 --RedrawingDisabled;
2534 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535}
2536
2537/*
2538 * go to the last know line number for the current buffer
2539 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002540 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002541buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542{
2543 pos_T *fpos;
2544
2545 fpos = buflist_findfpos(curbuf);
2546
2547 curwin->w_cursor.lnum = fpos->lnum;
2548 check_cursor_lnum();
2549
2550 if (p_sol)
2551 curwin->w_cursor.col = 0;
2552 else
2553 {
2554 curwin->w_cursor.col = fpos->col;
2555 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 curwin->w_set_curswant = TRUE;
2558 }
2559}
2560
Bram Moolenaar81695252004-12-29 20:58:21 +00002561/*
2562 * Find file in buffer list by name (it has to be for the current window).
2563 * Returns NULL if not found.
2564 */
2565 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002566buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002567{
2568 char_u *ffname;
2569 buf_T *buf = NULL;
2570
Bram Moolenaarc667da52019-11-30 20:52:27 +01002571 // First make the name into a full path name
Bram Moolenaar81695252004-12-29 20:58:21 +00002572 ffname = FullName_save(fname,
2573#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002574 TRUE // force expansion, get rid of symbolic links
Bram Moolenaar81695252004-12-29 20:58:21 +00002575#else
2576 FALSE
2577#endif
2578 );
2579 if (ffname != NULL)
2580 {
2581 buf = buflist_findname(ffname);
2582 vim_free(ffname);
2583 }
2584 return buf;
2585}
Bram Moolenaar81695252004-12-29 20:58:21 +00002586
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587/*
2588 * Find file in buffer list by name (it has to be for the current window).
2589 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002590 * Skips dummy buffers.
2591 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 */
2593 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002594buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595{
2596#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002597 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598
2599 if (mch_stat((char *)ffname, &st) < 0)
2600 st.st_dev = (dev_T)-1;
2601 return buflist_findname_stat(ffname, &st);
2602}
2603
2604/*
2605 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2606 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002607 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 */
2609 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002610buflist_findname_stat(
2611 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002612 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613{
2614#endif
2615 buf_T *buf;
2616
Bram Moolenaarc667da52019-11-30 20:52:27 +01002617 // Start at the last buffer, expect to find a match sooner.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002618 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaar81695252004-12-29 20:58:21 +00002619 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620#ifdef UNIX
2621 , stp
2622#endif
2623 ))
2624 return buf;
2625 return NULL;
2626}
2627
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628/*
2629 * Find file in buffer list by a regexp pattern.
2630 * Return fnum of the found buffer.
2631 * Return < 0 for error.
2632 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002634buflist_findpat(
2635 char_u *pattern,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002636 char_u *pattern_end, // pointer to first char after pattern
2637 int unlisted, // find unlisted buffers
2638 int diffmode UNUSED, // find diff-mode buffers only
2639 int curtab_only) // find buffers in current tab only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640{
2641 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 int match = -1;
2643 int find_listed;
2644 char_u *pat;
2645 char_u *patend;
2646 int attempt;
2647 char_u *p;
2648 int toggledollar;
2649
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002650 // "%" is current file, "%%" or "#" is alternate file
2651 if ((pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2652 || (in_vim9script() && pattern_end == pattern + 2
2653 && pattern[0] == '%' && pattern[1] == '%'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 {
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002655 if (*pattern == '#' || pattern_end == pattern + 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 match = curwin->w_alt_fnum;
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002657 else
2658 match = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659#ifdef FEAT_DIFF
2660 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2661 match = -1;
2662#endif
2663 }
2664
2665 /*
2666 * Try four ways of matching a listed buffer:
2667 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002668 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 * attempt == 2: with '$' at end (only match at end)
2670 * attempt == 3: with '^' at start and '$' at end (only full match)
2671 * Repeat this for finding an unlisted buffer if there was no matching
2672 * listed buffer.
2673 */
2674 else
2675 {
2676 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2677 if (pat == NULL)
2678 return -1;
2679 patend = pat + STRLEN(pat) - 1;
2680 toggledollar = (patend > pat && *patend == '$');
2681
Bram Moolenaarc667da52019-11-30 20:52:27 +01002682 // First try finding a listed buffer. If not found and "unlisted"
2683 // is TRUE, try finding an unlisted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 find_listed = TRUE;
2685 for (;;)
2686 {
2687 for (attempt = 0; attempt <= 3; ++attempt)
2688 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002689 regmatch_T regmatch;
2690
Bram Moolenaarc667da52019-11-30 20:52:27 +01002691 // may add '^' and '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 if (toggledollar)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002693 *patend = (attempt < 2) ? NUL : '$'; // add/remove '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 p = pat;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002695 if (*p == '^' && !(attempt & 1)) // add/remove '^'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 ++p;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01002697 regmatch.regprog = vim_regcomp(p, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002699 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002700 {
2701 if (regmatch.regprog == NULL)
2702 {
2703 // invalid pattern, possibly after switching engine
2704 vim_free(pat);
2705 return -1;
2706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 if (buf->b_p_bl == find_listed
2708#ifdef FEAT_DIFF
2709 && (!diffmode || diff_mode_buf(buf))
2710#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002711 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002713 if (curtab_only)
2714 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002715 // Ignore the match if the buffer is not open in
2716 // the current tab.
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002717 win_T *wp;
2718
Bram Moolenaar29323592016-07-24 22:04:11 +02002719 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002720 if (wp->w_buffer == buf)
2721 break;
2722 if (wp == NULL)
2723 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002724 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002725 if (match >= 0) // already found a match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 {
2727 match = -2;
2728 break;
2729 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002730 match = buf->b_fnum; // remember first match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 }
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002734 vim_regfree(regmatch.regprog);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002735 if (match >= 0) // found one match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 break;
2737 }
2738
Bram Moolenaarc667da52019-11-30 20:52:27 +01002739 // Only search for unlisted buffers if there was no match with
2740 // a listed buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 if (!unlisted || !find_listed || match != -1)
2742 break;
2743 find_listed = FALSE;
2744 }
2745
2746 vim_free(pat);
2747 }
2748
2749 if (match == -2)
Bram Moolenaare1242042021-12-16 20:56:57 +00002750 semsg(_(e_more_than_one_match_for_str), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 else if (match < 0)
Bram Moolenaare1242042021-12-16 20:56:57 +00002752 semsg(_(e_no_matching_buffer_for_str), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 return match;
2754}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755
Bram Moolenaar52410572019-10-27 05:12:45 +01002756#ifdef FEAT_VIMINFO
2757typedef struct {
2758 buf_T *buf;
2759 char_u *match;
2760} bufmatch_T;
2761#endif
2762
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763/*
2764 * Find all buffer names that match.
2765 * For command line expansion of ":buf" and ":sbuf".
2766 * Return OK if matches found, FAIL otherwise.
2767 */
2768 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002769ExpandBufnames(
2770 char_u *pat,
2771 int *num_file,
2772 char_u ***file,
2773 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774{
2775 int count = 0;
2776 buf_T *buf;
2777 int round;
2778 char_u *p;
2779 int attempt;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002780 char_u *patc = NULL;
Bram Moolenaar52410572019-10-27 05:12:45 +01002781#ifdef FEAT_VIMINFO
2782 bufmatch_T *matches = NULL;
2783#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002784 int fuzzy;
2785 fuzmatch_str_T *fuzmatch = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786
Bram Moolenaarc667da52019-11-30 20:52:27 +01002787 *num_file = 0; // return values in case of FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 *file = NULL;
2789
Bram Moolenaarefcc3292019-12-30 21:59:03 +01002790#ifdef FEAT_DIFF
2791 if ((options & BUF_DIFF_FILTER) && !curwin->w_p_diff)
2792 return FAIL;
2793#endif
2794
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002795 fuzzy = cmdline_fuzzy_complete(pat);
2796
2797 // Make a copy of "pat" and change "^" to "\(^\|[\/]\)" (if doing regular
2798 // expression matching)
2799 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002801 if (*pat == '^')
2802 {
2803 patc = alloc(STRLEN(pat) + 11);
2804 if (patc == NULL)
2805 return FAIL;
2806 STRCPY(patc, "\\(^\\|[\\/]\\)");
2807 STRCPY(patc + 11, pat + 1);
2808 }
2809 else
2810 patc = pat;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002811 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00002812
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002813 // attempt == 0: try match with '\<', match at start of word
2814 // attempt == 1: try match without '\<', match anywhere
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002815 for (attempt = 0; attempt <= (fuzzy ? 0 : 1); ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002816 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002817 regmatch_T regmatch;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002818 int score = 0;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002819
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002820 if (!fuzzy)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002821 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002822 if (attempt > 0 && patc == pat)
2823 break; // there was no anchor, no need to try again
2824 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002827 // round == 1: Count the matches.
2828 // round == 2: Build the array to keep the matches.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 for (round = 1; round <= 2; ++round)
2830 {
2831 count = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002832 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002834 if (!buf->b_p_bl) // skip unlisted buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 continue;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002836#ifdef FEAT_DIFF
2837 if (options & BUF_DIFF_FILTER)
2838 // Skip buffers not suitable for
2839 // :diffget or :diffput completion.
Bram Moolenaarefcc3292019-12-30 21:59:03 +01002840 if (buf == curbuf || !diff_mode_buf(buf))
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002841 continue;
2842#endif
2843
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002844 if (!fuzzy)
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002845 {
2846 if (regmatch.regprog == NULL)
2847 {
2848 // invalid pattern, possibly after recompiling
2849 if (patc != pat)
2850 vim_free(patc);
2851 return FAIL;
2852 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002853 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002854 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002855 else
2856 {
2857 p = NULL;
2858 // first try matching with the short file name
2859 if ((score = fuzzy_match_str(buf->b_sfname, pat)) != 0)
2860 p = buf->b_sfname;
2861 if (p == NULL)
2862 {
2863 // next try matching with the full path file name
2864 if ((score = fuzzy_match_str(buf->b_ffname, pat)) != 0)
2865 p = buf->b_ffname;
2866 }
2867 }
2868
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002869 if (p == NULL)
2870 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002871
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002872 if (round == 1)
2873 {
2874 ++count;
2875 continue;
2876 }
2877
2878 if (options & WILD_HOME_REPLACE)
2879 p = home_replace_save(buf, p);
2880 else
2881 p = vim_strsave(p);
2882
2883 if (!fuzzy)
2884 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002885#ifdef FEAT_VIMINFO
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002886 if (matches != NULL)
2887 {
2888 matches[count].buf = buf;
2889 matches[count].match = p;
2890 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002892 else
2893#endif
2894 (*file)[count++] = p;
2895 }
2896 else
2897 {
2898 fuzmatch[count].idx = count;
2899 fuzmatch[count].str = p;
2900 fuzmatch[count].score = score;
2901 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 }
2903 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002904 if (count == 0) // no match found, break here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 break;
2906 if (round == 1)
2907 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002908 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002910 *file = ALLOC_MULT(char_u *, count);
2911 if (*file == NULL)
2912 {
2913 vim_regfree(regmatch.regprog);
2914 if (patc != pat)
2915 vim_free(patc);
2916 return FAIL;
2917 }
Bram Moolenaar52410572019-10-27 05:12:45 +01002918#ifdef FEAT_VIMINFO
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002919 if (options & WILD_BUFLASTUSED)
2920 matches = ALLOC_MULT(bufmatch_T, count);
Bram Moolenaar52410572019-10-27 05:12:45 +01002921#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002922 }
2923 else
2924 {
2925 fuzmatch = ALLOC_MULT(fuzmatch_str_T, count);
2926 if (fuzmatch == NULL)
2927 {
2928 *num_file = 0;
2929 *file = NULL;
2930 return FAIL;
2931 }
2932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 }
2934 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002935
2936 if (!fuzzy)
2937 {
2938 vim_regfree(regmatch.regprog);
2939 if (count) // match(es) found, break here
2940 break;
2941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 }
2943
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002944 if (!fuzzy && patc != pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002945 vim_free(patc);
2946
Bram Moolenaar52410572019-10-27 05:12:45 +01002947#ifdef FEAT_VIMINFO
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002948 if (!fuzzy)
Bram Moolenaar52410572019-10-27 05:12:45 +01002949 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002950 if (matches != NULL)
Bram Moolenaar52410572019-10-27 05:12:45 +01002951 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002952 int i;
2953 if (count > 1)
2954 qsort(matches, count, sizeof(bufmatch_T), buf_compare);
2955 // if the current buffer is first in the list, place it at the end
2956 if (matches[0].buf == curbuf)
2957 {
2958 for (i = 1; i < count; i++)
2959 (*file)[i-1] = matches[i].match;
2960 (*file)[count-1] = matches[0].match;
2961 }
2962 else
2963 {
2964 for (i = 0; i < count; i++)
2965 (*file)[i] = matches[i].match;
2966 }
2967 vim_free(matches);
Bram Moolenaar52410572019-10-27 05:12:45 +01002968 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002969 }
2970 else
2971 {
2972 if (fuzzymatches_to_strmatches(fuzmatch, file, count, FALSE) == FAIL)
2973 return FAIL;
Bram Moolenaar52410572019-10-27 05:12:45 +01002974 }
2975#endif
2976
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 *num_file = count;
2978 return (count == 0 ? FAIL : OK);
2979}
2980
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981/*
2982 * Check for a match on the file name for buffer "buf" with regprog "prog".
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002983 * Note that rmp->regprog may become NULL when switching regexp engine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 */
2985 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002986buflist_match(
2987 regmatch_T *rmp,
2988 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002989 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990{
2991 char_u *match;
2992
Bram Moolenaarc667da52019-11-30 20:52:27 +01002993 // First try the short file name, then the long file name.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002994 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaara59f2df2022-05-11 11:42:28 +01002995 if (match == NULL && rmp->regprog != NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002996 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997
2998 return match;
2999}
3000
3001/*
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003002 * Try matching the regexp in "rmp->regprog" with file name "name".
3003 * Note that rmp->regprog may become NULL when switching regexp engine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 * Return "name" when there is a match, NULL when not.
3005 */
3006 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003007fname_match(
3008 regmatch_T *rmp,
3009 char_u *name,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003010 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011{
3012 char_u *match = NULL;
3013 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003015 // extra check for valid arguments
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003016 if (name == NULL || rmp->regprog == NULL)
3017 return NULL;
3018
3019 // Ignore case when 'fileignorecase' or the argument is set.
3020 rmp->rm_ic = p_fic || ignore_case;
3021 if (vim_regexec(rmp, name, (colnr_T)0))
3022 match = name;
3023 else if (rmp->regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 {
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003025 // Replace $(HOME) with '~' and try matching again.
3026 p = home_replace_save(NULL, name);
3027 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 match = name;
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003029 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 }
3031
3032 return match;
3033}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034
3035/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02003036 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 */
3038 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003039buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040{
Bram Moolenaar480778b2016-07-14 22:09:39 +02003041 char_u key[VIM_SIZEOF_INT * 2 + 1];
3042 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043
3044 if (nr == 0)
3045 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02003046 sprintf((char *)key, "%x", nr);
3047 hi = hash_find(&buf_hashtab, key);
3048
3049 if (!HASHITEM_EMPTY(hi))
3050 return (buf_T *)(hi->hi_key
3051 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 return NULL;
3053}
3054
3055/*
3056 * Get name of file 'n' in the buffer list.
3057 * When the file has no name an empty string is returned.
3058 * home_replace() is used to shorten the file name (used for marks).
3059 * Returns a pointer to allocated memory, of NULL when failed.
3060 */
3061 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003062buflist_nr2name(
3063 int n,
3064 int fullname,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003065 int helptail) // for help buffers return tail only
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066{
3067 buf_T *buf;
3068
3069 buf = buflist_findnr(n);
3070 if (buf == NULL)
3071 return NULL;
3072 return home_replace_save(helptail ? buf : NULL,
3073 fullname ? buf->b_ffname : buf->b_fname);
3074}
3075
3076/*
3077 * Set the "lnum" and "col" for the buffer "buf" and the current window.
3078 * When "copy_options" is TRUE save the local window option values.
3079 * When "lnum" is 0 only do the options.
3080 */
Bram Moolenaardefa0672019-07-21 19:25:37 +02003081 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003082buflist_setfpos(
3083 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003084 win_T *win, // may be NULL when using :badd
Bram Moolenaar7454a062016-01-30 15:14:10 +01003085 linenr_T lnum,
3086 colnr_T col,
3087 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088{
3089 wininfo_T *wip;
3090
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003091 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 if (wip->wi_win == win)
3093 break;
3094 if (wip == NULL)
3095 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003096 // allocate a new entry
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003097 wip = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 if (wip == NULL)
3099 return;
3100 wip->wi_win = win;
Bram Moolenaarc667da52019-11-30 20:52:27 +01003101 if (lnum == 0) // set lnum even when it's 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 lnum = 1;
3103 }
3104 else
3105 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003106 // remove the entry from the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 if (wip->wi_prev)
3108 wip->wi_prev->wi_next = wip->wi_next;
3109 else
3110 buf->b_wininfo = wip->wi_next;
3111 if (wip->wi_next)
3112 wip->wi_next->wi_prev = wip->wi_prev;
3113 if (copy_options && wip->wi_optset)
3114 {
3115 clear_winopt(&wip->wi_opt);
3116#ifdef FEAT_FOLDING
3117 deleteFoldRecurse(&wip->wi_folds);
3118#endif
3119 }
3120 }
3121 if (lnum != 0)
3122 {
3123 wip->wi_fpos.lnum = lnum;
3124 wip->wi_fpos.col = col;
3125 }
LemonBoydb0ea7f2022-04-10 17:59:26 +01003126 if (win != NULL)
3127 wip->wi_changelistidx = win->w_changelistidx;
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003128 if (copy_options && win != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003130 // Save the window-specific option values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
3132#ifdef FEAT_FOLDING
3133 wip->wi_fold_manual = win->w_fold_manual;
3134 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
3135#endif
3136 wip->wi_optset = TRUE;
3137 }
3138
Bram Moolenaarc667da52019-11-30 20:52:27 +01003139 // insert the entry in front of the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 wip->wi_next = buf->b_wininfo;
3141 buf->b_wininfo = wip;
3142 wip->wi_prev = NULL;
3143 if (wip->wi_next)
3144 wip->wi_next->wi_prev = wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145}
3146
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003147#ifdef FEAT_DIFF
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003148/*
3149 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
3150 * page. That's because a diff is local to a tab page.
3151 */
3152 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003153wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003154{
3155 win_T *wp;
3156
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003157 if (!wip->wi_opt.wo_diff)
3158 return FALSE;
3159
3160 FOR_ALL_WINDOWS(wp)
3161 // return FALSE when it's a window in the current tab page, thus
3162 // the buffer was in diff mode here
3163 if (wip->wi_win == wp)
3164 return FALSE;
3165 return TRUE;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003166}
3167#endif
3168
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169/*
3170 * Find info for the current window in buffer "buf".
3171 * If not found, return the info for the most recently used window.
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003172 * When "need_options" is TRUE skip entries where wi_optset is FALSE.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003173 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
3174 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 * Returns NULL when there isn't any info.
3176 */
3177 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003178find_wininfo(
3179 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003180 int need_options,
Bram Moolenaar7454a062016-01-30 15:14:10 +01003181 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182{
3183 wininfo_T *wip;
3184
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003185 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003186 if (wip->wi_win == curwin
3187#ifdef FEAT_DIFF
3188 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
3189#endif
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003190
3191 && (!need_options || wip->wi_optset))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003193
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003194 if (wip != NULL)
3195 return wip;
3196
Bram Moolenaarc667da52019-11-30 20:52:27 +01003197 // If no wininfo for curwin, use the first in the list (that doesn't have
3198 // 'diff' set and is in another tab page).
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003199 // If "need_options" is TRUE skip entries that don't have options set,
3200 // unless the window is editing "buf", so we can copy from the window
3201 // itself.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003202#ifdef FEAT_DIFF
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003203 if (skip_diff_buffer)
3204 {
3205 FOR_ALL_BUF_WININFO(buf, wip)
3206 if (!wininfo_other_tab_diff(wip)
3207 && (!need_options || wip->wi_optset
3208 || (wip->wi_win != NULL
3209 && wip->wi_win->w_buffer == buf)))
3210 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003211 }
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00003212 else
3213#endif
3214 wip = buf->b_wininfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 return wip;
3216}
3217
3218/*
3219 * Reset the local window options to the values last used in this window.
3220 * If the buffer wasn't used in this window before, use the values from
3221 * the most recently used window. If the values were never set, use the
3222 * global values for the window.
3223 */
3224 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003225get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226{
3227 wininfo_T *wip;
3228
3229 clear_winopt(&curwin->w_onebuf_opt);
3230#ifdef FEAT_FOLDING
3231 clearFolding(curwin);
3232#endif
3233
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003234 wip = find_wininfo(buf, TRUE, TRUE);
Bram Moolenaar25782a72018-05-13 18:05:33 +02003235 if (wip != NULL && wip->wi_win != NULL
3236 && wip->wi_win != curwin && wip->wi_win->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003238 // The buffer is currently displayed in the window: use the actual
3239 // option values instead of the saved (possibly outdated) values.
Bram Moolenaar25782a72018-05-13 18:05:33 +02003240 win_T *wp = wip->wi_win;
3241
3242 copy_winopt(&wp->w_onebuf_opt, &curwin->w_onebuf_opt);
3243#ifdef FEAT_FOLDING
3244 curwin->w_fold_manual = wp->w_fold_manual;
3245 curwin->w_foldinvalid = TRUE;
3246 cloneFoldGrowArray(&wp->w_folds, &curwin->w_folds);
3247#endif
3248 }
3249 else if (wip != NULL && wip->wi_optset)
3250 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003251 // the buffer was displayed in the current window earlier
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
3253#ifdef FEAT_FOLDING
3254 curwin->w_fold_manual = wip->wi_fold_manual;
3255 curwin->w_foldinvalid = TRUE;
3256 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
3257#endif
3258 }
3259 else
3260 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
LemonBoydb0ea7f2022-04-10 17:59:26 +01003261 if (wip != NULL)
3262 curwin->w_changelistidx = wip->wi_changelistidx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263
3264#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +01003265 // Set 'foldlevel' to 'foldlevelstart' if it's not negative.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 if (p_fdls >= 0)
3267 curwin->w_p_fdl = p_fdls;
3268#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02003269 after_copy_winopt(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270}
3271
3272/*
3273 * Find the position (lnum and col) for the buffer 'buf' for the current
3274 * window.
3275 * Returns a pointer to no_position if no position is found.
3276 */
3277 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003278buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279{
3280 wininfo_T *wip;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003281 static pos_T no_position = {1, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003283 wip = find_wininfo(buf, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 if (wip != NULL)
3285 return &(wip->wi_fpos);
3286 else
3287 return &no_position;
3288}
3289
3290/*
3291 * Find the lnum for the buffer 'buf' for the current window.
3292 */
3293 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01003294buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295{
3296 return buflist_findfpos(buf)->lnum;
3297}
3298
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02003300 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003303buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304{
Bram Moolenaar52410572019-10-27 05:12:45 +01003305 buf_T *buf = firstbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 int len;
3307 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003308 int ro_char;
3309 int changed_char;
Bram Moolenaar0751f512018-03-29 16:37:16 +02003310#ifdef FEAT_TERMINAL
3311 int job_running;
3312 int job_none_open;
3313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314
Bram Moolenaar52410572019-10-27 05:12:45 +01003315#ifdef FEAT_VIMINFO
3316 garray_T buflist;
3317 buf_T **buflist_data = NULL, **p;
3318
3319 if (vim_strchr(eap->arg, 't'))
3320 {
3321 ga_init2(&buflist, sizeof(buf_T *), 50);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003322 FOR_ALL_BUFFERS(buf)
Bram Moolenaar52410572019-10-27 05:12:45 +01003323 {
3324 if (ga_grow(&buflist, 1) == OK)
3325 ((buf_T **)buflist.ga_data)[buflist.ga_len++] = buf;
3326 }
3327
3328 qsort(buflist.ga_data, (size_t)buflist.ga_len,
3329 sizeof(buf_T *), buf_compare);
3330
Bram Moolenaar3b991522019-11-06 23:26:20 +01003331 buflist_data = (buf_T **)buflist.ga_data;
3332 buf = *buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003333 }
Bram Moolenaar3b991522019-11-06 23:26:20 +01003334 p = buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003335
Bram Moolenaar3b991522019-11-06 23:26:20 +01003336 for (; buf != NULL && !got_int; buf = buflist_data != NULL
Bram Moolenaar52410572019-10-27 05:12:45 +01003337 ? (++p < buflist_data + buflist.ga_len ? *p : NULL)
3338 : buf->b_next)
3339#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
Bram Moolenaar52410572019-10-27 05:12:45 +01003341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 {
Bram Moolenaar0751f512018-03-29 16:37:16 +02003343#ifdef FEAT_TERMINAL
3344 job_running = term_job_running(buf->b_term);
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003345 job_none_open = term_none_open(buf->b_term);
Bram Moolenaar0751f512018-03-29 16:37:16 +02003346#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003347 // skip unlisted buffers, unless ! was used
Bram Moolenaard51cb702015-07-21 15:03:06 +02003348 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
3349 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
3350 || (vim_strchr(eap->arg, '+')
3351 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
3352 || (vim_strchr(eap->arg, 'a')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003353 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
Bram Moolenaard51cb702015-07-21 15:03:06 +02003354 || (vim_strchr(eap->arg, 'h')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003355 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3356#ifdef FEAT_TERMINAL
3357 || (vim_strchr(eap->arg, 'R')
3358 && (!job_running || (job_running && job_none_open)))
3359 || (vim_strchr(eap->arg, '?')
3360 && (!job_running || (job_running && !job_none_open)))
3361 || (vim_strchr(eap->arg, 'F')
3362 && (job_running || buf->b_term == NULL))
3363#endif
Bram Moolenaard51cb702015-07-21 15:03:06 +02003364 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3365 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3366 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3367 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3368 || (vim_strchr(eap->arg, '#')
3369 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003372 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 else
3374 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003375 if (message_filtered(NameBuff))
3376 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003378 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3379 : (bufIsChanged(buf) ? '+' : ' ');
3380#ifdef FEAT_TERMINAL
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003381 if (job_running)
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003382 {
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003383 if (job_none_open)
Bram Moolenaar4033c552017-09-16 20:54:51 +02003384 ro_char = '?';
3385 else
3386 ro_char = 'R';
Bram Moolenaarc667da52019-11-30 20:52:27 +01003387 changed_char = ' '; // bufIsChanged() returns TRUE to avoid
3388 // closing, but it's not actually changed.
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003389 }
3390 else if (buf->b_term != NULL)
3391 ro_char = 'F';
3392 else
3393#endif
3394 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3395
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003396 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00003397 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 buf->b_fnum,
3399 buf->b_p_bl ? ' ' : 'u',
3400 buf == curbuf ? '%' :
3401 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3402 buf->b_ml.ml_mfp == NULL ? ' ' :
3403 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003404 ro_char,
3405 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003406 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003407 if (len > IOSIZE - 20)
3408 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409
Bram Moolenaarc667da52019-11-30 20:52:27 +01003410 // put "line 999" in column 40 or after the file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 i = 40 - vim_strsize(IObuff);
3412 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 IObuff[len++] = ' ';
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003414 while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar52410572019-10-27 05:12:45 +01003415#ifdef FEAT_VIMINFO
3416 if (vim_strchr(eap->arg, 't') && buf->b_last_used)
3417 add_time(IObuff + len, (size_t)(IOSIZE - len), buf->b_last_used);
3418 else
3419#endif
3420 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3421 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003422 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 msg_outtrans(IObuff);
Bram Moolenaarc667da52019-11-30 20:52:27 +01003424 out_flush(); // output one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 ui_breakcheck();
3426 }
Bram Moolenaar52410572019-10-27 05:12:45 +01003427
3428#ifdef FEAT_VIMINFO
3429 if (buflist_data)
3430 ga_clear(&buflist);
3431#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433
3434/*
3435 * Get file name and line number for file 'fnum'.
3436 * Used by DoOneCmd() for translating '%' and '#'.
3437 * Used by insert_reg() and cmdline_paste() for '#' register.
3438 * Return FAIL if not found, OK for success.
3439 */
3440 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003441buflist_name_nr(
3442 int fnum,
3443 char_u **fname,
3444 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445{
3446 buf_T *buf;
3447
3448 buf = buflist_findnr(fnum);
3449 if (buf == NULL || buf->b_fname == NULL)
3450 return FAIL;
3451
3452 *fname = buf->b_fname;
3453 *lnum = buflist_findlnum(buf);
3454
3455 return OK;
3456}
3457
3458/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003459 * Set the file name for "buf"' to "ffname_arg", short file name to
3460 * "sfname_arg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 * The file name with the full path is also remembered, for when :cd is used.
3462 * Returns FAIL for failure (file name already in use by other buffer)
3463 * OK otherwise.
3464 */
3465 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003466setfname(
3467 buf_T *buf,
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003468 char_u *ffname_arg,
3469 char_u *sfname_arg,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003470 int message) // give message when buffer already exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003472 char_u *ffname = ffname_arg;
3473 char_u *sfname = sfname_arg;
Bram Moolenaar81695252004-12-29 20:58:21 +00003474 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003476 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477#endif
3478
3479 if (ffname == NULL || *ffname == NUL)
3480 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003481 // Removing the name.
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003482 if (buf->b_sfname != buf->b_ffname)
3483 VIM_CLEAR(buf->b_sfname);
3484 else
3485 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003486 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487#ifdef UNIX
3488 st.st_dev = (dev_T)-1;
3489#endif
3490 }
3491 else
3492 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003493 fname_expand(buf, &ffname, &sfname); // will allocate ffname
3494 if (ffname == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 return FAIL;
3496
3497 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01003498 * If the file name is already used in another buffer:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 * - if the buffer is loaded, fail
3500 * - if the buffer is not loaded, delete it from the list
3501 */
3502#ifdef UNIX
3503 if (mch_stat((char *)ffname, &st) < 0)
3504 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003505#endif
3506 if (!(buf->b_flags & BF_DUMMY))
3507#ifdef UNIX
3508 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003510 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511#endif
3512 if (obuf != NULL && obuf != buf)
3513 {
Bram Moolenaard3710cf2021-10-04 23:13:13 +01003514 win_T *win;
3515 tabpage_T *tab;
3516 int in_use = FALSE;
3517
3518 // during startup a window may use a buffer that is not loaded yet
3519 FOR_ALL_TAB_WINDOWS(tab, win)
3520 if (win->w_buffer == obuf)
3521 in_use = TRUE;
3522
3523 // it's loaded or used in a window, fail
3524 if (obuf->b_ml.ml_mfp != NULL || in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 {
3526 if (message)
Bram Moolenaare1242042021-12-16 20:56:57 +00003527 emsg(_(e_buffer_with_this_name_already_exists));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 vim_free(ffname);
3529 return FAIL;
3530 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003531 // delete from the list
Bram Moolenaara6e8f882019-12-14 16:18:15 +01003532 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 }
3534 sfname = vim_strsave(sfname);
3535 if (ffname == NULL || sfname == NULL)
3536 {
3537 vim_free(sfname);
3538 vim_free(ffname);
3539 return FAIL;
3540 }
3541#ifdef USE_FNAME_CASE
Bram Moolenaarc667da52019-11-30 20:52:27 +01003542 fname_case(sfname, 0); // set correct case for short file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543#endif
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003544 if (buf->b_sfname != buf->b_ffname)
3545 vim_free(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 vim_free(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 buf->b_ffname = ffname;
3548 buf->b_sfname = sfname;
3549 }
3550 buf->b_fname = buf->b_sfname;
3551#ifdef UNIX
3552 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003553 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 else
3555 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003556 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 buf->b_dev = st.st_dev;
3558 buf->b_ino = st.st_ino;
3559 }
3560#endif
3561
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563
3564 buf_name_changed(buf);
3565 return OK;
3566}
3567
3568/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003569 * Crude way of changing the name of a buffer. Use with care!
3570 * The name should be relative to the current directory.
3571 */
3572 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003573buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003574{
3575 buf_T *buf;
3576
3577 buf = buflist_findnr(fnum);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00003578 if (buf == NULL)
3579 return;
3580
3581 if (buf->b_sfname != buf->b_ffname)
3582 vim_free(buf->b_sfname);
3583 vim_free(buf->b_ffname);
3584 buf->b_ffname = vim_strsave(name);
3585 buf->b_sfname = NULL;
3586 // Allocate ffname and expand into full path. Also resolves .lnk
3587 // files on Win32.
3588 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
3589 buf->b_fname = buf->b_sfname;
Bram Moolenaar86b68352004-12-27 21:59:20 +00003590}
3591
3592/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 * Take care of what needs to be done when the name of buffer "buf" has
3594 * changed.
3595 */
3596 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003597buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598{
3599 /*
3600 * If the file name changed, also change the name of the swapfile
3601 */
3602 if (buf->b_ml.ml_mfp != NULL)
3603 ml_setname(buf);
3604
Bram Moolenaar3ad69532021-11-19 17:01:08 +00003605#ifdef FEAT_TERMINAL
3606 if (buf->b_term != NULL)
3607 term_clear_status_text(buf->b_term);
3608#endif
3609
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 if (curwin->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003611 check_arg_idx(curwin); // check file name for arg list
Bram Moolenaarc667da52019-11-30 20:52:27 +01003612 maketitle(); // set window title
Bram Moolenaarc667da52019-11-30 20:52:27 +01003613 status_redraw_all(); // status lines need to be redrawn
3614 fmarks_check_names(buf); // check named file marks
3615 ml_timestamp(buf); // reset timestamp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616}
3617
3618/*
3619 * set alternate file name for current window
3620 *
3621 * Used by do_one_cmd(), do_write() and do_ecmd().
3622 * Return the buffer.
3623 */
3624 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003625setaltfname(
3626 char_u *ffname,
3627 char_u *sfname,
3628 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629{
3630 buf_T *buf;
3631
Bram Moolenaarc667da52019-11-30 20:52:27 +01003632 // Create a buffer. 'buflisted' is not set if it's a new buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaare1004402020-10-24 20:49:43 +02003634 if (buf != NULL && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 curwin->w_alt_fnum = buf->b_fnum;
3636 return buf;
3637}
3638
3639/*
3640 * Get alternate file name for current window.
3641 * Return NULL if there isn't any, and give error message if requested.
3642 */
3643 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003644getaltfname(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003645 int errmsg) // give error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646{
3647 char_u *fname;
3648 linenr_T dummy;
3649
3650 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3651 {
3652 if (errmsg)
Bram Moolenaar108010a2021-06-27 22:03:33 +02003653 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 return NULL;
3655 }
3656 return fname;
3657}
3658
3659/*
3660 * Add a file name to the buflist and return its number.
3661 * Uses same flags as buflist_new(), except BLN_DUMMY.
3662 *
3663 * used by qf_init(), main() and doarglist()
3664 */
3665 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003666buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667{
3668 buf_T *buf;
3669
3670 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3671 if (buf != NULL)
3672 return buf->b_fnum;
3673 return 0;
3674}
3675
3676#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3677/*
3678 * Adjust slashes in file names. Called after 'shellslash' was set.
3679 */
3680 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003681buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682{
3683 buf_T *bp;
3684
Bram Moolenaar29323592016-07-24 22:04:11 +02003685 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 {
3687 if (bp->b_ffname != NULL)
3688 slash_adjust(bp->b_ffname);
3689 if (bp->b_sfname != NULL)
3690 slash_adjust(bp->b_sfname);
3691 }
3692}
3693#endif
3694
3695/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003696 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 * Also save the local window option values.
3698 */
3699 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003700buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003702 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703}
3704
3705/*
3706 * Return TRUE if 'ffname' is not the same file as current file.
3707 * Fname must have a full path (expanded by mch_FullName()).
3708 */
3709 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003710otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711{
3712 return otherfile_buf(curbuf, ffname
3713#ifdef UNIX
3714 , NULL
3715#endif
3716 );
3717}
3718
3719 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003720otherfile_buf(
3721 buf_T *buf,
3722 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003724 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003726 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727{
Bram Moolenaarc667da52019-11-30 20:52:27 +01003728 // no name is different
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3730 return TRUE;
3731 if (fnamecmp(ffname, buf->b_ffname) == 0)
3732 return FALSE;
3733#ifdef UNIX
3734 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003735 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736
Bram Moolenaarc667da52019-11-30 20:52:27 +01003737 // If no stat_T given, get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 if (stp == NULL)
3739 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003740 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 st.st_dev = (dev_T)-1;
3742 stp = &st;
3743 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003744 // Use dev/ino to check if the files are the same, even when the names
3745 // are different (possible with links). Still need to compare the
3746 // name above, for when the file doesn't exist yet.
3747 // Problem: The dev/ino changes when a file is deleted (and created
3748 // again) and remains the same when renamed/moved. We don't want to
3749 // mch_stat() each buffer each time, that would be too slow. Get the
3750 // dev/ino again when they appear to match, but not when they appear
3751 // to be different: Could skip a buffer when it's actually the same
3752 // file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 if (buf_same_ino(buf, stp))
3754 {
3755 buf_setino(buf);
3756 if (buf_same_ino(buf, stp))
3757 return FALSE;
3758 }
3759 }
3760#endif
3761 return TRUE;
3762}
3763
3764#if defined(UNIX) || defined(PROTO)
3765/*
3766 * Set inode and device number for a buffer.
3767 * Must always be called when b_fname is changed!.
3768 */
3769 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003770buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003772 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773
3774 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3775 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003776 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 buf->b_dev = st.st_dev;
3778 buf->b_ino = st.st_ino;
3779 }
3780 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003781 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782}
3783
3784/*
3785 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3786 */
3787 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003788buf_same_ino(
3789 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003790 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003792 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 && stp->st_dev == buf->b_dev
3794 && stp->st_ino == buf->b_ino);
3795}
3796#endif
3797
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003798/*
3799 * Print info about the current buffer.
3800 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003802fileinfo(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003803 int fullname, // when non-zero print full path
Bram Moolenaar7454a062016-01-30 15:14:10 +01003804 int shorthelp,
3805 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806{
3807 char_u *name;
3808 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003809 char *p;
3810 char *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003811 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003813 buffer = alloc(IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 if (buffer == NULL)
3815 return;
3816
Bram Moolenaarc667da52019-11-30 20:52:27 +01003817 if (fullname > 1) // 2 CTRL-G: include buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003819 vim_snprintf(buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 p = buffer + STRLEN(buffer);
3821 }
3822 else
3823 p = buffer;
3824
3825 *p++ = '"';
3826 if (buf_spname(curbuf) != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003827 vim_strncpy((char_u *)p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 else
3829 {
3830 if (!fullname && curbuf->b_fname != NULL)
3831 name = curbuf->b_fname;
3832 else
3833 name = curbuf->b_ffname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003834 home_replace(shorthelp ? curbuf : NULL, name, (char_u *)p,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 (int)(IOSIZE - (p - buffer)), TRUE);
3836 }
3837
Bram Moolenaar32526b32019-01-19 17:43:09 +01003838 vim_snprintf_add(buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 curbufIsChanged() ? (shortmess(SHM_MOD)
3840 ? " [+]" : _(" [Modified]")) : " ",
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01003841 (curbuf->b_flags & BF_NOTEDITED) && !bt_dontwrite(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 ? _("[Not edited]") : "",
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01003843 (curbuf->b_flags & BF_NEW) && !bt_dontwrite(curbuf)
Bram Moolenaar722e5052020-06-12 22:31:00 +02003844 ? new_file_message() : "",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003846 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 : _("[readonly]")) : "",
3848 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3849 || curbuf->b_p_ro) ?
3850 " " : "");
Bram Moolenaarc667da52019-11-30 20:52:27 +01003851 // With 32 bit longs and more than 21,474,836 lines multiplying by 100
3852 // causes an overflow, thus for large numbers divide instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 if (curwin->w_cursor.lnum > 1000000L)
3854 n = (int)(((long)curwin->w_cursor.lnum) /
3855 ((long)curbuf->b_ml.ml_line_count / 100L));
3856 else
3857 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3858 (long)curbuf->b_ml.ml_line_count);
3859 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003860 vim_snprintf_add(buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 else if (p_ru)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003862 // Current line and column are already on the screen -- webb
Bram Moolenaar32526b32019-01-19 17:43:09 +01003863 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003864 NGETTEXT("%ld line --%d%%--", "%ld lines --%d%%--",
3865 curbuf->b_ml.ml_line_count),
3866 (long)curbuf->b_ml.ml_line_count, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 else
3868 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003869 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 _("line %ld of %ld --%d%%-- col "),
3871 (long)curwin->w_cursor.lnum,
3872 (long)curbuf->b_ml.ml_line_count,
3873 n);
3874 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003875 len = STRLEN(buffer);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003876 col_print((char_u *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3878 }
3879
Bram Moolenaar32526b32019-01-19 17:43:09 +01003880 (void)append_arg_number(curwin, (char_u *)buffer, IOSIZE,
3881 !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882
3883 if (dont_truncate)
3884 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003885 // Temporarily set msg_scroll to avoid the message being truncated.
3886 // First call msg_start() to get the message in the right place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 msg_start();
3888 n = msg_scroll;
3889 msg_scroll = TRUE;
3890 msg(buffer);
3891 msg_scroll = n;
3892 }
3893 else
3894 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003895 p = msg_trunc_attr(buffer, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaarc667da52019-11-30 20:52:27 +01003897 // Need to repeat the message after redrawing when:
3898 // - When restart_edit is set (otherwise there will be a delay
3899 // before redrawing).
3900 // - When the screen was scrolled but there is no wait-return
3901 // prompt.
Bram Moolenaar32526b32019-01-19 17:43:09 +01003902 set_keep_msg((char_u *)p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 }
3904
3905 vim_free(buffer);
3906}
3907
3908 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003909col_print(
3910 char_u *buf,
3911 size_t buflen,
3912 int col,
3913 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914{
3915 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003916 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003918 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919}
3920
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921static char_u *lasttitle = NULL;
3922static char_u *lasticon = NULL;
3923
Bram Moolenaar84a93082018-06-16 22:58:15 +02003924/*
3925 * Put the file name in the title bar and icon of the window.
3926 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003928maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929{
3930 char_u *p;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003931 char_u *title_str = NULL;
3932 char_u *icon_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 int maxlen = 0;
3934 int len;
3935 int mustset;
3936 char_u buf[IOSIZE];
3937 int off;
3938
3939 if (!redrawing())
3940 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003941 // Postpone updating the title when 'lazyredraw' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 need_maketitle = TRUE;
3943 return;
3944 }
3945
3946 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003947 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003948 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949
3950 if (p_title)
3951 {
3952 if (p_titlelen > 0)
3953 {
3954 maxlen = p_titlelen * Columns / 100;
3955 if (maxlen < 10)
3956 maxlen = 10;
3957 }
3958
Bram Moolenaar84a93082018-06-16 22:58:15 +02003959 title_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 if (*p_titlestring != NUL)
3961 {
3962#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003963 if (stl_syntax & STL_IN_TITLE)
Luuk van Baal7b224fd2022-11-07 12:16:51 +00003964 build_stl_str_hl(curwin, title_str, sizeof(buf), p_titlestring,
3965 (char_u *)"titlestring", 0,
3966 0, maxlen, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 else
3968#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003969 title_str = p_titlestring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 }
3971 else
3972 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003973 // format: "fname + (path) (1 of 2) - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974
Bram Moolenaar2c666692012-09-05 13:30:40 +02003975#define SPACE_FOR_FNAME (IOSIZE - 100)
3976#define SPACE_FOR_DIR (IOSIZE - 20)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003977#define SPACE_FOR_ARGNR (IOSIZE - 10) // at least room for " - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003979 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar21554412017-07-24 21:44:43 +02003980#ifdef FEAT_TERMINAL
3981 else if (curbuf->b_term != NULL)
3982 {
3983 vim_strncpy(buf, term_get_status_text(curbuf->b_term),
3984 SPACE_FOR_FNAME);
3985 }
3986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 else
3988 {
3989 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003990 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 }
3993
Bram Moolenaar21554412017-07-24 21:44:43 +02003994#ifdef FEAT_TERMINAL
3995 if (curbuf->b_term == NULL)
3996#endif
3997 switch (bufIsChanged(curbuf)
3998 + (curbuf->b_p_ro * 2)
3999 + (!curbuf->b_p_ma * 4))
4000 {
4001 case 1: STRCAT(buf, " +"); break;
4002 case 2: STRCAT(buf, " ="); break;
4003 case 3: STRCAT(buf, " =+"); break;
4004 case 4:
4005 case 6: STRCAT(buf, " -"); break;
4006 case 5:
4007 case 7: STRCAT(buf, " -+"); break;
4008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009
Bram Moolenaar21554412017-07-24 21:44:43 +02004010 if (curbuf->b_fname != NULL
4011#ifdef FEAT_TERMINAL
4012 && curbuf->b_term == NULL
4013#endif
4014 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004016 // Get path of file, replace home dir with ~
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 off = (int)STRLEN(buf);
4018 buf[off++] = ' ';
4019 buf[off++] = '(';
4020 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02004021 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc667da52019-11-30 20:52:27 +01004023 // avoid "c:/name" to be reduced to "c"
Keith Thompson184f71c2024-01-04 21:19:04 +01004024 if (SAFE_isalpha(buf[off]) && buf[off + 1] == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 off += 2;
4026#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01004027 // remove the file name
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004028 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 if (p == buf + off)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02004030 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004031 // must be a help buffer
Bram Moolenaar21554412017-07-24 21:44:43 +02004032 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02004033 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02004034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004037
Bram Moolenaarc667da52019-11-30 20:52:27 +01004038 // Translate unprintable chars and concatenate. Keep some
4039 // room for the server name. When there is no room (very long
4040 // file name) use (...).
Bram Moolenaar2c666692012-09-05 13:30:40 +02004041 if (off < SPACE_FOR_DIR)
4042 {
4043 p = transstr(buf + off);
4044 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
4045 vim_free(p);
4046 }
4047 else
4048 {
4049 vim_strncpy(buf + off, (char_u *)"...",
4050 (size_t)(SPACE_FOR_ARGNR - off));
4051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 STRCAT(buf, ")");
4053 }
4054
Bram Moolenaar2c666692012-09-05 13:30:40 +02004055 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056
4057#if defined(FEAT_CLIENTSERVER)
4058 if (serverName != NULL)
4059 {
4060 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02004061 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 }
4063 else
4064#endif
4065 STRCAT(buf, " - VIM");
4066
4067 if (maxlen > 0)
4068 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004069 // make it shorter by removing a bit in the middle
Bram Moolenaarf31b7642012-01-20 20:44:43 +01004070 if (vim_strsize(buf) > maxlen)
4071 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 }
4073 }
4074 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02004075 mustset = value_changed(title_str, &lasttitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076
4077 if (p_icon)
4078 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02004079 icon_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 if (*p_iconstring != NUL)
4081 {
4082#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00004083 if (stl_syntax & STL_IN_ICON)
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004084 build_stl_str_hl(curwin, icon_str, sizeof(buf), p_iconstring,
4085 (char_u *)"iconstring", 0, 0, 0, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 else
4087#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02004088 icon_str = p_iconstring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 }
4090 else
4091 {
4092 if (buf_spname(curbuf) != NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004093 p = buf_spname(curbuf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004094 else // use file name only in icon
Bram Moolenaar84a93082018-06-16 22:58:15 +02004095 p = gettail(curbuf->b_ffname);
4096 *icon_str = NUL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004097 // Truncate name at 100 bytes.
Bram Moolenaar84a93082018-06-16 22:58:15 +02004098 len = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 if (len > 100)
4100 {
4101 len -= 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 if (has_mbyte)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004103 len += (*mb_tail_off)(p, p + len) + 1;
Bram Moolenaar84a93082018-06-16 22:58:15 +02004104 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02004106 STRCPY(icon_str, p);
4107 trans_characters(icon_str, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 }
4109 }
4110
Bram Moolenaar84a93082018-06-16 22:58:15 +02004111 mustset |= value_changed(icon_str, &lasticon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112
4113 if (mustset)
4114 resettitle();
4115}
4116
4117/*
4118 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
4119 * from "str" if it does.
Bram Moolenaar84a93082018-06-16 22:58:15 +02004120 * Return TRUE if resettitle() is to be called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 */
4122 static int
Bram Moolenaar84a93082018-06-16 22:58:15 +02004123value_changed(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124{
4125 if ((str == NULL) != (*last == NULL)
4126 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
4127 {
4128 vim_free(*last);
4129 if (str == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004130 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 *last = NULL;
Bram Moolenaar40385db2018-08-07 22:31:44 +02004132 mch_restore_title(
4133 last == &lasttitle ? SAVE_RESTORE_TITLE : SAVE_RESTORE_ICON);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 else
Bram Moolenaar84a93082018-06-16 22:58:15 +02004136 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 *last = vim_strsave(str);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004138 return TRUE;
4139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 }
4141 return FALSE;
4142}
4143
4144/*
4145 * Put current window title back (used after calling a shell)
4146 */
4147 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004148resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149{
4150 mch_settitle(lasttitle, lasticon);
4151}
Bram Moolenaarea408852005-06-25 22:49:46 +00004152
4153# if defined(EXITFREE) || defined(PROTO)
4154 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004155free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00004156{
4157 vim_free(lasttitle);
4158 vim_free(lasticon);
4159}
4160# endif
4161
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004163#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004164
4165/*
4166 * Used for building in the status line.
4167 */
4168typedef struct
4169{
4170 char_u *stl_start;
4171 int stl_minwid;
4172 int stl_maxwid;
4173 enum {
4174 Normal,
4175 Empty,
4176 Group,
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004177 Separate,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004178 Highlight,
4179 TabPage,
4180 Trunc
4181 } stl_type;
4182} stl_item_T;
4183
4184static size_t stl_items_len = 20; // Initial value, grows as needed.
4185static stl_item_T *stl_items = NULL;
4186static int *stl_groupitem = NULL;
4187static stl_hlrec_T *stl_hltab = NULL;
4188static stl_hlrec_T *stl_tabtab = NULL;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004189static int *stl_separator_locations = NULL;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004190
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004192 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 * Return length of string in screen cells.
4194 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004195 * Normally works for window "wp", except when working for 'tabline' then it
4196 * is "curwin".
4197 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 * Items are drawn interspersed with the text that surrounds it
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004199 * Specials: %-<wid>(xxx%) => group, %= => separation marker, %< => truncation
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
4201 *
4202 * If maxwidth is not zero, the string will be filled at any middle marker
4203 * or truncated if too long, fillchar is used for all whitespace.
4204 */
4205 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004206build_stl_str_hl(
4207 win_T *wp,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004208 char_u *out, // buffer to write into != NameBuff
4209 size_t outlen, // length of out[]
Bram Moolenaar7454a062016-01-30 15:14:10 +01004210 char_u *fmt,
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004211 char_u *opt_name, // option name corresponding to "fmt"
4212 int opt_scope, // scope for "opt_name"
Bram Moolenaar7454a062016-01-30 15:14:10 +01004213 int fillchar,
4214 int maxwidth,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004215 stl_hlrec_T **hltab, // return: HL attributes (can be NULL)
4216 stl_hlrec_T **tabtab) // return: tab page nrs (can be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217{
Bram Moolenaar10772302019-01-20 18:25:54 +01004218 linenr_T lnum;
4219 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 char_u *p;
4221 char_u *s;
4222 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004223 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224#ifdef FEAT_EVAL
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004225 int use_sandbox;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004226 win_T *save_curwin;
4227 buf_T *save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004228 int save_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229#endif
4230 int empty_line;
4231 colnr_T virtcol;
4232 long l;
4233 long n;
4234 int prevchar_isflag;
4235 int prevchar_isitem;
4236 int itemisflag;
4237 int fillable;
4238 char_u *str;
4239 long num;
4240 int width;
4241 int itemcnt;
4242 int curitem;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004243 int group_end_userhl;
4244 int group_start_userhl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 int groupdepth;
shadmansaleh30e3de22021-05-15 17:23:28 +02004246#ifdef FEAT_EVAL
4247 int evaldepth;
4248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 int minwid;
4250 int maxwid;
4251 int zeropad;
4252 char_u base;
4253 char_u opt;
4254#define TMPLEN 70
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004255 char_u buf_tmp[TMPLEN];
4256 char_u win_tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004257 char_u *usefmt = fmt;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004258 stl_hlrec_T *sp;
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01004259 int save_redraw_not_allowed = redraw_not_allowed;
Bram Moolenaar0e1f36f2022-02-15 16:17:44 +00004260 int save_KeyTyped = KeyTyped;
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004261 // TODO: find out why using called_emsg_before makes tests fail, does it
4262 // matter?
4263 // int called_emsg_before = called_emsg;
4264 int did_emsg_before = did_emsg;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004265
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01004266 // When inside update_screen() we do not want redrawing a statusline,
4267 // ruler, title, etc. to trigger another redraw, it may cause an endless
4268 // loop.
4269 if (updating_screen)
4270 redraw_not_allowed = TRUE;
4271
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004272 if (stl_items == NULL)
4273 {
4274 stl_items = ALLOC_MULT(stl_item_T, stl_items_len);
4275 stl_groupitem = ALLOC_MULT(int, stl_items_len);
Brandon Richardsona493b652022-02-19 11:45:03 +00004276
4277 // Allocate one more, because the last element is used to indicate the
4278 // end of the list.
4279 stl_hltab = ALLOC_MULT(stl_hlrec_T, stl_items_len + 1);
4280 stl_tabtab = ALLOC_MULT(stl_hlrec_T, stl_items_len + 1);
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004281
4282 stl_separator_locations = ALLOC_MULT(int, stl_items_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004283 }
4284
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004285#ifdef FEAT_EVAL
Luuk van Baal7b224fd2022-11-07 12:16:51 +00004286 // if "fmt" was set insecurely it needs to be evaluated in the sandbox
4287 use_sandbox = was_set_insecurely(opt_name, opt_scope);
4288
4289 // When the format starts with "%!" then evaluate it as an expression and
4290 // use the result as the actual format string.
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004291 if (fmt[0] == '%' && fmt[1] == '!')
4292 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004293 typval_T tv;
4294
4295 tv.v_type = VAR_NUMBER;
4296 tv.vval.v_number = wp->w_id;
4297 set_var((char_u *)"g:statusline_winid", &tv, FALSE);
4298
Bram Moolenaara4e0b972022-10-01 19:43:52 +01004299 usefmt = eval_to_string_safe(fmt + 2, use_sandbox, FALSE, FALSE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004300 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00004301 usefmt = fmt;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004302
4303 do_unlet((char_u *)"g:statusline_winid", TRUE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004304 }
4305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306
4307 if (fillchar == 0)
4308 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004309
Bram Moolenaar10772302019-01-20 18:25:54 +01004310 // The cursor in windows other than the current one isn't always
4311 // up-to-date, esp. because of autocommands and timers.
4312 lnum = wp->w_cursor.lnum;
4313 if (lnum > wp->w_buffer->b_ml.ml_line_count)
4314 {
4315 lnum = wp->w_buffer->b_ml.ml_line_count;
4316 wp->w_cursor.lnum = lnum;
4317 }
4318
4319 // Get line & check if empty (cursorpos will show "0-1"). Note that
4320 // p will become invalid when getting another buffer line.
4321 p = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar567199b2013-04-24 16:52:36 +02004322 empty_line = (*p == NUL);
4323
Bram Moolenaar10772302019-01-20 18:25:54 +01004324 // Get the byte value now, in case we need it below. This is more efficient
4325 // than making a copy of the line.
4326 len = STRLEN(p);
4327 if (wp->w_cursor.col > (colnr_T)len)
4328 {
4329 // Line may have changed since checking the cursor column, or the lnum
4330 // was adjusted above.
4331 wp->w_cursor.col = (colnr_T)len;
Bram Moolenaar10772302019-01-20 18:25:54 +01004332 wp->w_cursor.coladd = 0;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004333 byteval = 0;
Bram Moolenaar10772302019-01-20 18:25:54 +01004334 }
Bram Moolenaar567199b2013-04-24 16:52:36 +02004335 else
Bram Moolenaar567199b2013-04-24 16:52:36 +02004336 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337
4338 groupdepth = 0;
shadmansaleh30e3de22021-05-15 17:23:28 +02004339#ifdef FEAT_EVAL
4340 evaldepth = 0;
4341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 p = out;
4343 curitem = 0;
4344 prevchar_isflag = TRUE;
4345 prevchar_isitem = FALSE;
zeertzjq122dea72022-07-27 15:48:45 +01004346 for (s = usefmt; *s != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004348 if (curitem == (int)stl_items_len)
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004349 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004350 size_t new_len = stl_items_len * 3 / 2;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004351
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004352 stl_item_T *new_items =
4353 vim_realloc(stl_items, sizeof(stl_item_T) * new_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004354 if (new_items == NULL)
4355 break;
4356 stl_items = new_items;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004357
4358 int *new_groupitem =
4359 vim_realloc(stl_groupitem, sizeof(int) * new_len);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004360 if (new_groupitem == NULL)
4361 break;
4362 stl_groupitem = new_groupitem;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004363
4364 stl_hlrec_T *new_hlrec = vim_realloc(stl_hltab,
Brandon Richardsona493b652022-02-19 11:45:03 +00004365 sizeof(stl_hlrec_T) * (new_len + 1));
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004366 if (new_hlrec == NULL)
4367 break;
4368 stl_hltab = new_hlrec;
Brandon Richardsona493b652022-02-19 11:45:03 +00004369 new_hlrec = vim_realloc(stl_tabtab,
4370 sizeof(stl_hlrec_T) * (new_len + 1));
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004371 if (new_hlrec == NULL)
4372 break;
4373 stl_tabtab = new_hlrec;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004374
4375 int *new_separator_locs = vim_realloc(stl_separator_locations,
4376 sizeof(int) * new_len);
4377 if (new_separator_locs == NULL)
4378 break;
4379 stl_separator_locations = new_separator_locs;;
4380
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004381 stl_items_len = new_len;
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004382 }
4383
zeertzjq122dea72022-07-27 15:48:45 +01004384 if (*s != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 prevchar_isflag = prevchar_isitem = FALSE;
4386
4387 /*
4388 * Handle up to the next '%' or the end.
4389 */
4390 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
4391 *p++ = *s++;
4392 if (*s == NUL || p + 1 >= out + outlen)
4393 break;
4394
4395 /*
4396 * Handle one '%' item.
4397 */
4398 s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004399 if (*s == NUL) // ignore trailing %
Bram Moolenaar1d87f512011-02-01 21:55:01 +01004400 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 if (*s == '%')
4402 {
4403 if (p + 1 >= out + outlen)
4404 break;
4405 *p++ = *s++;
4406 prevchar_isflag = prevchar_isitem = FALSE;
4407 continue;
4408 }
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004409 // STL_SEPARATE: Separation between items, filled with white space.
4410 if (*s == STL_SEPARATE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 {
4412 s++;
4413 if (groupdepth > 0)
4414 continue;
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00004415 stl_items[curitem].stl_type = Separate;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004416 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 continue;
4418 }
4419 if (*s == STL_TRUNCMARK)
4420 {
4421 s++;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004422 stl_items[curitem].stl_type = Trunc;
4423 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 continue;
4425 }
4426 if (*s == ')')
4427 {
4428 s++;
4429 if (groupdepth < 1)
4430 continue;
4431 groupdepth--;
4432
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004433 t = stl_items[stl_groupitem[groupdepth]].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 *p = NUL;
4435 l = vim_strsize(t);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004436 if (curitem > stl_groupitem[groupdepth] + 1
4437 && stl_items[stl_groupitem[groupdepth]].stl_minwid == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004439 // remove group if all items are empty and highlight group
4440 // doesn't change
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004441 group_start_userhl = group_end_userhl = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004442 for (n = stl_groupitem[groupdepth] - 1; n >= 0; n--)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004443 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004444 if (stl_items[n].stl_type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004445 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004446 group_start_userhl = group_end_userhl =
4447 stl_items[n].stl_minwid;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004448 break;
4449 }
4450 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004451 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004452 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004453 if (stl_items[n].stl_type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 break;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004455 if (stl_items[n].stl_type == Highlight)
4456 group_end_userhl = stl_items[n].stl_minwid;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004457 }
4458 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 {
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004460 // empty group
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 p = t;
4462 l = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004463 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004464 {
4465 // do not use the highlighting from the removed group
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004466 if (stl_items[n].stl_type == Highlight)
4467 stl_items[n].stl_type = Empty;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004468 // adjust the start position of TabPage to the next
4469 // item position
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004470 if (stl_items[n].stl_type == TabPage)
4471 stl_items[n].stl_start = p;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 }
4474 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004475 if (l > stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004477 // truncate, remove n bytes of text at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 if (has_mbyte)
4479 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004480 // Find the first character that should be included.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 n = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004482 while (l >= stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 {
4484 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004485 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 }
4487 }
4488 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004489 n = (long)(p - t) - stl_items[stl_groupitem[groupdepth]]
4490 .stl_maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491
4492 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004493 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 p = p - n + 1;
Bram Moolenaar13505972019-01-24 15:04:48 +01004495
4496 // Fill up space left over by half a double-wide char.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004497 while (++l < stl_items[stl_groupitem[groupdepth]].stl_minwid)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004498 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499
Bram Moolenaarc667da52019-11-30 20:52:27 +01004500 // correct the start of the items for the truncation
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004501 for (l = stl_groupitem[groupdepth] + 1; l < curitem; l++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 {
LemonBoy57ff5262022-05-09 21:03:47 +01004503 // Minus one for the leading '<' added above.
4504 stl_items[l].stl_start -= n - 1;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004505 if (stl_items[l].stl_start < t)
4506 stl_items[l].stl_start = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 }
4508 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004509 else if (abs(stl_items[stl_groupitem[groupdepth]].stl_minwid) > l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004511 // fill
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004512 n = stl_items[stl_groupitem[groupdepth]].stl_minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 if (n < 0)
4514 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004515 // fill by appending characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 n = 0 - n;
4517 while (l++ < n && p + 1 < out + outlen)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004518 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 }
4520 else
4521 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004522 // fill by inserting characters
Bram Moolenaar008bff92021-03-04 21:55:58 +01004523 l = (n - l) * MB_CHAR2LEN(fillchar);
4524 mch_memmove(t + l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004526 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 p += l;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004528 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
4529 stl_items[n].stl_start += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 for ( ; l > 0; l--)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004531 MB_CHAR2BYTES(fillchar, t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 }
4533 }
4534 continue;
4535 }
4536 minwid = 0;
4537 maxwid = 9999;
4538 zeropad = FALSE;
4539 l = 1;
4540 if (*s == '0')
4541 {
4542 s++;
4543 zeropad = TRUE;
4544 }
4545 if (*s == '-')
4546 {
4547 s++;
4548 l = -1;
4549 }
4550 if (VIM_ISDIGIT(*s))
4551 {
4552 minwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004553 if (minwid < 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 minwid = 0;
4555 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004556 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004558 stl_items[curitem].stl_type = Highlight;
4559 stl_items[curitem].stl_start = p;
4560 stl_items[curitem].stl_minwid = minwid > 9 ? 1 : minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 s++;
4562 curitem++;
4563 continue;
4564 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004565 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4566 {
4567 if (*s == STL_TABCLOSENR)
4568 {
4569 if (minwid == 0)
4570 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004571 // %X ends the close label, go back to the previously
4572 // define tab label nr.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004573 for (n = curitem - 1; n >= 0; --n)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004574 if (stl_items[n].stl_type == TabPage
4575 && stl_items[n].stl_minwid >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004576 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004577 minwid = stl_items[n].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004578 break;
4579 }
4580 }
4581 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01004582 // close nrs are stored as negative values
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004583 minwid = - minwid;
4584 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004585 stl_items[curitem].stl_type = TabPage;
4586 stl_items[curitem].stl_start = p;
4587 stl_items[curitem].stl_minwid = minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004588 s++;
4589 curitem++;
4590 continue;
4591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 if (*s == '.')
4593 {
4594 s++;
4595 if (VIM_ISDIGIT(*s))
4596 {
4597 maxwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004598 if (maxwid <= 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 maxwid = 50;
4600 }
4601 }
4602 minwid = (minwid > 50 ? 50 : minwid) * l;
4603 if (*s == '(')
4604 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004605 stl_groupitem[groupdepth++] = curitem;
4606 stl_items[curitem].stl_type = Group;
4607 stl_items[curitem].stl_start = p;
4608 stl_items[curitem].stl_minwid = minwid;
4609 stl_items[curitem].stl_maxwid = maxwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 s++;
4611 curitem++;
4612 continue;
4613 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004614#ifdef FEAT_EVAL
4615 // Denotes end of expanded %{} block
4616 if (*s == '}' && evaldepth > 0)
4617 {
4618 s++;
4619 evaldepth--;
4620 continue;
4621 }
4622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 if (vim_strchr(STL_ALL, *s) == NULL)
4624 {
Bram Moolenaar7b17eb42023-01-04 14:31:49 +00004625 if (*s == NUL) // can happen with "%0"
4626 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 s++;
4628 continue;
4629 }
4630 opt = *s++;
4631
Bram Moolenaarc667da52019-11-30 20:52:27 +01004632 // OK - now for the real work
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 base = 'D';
4634 itemisflag = FALSE;
4635 fillable = TRUE;
4636 num = -1;
4637 str = NULL;
4638 switch (opt)
4639 {
4640 case STL_FILEPATH:
4641 case STL_FULLPATH:
4642 case STL_FILENAME:
Bram Moolenaarc667da52019-11-30 20:52:27 +01004643 fillable = FALSE; // don't change ' ' to fillchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004645 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 else
4647 {
4648 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004649 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4651 }
4652 trans_characters(NameBuff, MAXPATHL);
4653 if (opt != STL_FILENAME)
4654 str = NameBuff;
4655 else
4656 str = gettail(NameBuff);
4657 break;
4658
Bram Moolenaarc667da52019-11-30 20:52:27 +01004659 case STL_VIM_EXPR: // '{'
shadmansaleh30e3de22021-05-15 17:23:28 +02004660 {
4661#ifdef FEAT_EVAL
4662 char_u *block_start = s - 1;
4663#endif
4664 int reevaluate = (*s == '%');
4665
4666 if (reevaluate)
4667 s++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 itemisflag = TRUE;
4669 t = p;
shadmansaleh30e3de22021-05-15 17:23:28 +02004670 while ((*s != '}' || (reevaluate && s[-1] != '%'))
4671 && *s != NUL && p + 1 < out + outlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 *p++ = *s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004673 if (*s != '}') // missing '}' or out of space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 break;
4675 s++;
shadmansaleh30e3de22021-05-15 17:23:28 +02004676 if (reevaluate)
4677 p[-1] = 0; // remove the % at the end of %{% expr %}
4678 else
4679 *p = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 p = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681#ifdef FEAT_EVAL
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004682 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp),
4683 "%d", curbuf->b_fnum);
4684 set_internal_string_var((char_u *)"g:actual_curbuf", buf_tmp);
4685 vim_snprintf((char *)win_tmp, sizeof(win_tmp), "%d", curwin->w_id);
4686 set_internal_string_var((char_u *)"g:actual_curwin", win_tmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004688 save_curbuf = curbuf;
4689 save_curwin = curwin;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004690 save_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 curwin = wp;
4692 curbuf = wp->w_buffer;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004693 // Visual mode is only valid in the current window.
4694 if (curwin != save_curwin)
4695 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696
Bram Moolenaara4e0b972022-10-01 19:43:52 +01004697 str = eval_to_string_safe(p, use_sandbox, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004699 curwin = save_curwin;
4700 curbuf = save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004701 VIsual_active = save_VIsual_active;
Bram Moolenaar01824652005-01-31 18:58:23 +00004702 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004703 do_unlet((char_u *)"g:actual_curwin", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704
4705 if (str != NULL && *str != 0)
4706 {
4707 if (*skipdigits(str) == NUL)
4708 {
4709 num = atoi((char *)str);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004710 VIM_CLEAR(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 itemisflag = FALSE;
4712 }
4713 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004714
4715 // If the output of the expression needs to be evaluated
4716 // replace the %{} block with the result of evaluation
4717 if (reevaluate && str != NULL && *str != 0
4718 && strchr((const char *)str, '%') != NULL
4719 && evaldepth < MAX_STL_EVAL_DEPTH)
4720 {
4721 size_t parsed_usefmt = (size_t)(block_start - usefmt);
4722 size_t str_length = strlen((const char *)str);
4723 size_t fmt_length = strlen((const char *)s);
4724 size_t new_fmt_len = parsed_usefmt
4725 + str_length + fmt_length + 3;
4726 char_u *new_fmt = (char_u *)alloc(new_fmt_len * sizeof(char_u));
4727 char_u *new_fmt_p = new_fmt;
4728
4729 new_fmt_p = (char_u *)memcpy(new_fmt_p, usefmt, parsed_usefmt)
4730 + parsed_usefmt;
4731 new_fmt_p = (char_u *)memcpy(new_fmt_p , str, str_length)
4732 + str_length;
4733 new_fmt_p = (char_u *)memcpy(new_fmt_p, "%}", 2) + 2;
4734 new_fmt_p = (char_u *)memcpy(new_fmt_p , s, fmt_length)
4735 + fmt_length;
4736 *new_fmt_p = 0;
4737 new_fmt_p = NULL;
4738
4739 if (usefmt != fmt)
4740 vim_free(usefmt);
4741 VIM_CLEAR(str);
4742 usefmt = new_fmt;
4743 s = usefmt + parsed_usefmt;
4744 evaldepth++;
4745 continue;
4746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747#endif
4748 break;
shadmansaleh30e3de22021-05-15 17:23:28 +02004749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 case STL_LINE:
4751 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4752 ? 0L : (long)(wp->w_cursor.lnum);
4753 break;
4754
4755 case STL_NUMLINES:
4756 num = wp->w_buffer->b_ml.ml_line_count;
4757 break;
4758
4759 case STL_COLUMN:
Bram Moolenaar24959102022-05-07 20:01:16 +01004760 num = (State & MODE_INSERT) == 0 && empty_line
4761 ? 0 : (int)wp->w_cursor.col + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 break;
4763
4764 case STL_VIRTCOL:
4765 case STL_VIRTCOL_ALT:
zeertzjq0f112052022-01-14 20:11:38 +00004766 virtcol = wp->w_virtcol + 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004767 // Don't display %V if it's the same as %c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 if (opt == STL_VIRTCOL_ALT
Bram Moolenaar24959102022-05-07 20:01:16 +01004769 && (virtcol == (colnr_T)((State & MODE_INSERT) == 0
4770 && empty_line ? 0 : (int)wp->w_cursor.col + 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 break;
4772 num = (long)virtcol;
4773 break;
4774
4775 case STL_PERCENTAGE:
4776 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4777 (long)wp->w_buffer->b_ml.ml_line_count);
4778 break;
4779
4780 case STL_ALTPERCENT:
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004781 str = buf_tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004782 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 break;
4784
Luuk van Baalba936f62022-12-15 13:15:39 +00004785 case STL_SHOWCMD:
4786 if (p_sc && STRCMP(opt_name, p_sloc) == 0)
4787 str = showcmd_buf;
4788 break;
4789
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 case STL_ARGLISTSTAT:
4791 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004792 buf_tmp[0] = 0;
4793 if (append_arg_number(wp, buf_tmp, (int)sizeof(buf_tmp), FALSE))
4794 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 break;
4796
4797 case STL_KEYMAP:
4798 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004799 if (get_keymap_str(wp, (char_u *)"<%s>", buf_tmp, TMPLEN))
4800 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 break;
4802 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004803#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004804 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805#else
4806 num = 0;
4807#endif
4808 break;
4809
4810 case STL_BUFNO:
4811 num = wp->w_buffer->b_fnum;
4812 break;
4813
4814 case STL_OFFSET_X:
4815 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004816 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 case STL_OFFSET:
4818#ifdef FEAT_BYTEOFF
4819 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
Bram Moolenaar24959102022-05-07 20:01:16 +01004820 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0
4821 ? 0L : l + 1 + ((State & MODE_INSERT) == 0 && empty_line
4822 ? 0 : (int)wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823#endif
4824 break;
4825
4826 case STL_BYTEVAL_X:
4827 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004828 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004830 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 if (num == NL)
4832 num = 0;
4833 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4834 num = NL;
4835 break;
4836
4837 case STL_ROFLAG:
4838 case STL_ROFLAG_ALT:
4839 itemisflag = TRUE;
4840 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004841 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 break;
4843
4844 case STL_HELPFLAG:
4845 case STL_HELPFLAG_ALT:
4846 itemisflag = TRUE;
4847 if (wp->w_buffer->b_help)
4848 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004849 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 break;
4851
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 case STL_FILETYPE:
4853 if (*wp->w_buffer->b_p_ft != NUL
4854 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4855 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004856 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), "[%s]",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004857 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004858 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 }
4860 break;
4861
4862 case STL_FILETYPE_ALT:
4863 itemisflag = TRUE;
4864 if (*wp->w_buffer->b_p_ft != NUL
4865 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4866 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004867 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), ",%s",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004868 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004869 for (t = buf_tmp; *t != 0; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 *t = TOUPPER_LOC(*t);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004871 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 }
4873 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874
Bram Moolenaar4033c552017-09-16 20:54:51 +02004875#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 case STL_PREVIEWFLAG:
4877 case STL_PREVIEWFLAG_ALT:
4878 itemisflag = TRUE;
4879 if (wp->w_p_pvw)
4880 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4881 : _("[Preview]"));
4882 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004883
4884 case STL_QUICKFIX:
4885 if (bt_quickfix(wp->w_buffer))
4886 str = (char_u *)(wp->w_llist_ref
4887 ? _(msg_loclist)
4888 : _(msg_qflist));
4889 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890#endif
4891
4892 case STL_MODIFIED:
4893 case STL_MODIFIED_ALT:
4894 itemisflag = TRUE;
4895 switch ((opt == STL_MODIFIED_ALT)
4896 + bufIsChanged(wp->w_buffer) * 2
4897 + (!wp->w_buffer->b_p_ma) * 4)
4898 {
4899 case 2: str = (char_u *)"[+]"; break;
4900 case 3: str = (char_u *)",+"; break;
4901 case 4: str = (char_u *)"[-]"; break;
4902 case 5: str = (char_u *)",-"; break;
4903 case 6: str = (char_u *)"[+-]"; break;
4904 case 7: str = (char_u *)",+-"; break;
4905 }
4906 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004907
4908 case STL_HIGHLIGHT:
4909 t = s;
4910 while (*s != '#' && *s != NUL)
4911 ++s;
4912 if (*s == '#')
4913 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004914 stl_items[curitem].stl_type = Highlight;
4915 stl_items[curitem].stl_start = p;
4916 stl_items[curitem].stl_minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004917 curitem++;
4918 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004919 if (*s != NUL)
4920 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004921 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 }
4923
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004924 stl_items[curitem].stl_start = p;
4925 stl_items[curitem].stl_type = Normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 if (str != NULL && *str)
4927 {
4928 t = str;
4929 if (itemisflag)
4930 {
4931 if ((t[0] && t[1])
4932 && ((!prevchar_isitem && *t == ',')
4933 || (prevchar_isflag && *t == ' ')))
4934 t++;
4935 prevchar_isflag = TRUE;
4936 }
4937 l = vim_strsize(t);
4938 if (l > 0)
4939 prevchar_isitem = TRUE;
4940 if (l > maxwid)
4941 {
4942 while (l >= maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 if (has_mbyte)
4944 {
4945 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004946 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 }
4948 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 l -= byte2cells(*t++);
4950 if (p + 1 >= out + outlen)
4951 break;
4952 *p++ = '<';
4953 }
4954 if (minwid > 0)
4955 {
4956 for (; l < minwid && p + 1 < out + outlen; l++)
4957 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004958 // Don't put a "-" in front of a digit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4960 *p++ = ' ';
4961 else
Bram Moolenaar008bff92021-03-04 21:55:58 +01004962 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 }
4964 minwid = 0;
4965 }
4966 else
4967 minwid *= -1;
Bram Moolenaar008bff92021-03-04 21:55:58 +01004968 for (; *t && p + 1 < out + outlen; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004970 // Change a space by fillchar, unless fillchar is '-' and a
4971 // digit follows.
Bram Moolenaar008bff92021-03-04 21:55:58 +01004972 if (fillable && *t == ' '
4973 && (!VIM_ISDIGIT(*(t + 1)) || fillchar != '-'))
4974 MB_CHAR2BYTES(fillchar, p);
4975 else
4976 *p++ = *t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 }
4978 for (; l < minwid && p + 1 < out + outlen; l++)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004979 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 }
4981 else if (num >= 0)
4982 {
4983 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4984 char_u nstr[20];
4985
4986 if (p + 20 >= out + outlen)
Bram Moolenaarc667da52019-11-30 20:52:27 +01004987 break; // not sufficient space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 prevchar_isitem = TRUE;
4989 t = nstr;
4990 if (opt == STL_VIRTCOL_ALT)
4991 {
4992 *t++ = '-';
4993 minwid--;
4994 }
4995 *t++ = '%';
4996 if (zeropad)
4997 *t++ = '0';
4998 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004999 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 *t = 0;
5001
5002 for (n = num, l = 1; n >= nbase; n /= nbase)
5003 l++;
5004 if (opt == STL_VIRTCOL_ALT)
5005 l++;
5006 if (l > maxwid)
5007 {
5008 l += 2;
5009 n = l - maxwid;
5010 while (l-- > maxwid)
5011 num /= nbase;
5012 *t++ = '>';
5013 *t++ = '%';
5014 *t = t[-3];
5015 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005016 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
5017 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 }
5019 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005020 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
5021 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 p += STRLEN(p);
5023 }
5024 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005025 stl_items[curitem].stl_type = Empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01005027 if (num >= 0 || (!itemisflag && str != NULL && *str != NUL))
5028 prevchar_isflag = FALSE; // Item not NULL, but not a flag
5029 //
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 if (opt == STL_VIM_EXPR)
5031 vim_free(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 curitem++;
5033 }
5034 *p = NUL;
5035 itemcnt = curitem;
5036
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005037#ifdef FEAT_EVAL
5038 if (usefmt != fmt)
5039 vim_free(usefmt);
5040#endif
5041
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 width = vim_strsize(out);
5043 if (maxwidth > 0 && width > maxwidth)
5044 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005045 // Result is too long, must truncate somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 l = 0;
5047 if (itemcnt == 0)
5048 s = out;
5049 else
5050 {
5051 for ( ; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005052 if (stl_items[l].stl_type == Trunc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005054 // Truncate at %< item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005055 s = stl_items[l].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 break;
5057 }
5058 if (l == itemcnt)
5059 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005060 // No %< item, truncate first item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005061 s = stl_items[0].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 l = 0;
5063 }
5064 }
5065
5066 if (width - vim_strsize(s) >= maxwidth)
5067 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005068 // Truncation mark is beyond max length
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 if (has_mbyte)
5070 {
5071 s = out;
5072 width = 0;
5073 for (;;)
5074 {
5075 width += ptr2cells(s);
5076 if (width >= maxwidth)
5077 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005078 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005080 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 while (++width < maxwidth)
Bram Moolenaar008bff92021-03-04 21:55:58 +01005082 MB_CHAR2BYTES(fillchar, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 }
5084 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 s = out + maxwidth - 1;
5086 for (l = 0; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005087 if (stl_items[l].stl_start > s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 break;
5089 itemcnt = l;
5090 *s++ = '>';
5091 *s = 0;
5092 }
5093 else
5094 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 if (has_mbyte)
5096 {
5097 n = 0;
5098 while (width >= maxwidth)
5099 {
5100 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005101 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 }
5103 }
5104 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 n = width - maxwidth + 1;
5106 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00005107 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 *s = '<';
5109
Bram Moolenaarc667da52019-11-30 20:52:27 +01005110 --n; // count the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 for (; l < itemcnt; l++)
5112 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005113 if (stl_items[l].stl_start - n >= s)
5114 stl_items[l].stl_start -= n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005116 stl_items[l].stl_start = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 }
zeertzjqd392a742023-07-01 20:24:40 +01005118
5119 // Fill up for half a double-wide character.
5120 while (++width < maxwidth)
5121 {
5122 s = s + STRLEN(s);
5123 MB_CHAR2BYTES(fillchar, s);
5124 *s = NUL;
5125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 }
5127 width = maxwidth;
5128 }
5129 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
5130 {
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00005131 // Find how many separators there are, which we will use when
5132 // figuring out how many groups there are.
5133 int num_separators = 0;
5134
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 for (l = 0; l < itemcnt; l++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 {
Yegappan Lakshmanan3ec78f92023-02-11 11:15:25 +00005137 if (stl_items[l].stl_type == Separate)
5138 {
5139 // Create an array of the start location for each separator
5140 // mark.
5141 stl_separator_locations[num_separators] = l;
5142 num_separators++;
5143 }
5144 }
5145
5146 // If we have separated groups, then we deal with it now
5147 if (num_separators)
5148 {
5149 int standard_spaces;
5150 int final_spaces;
5151
5152 standard_spaces = (maxwidth - width) / num_separators;
5153 final_spaces = (maxwidth - width) -
5154 standard_spaces * (num_separators - 1);
5155 for (l = 0; l < num_separators; l++)
5156 {
5157 int dislocation = (l == (num_separators - 1)) ?
5158 final_spaces : standard_spaces;
5159 dislocation *= MB_CHAR2LEN(fillchar);
5160 char_u *start = stl_items[stl_separator_locations[l]].stl_start;
5161 char_u *seploc = start + dislocation;
5162 STRMOVE(seploc, start);
5163 for (s = start; s < seploc;)
5164 MB_CHAR2BYTES(fillchar, s);
5165
5166 for (int i = stl_separator_locations[l] + 1; i < itemcnt; i++)
5167 stl_items[i].stl_start += dislocation;
5168 }
5169
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 width = maxwidth;
5171 }
5172 }
5173
Bram Moolenaarc667da52019-11-30 20:52:27 +01005174 // Store the info about highlighting.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005175 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005177 *hltab = stl_hltab;
5178 sp = stl_hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 for (l = 0; l < itemcnt; l++)
5180 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005181 if (stl_items[l].stl_type == Highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005183 sp->start = stl_items[l].stl_start;
5184 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005185 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 }
5187 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005188 sp->start = NULL;
5189 sp->userhl = 0;
5190 }
5191
Bram Moolenaarc667da52019-11-30 20:52:27 +01005192 // Store the info about tab pages labels.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005193 if (tabtab != NULL)
5194 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005195 *tabtab = stl_tabtab;
5196 sp = stl_tabtab;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005197 for (l = 0; l < itemcnt; l++)
5198 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005199 if (stl_items[l].stl_type == TabPage)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005200 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005201 sp->start = stl_items[l].stl_start;
5202 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005203 sp++;
5204 }
5205 }
5206 sp->start = NULL;
5207 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 }
5209
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01005210 redraw_not_allowed = save_redraw_not_allowed;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02005211
Bram Moolenaar0e1f36f2022-02-15 16:17:44 +00005212 // A user function may reset KeyTyped, restore it.
5213 KeyTyped = save_KeyTyped;
5214
Luuk van Baal7b224fd2022-11-07 12:16:51 +00005215 // Check for an error. If there is one the display will be messed up and
5216 // might loop redrawing. Avoid that by making the corresponding option
5217 // empty.
5218 // TODO: find out why using called_emsg_before makes tests fail, does it
5219 // matter?
5220 // if (called_emsg > called_emsg_before)
5221 if (did_emsg > did_emsg_before)
5222 set_string_option_direct(opt_name, -1, (char_u *)"",
5223 OPT_FREE | opt_scope, SID_ERROR);
5224
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 return width;
5226}
Bram Moolenaarc667da52019-11-30 20:52:27 +01005227#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229/*
Emir SARI971cd2b2023-04-29 12:09:53 +01005230 * Get relative cursor position in window into "buf[buflen]", in the localized
5231 * percentage form like %99, 99%; using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 */
5233 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005234get_rel_pos(
5235 win_T *wp,
5236 char_u *buf,
5237 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005239 long above; // number of lines above window
5240 long below; // number of lines below window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241
Bram Moolenaarc667da52019-11-30 20:52:27 +01005242 if (buflen < 3) // need at least 3 chars for writing
Bram Moolenaar0027c212015-01-07 13:31:52 +01005243 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 above = wp->w_topline - 1;
5245#ifdef FEAT_DIFF
5246 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02005247 if (wp->w_topline == 1 && wp->w_topfill >= 1)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005248 above = 0; // All buffer lines are displayed and there is an
5249 // indication of filler lines, that can be considered
5250 // seeing all lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251#endif
5252 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
5253 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005254 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
Emir SARI971cd2b2023-04-29 12:09:53 +01005255 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005257 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 else
Emir SARI971cd2b2023-04-29 12:09:53 +01005259 {
5260 int perc = (above > 1000000L)
5261 ? (int)(above / ((above + below) / 100L))
5262 : (int)(above * 100L / (above + below));
5263
5264 char *p = (char *)buf;
5265 size_t l = buflen;
5266 if (perc < 10)
5267 {
5268 // prepend one space
5269 buf[0] = ' ';
5270 ++p;
5271 --l;
5272 }
5273 // localized percentage value
5274 vim_snprintf(p, l, _("%d%%"), perc);
5275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277
5278/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005279 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 * Return TRUE if it was appended.
5281 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005282 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005283append_arg_number(
5284 win_T *wp,
5285 char_u *buf,
5286 int buflen,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005287 int add_file) // Add "file" before the arg number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005289 if (ARGCOUNT <= 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 return FALSE;
5291
Bram Moolenaara8490a42023-05-23 18:00:58 +01005292 char *msg;
5293 switch ((wp->w_arg_idx_invalid ? 1 : 0) + (add_file ? 2 : 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 {
Bram Moolenaara8490a42023-05-23 18:00:58 +01005295 case 0: msg = _(" (%d of %d)"); break;
5296 case 1: msg = _(" ((%d) of %d)"); break;
5297 case 2: msg = _(" (file %d of %d)"); break;
5298 case 3: msg = _(" (file (%d) of %d)"); break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 }
Bram Moolenaara8490a42023-05-23 18:00:58 +01005300
5301 char_u *p = buf + STRLEN(buf); // go to the end of the buffer
5302 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)), msg,
5303 wp->w_arg_idx + 1, ARGCOUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 return TRUE;
5305}
5306
5307/*
5308 * If fname is not a full path, make it a full path.
5309 * Returns pointer to allocated memory (NULL for failure).
5310 */
5311 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005312fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313{
5314 /*
5315 * Force expanding the path always for Unix, because symbolic links may
5316 * mess up the full path name, even though it starts with a '/'.
5317 * Also expand when there is ".." in the file name, try to remove it,
5318 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00005319 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 * For MS-Windows also expand names like "longna~1" to "longname".
5321 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00005322#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 return FullName_save(fname, TRUE);
5324#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00005325 if (!vim_isAbsName(fname)
5326 || strstr((char *)fname, "..") != NULL
5327 || strstr((char *)fname, "//") != NULL
5328# ifdef BACKSLASH_IN_FILENAME
5329 || strstr((char *)fname, "\\\\") != NULL
5330# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005331# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00005333# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 )
5335 return FullName_save(fname, FALSE);
5336
5337 fname = vim_strsave(fname);
5338
Bram Moolenaar9b942202007-10-03 12:31:33 +00005339# ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01005340 if (fname != NULL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005341 fname_case(fname, 0); // set correct case for file name
Bram Moolenaar9b942202007-10-03 12:31:33 +00005342# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343
5344 return fname;
5345#endif
5346}
5347
5348/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005349 * Make "*ffname" a full file name, set "*sfname" to "*ffname" if not NULL.
5350 * "*ffname" becomes a pointer to allocated memory (or NULL).
5351 * When resolving a link both "*sfname" and "*ffname" will point to the same
5352 * allocated memory.
5353 * The "*ffname" and "*sfname" pointer values on call will not be freed.
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005354 * Note that the resulting "*ffname" pointer should be considered not allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005357fname_expand(
5358 buf_T *buf UNUSED,
5359 char_u **ffname,
5360 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005362 if (*ffname == NULL) // no file name given, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 return;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005364 if (*sfname == NULL) // no short file name given, use ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 *sfname = *ffname;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005366 *ffname = fix_fname(*ffname); // expand to full path
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367
5368#ifdef FEAT_SHORTCUT
5369 if (!buf->b_p_bin)
5370 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005371 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005373 // If the file name is a shortcut file, use the file it links to.
Bram Moolenaardce1e892019-02-10 23:18:53 +01005374 rfname = mch_resolve_path(*ffname, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005375 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 {
5377 vim_free(*ffname);
5378 *ffname = rfname;
5379 *sfname = rfname;
5380 }
5381 }
5382#endif
5383}
5384
5385/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 * Open a window for a number of buffers.
5387 */
5388 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005389ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390{
5391 buf_T *buf;
5392 win_T *wp, *wpnext;
5393 int split_ret = OK;
5394 int p_ea_save;
5395 int open_wins = 0;
5396 int r;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005397 int count; // Maximum number of windows to open.
5398 int all; // When TRUE also load inactive buffers.
Bram Moolenaare1004402020-10-24 20:49:43 +02005399 int had_tab = cmdmod.cmod_tab;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005400 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401
Bram Moolenaarc667da52019-11-30 20:52:27 +01005402 if (eap->addr_count == 0) // make as many windows as possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 count = 9999;
5404 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01005405 count = eap->line2; // make as many windows as specified
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5407 all = FALSE;
5408 else
5409 all = TRUE;
5410
Pavel Mayorove1121b12023-02-20 14:35:20 +00005411 // Stop Visual mode, the cursor and "VIsual" may very well be invalid after
5412 // switching to another buffer.
5413 reset_VIsual_and_resel();
5414
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 setpcmark();
5416
5417#ifdef FEAT_GUI
5418 need_mouse_correct = TRUE;
5419#endif
5420
5421 /*
5422 * Close superfluous windows (two windows for the same buffer).
5423 * Also close windows that are not full-width.
5424 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005425 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005426 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005427 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005429 tpnext = curtab->tp_next;
5430 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005432 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005433 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005434 || ((cmdmod.cmod_split & WSP_VERT)
5435 ? wp->w_height + wp->w_status_height < Rows - p_ch
5436 - tabline_height()
5437 : wp->w_width != Columns)
5438 || (had_tab > 0 && wp != firstwin))
5439 && !ONE_WINDOW
5440 && !(wp->w_closing || wp->w_buffer->b_locked > 0)
5441 && !win_unlisted(wp))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005442 {
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005443 if (win_close(wp, FALSE) == FAIL)
5444 break;
5445 // Just in case an autocommand does something strange with
5446 // windows: start all over...
5447 wpnext = firstwin;
5448 tpnext = first_tabpage;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005449 open_wins = 0;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005450 }
5451 else
5452 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005454
Bram Moolenaarc667da52019-11-30 20:52:27 +01005455 // Without the ":tab" modifier only do the current tab page.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005456 if (had_tab == 0 || tpnext == NULL)
5457 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005458 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 }
5460
5461 /*
5462 * Go through the buffer list. When a buffer doesn't have a window yet,
5463 * open one. Otherwise move the window to the right position.
5464 * Watch out for autocommands that delete buffers or windows!
5465 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01005466 // Don't execute Win/Buf Enter/Leave autocommands here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 ++autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 win_enter(lastwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5471 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005472 // Check if this buffer needs a window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5474 continue;
5475
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005476 if (had_tab != 0)
5477 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005478 // With the ":tab" modifier don't move the window.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005479 if (buf->b_nwindows > 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005480 wp = lastwin; // buffer has a window, skip it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005481 else
5482 wp = NULL;
5483 }
5484 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005485 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005486 // Check if this buffer already has a window
Bram Moolenaar29323592016-07-24 22:04:11 +02005487 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005488 if (wp->w_buffer == buf)
5489 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005490 // If the buffer already has a window, move it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005491 if (wp != NULL)
5492 win_move_after(wp, curwin);
5493 }
5494
5495 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005497 bufref_T bufref;
5498
5499 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005500
Bram Moolenaarc667da52019-11-30 20:52:27 +01005501 // Split the window and put the buffer in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 p_ea_save = p_ea;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005503 p_ea = TRUE; // use space from all windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5505 ++open_wins;
5506 p_ea = p_ea_save;
5507 if (split_ret == FAIL)
5508 continue;
5509
Bram Moolenaarc667da52019-11-30 20:52:27 +01005510 // Open the buffer in this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005513 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005515 // autocommands deleted the buffer!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 swap_exists_action = SEA_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 break;
5518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 if (swap_exists_action == SEA_QUIT)
5520 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005521#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005522 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005523
Bram Moolenaarc667da52019-11-30 20:52:27 +01005524 // Reset the error/interrupt/exception state here so that
5525 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005526 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005527#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005528
Bram Moolenaarc667da52019-11-30 20:52:27 +01005529 // User selected Quit at ATTENTION prompt; close this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 win_close(curwin, TRUE);
5531 --open_wins;
5532 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005533 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005534
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005535#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005536 // Restore the error/interrupt/exception state if not
5537 // discarded by a new aborting error, interrupt, or uncaught
5538 // exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005539 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005540#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 }
5542 else
5543 handle_swap_exists(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 }
5545
5546 ui_breakcheck();
5547 if (got_int)
5548 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005549 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 break;
5551 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005552#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01005553 // Autocommands deleted the buffer or aborted script processing!!!
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005554 if (aborting())
5555 break;
5556#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01005557 // When ":tab" was used open a new tab for a new window repeatedly.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005558 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
Bram Moolenaare1004402020-10-24 20:49:43 +02005559 cmdmod.cmod_tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 --autocmd_no_enter;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005562 win_enter(firstwin, FALSE); // back to first window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564
5565 /*
5566 * Close superfluous windows.
5567 */
5568 for (wp = lastwin; open_wins > count; )
5569 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005570 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 || autowrite(wp->w_buffer, FALSE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 if (!win_valid(wp))
5573 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005574 // BufWrite Autocommands made the window invalid, start over
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 wp = lastwin;
5576 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005577 else if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005579 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 --open_wins;
5581 wp = lastwin;
5582 }
5583 else
5584 {
5585 wp = wp->w_prev;
5586 if (wp == NULL)
5587 break;
5588 }
5589 }
5590}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005593static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005594
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595/*
5596 * do_modelines() - process mode lines for the current file
5597 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005598 * "flags" can be:
5599 * OPT_WINONLY only set options local to window
5600 * OPT_NOWIN don't set options local to window
5601 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 * Returns immediately if the "ml" option isn't set.
5603 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005605do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005607 linenr_T lnum;
5608 int nmlines;
5609 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610
5611 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5612 return;
5613
Bram Moolenaarc667da52019-11-30 20:52:27 +01005614 // Disallow recursive entry here. Can happen when executing a modeline
5615 // triggers an autocommand, which reloads modelines with a ":do".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 if (entered)
5617 return;
5618
5619 ++entered;
Hu Jialun9dcd3492021-08-28 20:42:50 +02005620 for (lnum = 1; curbuf->b_p_ml && lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005622 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 nmlines = 0;
5624
Hu Jialun9dcd3492021-08-28 20:42:50 +02005625 for (lnum = curbuf->b_ml.ml_line_count; curbuf->b_p_ml && lnum > 0 && lnum > nmlines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005627 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 nmlines = 0;
5629 --entered;
5630}
5631
Bram Moolenaarc667da52019-11-30 20:52:27 +01005632#include "version.h" // for version number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633
5634/*
5635 * chk_modeline() - check a single line for a mode string
5636 * Return FAIL if an error encountered.
5637 */
5638 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005639chk_modeline(
5640 linenr_T lnum,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005641 int flags) // Same as for do_modelines().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642{
5643 char_u *s;
5644 char_u *e;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005645 char_u *linecopy; // local copy of any modeline found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 int prev;
5647 int vers;
5648 int end;
5649 int retval = OK;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005650 sctx_T save_current_sctx;
ichizok7e5fe382023-04-15 13:17:50 +01005651 ESTACK_CHECK_DECLARATION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652
5653 prev = -1;
5654 for (s = ml_get(lnum); *s != NUL; ++s)
5655 {
5656 if (prev == -1 || vim_isspace(prev))
5657 {
5658 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5659 || STRNCMP(s, "vi:", (size_t)3) == 0)
5660 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005661 // Accept both "vim" and "Vim".
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005662 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 {
5664 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5665 e = s + 4;
5666 else
5667 e = s + 3;
5668 vers = getdigits(&e);
5669 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005670 && (s[0] != 'V'
5671 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 && (s[3] == ':'
Keith Thompson184f71c2024-01-04 21:19:04 +01005673 || (VIM_VERSION_100 >= vers && SAFE_isdigit(s[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 || (VIM_VERSION_100 < vers && s[3] == '<')
5675 || (VIM_VERSION_100 > vers && s[3] == '>')
5676 || (VIM_VERSION_100 == vers && s[3] == '=')))
5677 break;
5678 }
5679 }
5680 prev = *s;
5681 }
5682
5683 if (*s)
5684 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005685 do // skip over "ex:", "vi:" or "vim:"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 ++s;
5687 while (s[-1] != ':');
5688
Bram Moolenaarc667da52019-11-30 20:52:27 +01005689 s = linecopy = vim_strsave(s); // copy the line, it will change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 if (linecopy == NULL)
5691 return FAIL;
5692
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005693 // prepare for emsg()
5694 estack_push(ETYPE_MODELINE, (char_u *)"modelines", lnum);
ichizok7e5fe382023-04-15 13:17:50 +01005695 ESTACK_CHECK_SETUP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696
5697 end = FALSE;
5698 while (end == FALSE)
5699 {
5700 s = skipwhite(s);
5701 if (*s == NUL)
5702 break;
5703
5704 /*
5705 * Find end of set command: ':' or end of line.
5706 * Skip over "\:", replacing it with ":".
5707 */
5708 for (e = s; *e != ':' && *e != NUL; ++e)
5709 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005710 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 if (*e == NUL)
5712 end = TRUE;
5713
5714 /*
5715 * If there is a "set" command, require a terminating ':' and
5716 * ignore the stuff after the ':'.
5717 * "vi:set opt opt opt: foo" -- foo not interpreted
5718 * "vi:opt opt opt: foo" -- foo interpreted
5719 * Accept "se" for compatibility with Elvis.
5720 */
5721 if (STRNCMP(s, "set ", (size_t)4) == 0
5722 || STRNCMP(s, "se ", (size_t)3) == 0)
5723 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005724 if (*e != ':') // no terminating ':'?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 break;
5726 end = TRUE;
5727 s = vim_strchr(s, ' ') + 1;
5728 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005729 *e = NUL; // truncate the set command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730
Bram Moolenaarc667da52019-11-30 20:52:27 +01005731 if (*s != NUL) // skip over an empty "::"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 {
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005733 int secure_save = secure;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005734
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005735 save_current_sctx = current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005736 current_sctx.sc_version = 1;
5737#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005738 current_sctx.sc_sid = SID_MODELINE;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005739 current_sctx.sc_seq = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005740 current_sctx.sc_lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005742
Bram Moolenaar5958f952018-11-20 04:25:21 +01005743 // Make sure no risky things are executed as a side effect.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01005744 secure = 1;
Bram Moolenaar5958f952018-11-20 04:25:21 +01005745
Bram Moolenaara3227e22006-03-08 21:32:40 +00005746 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar5958f952018-11-20 04:25:21 +01005747
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005748 secure = secure_save;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005749 current_sctx = save_current_sctx;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005750 if (retval == FAIL) // stop if error found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 break;
5752 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005753 s = e + 1; // advance to next part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 }
5755
ichizok7e5fe382023-04-15 13:17:50 +01005756 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005757 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 vim_free(linecopy);
5759 }
5760 return retval;
5761}
5762
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005763/*
Bram Moolenaar91335e52018-08-01 17:53:12 +02005764 * Return TRUE if "buf" is a normal buffer, 'buftype' is empty.
5765 */
5766 int
5767bt_normal(buf_T *buf)
5768{
5769 return buf != NULL && buf->b_p_bt[0] == NUL;
5770}
5771
5772/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005773 * Return TRUE if "buf" is the quickfix buffer.
5774 */
5775 int
Bram Moolenaar340dafd2022-08-25 16:16:45 +01005776bt_quickfix(buf_T *buf UNUSED)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005777{
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005778#ifdef FEAT_QUICKFIX
Christian Brabandt6e60cf42023-09-03 21:43:46 +02005779 return buf != NULL && buf_valid(buf) && buf->b_p_bt[0] == 'q';
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005780#else
5781 return FALSE;
Bram Moolenaar113e1072019-01-20 15:30:40 +01005782#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005783}
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005784
5785/*
5786 * Return TRUE if "buf" is a terminal buffer.
5787 */
5788 int
Bram Moolenaar340dafd2022-08-25 16:16:45 +01005789bt_terminal(buf_T *buf UNUSED)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005790{
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005791#if defined(FEAT_TERMINAL)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005792 return buf != NULL && buf->b_p_bt[0] == 't';
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005793#else
5794 return FALSE;
Bram Moolenaar113e1072019-01-20 15:30:40 +01005795#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005796}
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005797
5798/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005799 * Return TRUE if "buf" is a help buffer.
5800 */
5801 int
5802bt_help(buf_T *buf)
5803{
5804 return buf != NULL && buf->b_help;
5805}
5806
5807/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02005808 * Return TRUE if "buf" is a prompt buffer.
5809 */
5810 int
5811bt_prompt(buf_T *buf)
5812{
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005813 return buf != NULL && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'r';
5814}
5815
Dominique Pelle748b3082022-01-08 12:41:16 +00005816#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005817/*
5818 * Return TRUE if "buf" is a buffer for a popup window.
5819 */
5820 int
5821bt_popup(buf_T *buf)
5822{
5823 return buf != NULL && buf->b_p_bt != NULL
5824 && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'o';
Bram Moolenaarf2732452018-06-03 14:47:35 +02005825}
Dominique Pelle748b3082022-01-08 12:41:16 +00005826#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005827
5828/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005829 * Return TRUE if "buf" is a "nofile", "acwrite", "terminal" or "prompt"
Bram Moolenaarc3126192022-08-26 12:58:17 +01005830 * buffer. This means the buffer name may not be a file name, at least not for
5831 * writing the buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005832 */
5833 int
Bram Moolenaar26910de2019-06-15 19:37:15 +02005834bt_nofilename(buf_T *buf)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005835{
5836 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5837 || buf->b_p_bt[0] == 'a'
Bram Moolenaarf2732452018-06-03 14:47:35 +02005838 || buf->b_p_bt[0] == 't'
5839 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005840}
5841
Bram Moolenaarc3126192022-08-26 12:58:17 +01005842/*
5843 * Return TRUE if "buf" is a "nofile", "quickfix", "terminal" or "prompt"
5844 * buffer. This means the buffer is not to be read from a file.
5845 */
5846 static int
5847bt_nofileread(buf_T *buf)
5848{
5849 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5850 || buf->b_p_bt[0] == 't'
5851 || buf->b_p_bt[0] == 'q'
5852 || buf->b_p_bt[0] == 'p');
5853}
5854
Dominique Pelle748b3082022-01-08 12:41:16 +00005855#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005856/*
Bram Moolenaar26910de2019-06-15 19:37:15 +02005857 * Return TRUE if "buf" has 'buftype' set to "nofile".
5858 */
5859 int
5860bt_nofile(buf_T *buf)
5861{
5862 return buf != NULL && buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f';
5863}
Dominique Pelle748b3082022-01-08 12:41:16 +00005864#endif
Bram Moolenaar26910de2019-06-15 19:37:15 +02005865
5866/*
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01005867 * Return TRUE if "buf" is a "nowrite", "nofile", "terminal", "prompt", or
5868 * "popup" buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005869 */
5870 int
5871bt_dontwrite(buf_T *buf)
5872{
Bram Moolenaarf2732452018-06-03 14:47:35 +02005873 return buf != NULL && (buf->b_p_bt[0] == 'n'
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005874 || buf->b_p_bt[0] == 't'
5875 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005876}
5877
5878 int
5879bt_dontwrite_msg(buf_T *buf)
5880{
5881 if (bt_dontwrite(buf))
5882 {
Bram Moolenaarf1474d82021-12-31 19:59:55 +00005883 emsg(_(e_cannot_write_buftype_option_is_set));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005884 return TRUE;
5885 }
5886 return FALSE;
5887}
5888
5889/*
5890 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
5891 * and 'bufhidden'.
5892 */
5893 int
5894buf_hide(buf_T *buf)
5895{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005896 // 'bufhidden' overrules 'hidden' and ":hide", check it first
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005897 switch (buf->b_p_bh[0])
5898 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005899 case 'u': // "unload"
5900 case 'w': // "wipe"
5901 case 'd': return FALSE; // "delete"
5902 case 'h': return TRUE; // "hide"
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005903 }
Bram Moolenaare1004402020-10-24 20:49:43 +02005904 return (p_hid || (cmdmod.cmod_flags & CMOD_HIDE));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005905}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906
5907/*
5908 * Return special buffer name.
5909 * Returns NULL when the buffer has a normal file name.
5910 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005911 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005912buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913{
Bram Moolenaar4033c552017-09-16 20:54:51 +02005914#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005916 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005917 /*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005918 * Differentiate between the quickfix and location list buffers using
5919 * the buffer number stored in the global quickfix stack.
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005920 */
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005921 if (buf->b_fnum == qf_stack_get_bufnr())
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005922 return (char_u *)_(msg_qflist);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005923 else
5924 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02005927
Bram Moolenaarc667da52019-11-30 20:52:27 +01005928 // There is no _file_ when 'buftype' is "nofile", b_sfname
5929 // contains the name as specified by the user.
Bram Moolenaar26910de2019-06-15 19:37:15 +02005930 if (bt_nofilename(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 {
Bram Moolenaar21554412017-07-24 21:44:43 +02005932#ifdef FEAT_TERMINAL
5933 if (buf->b_term != NULL)
5934 return term_get_status_text(buf->b_term);
5935#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02005936 if (buf->b_fname != NULL)
5937 return buf->b_fname;
Sean Dewar1fb41032023-08-16 17:15:05 +01005938 if (buf == cmdwin_buf)
5939 return (char_u *)_("[Command Line]");
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02005940#ifdef FEAT_JOB_CHANNEL
5941 if (bt_prompt(buf))
5942 return (char_u *)_("[Prompt]");
5943#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005944#ifdef FEAT_PROP_POPUP
Bram Moolenaarc6896e22019-05-30 22:32:34 +02005945 if (bt_popup(buf))
5946 return (char_u *)_("[Popup]");
5947#endif
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005948 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 }
Bram Moolenaar21554412017-07-24 21:44:43 +02005950
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 if (buf->b_fname == NULL)
Bram Moolenaar00806bc2020-11-05 19:36:38 +01005952 return buf_get_fname(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 return NULL;
5954}
5955
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956/*
Bram Moolenaar00806bc2020-11-05 19:36:38 +01005957 * Get "buf->b_fname", use "[No Name]" if it is NULL.
5958 */
5959 char_u *
5960buf_get_fname(buf_T *buf)
5961{
5962 if (buf->b_fname == NULL)
5963 return (char_u *)_("[No Name]");
5964 return buf->b_fname;
5965}
5966
5967/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5969 */
5970 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005971set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972{
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005973 if (on == curbuf->b_p_bl)
5974 return;
5975
5976 curbuf->b_p_bl = on;
5977 if (on)
5978 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5979 else
5980 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981}
5982
5983/*
5984 * Read the file for "buf" again and check if the contents changed.
5985 * Return TRUE if it changed or this could not be checked.
5986 */
5987 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005988buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989{
5990 buf_T *newbuf;
5991 int differ = TRUE;
5992 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 exarg_T ea;
5995
Bram Moolenaarc667da52019-11-30 20:52:27 +01005996 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5998 if (newbuf == NULL)
5999 return TRUE;
6000
Bram Moolenaarc667da52019-11-30 20:52:27 +01006001 // Force the 'fileencoding' and 'fileformat' to be equal.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 if (prep_exarg(&ea, buf) == FAIL)
6003 {
6004 wipe_buffer(newbuf, FALSE);
6005 return TRUE;
6006 }
6007
Bram Moolenaare76062c2022-11-28 18:51:43 +00006008 // Set curwin/curbuf to buf and save a few things.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00006010 if (curbuf != newbuf)
6011 {
6012 // Failed to find a window for "newbuf".
6013 wipe_buffer(newbuf, FALSE);
6014 return TRUE;
6015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016
Christian Brabandt41e6f7d2023-10-11 21:08:13 +02006017 // We don't want to trigger autocommands now, they may have nasty
6018 // side-effects like wiping buffers
6019 block_autocmds();
Bram Moolenaar4770d092006-01-12 23:22:24 +00006020 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 && readfile(buf->b_ffname, buf->b_fname,
6022 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
6023 &ea, READ_NEW | READ_DUMMY) == OK)
6024 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01006025 // compare the two files line by line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
6027 {
6028 differ = FALSE;
6029 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6030 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
6031 {
6032 differ = TRUE;
6033 break;
6034 }
6035 }
6036 }
6037 vim_free(ea.cmd);
6038
Bram Moolenaarc667da52019-11-30 20:52:27 +01006039 // restore curwin/curbuf and a few other things
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041
Bram Moolenaarc667da52019-11-30 20:52:27 +01006042 if (curbuf != newbuf) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 wipe_buffer(newbuf, FALSE);
6044
Christian Brabandt41e6f7d2023-10-11 21:08:13 +02006045 unblock_autocmds();
6046
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 return differ;
6048}
6049
6050/*
6051 * Wipe out a buffer and decrement the last buffer number if it was used for
6052 * this buffer. Call this to wipe out a temp buffer that does not contain any
6053 * marks.
6054 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006056wipe_buffer(
6057 buf_T *buf,
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006058 int aucmd) // When TRUE trigger autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059{
6060 if (buf->b_fnum == top_file_num - 1)
6061 --top_file_num;
6062
Bram Moolenaarc667da52019-11-30 20:52:27 +01006063 if (!aucmd) // Don't trigger BufDelete autocommands here.
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006064 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006065
Bram Moolenaara6e8f882019-12-14 16:18:15 +01006066 close_buffer(NULL, buf, DOBUF_WIPE, FALSE, TRUE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006067
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006069 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070}