blob: 0714f62f817ec88444f769e1bdde27c9f0e95fac [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{
149 if (buf->b_ml.ml_mfp == NULL)
150 {
151 aco_save_T aco;
152
153 aucmd_prepbuf(&aco, buf);
Bram Moolenaar188639d2022-04-04 16:57:21 +0100154 if (swap_exists_action != SEA_READONLY)
155 swap_exists_action = SEA_NONE;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200156 open_buffer(FALSE, NULL, 0);
157 aucmd_restbuf(&aco);
158 }
159}
Dominique Pelle748b3082022-01-08 12:41:16 +0000160#endif
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200161
162/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200163 * Open current buffer, that is: open the memfile and read the file into
164 * memory.
165 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166 */
167 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100168open_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +0100169 int read_stdin, // read file from stdin
170 exarg_T *eap, // for forced 'ff' and 'fenc' or NULL
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100171 int flags_arg) // extra flags for readfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172{
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100173 int flags = flags_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174 int retval = OK;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200175 bufref_T old_curbuf;
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100176#ifdef FEAT_SYN_HL
177 long old_tw = curbuf->b_p_tw;
178#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200179 int read_fifo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100181 // The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
182 // When re-entering the same buffer, it should not change, because the
183 // user may have reset the flag by hand.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 if (readonlymode && curbuf->b_ffname != NULL
185 && (curbuf->b_flags & BF_NEVERLOADED))
186 curbuf->b_p_ro = TRUE;
187
Bram Moolenaar4770d092006-01-12 23:22:24 +0000188 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100190 // There MUST be a memfile, otherwise we can't do anything
191 // If we can't create one for the current buffer, take another buffer
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100192 close_buffer(NULL, curbuf, 0, FALSE, FALSE);
Bram Moolenaar29323592016-07-24 22:04:11 +0200193 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 if (curbuf->b_ml.ml_mfp != NULL)
195 break;
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100196 // If there is no memfile at all, exit.
197 // This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 if (curbuf == NULL)
199 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +0000200 emsg(_(e_cannot_allocate_any_buffer_exiting));
Bram Moolenaar6f10c702019-08-20 22:58:37 +0200201
202 // Don't try to do any saving, with "curbuf" NULL almost nothing
203 // will work.
204 v_dying = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205 getout(2);
206 }
Bram Moolenaar6f10c702019-08-20 22:58:37 +0200207
Bram Moolenaar40bcec12021-12-05 22:19:27 +0000208 emsg(_(e_cannot_allocate_buffer_using_other_one));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 enter_buffer(curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100210#ifdef FEAT_SYN_HL
211 if (old_tw != curbuf->b_p_tw)
212 check_colorcolumn(curwin);
213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 return FAIL;
215 }
216
Bram Moolenaarc667da52019-11-30 20:52:27 +0100217 // The autocommands in readfile() may change the buffer, but only AFTER
218 // reading the file.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200219 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220 modified_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221
Bram Moolenaarc667da52019-11-30 20:52:27 +0100222 // mark cursor position as being invalid
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100223 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100225 // A buffer without an actual file should not use the buffer name to read a
226 // file.
Bram Moolenaarc3126192022-08-26 12:58:17 +0100227 if (bt_nofileread(curbuf))
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100228 flags |= READ_NOFILE;
229
Bram Moolenaar2eddbac2022-08-25 12:45:21 +0100230 // Read the file if there is one.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 if (curbuf->b_ffname != NULL
232#ifdef FEAT_NETBEANS_INTG
233 && netbeansReadFile
234#endif
235 )
236 {
Bram Moolenaar426dd022016-03-15 15:09:29 +0100237 int old_msg_silent = msg_silent;
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200238#ifdef UNIX
239 int save_bin = curbuf->b_p_bin;
240 int perm;
241#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242#ifdef FEAT_NETBEANS_INTG
243 int oldFire = netbeansFireChanges;
244
245 netbeansFireChanges = 0;
246#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200247#ifdef UNIX
248 perm = mch_getperm(curbuf->b_ffname);
Bram Moolenaard569bb02018-08-11 13:57:20 +0200249 if (perm >= 0 && (S_ISFIFO(perm)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200250 || S_ISSOCK(perm)
Bram Moolenaarf04507d2016-08-20 15:05:39 +0200251# ifdef OPEN_CHR_FILES
252 || (S_ISCHR(perm) && is_dev_fd_file(curbuf->b_ffname))
253# endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200254 ))
255 read_fifo = TRUE;
256 if (read_fifo)
257 curbuf->b_p_bin = TRUE;
258#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100259 if (shortmess(SHM_FILEINFO))
260 msg_silent = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200262 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200263 flags | READ_NEW | (read_fifo ? READ_FIFO : 0));
264#ifdef UNIX
265 if (read_fifo)
266 {
267 curbuf->b_p_bin = save_bin;
268 if (retval == OK)
269 retval = read_buffer(FALSE, eap, flags);
270 }
271#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100272 msg_silent = old_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273#ifdef FEAT_NETBEANS_INTG
274 netbeansFireChanges = oldFire;
275#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100276 // Help buffer is filtered.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +0200277 if (bt_help(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278 fix_help_buffer();
279 }
280 else if (read_stdin)
281 {
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200282 int save_bin = curbuf->b_p_bin;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100284 // First read the text in binary mode into the buffer.
285 // Then read from that same buffer and append at the end. This makes
286 // it possible to retry when 'fileformat' or 'fileencoding' was
287 // guessed wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 curbuf->b_p_bin = TRUE;
289 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200290 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
291 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292 curbuf->b_p_bin = save_bin;
293 if (retval == OK)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200294 retval = read_buffer(TRUE, eap, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295 }
296
Bram Moolenaarc667da52019-11-30 20:52:27 +0100297 // if first time loading this buffer, init b_chartab[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 if (curbuf->b_flags & BF_NEVERLOADED)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100299 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 (void)buf_init_chartab(curbuf, FALSE);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100301 parse_cino(curbuf);
302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100304 // Set/reset the Changed flag first, autocmds may change the buffer.
305 // Apply the automatic commands, before processing the modelines.
306 // So the modelines have priority over autocommands.
307 //
Bram Moolenaarc667da52019-11-30 20:52:27 +0100308 // When reading stdin, the buffer contents always needs writing, so set
309 // the changed flag. Unless in readonly mode: "ls | gview -".
310 // When interrupted and 'cpoptions' contains 'i' set changed flag.
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000311 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100312 || modified_was_set // ":set modified" used in autocmd
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100313#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000316 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100318 else if (retval == OK && !read_stdin && !read_fifo)
Bram Moolenaarc024b462019-06-08 18:07:21 +0200319 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaarc667da52019-11-30 20:52:27 +0100320 save_file_ff(curbuf); // keep this fileformat
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321
Bram Moolenaarc667da52019-11-30 20:52:27 +0100322 // Set last_changedtick to avoid triggering a TextChanged autocommand right
323 // after it was added.
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100324 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Christian Brabandtdb3b4462021-10-16 11:58:55 +0100325 curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100326 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100327
Bram Moolenaarc667da52019-11-30 20:52:27 +0100328 // require "!" to overwrite the file, because it wasn't read completely
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329#ifdef FEAT_EVAL
330 if (aborting())
331#else
332 if (got_int)
333#endif
334 curbuf->b_flags |= BF_READERR;
335
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000336#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +0100337 // Need to update automatic folding. Do this before the autocommands,
338 // they may use the fold info.
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000339 foldUpdateAll(curwin);
340#endif
341
Bram Moolenaarc667da52019-11-30 20:52:27 +0100342 // need to set w_topline, unless some autocommand already did that.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 if (!(curwin->w_valid & VALID_TOPLINE))
344 {
345 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100346#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100350#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100352#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354#endif
355
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100356 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100358 // The autocommands may have changed the current buffer. Apply the
359 // modelines to the correct buffer, if it still exists and is loaded.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200360 if (bufref_valid(&old_curbuf) && old_curbuf.br_buf->b_ml.ml_mfp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 {
362 aco_save_T aco;
363
Bram Moolenaarc667da52019-11-30 20:52:27 +0100364 // Go to the buffer that was opened.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200365 aucmd_prepbuf(&aco, old_curbuf.br_buf);
Bram Moolenaara3227e22006-03-08 21:32:40 +0000366 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
368
Bram Moolenaar1d30fde2021-10-20 21:58:42 +0100369 if ((flags & READ_NOWINENTER) == 0)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100370#ifdef FEAT_EVAL
Bram Moolenaar1d30fde2021-10-20 21:58:42 +0100371 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE,
372 curbuf, &retval);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100373#else
Bram Moolenaar1d30fde2021-10-20 21:58:42 +0100374 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100375#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376
Bram Moolenaarc667da52019-11-30 20:52:27 +0100377 // restore curwin/curbuf and a few other things
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 aucmd_restbuf(&aco);
379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 }
381
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 return retval;
383}
384
385/*
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200386 * Store "buf" in "bufref" and set the free count.
387 */
388 void
389set_bufref(bufref_T *bufref, buf_T *buf)
390{
391 bufref->br_buf = buf;
Bram Moolenaarfadacf02017-06-19 20:35:32 +0200392 bufref->br_fnum = buf == NULL ? 0 : buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200393 bufref->br_buf_free_count = buf_free_count;
394}
395
396/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200397 * Return TRUE if "bufref->br_buf" points to the same buffer as when
398 * set_bufref() was called and it is a valid buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200399 * Only goes through the buffer list if buf_free_count changed.
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200400 * Also checks if b_fnum is still the same, a :bwipe followed by :new might get
401 * the same allocated memory, but it's a different buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200402 */
403 int
404bufref_valid(bufref_T *bufref)
405{
406 return bufref->br_buf_free_count == buf_free_count
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200407 ? TRUE : buf_valid(bufref->br_buf)
408 && bufref->br_fnum == bufref->br_buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200409}
410
411/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200413 * This can be slow if there are many buffers, prefer using bufref_valid().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 */
415 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100416buf_valid(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417{
418 buf_T *bp;
419
Bram Moolenaarc667da52019-11-30 20:52:27 +0100420 // Assume that we more often have a recent buffer, start with the last
421 // one.
Bram Moolenaar00d253e2020-04-06 22:13:01 +0200422 FOR_ALL_BUFS_FROM_LAST(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 if (bp == buf)
424 return TRUE;
425 return FALSE;
426}
427
428/*
Bram Moolenaar480778b2016-07-14 22:09:39 +0200429 * A hash table used to quickly lookup a buffer by its number.
430 */
431static hashtab_T buf_hashtab;
432
433 static void
434buf_hashtab_add(buf_T *buf)
435{
436 sprintf((char *)buf->b_key, "%x", buf->b_fnum);
437 if (hash_add(&buf_hashtab, buf->b_key) == FAIL)
Bram Moolenaarf1474d82021-12-31 19:59:55 +0000438 emsg(_(e_buffer_cannot_be_registered));
Bram Moolenaar480778b2016-07-14 22:09:39 +0200439}
440
441 static void
442buf_hashtab_remove(buf_T *buf)
443{
444 hashitem_T *hi = hash_find(&buf_hashtab, buf->b_key);
445
446 if (!HASHITEM_EMPTY(hi))
447 hash_remove(&buf_hashtab, hi);
448}
449
Bram Moolenaar94f01952018-09-01 15:30:03 +0200450/*
451 * Return TRUE when buffer "buf" can be unloaded.
452 * Give an error message and return FALSE when the buffer is locked or the
453 * screen is being redrawn and the buffer is in a window.
454 */
455 static int
456can_unload_buffer(buf_T *buf)
457{
458 int can_unload = !buf->b_locked;
459
460 if (can_unload && updating_screen)
461 {
462 win_T *wp;
463
464 FOR_ALL_WINDOWS(wp)
465 if (wp->w_buffer == buf)
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200466 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200467 can_unload = FALSE;
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200468 break;
469 }
Bram Moolenaar94f01952018-09-01 15:30:03 +0200470 }
471 if (!can_unload)
Bram Moolenaaref976322022-09-28 11:48:30 +0100472 {
473 char_u *fname = buf->b_fname != NULL ? buf->b_fname : buf->b_ffname;
474
475 semsg(_(e_attempt_to_delete_buffer_that_is_in_use_str),
476 fname != NULL ? fname : (char_u *)"[No Name]");
477 }
Bram Moolenaar94f01952018-09-01 15:30:03 +0200478 return can_unload;
479}
Bram Moolenaara997b452018-04-17 23:24:06 +0200480
Bram Moolenaar480778b2016-07-14 22:09:39 +0200481/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 * Close the link to a buffer.
483 * "action" is used when there is no longer a window for the buffer.
484 * It can be:
485 * 0 buffer becomes hidden
486 * DOBUF_UNLOAD buffer is unloaded
487 * DOBUF_DELETE buffer is unloaded and removed from buffer list
488 * DOBUF_WIPE buffer is unloaded and really deleted
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200489 * DOBUF_WIPE_REUSE idem, and add to buf_reuse list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 * When doing all but the first one on the current buffer, the caller should
491 * get a new buffer very soon!
492 *
493 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100494 *
495 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
496 * cause there to be only one window with this buffer. e.g. when ":quit" is
497 * supposed to close the window but autocommands close all other windows.
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100498 *
499 * When "ignore_abort" is TRUE don't abort even when aborting() returns TRUE.
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100500 *
501 * Return TRUE when we got to the end and b_nwindows was decremented.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 */
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100503 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100504close_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +0100505 win_T *win, // if not NULL, set b_last_cursor
Bram Moolenaar7454a062016-01-30 15:14:10 +0100506 buf_T *buf,
507 int action,
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100508 int abort_if_last,
509 int ignore_abort)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100512 int nwindows;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200513 bufref_T bufref;
Bram Moolenaar3a117e12016-10-30 21:57:52 +0100514 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200515 win_T *the_curwin = curwin;
516 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 int unload_buf = (action != 0);
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200518 int wipe_buf = (action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
519 int del_buf = (action == DOBUF_DEL || wipe_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520
Bram Moolenaarcee52202020-03-11 14:19:58 +0100521 CHECK_CURBUF;
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100522
523 // Force unloading or deleting when 'bufhidden' says so.
524 // The caller must take care of NOT deleting/freeing when 'bufhidden' is
525 // "hide" (otherwise we could never free or delete a buffer).
Bram Moolenaarc667da52019-11-30 20:52:27 +0100526 if (buf->b_p_bh[0] == 'd') // 'bufhidden' == "delete"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200527 {
528 del_buf = TRUE;
529 unload_buf = TRUE;
530 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100531 else if (buf->b_p_bh[0] == 'w') // 'bufhidden' == "wipe"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200532 {
533 del_buf = TRUE;
534 unload_buf = TRUE;
535 wipe_buf = TRUE;
536 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100537 else if (buf->b_p_bh[0] == 'u') // 'bufhidden' == "unload"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200538 unload_buf = TRUE;
539
Bram Moolenaar94053a52017-08-01 21:44:33 +0200540#ifdef FEAT_TERMINAL
Yee Cheng Chin42826332022-10-10 11:46:16 +0100541 // depending on how we get here b_nwindows may already be zero
542 if (bt_terminal(buf) && (buf->b_nwindows <= 1 || del_buf))
Bram Moolenaar94053a52017-08-01 21:44:33 +0200543 {
Bram Moolenaarcee52202020-03-11 14:19:58 +0100544 CHECK_CURBUF;
Bram Moolenaar94053a52017-08-01 21:44:33 +0200545 if (term_job_running(buf->b_term))
546 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200547 if (wipe_buf || unload_buf)
Bram Moolenaara997b452018-04-17 23:24:06 +0200548 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200549 if (!can_unload_buffer(buf))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100550 return FALSE;
Bram Moolenaar94f01952018-09-01 15:30:03 +0200551
Bram Moolenaarc667da52019-11-30 20:52:27 +0100552 // Wiping out or unloading a terminal buffer kills the job.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200553 free_terminal(buf);
Yee Cheng Chin42826332022-10-10 11:46:16 +0100554
555 // A terminal buffer is wiped out when job has finished.
556 del_buf = TRUE;
557 unload_buf = TRUE;
558 wipe_buf = TRUE;
Bram Moolenaara997b452018-04-17 23:24:06 +0200559 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200560 else
561 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100562 // The job keeps running, hide the buffer.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200563 del_buf = FALSE;
564 unload_buf = FALSE;
565 }
566 }
Bram Moolenaarc9f8b842020-11-24 19:36:16 +0100567 else if (buf->b_p_bh[0] == 'h' && !del_buf)
568 {
569 // Hide a terminal buffer.
570 unload_buf = FALSE;
571 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200572 else
573 {
Yee Cheng Chin42826332022-10-10 11:46:16 +0100574 if (del_buf || unload_buf)
575 {
576 // A terminal buffer is wiped out if the job has finished.
577 // We only do this when there's an intention to unload the
578 // buffer. This way, :hide and other similar commands won't
579 // wipe the buffer.
580 del_buf = TRUE;
581 unload_buf = TRUE;
582 wipe_buf = TRUE;
583 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200584 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100585 CHECK_CURBUF;
Bram Moolenaar94053a52017-08-01 21:44:33 +0200586 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200587#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588
Bram Moolenaarc667da52019-11-30 20:52:27 +0100589 // Disallow deleting the buffer when it is locked (already being closed or
590 // halfway a command that relies on it). Unloading is allowed.
Bram Moolenaar94f01952018-09-01 15:30:03 +0200591 if ((del_buf || wipe_buf) && !can_unload_buffer(buf))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100592 return FALSE;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200593
Bram Moolenaarc667da52019-11-30 20:52:27 +0100594 // check no autocommands closed the window
Bram Moolenaar4033c552017-09-16 20:54:51 +0200595 if (win != NULL && win_valid_any_tab(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100597 // Set b_last_cursor when closing the last window for the buffer.
598 // Remember the last cursor position and window options of the buffer.
599 // This used to be only for the current window, but then options like
600 // 'foldmethod' may be lost with a ":only" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 if (buf->b_nwindows == 1)
602 set_last_cursor(win);
603 buflist_setfpos(buf, win,
604 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
605 win->w_cursor.col, TRUE);
606 }
607
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200608 set_bufref(&bufref, buf);
609
Bram Moolenaarc667da52019-11-30 20:52:27 +0100610 // When the buffer is no longer in a window, trigger BufWinLeave
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 if (buf->b_nwindows == 1)
612 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200613 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100614 ++buf->b_locked_split;
Bram Moolenaar82404332016-07-10 17:00:38 +0200615 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
616 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200617 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100618 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100619 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200620aucmd_abort:
Bram Moolenaarf1474d82021-12-31 19:59:55 +0000621 emsg(_(e_autocommands_caused_command_to_abort));
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100622 return FALSE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100623 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200624 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100625 --buf->b_locked_split;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200626 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100627 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200628 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629
Bram Moolenaarc667da52019-11-30 20:52:27 +0100630 // When the buffer becomes hidden, but is not unloaded, trigger
631 // BufHidden
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 if (!unload_buf)
633 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200634 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100635 ++buf->b_locked_split;
Bram Moolenaar82404332016-07-10 17:00:38 +0200636 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
637 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200638 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100639 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200640 goto aucmd_abort;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200641 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100642 --buf->b_locked_split;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200643 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100644 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200645 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100647#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100648 // autocmds may abort script processing
649 if (!ignore_abort && aborting())
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100650 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200653
Bram Moolenaarc667da52019-11-30 20:52:27 +0100654 // If the buffer was in curwin and the window has changed, go back to that
655 // window, if it still exists. This avoids that ":edit x" triggering a
656 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200657 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
658 {
659 block_autocmds();
660 goto_tabpage_win(the_curtab, the_curwin);
661 unblock_autocmds();
662 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200663
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 nwindows = buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665
Bram Moolenaarc667da52019-11-30 20:52:27 +0100666 // decrease the link count from windows (unless not in any window)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 if (buf->b_nwindows > 0)
668 --buf->b_nwindows;
669
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100670#ifdef FEAT_DIFF
671 if (diffopt_hiddenoff() && !unload_buf && buf->b_nwindows == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100672 diff_buf_delete(buf); // Clear 'diff' for hidden buffer.
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100673#endif
674
Bram Moolenaarc667da52019-11-30 20:52:27 +0100675 // Return when a window is displaying the buffer or when it's not
676 // unloaded.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100678 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679
Bram Moolenaarc667da52019-11-30 20:52:27 +0100680 // Always remove the buffer when there is no file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 if (buf->b_ffname == NULL)
682 del_buf = TRUE;
683
Bram Moolenaarc667da52019-11-30 20:52:27 +0100684 // When closing the current buffer stop Visual mode before freeing
685 // anything.
Bram Moolenaar4930a762016-09-11 14:39:53 +0200686 if (buf == curbuf && VIsual_active
Bram Moolenaar9a27c7f2016-09-09 12:57:09 +0200687#if defined(EXITFREE)
688 && !entered_free_all_mem
689#endif
690 )
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200691 end_visual_mode();
692
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100693 // Free all things allocated for this buffer.
694 // Also calls the "BufDelete" autocommands when del_buf is TRUE.
695 //
Bram Moolenaarc667da52019-11-30 20:52:27 +0100696 // Remember if we are closing the current buffer. Restore the number of
697 // windows, so that autocommands in buf_freeall() don't get confused.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 is_curbuf = (buf == curbuf);
699 buf->b_nwindows = nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100701 buf_freeall(buf, (del_buf ? BFA_DEL : 0)
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100702 + (wipe_buf ? BFA_WIPE : 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100703 + (ignore_abort ? BFA_IGNORE_ABORT : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704
Bram Moolenaarc667da52019-11-30 20:52:27 +0100705 // Autocommands may have deleted the buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200706 if (!bufref_valid(&bufref))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100707 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100708#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100709 // autocmds may abort script processing
710 if (!ignore_abort && aborting())
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100711 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100714 // It's possible that autocommands change curbuf to the one being deleted.
715 // This might cause the previous curbuf to be deleted unexpectedly. But
716 // in some cases it's OK to delete the curbuf, because a new one is
717 // obtained anyway. Therefore only return if curbuf changed to the
718 // deleted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 if (buf == curbuf && !is_curbuf)
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100720 return FALSE;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200721
Bram Moolenaar4033c552017-09-16 20:54:51 +0200722 if (win_valid_any_tab(win) && win->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100723 win->w_buffer = NULL; // make sure we don't use the buffer now
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200724
Bram Moolenaarc667da52019-11-30 20:52:27 +0100725 // Autocommands may have opened or closed windows for this buffer.
726 // Decrement the count for the close we do here.
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200727 if (buf->b_nwindows > 0)
728 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 /*
731 * Remove the buffer from the list.
732 */
733 if (wipe_buf)
734 {
Bram Moolenaar347538f2022-03-26 16:28:06 +0000735 // Do not wipe out the buffer if it is used in a window.
736 if (buf->b_nwindows > 0)
737 return FALSE;
738
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200739 if (action == DOBUF_WIPE_REUSE)
740 {
741 // we can re-use this buffer number, store it
742 if (buf_reuse.ga_itemsize == 0)
743 ga_init2(&buf_reuse, sizeof(int), 50);
744 if (ga_grow(&buf_reuse, 1) == OK)
745 ((int *)buf_reuse.ga_data)[buf_reuse.ga_len++] = buf->b_fnum;
746 }
Bram Moolenaar3d6014f2018-10-11 19:27:47 +0200747 if (buf->b_sfname != buf->b_ffname)
748 VIM_CLEAR(buf->b_sfname);
749 else
750 buf->b_sfname = NULL;
751 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 if (buf->b_prev == NULL)
753 firstbuf = buf->b_next;
754 else
755 buf->b_prev->b_next = buf->b_next;
756 if (buf->b_next == NULL)
757 lastbuf = buf->b_prev;
758 else
759 buf->b_next->b_prev = buf->b_prev;
760 free_buffer(buf);
761 }
762 else
763 {
764 if (del_buf)
765 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100766 // Free all internal variables and reset option values, to make
767 // ":bdel" compatible with Vim 5.7.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 free_buffer_stuff(buf, TRUE);
769
Bram Moolenaarc667da52019-11-30 20:52:27 +0100770 // Make it look like a new buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
772
Bram Moolenaarc667da52019-11-30 20:52:27 +0100773 // Init the options when loaded again.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 buf->b_p_initialized = FALSE;
775 }
776 buf_clear_file(buf);
777 if (del_buf)
778 buf->b_p_bl = FALSE;
779 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100780 // NOTE: at this point "curbuf" may be invalid!
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100781 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782}
783
784/*
785 * Make buffer not contain a file.
786 */
787 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100788buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789{
790 buf->b_ml.ml_line_count = 1;
Bram Moolenaarc024b462019-06-08 18:07:21 +0200791 unchanged(buf, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 buf->b_shortname = FALSE;
Bram Moolenaar15775372022-10-29 20:01:52 +0100793 buf->b_p_eof = FALSE;
794 buf->b_start_eof = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 buf->b_p_eol = TRUE;
796 buf->b_start_eol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000798 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 buf->b_ml.ml_mfp = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +0100800 buf->b_ml.ml_flags = ML_EMPTY; // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801#ifdef FEAT_NETBEANS_INTG
802 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803#endif
804}
805
806/*
807 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200808 * the file. Careful: get here with "curwin" NULL when exiting.
809 * flags:
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100810 * BFA_DEL buffer is going to be deleted
811 * BFA_WIPE buffer is going to be wiped out
812 * BFA_KEEP_UNDO do not free undo information
813 * BFA_IGNORE_ABORT don't abort even when aborting() returns TRUE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100816buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200819 bufref_T bufref;
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200820 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaar5a497892016-09-03 16:29:04 +0200821 win_T *the_curwin = curwin;
822 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823
Bram Moolenaarc667da52019-11-30 20:52:27 +0100824 // Make sure the buffer isn't closed by autocommands.
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200825 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100826 ++buf->b_locked_split;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200827 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200828 if (buf->b_ml.ml_mfp != NULL)
829 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200830 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
831 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200832 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100833 // autocommands deleted the buffer
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200834 return;
835 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200836 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200838 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
839 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200840 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100841 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 return;
843 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200844 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200846 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
847 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200848 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100849 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 return;
851 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200852 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100853 --buf->b_locked_split;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200854
Bram Moolenaarc667da52019-11-30 20:52:27 +0100855 // If the buffer was in curwin and the window has changed, go back to that
856 // window, if it still exists. This avoids that ":edit x" triggering a
857 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaar5a497892016-09-03 16:29:04 +0200858 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
859 {
860 block_autocmds();
861 goto_tabpage_win(the_curtab, the_curwin);
862 unblock_autocmds();
863 }
Bram Moolenaar5a497892016-09-03 16:29:04 +0200864
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100865#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100866 // autocmds may abort script processing
867 if ((flags & BFA_IGNORE_ABORT) == 0 && aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100869#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100871 // It's possible that autocommands change curbuf to the one being deleted.
872 // This might cause curbuf to be deleted unexpectedly. But in some cases
873 // it's OK to delete the curbuf, because a new one is obtained anyway.
874 // Therefore only return if curbuf changed to the deleted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 if (buf == curbuf && !is_curbuf)
876 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877#ifdef FEAT_DIFF
Bram Moolenaarc667da52019-11-30 20:52:27 +0100878 diff_buf_delete(buf); // Can't use 'diff' for unloaded buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200880#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100881 // Remove any ownsyntax, unless exiting.
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200882 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaar89c71222011-11-30 15:40:56 +0100883 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200884#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000885
886#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +0100887 // No folds in an empty buffer.
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000888 {
889 win_T *win;
890 tabpage_T *tp;
891
892 FOR_ALL_TAB_WINDOWS(tp, win)
893 if (win->w_buffer == buf)
894 clearFolding(win);
895 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000896#endif
897
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898#ifdef FEAT_TCL
899 tcl_buffer_free(buf);
900#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100901 ml_close(buf, TRUE); // close and delete the memline/memfile
902 buf->b_ml.ml_line_count = 0; // no lines in buffer
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200903 if ((flags & BFA_KEEP_UNDO) == 0)
904 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100905 u_blockfree(buf); // free the memory allocated for undo
906 u_clearall(buf); // reset all undo information
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100909 syntax_clear(&buf->b_s); // reset syntax info
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100911#ifdef FEAT_PROP_POPUP
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100912 clear_buf_prop_types(buf);
913#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100914 buf->b_flags &= ~BF_READERR; // a read error is no longer relevant
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915}
916
917/*
918 * Free a buffer structure and the things it contains related to the buffer
919 * itself (not the file, that must have been done already).
920 */
921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100922free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200924 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200926#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100927 // b:changedtick uses an item in buf_T, remove it now
Bram Moolenaarb2c87502017-10-14 21:15:58 +0200928 dictitem_remove(buf->b_vars, (dictitem_T *)&buf->b_ct_di);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200929 unref_var_dict(buf->b_vars);
Bram Moolenaar86173482019-10-01 17:02:16 +0200930 remove_listeners(buf);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200931#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200932#ifdef FEAT_LUA
933 lua_buffer_free(buf);
934#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000935#ifdef FEAT_MZSCHEME
936 mzscheme_buffer_free(buf);
937#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938#ifdef FEAT_PERL
939 perl_buf_free(buf);
940#endif
941#ifdef FEAT_PYTHON
942 python_buffer_free(buf);
943#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200944#ifdef FEAT_PYTHON3
945 python3_buffer_free(buf);
946#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947#ifdef FEAT_RUBY
948 ruby_buffer_free(buf);
949#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200950#ifdef FEAT_JOB_CHANNEL
951 channel_buffer_free(buf);
952#endif
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200953#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +0200954 free_terminal(buf);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200955#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +0200956#ifdef FEAT_JOB_CHANNEL
957 vim_free(buf->b_prompt_text);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200958 free_callback(&buf->b_prompt_callback);
Bram Moolenaar86173482019-10-01 17:02:16 +0200959 free_callback(&buf->b_prompt_interrupt);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200960#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200961
962 buf_hashtab_remove(buf);
963
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200964 aubuflocal_remove(buf);
965
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200966 if (autocmd_busy)
967 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100968 // Do not free the buffer structure while autocommands are executing,
969 // it's still needed. Free it when autocmd_busy is reset.
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200970 buf->b_next = au_pending_free_buf;
971 au_pending_free_buf = buf;
972 }
973 else
Bram Moolenaarcee52202020-03-11 14:19:58 +0100974 {
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200975 vim_free(buf);
Bram Moolenaarcee52202020-03-11 14:19:58 +0100976 if (curbuf == buf)
977 curbuf = NULL; // make clear it's not to be used
978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979}
980
981/*
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100982 * Initializes b:changedtick.
Bram Moolenaar79518e22017-02-17 16:31:35 +0100983 */
984 static void
985init_changedtick(buf_T *buf)
986{
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100987 dictitem_T *di = (dictitem_T *)&buf->b_ct_di;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100988
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100989 di->di_flags = DI_FLAGS_FIX | DI_FLAGS_RO;
990 di->di_tv.v_type = VAR_NUMBER;
991 di->di_tv.v_lock = VAR_FIXED;
992 di->di_tv.vval.v_number = 0;
993
Bram Moolenaar92769c32017-02-25 15:41:37 +0100994#ifdef FEAT_EVAL
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100995 STRCPY(buf->b_ct_di.di_key, "changedtick");
996 (void)dict_add(buf->b_vars, di);
Bram Moolenaar92769c32017-02-25 15:41:37 +0100997#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +0100998}
999
1000/*
Bram Moolenaarc3126192022-08-26 12:58:17 +01001001 * Free the b_wininfo list for buffer "buf".
1002 */
1003 static void
1004clear_wininfo(buf_T *buf)
1005{
1006 wininfo_T *wip;
1007
1008 while (buf->b_wininfo != NULL)
1009 {
1010 wip = buf->b_wininfo;
1011 buf->b_wininfo = wip->wi_next;
1012 free_wininfo(wip);
1013 }
1014}
1015
1016/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
1018 */
1019 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001020free_buffer_stuff(
1021 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001022 int free_options) // free options as well
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023{
1024 if (free_options)
1025 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001026 clear_wininfo(buf); // including window-local options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +02001028#ifdef FEAT_SPELL
1029 ga_clear(&buf->b_s.b_langp);
1030#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 }
1032#ifdef FEAT_EVAL
Bram Moolenaar79518e22017-02-17 16:31:35 +01001033 {
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001034 varnumber_T tick = CHANGEDTICK(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001035
Bram Moolenaarc667da52019-11-30 20:52:27 +01001036 vars_clear(&buf->b_vars->dv_hashtab); // free all buffer variables
Bram Moolenaar79518e22017-02-17 16:31:35 +01001037 hash_init(&buf->b_vars->dv_hashtab);
1038 init_changedtick(buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001039 CHANGEDTICK(buf) = tick;
Bram Moolenaarf10997a2020-01-03 21:00:02 +01001040 remove_listeners(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042#endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001043 uc_clear(&buf->b_ucmds); // clear local user commands
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044#ifdef FEAT_SIGNS
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001045 buf_delete_signs(buf, (char_u *)"*"); // delete any signs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00001047#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001048 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00001049#endif
Bram Moolenaar0c740e72022-07-25 19:07:04 +01001050#ifdef FEAT_PROP_POPUP
1051 ga_clear_strings(&buf->b_textprop_text);
1052#endif
zeertzjqc207fd22022-06-29 10:37:40 +01001053 map_clear_mode(buf, MAP_ALL_MODES, TRUE, FALSE); // clear local mappings
1054 map_clear_mode(buf, MAP_ALL_MODES, TRUE, TRUE); // clear local abbrevs
Bram Moolenaard23a8232018-02-10 18:45:26 +01001055 VIM_CLEAR(buf->b_start_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056}
1057
1058/*
Bram Moolenaar4882d982020-10-25 17:55:09 +01001059 * Free one wininfo_T.
1060 */
1061 void
1062free_wininfo(wininfo_T *wip)
1063{
1064 if (wip->wi_optset)
1065 {
1066 clear_winopt(&wip->wi_opt);
1067#ifdef FEAT_FOLDING
1068 deleteFoldRecurse(&wip->wi_folds);
1069#endif
1070 }
1071 vim_free(wip);
1072}
1073
1074/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 * Go to another buffer. Handles the result of the ATTENTION dialog.
1076 */
1077 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001078goto_buffer(
1079 exarg_T *eap,
1080 int start,
1081 int dir,
1082 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001084 bufref_T old_curbuf;
Bram Moolenaar188639d2022-04-04 16:57:21 +01001085 int save_sea = swap_exists_action;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001086
1087 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088
Bram Moolenaar188639d2022-04-04 16:57:21 +01001089 if (swap_exists_action == SEA_NONE)
1090 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
1092 start, dir, count, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
1094 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001095#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001096 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001097
Bram Moolenaarc667da52019-11-30 20:52:27 +01001098 // Reset the error/interrupt/exception state here so that
1099 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001100 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001101#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001102
Bram Moolenaarc667da52019-11-30 20:52:27 +01001103 // Quitting means closing the split window, nothing else.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 win_close(curwin, TRUE);
Bram Moolenaar188639d2022-04-04 16:57:21 +01001105 swap_exists_action = save_sea;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001106 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001107
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001108#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001109 // Restore the error/interrupt/exception state if not discarded by a
1110 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001111 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001112#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 }
1114 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001115 handle_swap_exists(&old_curbuf);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001116}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118/*
1119 * Handle the situation of swap_exists_action being set.
1120 * It is allowed for "old_curbuf" to be NULL or invalid.
1121 */
1122 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001123handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124{
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001125#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001126 cleanup_T cs;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001127#endif
1128#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001129 long old_tw = curbuf->b_p_tw;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001130#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001131 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001132
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 if (swap_exists_action == SEA_QUIT)
1134 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001135#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001136 // Reset the error/interrupt/exception state here so that
1137 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001138 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001139#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001140
Bram Moolenaarc667da52019-11-30 20:52:27 +01001141 // User selected Quit at ATTENTION prompt. Go back to previous
1142 // buffer. If that buffer is gone or the same as the current one,
1143 // open a new, empty buffer.
1144 swap_exists_action = SEA_NONE; // don't want it again
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001145 swap_exists_did_quit = TRUE;
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001146 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001147 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1148 || old_curbuf->br_buf == curbuf)
Bram Moolenaar1d97efc2021-07-04 13:27:11 +02001149 {
1150 // Block autocommands here because curwin->w_buffer is NULL.
1151 block_autocmds();
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001152 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar1d97efc2021-07-04 13:27:11 +02001153 unblock_autocmds();
1154 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001155 else
1156 buf = old_curbuf->br_buf;
1157 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001158 {
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001159 int old_msg_silent = msg_silent;
1160
1161 if (shortmess(SHM_FILEINFO))
1162 msg_silent = 1; // prevent fileinfo message
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001163 enter_buffer(buf);
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001164 // restore msg_silent, so that the command line will be shown
1165 msg_silent = old_msg_silent;
1166
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001167#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001168 if (old_tw != curbuf->b_p_tw)
1169 check_colorcolumn(curwin);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001170#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001171 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001172 // If "old_curbuf" is NULL we are in big trouble here...
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001173
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001174#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001175 // Restore the error/interrupt/exception state if not discarded by a
1176 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001177 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 }
1180 else if (swap_exists_action == SEA_RECOVER)
1181 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001182#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001183 // Reset the error/interrupt/exception state here so that
1184 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001185 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001186#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001187
Bram Moolenaarc667da52019-11-30 20:52:27 +01001188 // User selected Recover at ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 msg_scroll = TRUE;
Bram Moolenaar99499b12019-05-23 21:35:48 +02001190 ml_recover(FALSE);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001191 msg_puts("\n"); // don't overwrite the last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001193 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001194
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001195#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001196 // Restore the error/interrupt/exception state if not discarded by a
1197 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001198 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 }
1201 swap_exists_action = SEA_NONE;
1202}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204/*
Bram Moolenaara02471e2014-01-10 16:43:14 +01001205 * Make the current buffer empty.
1206 * Used when it is wiped out and it's the last buffer.
1207 */
1208 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001209empty_curbuf(
1210 int close_others,
1211 int forceit,
1212 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001213{
1214 int retval;
1215 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001216 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001217
1218 if (action == DOBUF_UNLOAD)
1219 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001220 emsg(_(e_cannot_unload_last_buffer));
Bram Moolenaara02471e2014-01-10 16:43:14 +01001221 return FAIL;
1222 }
1223
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001224 set_bufref(&bufref, buf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001225 if (close_others)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001226 // Close any other windows on this buffer, then make it empty.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001227 close_windows(buf, TRUE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001228
1229 setpcmark();
1230 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1231 forceit ? ECMD_FORCEIT : 0, curwin);
1232
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001233 // do_ecmd() may create a new buffer, then we have to delete
1234 // the old one. But do_ecmd() may have done that already, check
1235 // if the buffer still exists.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001236 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001237 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001238 if (!close_others)
1239 need_fileinfo = FALSE;
1240 return retval;
1241}
Bram Moolenaar94f01952018-09-01 15:30:03 +02001242
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243/*
1244 * Implementation of the commands for the buffer list.
1245 *
1246 * action == DOBUF_GOTO go to specified buffer
1247 * action == DOBUF_SPLIT split window and go to specified buffer
1248 * action == DOBUF_UNLOAD unload specified buffer(s)
1249 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1250 * action == DOBUF_WIPE delete specified buffer(s) really
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001251 * action == DOBUF_WIPE_REUSE idem, and add number to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 *
1253 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1254 * start == DOBUF_FIRST go to "count" buffer from first buffer
1255 * start == DOBUF_LAST go to "count" buffer from last buffer
1256 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1257 *
1258 * Return FAIL or OK.
1259 */
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001260 static int
1261do_buffer_ext(
Bram Moolenaar7454a062016-01-30 15:14:10 +01001262 int action,
1263 int start,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001264 int dir, // FORWARD or BACKWARD
1265 int count, // buffer number or number of buffers
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001266 int flags) // DOBUF_FORCEIT etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267{
1268 buf_T *buf;
1269 buf_T *bp;
1270 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001271 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272
1273 switch (start)
1274 {
1275 case DOBUF_FIRST: buf = firstbuf; break;
1276 case DOBUF_LAST: buf = lastbuf; break;
1277 default: buf = curbuf; break;
1278 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001279 if (start == DOBUF_MOD) // find next modified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 {
1281 while (count-- > 0)
1282 {
1283 do
1284 {
1285 buf = buf->b_next;
1286 if (buf == NULL)
1287 buf = firstbuf;
1288 }
1289 while (buf != curbuf && !bufIsChanged(buf));
1290 }
1291 if (!bufIsChanged(buf))
1292 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001293 emsg(_(e_no_modified_buffer_found));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 return FAIL;
1295 }
1296 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001297 else if (start == DOBUF_FIRST && count) // find specified buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 {
1299 while (buf != NULL && buf->b_fnum != count)
1300 buf = buf->b_next;
1301 }
1302 else
1303 {
1304 bp = NULL;
1305 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1306 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001307 // remember the buffer where we start, we come back there when all
1308 // buffers are unlisted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 if (bp == NULL)
1310 bp = buf;
1311 if (dir == FORWARD)
1312 {
1313 buf = buf->b_next;
1314 if (buf == NULL)
1315 buf = firstbuf;
1316 }
1317 else
1318 {
1319 buf = buf->b_prev;
1320 if (buf == NULL)
1321 buf = lastbuf;
1322 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001323 // don't count unlisted buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 if (unload || buf->b_p_bl)
1325 {
1326 --count;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001327 bp = NULL; // use this buffer as new starting point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 }
1329 if (bp == buf)
1330 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001331 // back where we started, didn't find anything.
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001332 emsg(_(e_there_is_no_listed_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 return FAIL;
1334 }
1335 }
1336 }
1337
Bram Moolenaarc667da52019-11-30 20:52:27 +01001338 if (buf == NULL) // could not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 {
1340 if (start == DOBUF_FIRST)
1341 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001342 // don't warn when deleting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 if (!unload)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001344 semsg(_(e_buffer_nr_does_not_exist), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 }
1346 else if (dir == FORWARD)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001347 emsg(_(e_cannot_go_beyond_last_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 else
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001349 emsg(_(e_cannot_go_before_first_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 return FAIL;
1351 }
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001352#ifdef FEAT_PROP_POPUP
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001353 if ((flags & DOBUF_NOPOPUP) && bt_popup(buf) && !bt_terminal(buf))
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001354 return OK;
1355#endif
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01001356 if ((action == DOBUF_GOTO || action == DOBUF_SPLIT)
1357 && (buf->b_flags & BF_DUMMY))
1358 {
1359 // disallow navigating to the dummy buffer
1360 semsg(_(e_buffer_nr_does_not_exist), count);
1361 return FAIL;
1362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363
1364#ifdef FEAT_GUI
1365 need_mouse_correct = TRUE;
1366#endif
1367
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001369 * delete buffer "buf" from memory and/or the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 */
1371 if (unload)
1372 {
1373 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001374 bufref_T bufref;
1375
Bram Moolenaar94f01952018-09-01 15:30:03 +02001376 if (!can_unload_buffer(buf))
Bram Moolenaara997b452018-04-17 23:24:06 +02001377 return FAIL;
Bram Moolenaara997b452018-04-17 23:24:06 +02001378
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001379 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380
Bram Moolenaarc667da52019-11-30 20:52:27 +01001381 // When unloading or deleting a buffer that's already unloaded and
1382 // unlisted: fail silently.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001383 if (action != DOBUF_WIPE && action != DOBUF_WIPE_REUSE
1384 && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 return FAIL;
1386
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001387 if ((flags & DOBUF_FORCEIT) == 0 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001389#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001390 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001392# ifdef FEAT_TERMINAL
1393 if (term_job_running(buf->b_term))
1394 {
1395 if (term_confirm_stop(buf) == FAIL)
1396 return FAIL;
1397 }
1398 else
1399# endif
1400 {
1401 dialog_changed(buf, FALSE);
1402 if (!bufref_valid(&bufref))
1403 // Autocommand deleted buffer, oops! It's not changed
1404 // now.
1405 return FAIL;
1406 // If it's still changed fail silently, the dialog already
1407 // mentioned why it fails.
1408 if (bufIsChanged(buf))
1409 return FAIL;
1410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001412 else
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001413#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001415 no_write_message_buf(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 return FAIL;
1417 }
1418 }
1419
Bram Moolenaarc667da52019-11-30 20:52:27 +01001420 // When closing the current buffer stop Visual mode.
Bram Moolenaar4930a762016-09-11 14:39:53 +02001421 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001422 end_visual_mode();
1423
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001424 // If deleting the last (listed) buffer, make it empty.
1425 // The last (listed) buffer cannot be unloaded.
Bram Moolenaar29323592016-07-24 22:04:11 +02001426 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 if (bp->b_p_bl && bp != buf)
1428 break;
1429 if (bp == NULL && buf == curbuf)
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001430 return empty_curbuf(TRUE, (flags & DOBUF_FORCEIT), action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001432 // If the deleted buffer is the current one, close the current window
1433 // (unless it's the only window). Repeat this so long as we end up in
1434 // a window with this buffer.
Bram Moolenaarf740b292006-02-16 22:11:02 +00001435 while (buf == curbuf
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001436 && !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
Bram Moolenaar459ca562016-11-10 18:16:33 +01001437 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001438 {
1439 if (win_close(curwin, FALSE) == FAIL)
1440 break;
1441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001443 // If the buffer to be deleted is not the current one, delete it here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 if (buf != curbuf)
1445 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001446 close_windows(buf, FALSE);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001447 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows <= 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001448 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 return OK;
1450 }
1451
1452 /*
1453 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001454 * There should be another, otherwise it would have been handled
1455 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001456 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 * Then prefer the buffer we most recently visited.
1458 * Else try to find one that is loaded, after the current buffer,
1459 * then before the current buffer.
1460 * Finally use any buffer.
1461 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001462 buf = NULL; // selected buffer
1463 bp = NULL; // used when no loaded buffer found
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001464 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1465 buf = au_new_curbuf.br_buf;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001466 else if (curwin->w_jumplistlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 {
1468 int jumpidx;
1469
1470 jumpidx = curwin->w_jumplistidx - 1;
1471 if (jumpidx < 0)
1472 jumpidx = curwin->w_jumplistlen - 1;
1473
1474 forward = jumpidx;
1475 while (jumpidx != curwin->w_jumplistidx)
1476 {
1477 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1478 if (buf != NULL)
1479 {
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001480 // Skip current and unlisted bufs. Also skip a quickfix
1481 // buffer, it might be deleted soon.
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001482 if (buf == curbuf || !buf->b_p_bl || bt_quickfix(buf))
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001483 buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 else if (buf->b_ml.ml_mfp == NULL)
1485 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001486 // skip unloaded buf, but may keep it for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 if (bp == NULL)
1488 bp = buf;
1489 buf = NULL;
1490 }
1491 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001492 if (buf != NULL) // found a valid buffer: stop searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001494 // advance to older entry in jump list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1496 break;
1497 if (--jumpidx < 0)
1498 jumpidx = curwin->w_jumplistlen - 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001499 if (jumpidx == forward) // List exhausted for sure
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 break;
1501 }
1502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503
Bram Moolenaarc667da52019-11-30 20:52:27 +01001504 if (buf == NULL) // No previous buffer, Try 2'nd approach
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 {
1506 forward = TRUE;
1507 buf = curbuf->b_next;
1508 for (;;)
1509 {
1510 if (buf == NULL)
1511 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001512 if (!forward) // tried both directions
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 break;
1514 buf = curbuf->b_prev;
1515 forward = FALSE;
1516 continue;
1517 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001518 // in non-help buffer, try to skip help buffers, and vv
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001519 if (buf->b_help == curbuf->b_help && buf->b_p_bl
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001520 && !bt_quickfix(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001522 if (buf->b_ml.ml_mfp != NULL) // found loaded buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001524 if (bp == NULL) // remember unloaded buf for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 bp = buf;
1526 }
1527 if (forward)
1528 buf = buf->b_next;
1529 else
1530 buf = buf->b_prev;
1531 }
1532 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001533 if (buf == NULL) // No loaded buffer, use unloaded one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 buf = bp;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001535 if (buf == NULL) // No loaded buffer, find listed one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001537 FOR_ALL_BUFFERS(buf)
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001538 if (buf->b_p_bl && buf != curbuf && !bt_quickfix(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 break;
1540 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001541 if (buf == NULL) // Still no buffer, just take one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 {
1543 if (curbuf->b_next != NULL)
1544 buf = curbuf->b_next;
1545 else
1546 buf = curbuf->b_prev;
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001547 if (bt_quickfix(buf))
1548 buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 }
1550 }
1551
Bram Moolenaara02471e2014-01-10 16:43:14 +01001552 if (buf == NULL)
1553 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001554 // Autocommands must have wiped out all other buffers. Only option
1555 // now is to make the current buffer empty.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001556 return empty_curbuf(FALSE, (flags & DOBUF_FORCEIT), action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001557 }
1558
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001560 * make "buf" the current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001562 if (action == DOBUF_SPLIT) // split window first
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001564 // If 'switchbuf' contains "useopen": jump to first window containing
1565 // "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00001566 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001567 return OK;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001568 // If 'switchbuf' contains "usetab": jump to first window in any tab
1569 // page containing "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00001570 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 return OK;
1572 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 return FAIL;
1574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575
Bram Moolenaarc667da52019-11-30 20:52:27 +01001576 // go to current buffer - nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 if (buf == curbuf)
1578 return OK;
1579
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001580 // Check if the current buffer may be abandoned.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001581 if (action == DOBUF_GOTO && !can_abandon(curbuf, (flags & DOBUF_FORCEIT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 {
1583#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001584 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001586# ifdef FEAT_TERMINAL
1587 if (term_job_running(curbuf->b_term))
1588 {
1589 if (term_confirm_stop(curbuf) == FAIL)
1590 return FAIL;
1591 // Manually kill the terminal here because this command will
1592 // hide it otherwise.
1593 free_terminal(curbuf);
1594 }
1595 else
1596# endif
1597 {
1598 bufref_T bufref;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001599
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001600 set_bufref(&bufref, buf);
1601 dialog_changed(curbuf, FALSE);
1602 if (!bufref_valid(&bufref))
1603 // Autocommand deleted buffer, oops!
1604 return FAIL;
1605
1606 if (bufIsChanged(curbuf))
1607 {
1608 no_write_message();
1609 return FAIL;
1610 }
1611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 }
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001613 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614#endif
1615 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001616 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 return FAIL;
1618 }
1619 }
1620
Bram Moolenaarc667da52019-11-30 20:52:27 +01001621 // Go to the other buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 set_curbuf(buf, action);
1623
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 if (action == DOBUF_SPLIT)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001625 RESET_BINDING(curwin); // reset 'scrollbind' and 'cursorbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001627#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001628 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 return FAIL;
1630#endif
1631
1632 return OK;
1633}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001635 int
1636do_buffer(
1637 int action,
1638 int start,
1639 int dir, // FORWARD or BACKWARD
1640 int count, // buffer number or number of buffers
1641 int forceit) // TRUE when using !
1642{
1643 return do_buffer_ext(action, start, dir, count,
1644 forceit ? DOBUF_FORCEIT : 0);
1645}
1646
1647/*
1648 * do_bufdel() - delete or unload buffer(s)
1649 *
1650 * addr_count == 0: ":bdel" - delete current buffer
1651 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1652 * buffer "end_bnr", then any other arguments.
1653 * addr_count == 2: ":N,N bdel" - delete buffers in range
1654 *
1655 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1656 * DOBUF_DEL (":bdel")
1657 *
1658 * Returns error message or NULL
1659 */
1660 char *
1661do_bufdel(
1662 int command,
1663 char_u *arg, // pointer to extra arguments
1664 int addr_count,
1665 int start_bnr, // first buffer number in a range
1666 int end_bnr, // buffer nr or last buffer nr in a range
1667 int forceit)
1668{
1669 int do_current = 0; // delete current buffer?
1670 int deleted = 0; // number of buffers deleted
1671 char *errormsg = NULL; // return value
1672 int bnr; // buffer number
1673 char_u *p;
1674
1675 if (addr_count == 0)
1676 {
1677 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1678 }
1679 else
1680 {
1681 if (addr_count == 2)
1682 {
1683 if (*arg) // both range and argument is not allowed
Bram Moolenaar74409f62022-01-01 15:58:22 +00001684 return ex_errmsg(e_trailing_characters_str, arg);
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001685 bnr = start_bnr;
1686 }
1687 else // addr_count == 1
1688 bnr = end_bnr;
1689
1690 for ( ;!got_int; ui_breakcheck())
1691 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001692 // Delete the current buffer last, otherwise when the
1693 // current buffer is deleted, the next buffer becomes
1694 // the current one and will be loaded, which may then
1695 // also be deleted, etc.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001696 if (bnr == curbuf->b_fnum)
1697 do_current = bnr;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00001698 else if (do_buffer_ext(command, DOBUF_FIRST, FORWARD, bnr,
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001699 DOBUF_NOPOPUP | (forceit ? DOBUF_FORCEIT : 0)) == OK)
1700 ++deleted;
1701
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001702 // find next buffer number to delete/unload
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001703 if (addr_count == 2)
1704 {
1705 if (++bnr > end_bnr)
1706 break;
1707 }
1708 else // addr_count == 1
1709 {
1710 arg = skipwhite(arg);
1711 if (*arg == NUL)
1712 break;
1713 if (!VIM_ISDIGIT(*arg))
1714 {
1715 p = skiptowhite_esc(arg);
1716 bnr = buflist_findpat(arg, p,
1717 command == DOBUF_WIPE || command == DOBUF_WIPE_REUSE,
1718 FALSE, FALSE);
1719 if (bnr < 0) // failed
1720 break;
1721 arg = p;
1722 }
1723 else
1724 bnr = getdigits(&arg);
1725 }
1726 }
1727 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1728 FORWARD, do_current, forceit) == OK)
1729 ++deleted;
1730
1731 if (deleted == 0)
1732 {
1733 if (command == DOBUF_UNLOAD)
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001734 STRCPY(IObuff, _(e_no_buffers_were_unloaded));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001735 else if (command == DOBUF_DEL)
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001736 STRCPY(IObuff, _(e_no_buffers_were_deleted));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001737 else
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001738 STRCPY(IObuff, _(e_no_buffers_were_wiped_out));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001739 errormsg = (char *)IObuff;
1740 }
1741 else if (deleted >= p_report)
1742 {
1743 if (command == DOBUF_UNLOAD)
1744 smsg(NGETTEXT("%d buffer unloaded",
1745 "%d buffers unloaded", deleted), deleted);
1746 else if (command == DOBUF_DEL)
1747 smsg(NGETTEXT("%d buffer deleted",
1748 "%d buffers deleted", deleted), deleted);
1749 else
1750 smsg(NGETTEXT("%d buffer wiped out",
1751 "%d buffers wiped out", deleted), deleted);
1752 }
1753 }
1754
1755
1756 return errormsg;
1757}
1758
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759/*
1760 * Set current buffer to "buf". Executes autocommands and closes current
1761 * buffer. "action" tells how to close the current buffer:
1762 * DOBUF_GOTO free or hide it
1763 * DOBUF_SPLIT nothing
1764 * DOBUF_UNLOAD unload it
1765 * DOBUF_DEL delete it
1766 * DOBUF_WIPE wipe it out
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001767 * DOBUF_WIPE_REUSE wipe it out and add to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 */
1769 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001770set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771{
1772 buf_T *prevbuf;
1773 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001774 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001775#ifdef FEAT_SYN_HL
1776 long old_tw = curbuf->b_p_tw;
1777#endif
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001778 bufref_T newbufref;
1779 bufref_T prevbufref;
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001780 int valid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781
1782 setpcmark();
Bram Moolenaare1004402020-10-24 20:49:43 +02001783 if ((cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001784 curwin->w_alt_fnum = curbuf->b_fnum; // remember alternate file
1785 buflist_altfpos(curwin); // remember curpos
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786
Bram Moolenaarc667da52019-11-30 20:52:27 +01001787 // Don't restart Select mode after switching to another buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789
Bram Moolenaarc667da52019-11-30 20:52:27 +01001790 // close_windows() or apply_autocmds() may change curbuf and wipe out "buf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 prevbuf = curbuf;
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001792 set_bufref(&prevbufref, prevbuf);
1793 set_bufref(&newbufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794
Bram Moolenaar983d83f2021-02-07 12:12:43 +01001795 // Autocommands may delete the current buffer and/or the buffer we want to
1796 // go to. In those cases don't close the buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001797 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001798 || (bufref_valid(&prevbufref)
1799 && bufref_valid(&newbufref)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001800#ifdef FEAT_EVAL
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001801 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001803 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001805#ifdef FEAT_SYN_HL
1806 if (prevbuf == curwin->w_buffer)
1807 reset_synblock(curwin);
1808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001810 close_windows(prevbuf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001811#if defined(FEAT_EVAL)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001812 if (bufref_valid(&prevbufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813#else
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001814 if (bufref_valid(&prevbufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001816 {
Bram Moolenaare25865a2012-07-06 16:22:02 +02001817 win_T *previouswin = curwin;
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001818
Bram Moolenaare615db02022-01-20 21:00:54 +00001819 // Do not sync when in Insert mode and the buffer is open in
1820 // another window, might be a timer doing something in another
1821 // window.
1822 if (prevbuf == curbuf
Bram Moolenaar24959102022-05-07 20:01:16 +01001823 && ((State & MODE_INSERT) == 0 || curbuf->b_nwindows <= 1))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001824 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1826 unload ? action : (action == DOBUF_GOTO
Bram Moolenaareb44a682017-08-03 22:44:55 +02001827 && !buf_hide(prevbuf)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001828 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0,
1829 FALSE, FALSE);
Bram Moolenaare25865a2012-07-06 16:22:02 +02001830 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001831 // autocommands changed curwin, Grr!
Bram Moolenaare25865a2012-07-06 16:22:02 +02001832 curwin = previouswin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001835 // An autocommand may have deleted "buf", already entered it (e.g., when
1836 // it did ":bunload") or aborted the script processing.
1837 // If curwin->w_buffer is null, enter_buffer() will make it valid again
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001838 valid = buf_valid(buf);
1839 if ((valid && buf != curbuf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001840#ifdef FEAT_EVAL
Bram Moolenaar4033c552017-09-16 20:54:51 +02001841 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001843 ) || curwin->w_buffer == NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001844 {
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001845 // If the buffer is not valid but curwin->w_buffer is NULL we must
1846 // enter some buffer. Using the last one is hopefully OK.
1847 if (!valid)
1848 enter_buffer(lastbuf);
1849 else
1850 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001851#ifdef FEAT_SYN_HL
1852 if (old_tw != curbuf->b_p_tw)
1853 check_colorcolumn(curwin);
1854#endif
1855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856}
1857
1858/*
1859 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001860 * Old curbuf must have been abandoned already! This also means "curbuf" may
1861 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001863 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001864enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865{
Bram Moolenaarcfeb8a52022-08-13 14:09:44 +01001866 // when closing the current buffer stop Visual mode
1867 if (VIsual_active
1868#if defined(EXITFREE)
1869 && !entered_free_all_mem
1870#endif
1871 )
1872 end_visual_mode();
1873
Bram Moolenaar010ee962019-09-25 20:37:36 +02001874 // Get the buffer in the current window.
1875 curwin->w_buffer = buf;
1876 curbuf = buf;
1877 ++curbuf->b_nwindows;
1878
1879 // Copy buffer and window local option values. Not for a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1881 if (!buf->b_help)
1882 get_winopts(buf);
1883#ifdef FEAT_FOLDING
1884 else
Bram Moolenaar010ee962019-09-25 20:37:36 +02001885 // Remove all folds in the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 clearFolding(curwin);
Bram Moolenaar010ee962019-09-25 20:37:36 +02001887 foldUpdateAll(curwin); // update folds (later).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888#endif
1889
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001891 if (curwin->w_p_diff)
1892 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893#endif
1894
Bram Moolenaara971b822011-09-14 14:43:25 +02001895#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001896 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001897#endif
1898
Bram Moolenaarc667da52019-11-30 20:52:27 +01001899 // Cursor on first line by default.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 curwin->w_cursor.lnum = 1;
1901 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001904 curwin->w_topline_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905
Bram Moolenaarc667da52019-11-30 20:52:27 +01001906 // mark cursor position as being invalid
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001907 curwin->w_valid = 0;
1908
Bram Moolenaar9cea87c2018-09-21 16:59:45 +02001909 buflist_setfpos(curbuf, curwin, curbuf->b_last_cursor.lnum,
1910 curbuf->b_last_cursor.col, TRUE);
1911
Bram Moolenaarc667da52019-11-30 20:52:27 +01001912 // Make sure the buffer is loaded.
1913 if (curbuf->b_ml.ml_mfp == NULL) // need to load the file
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001914 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001915 // If there is no filetype, allow for detecting one. Esp. useful for
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001916 // ":ball" used in an autocommand. If there already is a filetype we
Bram Moolenaarc667da52019-11-30 20:52:27 +01001917 // might prefer to keep it.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001918 if (*curbuf->b_p_ft == NUL)
1919 did_filetype = FALSE;
1920
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001921 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 else
1924 {
Bram Moolenaareda65222019-05-16 20:29:44 +02001925 if (!msg_silent && !shortmess(SHM_FILEINFO))
1926 need_fileinfo = TRUE; // display file info after redraw
1927
1928 // check if file changed
1929 (void)buf_check_timestamp(curbuf, FALSE);
1930
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001932#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001934#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1936 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 }
1938
Bram Moolenaarc667da52019-11-30 20:52:27 +01001939 // If autocommands did not change the cursor position, restore cursor lnum
1940 // and possibly cursor col.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 if (curwin->w_cursor.lnum == 1 && inindent(0))
1942 buflist_getfpos();
1943
Bram Moolenaarc667da52019-11-30 20:52:27 +01001944 check_arg_idx(curwin); // check for valid arg_idx
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 maketitle();
Bram Moolenaarc667da52019-11-30 20:52:27 +01001946 // when autocmds didn't change it
Bram Moolenaard4153d42008-11-15 15:06:17 +00001947 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001948 scroll_cursor_halfway(FALSE); // redisplay at correct position
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949
Bram Moolenaar009b2592004-10-24 19:18:58 +00001950#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarc667da52019-11-30 20:52:27 +01001951 // Send fileOpened event because we've changed buffers.
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001952 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001953#endif
1954
Bram Moolenaarc667da52019-11-30 20:52:27 +01001955 // Change directories when the 'acd' option is set.
Bram Moolenaar6f470022018-04-10 18:47:20 +02001956 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957
1958#ifdef FEAT_KEYMAP
1959 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001960 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001962#ifdef FEAT_SPELL
Bram Moolenaarc667da52019-11-30 20:52:27 +01001963 // May need to set the spell language. Can only do this after the buffer
1964 // has been properly setup.
Bram Moolenaar860cae12010-06-05 23:22:07 +02001965 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1966 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001967#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001968#ifdef FEAT_VIMINFO
1969 curbuf->b_last_used = vim_time();
1970#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001971
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001972 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973}
1974
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001975#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1976/*
1977 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001978 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001979 */
1980 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001981do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001982{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001983 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001984 && curbuf->b_ffname != NULL
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001985 && vim_chdirfile(curbuf->b_ffname, "auto") == OK)
Bram Moolenaar05268152021-11-18 18:53:45 +00001986 {
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001987 shorten_fnames(TRUE);
Bram Moolenaar05268152021-11-18 18:53:45 +00001988 last_chdir_reason = "autochdir";
1989 }
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001990}
1991#endif
1992
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001993 static void
1994no_write_message_buf(buf_T *buf UNUSED)
1995{
1996#ifdef FEAT_TERMINAL
1997 if (term_job_running(buf->b_term))
1998 emsg(_(e_job_still_running_add_bang_to_end_the_job));
1999 else
2000#endif
2001 semsg(_(e_no_write_since_last_change_for_buffer_nr_add_bang_to_override),
2002 buf->b_fnum);
2003}
2004
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002005 void
2006no_write_message(void)
2007{
2008#ifdef FEAT_TERMINAL
2009 if (term_job_running(curbuf->b_term))
Bram Moolenaarf1474d82021-12-31 19:59:55 +00002010 emsg(_(e_job_still_running_add_bang_to_end_the_job));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002011 else
2012#endif
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002013 emsg(_(e_no_write_since_last_change_add_bang_to_override));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002014}
2015
2016 void
Bram Moolenaar7a760922018-02-19 23:10:02 +01002017no_write_message_nobang(buf_T *buf UNUSED)
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002018{
2019#ifdef FEAT_TERMINAL
Bram Moolenaar7a760922018-02-19 23:10:02 +01002020 if (term_job_running(buf->b_term))
Bram Moolenaarf1474d82021-12-31 19:59:55 +00002021 emsg(_(e_job_still_running));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002022 else
2023#endif
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002024 emsg(_(e_no_write_since_last_change));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002025}
2026
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027/*
2028 * functions for dealing with the buffer list
2029 */
2030
2031/*
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002032 * Return TRUE if the current buffer is empty, unnamed, unmodified and used in
2033 * only one window. That means it can be re-used.
2034 */
2035 int
2036curbuf_reusable(void)
2037{
2038 return (curbuf != NULL
2039 && curbuf->b_ffname == NULL
2040 && curbuf->b_nwindows <= 1
2041 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY())
Bram Moolenaar39803d82019-04-07 12:04:51 +02002042 && !bt_quickfix(curbuf)
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002043 && !curbufIsChanged());
2044}
2045
2046/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 * Add a file name to the buffer list. Return a pointer to the buffer.
2048 * If the same file name already exists return a pointer to that buffer.
2049 * If it does not exist, or if fname == NULL, a new entry is created.
2050 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
2051 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
2052 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02002053 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02002054 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
2055 * if the buffer already exists.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002056 * If (flags & BLN_REUSE) is TRUE, may use buffer number from "buf_reuse".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 * This is the ONLY way to create a new buffer.
2058 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002060buflist_new(
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002061 char_u *ffname_arg, // full path of fname or relative
2062 char_u *sfname_arg, // short fname or NULL
2063 linenr_T lnum, // preferred cursor line
2064 int flags) // BLN_ defines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002066 char_u *ffname = ffname_arg;
2067 char_u *sfname = sfname_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 buf_T *buf;
2069#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002070 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071#endif
2072
Bram Moolenaar480778b2016-07-14 22:09:39 +02002073 if (top_file_num == 1)
2074 hash_init(&buf_hashtab);
2075
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002076 fname_expand(curbuf, &ffname, &sfname); // will allocate ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077
2078 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002079 * If the file name already exists in the list, update the entry.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 */
2081#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002082 // On Unix we can use inode numbers when the file exists. Works better
2083 // for hard links.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
2085 st.st_dev = (dev_T)-1;
2086#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02002087 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088#ifdef UNIX
2089 buflist_findname_stat(ffname, &st)
2090#else
2091 buflist_findname(ffname)
2092#endif
2093 ) != NULL)
2094 {
2095 vim_free(ffname);
2096 if (lnum != 0)
Bram Moolenaar89b693e2020-10-25 17:09:50 +01002097 buflist_setfpos(buf, (flags & BLN_NOCURWIN) ? NULL : curwin,
2098 lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02002099
2100 if ((flags & BLN_NOOPT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002101 // copy the options now, if 'cpo' doesn't have 's' and not done
2102 // already
Bram Moolenaar82404332016-07-10 17:00:38 +02002103 buf_copy_options(buf, 0);
2104
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 if ((flags & BLN_LISTED) && !buf->b_p_bl)
2106 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002107 bufref_T bufref;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002108
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 buf->b_p_bl = TRUE;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002110 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002112 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002113 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002114 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002115 return NULL;
2116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 }
2118 return buf;
2119 }
2120
2121 /*
2122 * If the current buffer has no name and no contents, use the current
2123 * buffer. Otherwise: Need to allocate a new buffer structure.
2124 *
2125 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00002126 * (A spell file buffer is allocated in spell.c, but that's not a normal
2127 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 */
2129 buf = NULL;
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002130 if ((flags & BLN_CURBUF) && curbuf_reusable())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 {
2132 buf = curbuf;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002133 // It's like this buffer is deleted. Watch out for autocommands that
2134 // change curbuf! If that happens, allocate a new buffer anyway.
Charlie Grovesfef44852022-04-19 16:24:12 +01002135 buf_freeall(buf, BFA_WIPE | BFA_DEL);
2136 if (buf != curbuf) // autocommands deleted the buffer!
2137 return NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002138#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002139 if (aborting()) // autocmds may abort script processing
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002140 {
2141 vim_free(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 return NULL;
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 }
2146 if (buf != curbuf || curbuf == NULL)
2147 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002148 buf = ALLOC_CLEAR_ONE(buf_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 if (buf == NULL)
2150 {
2151 vim_free(ffname);
2152 return NULL;
2153 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02002154#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002155 // init b: variables
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01002156 buf->b_vars = dict_alloc_id(aid_newbuf_bvars);
Bram Moolenaar429fa852013-04-15 12:27:36 +02002157 if (buf->b_vars == NULL)
2158 {
2159 vim_free(ffname);
2160 vim_free(buf);
2161 return NULL;
2162 }
2163 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
2164#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01002165 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 }
2167
2168 if (ffname != NULL)
2169 {
2170 buf->b_ffname = ffname;
2171 buf->b_sfname = vim_strsave(sfname);
2172 }
2173
2174 clear_wininfo(buf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002175 buf->b_wininfo = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176
2177 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
2178 || buf->b_wininfo == NULL)
2179 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002180 if (buf->b_sfname != buf->b_ffname)
2181 VIM_CLEAR(buf->b_sfname);
2182 else
2183 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01002184 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 if (buf != curbuf)
2186 free_buffer(buf);
2187 return NULL;
2188 }
2189
2190 if (buf == curbuf)
2191 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002192 free_buffer_stuff(buf, FALSE); // delete local variables et al.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002193
Bram Moolenaarc667da52019-11-30 20:52:27 +01002194 // Init the options.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002195 buf->b_p_initialized = FALSE;
2196 buf_copy_options(buf, BCO_ENTER);
2197
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198#ifdef FEAT_KEYMAP
Bram Moolenaarc667da52019-11-30 20:52:27 +01002199 // need to reload lmaps and set b:keymap_name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 curbuf->b_kmap_state |= KEYMAP_INIT;
2201#endif
2202 }
2203 else
2204 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002205 // put the new buffer at the end of the buffer list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 buf->b_next = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002207 if (firstbuf == NULL) // buffer list is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 {
2209 buf->b_prev = NULL;
2210 firstbuf = buf;
2211 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002212 else // append new buffer at end of list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 {
2214 lastbuf->b_next = buf;
2215 buf->b_prev = lastbuf;
2216 }
2217 lastbuf = buf;
2218
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002219 if ((flags & BLN_REUSE) && buf_reuse.ga_len > 0)
2220 {
2221 // Recycle a previously used buffer number. Used for buffers which
2222 // are normally hidden, e.g. in a popup window. Avoids that the
2223 // buffer number grows rapidly.
2224 --buf_reuse.ga_len;
2225 buf->b_fnum = ((int *)buf_reuse.ga_data)[buf_reuse.ga_len];
Bram Moolenaar99ebf222019-12-10 23:44:48 +01002226
2227 // Move buffer to the right place in the buffer list.
2228 while (buf->b_prev != NULL && buf->b_fnum < buf->b_prev->b_fnum)
2229 {
2230 buf_T *prev = buf->b_prev;
2231
2232 prev->b_next = buf->b_next;
2233 if (prev->b_next != NULL)
2234 prev->b_next->b_prev = prev;
2235 buf->b_next = prev;
2236 buf->b_prev = prev->b_prev;
2237 if (buf->b_prev != NULL)
2238 buf->b_prev->b_next = buf;
2239 prev->b_prev = buf;
2240 if (lastbuf == buf)
2241 lastbuf = prev;
2242 if (firstbuf == prev)
2243 firstbuf = buf;
2244 }
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002245 }
2246 else
2247 buf->b_fnum = top_file_num++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002248 if (top_file_num < 0) // wrap around (may cause duplicates)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002250 emsg(_("W14: Warning: List of file names overflow"));
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002251 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 {
2253 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002254 ui_delay(3001L, TRUE); // make sure it is noticed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 }
2256 top_file_num = 1;
2257 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002258 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002260 // Always copy the options from the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 buf_copy_options(buf, BCO_ALWAYS);
2262 }
2263
2264 buf->b_wininfo->wi_fpos.lnum = lnum;
2265 buf->b_wininfo->wi_win = curwin;
2266
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002267#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002268 hash_init(&buf->b_s.b_keywtab);
2269 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270#endif
2271
2272 buf->b_fname = buf->b_sfname;
2273#ifdef UNIX
2274 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002275 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 else
2277 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002278 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 buf->b_dev = st.st_dev;
2280 buf->b_ino = st.st_ino;
2281 }
2282#endif
2283 buf->b_u_synced = TRUE;
2284 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002285 if (flags & BLN_DUMMY)
2286 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 buf_clear_file(buf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002288 clrallmarks(buf); // clear marks
2289 fmarks_check_names(buf); // check file marks for this file
2290 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; // init 'buflisted'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 if (!(flags & BLN_DUMMY))
2292 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002293 bufref_T bufref;
2294
Bram Moolenaarc667da52019-11-30 20:52:27 +01002295 // Tricky: these autocommands may change the buffer list. They could
2296 // also split the window with re-using the one empty buffer. This may
2297 // result in unexpectedly losing the empty buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002298 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002299 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002300 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002301 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002303 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002304 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002305 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002306 return NULL;
2307 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002308#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002309 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313
2314 return buf;
2315}
2316
2317/*
2318 * Free the memory for the options of a buffer.
2319 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2320 * 'fileencoding'.
2321 */
2322 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002323free_buf_options(
2324 buf_T *buf,
2325 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326{
2327 if (free_p_ff)
2328 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 clear_string_option(&buf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 clear_string_option(&buf->b_p_bh);
2332 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 }
2334#ifdef FEAT_FIND_ID
2335 clear_string_option(&buf->b_p_def);
2336 clear_string_option(&buf->b_p_inc);
2337# ifdef FEAT_EVAL
2338 clear_string_option(&buf->b_p_inex);
2339# endif
2340#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002341#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 clear_string_option(&buf->b_p_inde);
2343 clear_string_option(&buf->b_p_indk);
2344#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002345#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2346 clear_string_option(&buf->b_p_bexpr);
2347#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002348#if defined(FEAT_CRYPT)
2349 clear_string_option(&buf->b_p_cm);
2350#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002351 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002352#if defined(FEAT_EVAL)
2353 clear_string_option(&buf->b_p_fex);
2354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355#ifdef FEAT_CRYPT
Bram Moolenaar131530a2021-07-29 20:37:49 +02002356# ifdef FEAT_SODIUM
K.Takata1a8825d2022-01-19 13:32:57 +00002357 if ((buf->b_p_key != NULL) && (*buf->b_p_key != NUL) &&
2358 (crypt_get_method_nr(buf) == CRYPT_M_SOD))
2359 crypt_sodium_munlock(buf->b_p_key, STRLEN(buf->b_p_key));
Bram Moolenaar131530a2021-07-29 20:37:49 +02002360# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 clear_string_option(&buf->b_p_key);
2362#endif
2363 clear_string_option(&buf->b_p_kp);
2364 clear_string_option(&buf->b_p_mps);
2365 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002366 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 clear_string_option(&buf->b_p_isk);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002368#ifdef FEAT_VARTABS
2369 clear_string_option(&buf->b_p_vsts);
Bram Moolenaarbdace832019-03-02 10:13:42 +01002370 vim_free(buf->b_p_vsts_nopaste);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002371 buf->b_p_vsts_nopaste = NULL;
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00002372 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002373 clear_string_option(&buf->b_p_vts);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002374 VIM_CLEAR(buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002375#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376#ifdef FEAT_KEYMAP
2377 clear_string_option(&buf->b_p_keymap);
Bram Moolenaar50138322018-01-28 17:05:16 +01002378 keymap_clear(&buf->b_kmap_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 ga_clear(&buf->b_kmap_ga);
2380#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 clear_string_option(&buf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382#ifdef FEAT_FOLDING
2383 clear_string_option(&buf->b_p_cms);
2384#endif
2385 clear_string_option(&buf->b_p_nf);
2386#ifdef FEAT_SYN_HL
2387 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002388 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002389#endif
2390#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002391 clear_string_option(&buf->b_s.b_p_spc);
2392 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002393 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002394 buf->b_s.b_cap_prog = NULL;
2395 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02002396 clear_string_option(&buf->b_s.b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 clear_string_option(&buf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 clear_string_option(&buf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 clear_string_option(&buf->b_p_cink);
2401 clear_string_option(&buf->b_p_cino);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002402 clear_string_option(&buf->b_p_lop);
Tom Praschan3506cf32022-04-07 12:39:08 +01002403 clear_string_option(&buf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 clear_string_option(&buf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 clear_string_option(&buf->b_p_cpt);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002406#ifdef FEAT_COMPL_FUNC
2407 clear_string_option(&buf->b_p_cfu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002408 free_callback(&buf->b_cfu_cb);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002409 clear_string_option(&buf->b_p_ofu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002410 free_callback(&buf->b_ofu_cb);
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +01002411 clear_string_option(&buf->b_p_tsrfu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002412 free_callback(&buf->b_tsrfu_cb);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002413#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414#ifdef FEAT_QUICKFIX
2415 clear_string_option(&buf->b_p_gp);
2416 clear_string_option(&buf->b_p_mp);
2417 clear_string_option(&buf->b_p_efm);
2418#endif
2419 clear_string_option(&buf->b_p_ep);
2420 clear_string_option(&buf->b_p_path);
2421 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002422 clear_string_option(&buf->b_p_tc);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002423#ifdef FEAT_EVAL
2424 clear_string_option(&buf->b_p_tfu);
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +00002425 free_callback(&buf->b_tfu_cb);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002426#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 clear_string_option(&buf->b_p_dict);
2428 clear_string_option(&buf->b_p_tsr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002429 clear_string_option(&buf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002431 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002432 clear_string_option(&buf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002433 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002434 clear_string_option(&buf->b_p_menc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435}
2436
2437/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002438 * Get alternate file "n".
2439 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2440 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 * if (options & GETF_SETMARK) call setpcmark()
2442 * if (options & GETF_ALT) we are jumping to an alternate file.
2443 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2444 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002445 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 */
2447 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002448buflist_getfile(
2449 int n,
2450 linenr_T lnum,
2451 int options,
2452 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453{
2454 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 pos_T *fpos;
2457 colnr_T col;
2458
2459 buf = buflist_findnr(n);
2460 if (buf == NULL)
2461 {
2462 if ((options & GETF_ALT) && n == 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02002463 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 else
Bram Moolenaare1242042021-12-16 20:56:57 +00002465 semsg(_(e_buffer_nr_not_found), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 return FAIL;
2467 }
2468
Bram Moolenaarc667da52019-11-30 20:52:27 +01002469 // if alternate file is the current buffer, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 if (buf == curbuf)
2471 return OK;
2472
Bram Moolenaar71223e22022-05-30 15:23:09 +01002473 if (text_or_buf_locked())
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002474 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475
Bram Moolenaarc667da52019-11-30 20:52:27 +01002476 // altfpos may be changed by getfile(), get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 if (lnum == 0)
2478 {
2479 fpos = buflist_findfpos(buf);
2480 lnum = fpos->lnum;
2481 col = fpos->col;
2482 }
2483 else
2484 col = 0;
2485
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 if (options & GETF_SWITCH)
2487 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002488 // If 'switchbuf' contains "useopen": jump to first window containing
2489 // "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00002490 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002492
Bram Moolenaarc667da52019-11-30 20:52:27 +01002493 // If 'switchbuf' contains "usetab": jump to first window in any tab
2494 // page containing "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00002495 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002496 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002497
Bram Moolenaarc667da52019-11-30 20:52:27 +01002498 // If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2499 // current buffer isn't empty: open new tab or window
Bram Moolenaara594d772015-06-19 14:41:49 +02002500 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002501 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002503 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002504 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002505 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2506 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002508 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 }
2510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511
2512 ++RedrawingDisabled;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002513 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2514 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 {
2516 --RedrawingDisabled;
2517
Bram Moolenaarc667da52019-11-30 20:52:27 +01002518 // cursor is at to BOL and w_cursor.lnum is checked due to getfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 if (!p_sol && col != 0)
2520 {
2521 curwin->w_cursor.col = col;
2522 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 curwin->w_set_curswant = TRUE;
2525 }
2526 return OK;
2527 }
2528 --RedrawingDisabled;
2529 return FAIL;
2530}
2531
2532/*
2533 * go to the last know line number for the current buffer
2534 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002535 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002536buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537{
2538 pos_T *fpos;
2539
2540 fpos = buflist_findfpos(curbuf);
2541
2542 curwin->w_cursor.lnum = fpos->lnum;
2543 check_cursor_lnum();
2544
2545 if (p_sol)
2546 curwin->w_cursor.col = 0;
2547 else
2548 {
2549 curwin->w_cursor.col = fpos->col;
2550 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 curwin->w_set_curswant = TRUE;
2553 }
2554}
2555
Martin Tournoijba43e762022-10-13 22:12:15 +01002556#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar81695252004-12-29 20:58:21 +00002557/*
2558 * Find file in buffer list by name (it has to be for the current window).
2559 * Returns NULL if not found.
2560 */
2561 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002562buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002563{
2564 char_u *ffname;
2565 buf_T *buf = NULL;
2566
Bram Moolenaarc667da52019-11-30 20:52:27 +01002567 // First make the name into a full path name
Bram Moolenaar81695252004-12-29 20:58:21 +00002568 ffname = FullName_save(fname,
2569#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002570 TRUE // force expansion, get rid of symbolic links
Bram Moolenaar81695252004-12-29 20:58:21 +00002571#else
2572 FALSE
2573#endif
2574 );
2575 if (ffname != NULL)
2576 {
2577 buf = buflist_findname(ffname);
2578 vim_free(ffname);
2579 }
2580 return buf;
2581}
2582#endif
2583
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584/*
2585 * Find file in buffer list by name (it has to be for the current window).
2586 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002587 * Skips dummy buffers.
2588 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 */
2590 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002591buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592{
2593#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002594 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595
2596 if (mch_stat((char *)ffname, &st) < 0)
2597 st.st_dev = (dev_T)-1;
2598 return buflist_findname_stat(ffname, &st);
2599}
2600
2601/*
2602 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2603 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002604 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 */
2606 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002607buflist_findname_stat(
2608 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002609 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610{
2611#endif
2612 buf_T *buf;
2613
Bram Moolenaarc667da52019-11-30 20:52:27 +01002614 // Start at the last buffer, expect to find a match sooner.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002615 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaar81695252004-12-29 20:58:21 +00002616 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617#ifdef UNIX
2618 , stp
2619#endif
2620 ))
2621 return buf;
2622 return NULL;
2623}
2624
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625/*
2626 * Find file in buffer list by a regexp pattern.
2627 * Return fnum of the found buffer.
2628 * Return < 0 for error.
2629 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002631buflist_findpat(
2632 char_u *pattern,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002633 char_u *pattern_end, // pointer to first char after pattern
2634 int unlisted, // find unlisted buffers
2635 int diffmode UNUSED, // find diff-mode buffers only
2636 int curtab_only) // find buffers in current tab only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637{
2638 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 int match = -1;
2640 int find_listed;
2641 char_u *pat;
2642 char_u *patend;
2643 int attempt;
2644 char_u *p;
2645 int toggledollar;
2646
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002647 // "%" is current file, "%%" or "#" is alternate file
2648 if ((pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2649 || (in_vim9script() && pattern_end == pattern + 2
2650 && pattern[0] == '%' && pattern[1] == '%'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 {
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002652 if (*pattern == '#' || pattern_end == pattern + 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 match = curwin->w_alt_fnum;
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002654 else
2655 match = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656#ifdef FEAT_DIFF
2657 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2658 match = -1;
2659#endif
2660 }
2661
2662 /*
2663 * Try four ways of matching a listed buffer:
2664 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002665 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 * attempt == 2: with '$' at end (only match at end)
2667 * attempt == 3: with '^' at start and '$' at end (only full match)
2668 * Repeat this for finding an unlisted buffer if there was no matching
2669 * listed buffer.
2670 */
2671 else
2672 {
2673 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2674 if (pat == NULL)
2675 return -1;
2676 patend = pat + STRLEN(pat) - 1;
2677 toggledollar = (patend > pat && *patend == '$');
2678
Bram Moolenaarc667da52019-11-30 20:52:27 +01002679 // First try finding a listed buffer. If not found and "unlisted"
2680 // is TRUE, try finding an unlisted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 find_listed = TRUE;
2682 for (;;)
2683 {
2684 for (attempt = 0; attempt <= 3; ++attempt)
2685 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002686 regmatch_T regmatch;
2687
Bram Moolenaarc667da52019-11-30 20:52:27 +01002688 // may add '^' and '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 if (toggledollar)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002690 *patend = (attempt < 2) ? NUL : '$'; // add/remove '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 p = pat;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002692 if (*p == '^' && !(attempt & 1)) // add/remove '^'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 ++p;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01002694 regmatch.regprog = vim_regcomp(p, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002696 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002697 {
2698 if (regmatch.regprog == NULL)
2699 {
2700 // invalid pattern, possibly after switching engine
2701 vim_free(pat);
2702 return -1;
2703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 if (buf->b_p_bl == find_listed
2705#ifdef FEAT_DIFF
2706 && (!diffmode || diff_mode_buf(buf))
2707#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002708 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002710 if (curtab_only)
2711 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002712 // Ignore the match if the buffer is not open in
2713 // the current tab.
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002714 win_T *wp;
2715
Bram Moolenaar29323592016-07-24 22:04:11 +02002716 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002717 if (wp->w_buffer == buf)
2718 break;
2719 if (wp == NULL)
2720 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002721 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002722 if (match >= 0) // already found a match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 {
2724 match = -2;
2725 break;
2726 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002727 match = buf->b_fnum; // remember first match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 }
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002731 vim_regfree(regmatch.regprog);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002732 if (match >= 0) // found one match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 break;
2734 }
2735
Bram Moolenaarc667da52019-11-30 20:52:27 +01002736 // Only search for unlisted buffers if there was no match with
2737 // a listed buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 if (!unlisted || !find_listed || match != -1)
2739 break;
2740 find_listed = FALSE;
2741 }
2742
2743 vim_free(pat);
2744 }
2745
2746 if (match == -2)
Bram Moolenaare1242042021-12-16 20:56:57 +00002747 semsg(_(e_more_than_one_match_for_str), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 else if (match < 0)
Bram Moolenaare1242042021-12-16 20:56:57 +00002749 semsg(_(e_no_matching_buffer_for_str), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 return match;
2751}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752
Bram Moolenaar52410572019-10-27 05:12:45 +01002753#ifdef FEAT_VIMINFO
2754typedef struct {
2755 buf_T *buf;
2756 char_u *match;
2757} bufmatch_T;
2758#endif
2759
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760/*
2761 * Find all buffer names that match.
2762 * For command line expansion of ":buf" and ":sbuf".
2763 * Return OK if matches found, FAIL otherwise.
2764 */
2765 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002766ExpandBufnames(
2767 char_u *pat,
2768 int *num_file,
2769 char_u ***file,
2770 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771{
2772 int count = 0;
2773 buf_T *buf;
2774 int round;
2775 char_u *p;
2776 int attempt;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002777 char_u *patc = NULL;
Bram Moolenaar52410572019-10-27 05:12:45 +01002778#ifdef FEAT_VIMINFO
2779 bufmatch_T *matches = NULL;
2780#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002781 int fuzzy;
2782 fuzmatch_str_T *fuzmatch = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783
Bram Moolenaarc667da52019-11-30 20:52:27 +01002784 *num_file = 0; // return values in case of FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 *file = NULL;
2786
Bram Moolenaarefcc3292019-12-30 21:59:03 +01002787#ifdef FEAT_DIFF
2788 if ((options & BUF_DIFF_FILTER) && !curwin->w_p_diff)
2789 return FAIL;
2790#endif
2791
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002792 fuzzy = cmdline_fuzzy_complete(pat);
2793
2794 // Make a copy of "pat" and change "^" to "\(^\|[\/]\)" (if doing regular
2795 // expression matching)
2796 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002798 if (*pat == '^')
2799 {
2800 patc = alloc(STRLEN(pat) + 11);
2801 if (patc == NULL)
2802 return FAIL;
2803 STRCPY(patc, "\\(^\\|[\\/]\\)");
2804 STRCPY(patc + 11, pat + 1);
2805 }
2806 else
2807 patc = pat;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002808 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00002809
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002810 // attempt == 0: try match with '\<', match at start of word
2811 // attempt == 1: try match without '\<', match anywhere
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002812 for (attempt = 0; attempt <= (fuzzy ? 0 : 1); ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002813 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002814 regmatch_T regmatch;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002815 int score = 0;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002816
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002817 if (!fuzzy)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002818 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002819 if (attempt > 0 && patc == pat)
2820 break; // there was no anchor, no need to try again
2821 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002824 // round == 1: Count the matches.
2825 // round == 2: Build the array to keep the matches.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 for (round = 1; round <= 2; ++round)
2827 {
2828 count = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002829 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002831 if (!buf->b_p_bl) // skip unlisted buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 continue;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002833#ifdef FEAT_DIFF
2834 if (options & BUF_DIFF_FILTER)
2835 // Skip buffers not suitable for
2836 // :diffget or :diffput completion.
Bram Moolenaarefcc3292019-12-30 21:59:03 +01002837 if (buf == curbuf || !diff_mode_buf(buf))
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002838 continue;
2839#endif
2840
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002841 if (!fuzzy)
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002842 {
2843 if (regmatch.regprog == NULL)
2844 {
2845 // invalid pattern, possibly after recompiling
2846 if (patc != pat)
2847 vim_free(patc);
2848 return FAIL;
2849 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002850 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002851 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002852 else
2853 {
2854 p = NULL;
2855 // first try matching with the short file name
2856 if ((score = fuzzy_match_str(buf->b_sfname, pat)) != 0)
2857 p = buf->b_sfname;
2858 if (p == NULL)
2859 {
2860 // next try matching with the full path file name
2861 if ((score = fuzzy_match_str(buf->b_ffname, pat)) != 0)
2862 p = buf->b_ffname;
2863 }
2864 }
2865
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002866 if (p == NULL)
2867 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002868
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002869 if (round == 1)
2870 {
2871 ++count;
2872 continue;
2873 }
2874
2875 if (options & WILD_HOME_REPLACE)
2876 p = home_replace_save(buf, p);
2877 else
2878 p = vim_strsave(p);
2879
2880 if (!fuzzy)
2881 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002882#ifdef FEAT_VIMINFO
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002883 if (matches != NULL)
2884 {
2885 matches[count].buf = buf;
2886 matches[count].match = p;
2887 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002889 else
2890#endif
2891 (*file)[count++] = p;
2892 }
2893 else
2894 {
2895 fuzmatch[count].idx = count;
2896 fuzmatch[count].str = p;
2897 fuzmatch[count].score = score;
2898 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 }
2900 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002901 if (count == 0) // no match found, break here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 break;
2903 if (round == 1)
2904 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002905 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002907 *file = ALLOC_MULT(char_u *, count);
2908 if (*file == NULL)
2909 {
2910 vim_regfree(regmatch.regprog);
2911 if (patc != pat)
2912 vim_free(patc);
2913 return FAIL;
2914 }
Bram Moolenaar52410572019-10-27 05:12:45 +01002915#ifdef FEAT_VIMINFO
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002916 if (options & WILD_BUFLASTUSED)
2917 matches = ALLOC_MULT(bufmatch_T, count);
Bram Moolenaar52410572019-10-27 05:12:45 +01002918#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002919 }
2920 else
2921 {
2922 fuzmatch = ALLOC_MULT(fuzmatch_str_T, count);
2923 if (fuzmatch == NULL)
2924 {
2925 *num_file = 0;
2926 *file = NULL;
2927 return FAIL;
2928 }
2929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 }
2931 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002932
2933 if (!fuzzy)
2934 {
2935 vim_regfree(regmatch.regprog);
2936 if (count) // match(es) found, break here
2937 break;
2938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 }
2940
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002941 if (!fuzzy && patc != pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002942 vim_free(patc);
2943
Bram Moolenaar52410572019-10-27 05:12:45 +01002944#ifdef FEAT_VIMINFO
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002945 if (!fuzzy)
Bram Moolenaar52410572019-10-27 05:12:45 +01002946 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002947 if (matches != NULL)
Bram Moolenaar52410572019-10-27 05:12:45 +01002948 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002949 int i;
2950 if (count > 1)
2951 qsort(matches, count, sizeof(bufmatch_T), buf_compare);
2952 // if the current buffer is first in the list, place it at the end
2953 if (matches[0].buf == curbuf)
2954 {
2955 for (i = 1; i < count; i++)
2956 (*file)[i-1] = matches[i].match;
2957 (*file)[count-1] = matches[0].match;
2958 }
2959 else
2960 {
2961 for (i = 0; i < count; i++)
2962 (*file)[i] = matches[i].match;
2963 }
2964 vim_free(matches);
Bram Moolenaar52410572019-10-27 05:12:45 +01002965 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002966 }
2967 else
2968 {
2969 if (fuzzymatches_to_strmatches(fuzmatch, file, count, FALSE) == FAIL)
2970 return FAIL;
Bram Moolenaar52410572019-10-27 05:12:45 +01002971 }
2972#endif
2973
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 *num_file = count;
2975 return (count == 0 ? FAIL : OK);
2976}
2977
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978/*
2979 * Check for a match on the file name for buffer "buf" with regprog "prog".
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002980 * Note that rmp->regprog may become NULL when switching regexp engine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 */
2982 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002983buflist_match(
2984 regmatch_T *rmp,
2985 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002986 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987{
2988 char_u *match;
2989
Bram Moolenaarc667da52019-11-30 20:52:27 +01002990 // First try the short file name, then the long file name.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002991 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaara59f2df2022-05-11 11:42:28 +01002992 if (match == NULL && rmp->regprog != NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002993 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994
2995 return match;
2996}
2997
2998/*
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002999 * Try matching the regexp in "rmp->regprog" with file name "name".
3000 * Note that rmp->regprog may become NULL when switching regexp engine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 * Return "name" when there is a match, NULL when not.
3002 */
3003 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003004fname_match(
3005 regmatch_T *rmp,
3006 char_u *name,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003007 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008{
3009 char_u *match = NULL;
3010 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003012 // extra check for valid arguments
3013 if (name != NULL && rmp->regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003015 // Ignore case when 'fileignorecase' or the argument is set.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01003016 rmp->rm_ic = p_fic || ignore_case;
3017 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 match = name;
Bram Moolenaar8e4b76d2022-05-07 11:28:06 +01003019 else if (rmp->regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003021 // Replace $(HOME) with '~' and try matching again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01003023 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 match = name;
3025 vim_free(p);
3026 }
3027 }
3028
3029 return match;
3030}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031
3032/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02003033 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 */
3035 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003036buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037{
Bram Moolenaar480778b2016-07-14 22:09:39 +02003038 char_u key[VIM_SIZEOF_INT * 2 + 1];
3039 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040
3041 if (nr == 0)
3042 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02003043 sprintf((char *)key, "%x", nr);
3044 hi = hash_find(&buf_hashtab, key);
3045
3046 if (!HASHITEM_EMPTY(hi))
3047 return (buf_T *)(hi->hi_key
3048 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 return NULL;
3050}
3051
3052/*
3053 * Get name of file 'n' in the buffer list.
3054 * When the file has no name an empty string is returned.
3055 * home_replace() is used to shorten the file name (used for marks).
3056 * Returns a pointer to allocated memory, of NULL when failed.
3057 */
3058 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003059buflist_nr2name(
3060 int n,
3061 int fullname,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003062 int helptail) // for help buffers return tail only
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063{
3064 buf_T *buf;
3065
3066 buf = buflist_findnr(n);
3067 if (buf == NULL)
3068 return NULL;
3069 return home_replace_save(helptail ? buf : NULL,
3070 fullname ? buf->b_ffname : buf->b_fname);
3071}
3072
3073/*
3074 * Set the "lnum" and "col" for the buffer "buf" and the current window.
3075 * When "copy_options" is TRUE save the local window option values.
3076 * When "lnum" is 0 only do the options.
3077 */
Bram Moolenaardefa0672019-07-21 19:25:37 +02003078 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003079buflist_setfpos(
3080 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003081 win_T *win, // may be NULL when using :badd
Bram Moolenaar7454a062016-01-30 15:14:10 +01003082 linenr_T lnum,
3083 colnr_T col,
3084 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085{
3086 wininfo_T *wip;
3087
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003088 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 if (wip->wi_win == win)
3090 break;
3091 if (wip == NULL)
3092 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003093 // allocate a new entry
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003094 wip = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 if (wip == NULL)
3096 return;
3097 wip->wi_win = win;
Bram Moolenaarc667da52019-11-30 20:52:27 +01003098 if (lnum == 0) // set lnum even when it's 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 lnum = 1;
3100 }
3101 else
3102 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003103 // remove the entry from the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 if (wip->wi_prev)
3105 wip->wi_prev->wi_next = wip->wi_next;
3106 else
3107 buf->b_wininfo = wip->wi_next;
3108 if (wip->wi_next)
3109 wip->wi_next->wi_prev = wip->wi_prev;
3110 if (copy_options && wip->wi_optset)
3111 {
3112 clear_winopt(&wip->wi_opt);
3113#ifdef FEAT_FOLDING
3114 deleteFoldRecurse(&wip->wi_folds);
3115#endif
3116 }
3117 }
3118 if (lnum != 0)
3119 {
3120 wip->wi_fpos.lnum = lnum;
3121 wip->wi_fpos.col = col;
3122 }
LemonBoydb0ea7f2022-04-10 17:59:26 +01003123 if (win != NULL)
3124 wip->wi_changelistidx = win->w_changelistidx;
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003125 if (copy_options && win != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003127 // Save the window-specific option values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
3129#ifdef FEAT_FOLDING
3130 wip->wi_fold_manual = win->w_fold_manual;
3131 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
3132#endif
3133 wip->wi_optset = TRUE;
3134 }
3135
Bram Moolenaarc667da52019-11-30 20:52:27 +01003136 // insert the entry in front of the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 wip->wi_next = buf->b_wininfo;
3138 buf->b_wininfo = wip;
3139 wip->wi_prev = NULL;
3140 if (wip->wi_next)
3141 wip->wi_next->wi_prev = wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142}
3143
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003144#ifdef FEAT_DIFF
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003145/*
3146 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
3147 * page. That's because a diff is local to a tab page.
3148 */
3149 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003150wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003151{
3152 win_T *wp;
3153
3154 if (wip->wi_opt.wo_diff)
3155 {
Bram Moolenaar29323592016-07-24 22:04:11 +02003156 FOR_ALL_WINDOWS(wp)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003157 // return FALSE when it's a window in the current tab page, thus
3158 // the buffer was in diff mode here
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003159 if (wip->wi_win == wp)
3160 return FALSE;
3161 return TRUE;
3162 }
3163 return FALSE;
3164}
3165#endif
3166
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167/*
3168 * Find info for the current window in buffer "buf".
3169 * If not found, return the info for the most recently used window.
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003170 * When "need_options" is TRUE skip entries where wi_optset is FALSE.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003171 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
3172 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 * Returns NULL when there isn't any info.
3174 */
3175 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003176find_wininfo(
3177 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003178 int need_options,
Bram Moolenaar7454a062016-01-30 15:14:10 +01003179 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180{
3181 wininfo_T *wip;
3182
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003183 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003184 if (wip->wi_win == curwin
3185#ifdef FEAT_DIFF
3186 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
3187#endif
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003188
3189 && (!need_options || wip->wi_optset))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003191
Bram Moolenaarc667da52019-11-30 20:52:27 +01003192 // If no wininfo for curwin, use the first in the list (that doesn't have
3193 // 'diff' set and is in another tab page).
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003194 // If "need_options" is TRUE skip entries that don't have options set,
3195 // unless the window is editing "buf", so we can copy from the window
3196 // itself.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003197 if (wip == NULL)
3198 {
3199#ifdef FEAT_DIFF
3200 if (skip_diff_buffer)
3201 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003202 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003203 if (!wininfo_other_tab_diff(wip)
3204 && (!need_options || wip->wi_optset
3205 || (wip->wi_win != NULL
3206 && wip->wi_win->w_buffer == buf)))
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003207 break;
3208 }
3209 else
3210#endif
3211 wip = buf->b_wininfo;
3212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 return wip;
3214}
3215
3216/*
3217 * Reset the local window options to the values last used in this window.
3218 * If the buffer wasn't used in this window before, use the values from
3219 * the most recently used window. If the values were never set, use the
3220 * global values for the window.
3221 */
3222 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003223get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224{
3225 wininfo_T *wip;
3226
3227 clear_winopt(&curwin->w_onebuf_opt);
3228#ifdef FEAT_FOLDING
3229 clearFolding(curwin);
3230#endif
3231
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003232 wip = find_wininfo(buf, TRUE, TRUE);
Bram Moolenaar25782a72018-05-13 18:05:33 +02003233 if (wip != NULL && wip->wi_win != NULL
3234 && wip->wi_win != curwin && wip->wi_win->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003236 // The buffer is currently displayed in the window: use the actual
3237 // option values instead of the saved (possibly outdated) values.
Bram Moolenaar25782a72018-05-13 18:05:33 +02003238 win_T *wp = wip->wi_win;
3239
3240 copy_winopt(&wp->w_onebuf_opt, &curwin->w_onebuf_opt);
3241#ifdef FEAT_FOLDING
3242 curwin->w_fold_manual = wp->w_fold_manual;
3243 curwin->w_foldinvalid = TRUE;
3244 cloneFoldGrowArray(&wp->w_folds, &curwin->w_folds);
3245#endif
3246 }
3247 else if (wip != NULL && wip->wi_optset)
3248 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003249 // the buffer was displayed in the current window earlier
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
3251#ifdef FEAT_FOLDING
3252 curwin->w_fold_manual = wip->wi_fold_manual;
3253 curwin->w_foldinvalid = TRUE;
3254 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
3255#endif
3256 }
3257 else
3258 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
LemonBoydb0ea7f2022-04-10 17:59:26 +01003259 if (wip != NULL)
3260 curwin->w_changelistidx = wip->wi_changelistidx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261
3262#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +01003263 // Set 'foldlevel' to 'foldlevelstart' if it's not negative.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 if (p_fdls >= 0)
3265 curwin->w_p_fdl = p_fdls;
3266#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02003267 after_copy_winopt(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268}
3269
3270/*
3271 * Find the position (lnum and col) for the buffer 'buf' for the current
3272 * window.
3273 * Returns a pointer to no_position if no position is found.
3274 */
3275 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003276buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277{
3278 wininfo_T *wip;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003279 static pos_T no_position = {1, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003281 wip = find_wininfo(buf, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 if (wip != NULL)
3283 return &(wip->wi_fpos);
3284 else
3285 return &no_position;
3286}
3287
3288/*
3289 * Find the lnum for the buffer 'buf' for the current window.
3290 */
3291 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01003292buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293{
3294 return buflist_findfpos(buf)->lnum;
3295}
3296
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02003298 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003301buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302{
Bram Moolenaar52410572019-10-27 05:12:45 +01003303 buf_T *buf = firstbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 int len;
3305 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003306 int ro_char;
3307 int changed_char;
Bram Moolenaar0751f512018-03-29 16:37:16 +02003308#ifdef FEAT_TERMINAL
3309 int job_running;
3310 int job_none_open;
3311#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312
Bram Moolenaar52410572019-10-27 05:12:45 +01003313#ifdef FEAT_VIMINFO
3314 garray_T buflist;
3315 buf_T **buflist_data = NULL, **p;
3316
3317 if (vim_strchr(eap->arg, 't'))
3318 {
3319 ga_init2(&buflist, sizeof(buf_T *), 50);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003320 FOR_ALL_BUFFERS(buf)
Bram Moolenaar52410572019-10-27 05:12:45 +01003321 {
3322 if (ga_grow(&buflist, 1) == OK)
3323 ((buf_T **)buflist.ga_data)[buflist.ga_len++] = buf;
3324 }
3325
3326 qsort(buflist.ga_data, (size_t)buflist.ga_len,
3327 sizeof(buf_T *), buf_compare);
3328
Bram Moolenaar3b991522019-11-06 23:26:20 +01003329 buflist_data = (buf_T **)buflist.ga_data;
3330 buf = *buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003331 }
Bram Moolenaar3b991522019-11-06 23:26:20 +01003332 p = buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003333
Bram Moolenaar3b991522019-11-06 23:26:20 +01003334 for (; buf != NULL && !got_int; buf = buflist_data != NULL
Bram Moolenaar52410572019-10-27 05:12:45 +01003335 ? (++p < buflist_data + buflist.ga_len ? *p : NULL)
3336 : buf->b_next)
3337#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
Bram Moolenaar52410572019-10-27 05:12:45 +01003339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 {
Bram Moolenaar0751f512018-03-29 16:37:16 +02003341#ifdef FEAT_TERMINAL
3342 job_running = term_job_running(buf->b_term);
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003343 job_none_open = term_none_open(buf->b_term);
Bram Moolenaar0751f512018-03-29 16:37:16 +02003344#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003345 // skip unlisted buffers, unless ! was used
Bram Moolenaard51cb702015-07-21 15:03:06 +02003346 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
3347 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
3348 || (vim_strchr(eap->arg, '+')
3349 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
3350 || (vim_strchr(eap->arg, 'a')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003351 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
Bram Moolenaard51cb702015-07-21 15:03:06 +02003352 || (vim_strchr(eap->arg, 'h')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003353 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3354#ifdef FEAT_TERMINAL
3355 || (vim_strchr(eap->arg, 'R')
3356 && (!job_running || (job_running && job_none_open)))
3357 || (vim_strchr(eap->arg, '?')
3358 && (!job_running || (job_running && !job_none_open)))
3359 || (vim_strchr(eap->arg, 'F')
3360 && (job_running || buf->b_term == NULL))
3361#endif
Bram Moolenaard51cb702015-07-21 15:03:06 +02003362 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3363 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3364 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3365 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3366 || (vim_strchr(eap->arg, '#')
3367 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003370 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 else
3372 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003373 if (message_filtered(NameBuff))
3374 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003376 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3377 : (bufIsChanged(buf) ? '+' : ' ');
3378#ifdef FEAT_TERMINAL
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003379 if (job_running)
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003380 {
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003381 if (job_none_open)
Bram Moolenaar4033c552017-09-16 20:54:51 +02003382 ro_char = '?';
3383 else
3384 ro_char = 'R';
Bram Moolenaarc667da52019-11-30 20:52:27 +01003385 changed_char = ' '; // bufIsChanged() returns TRUE to avoid
3386 // closing, but it's not actually changed.
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003387 }
3388 else if (buf->b_term != NULL)
3389 ro_char = 'F';
3390 else
3391#endif
3392 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3393
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003394 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00003395 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 buf->b_fnum,
3397 buf->b_p_bl ? ' ' : 'u',
3398 buf == curbuf ? '%' :
3399 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3400 buf->b_ml.ml_mfp == NULL ? ' ' :
3401 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003402 ro_char,
3403 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003404 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003405 if (len > IOSIZE - 20)
3406 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407
Bram Moolenaarc667da52019-11-30 20:52:27 +01003408 // put "line 999" in column 40 or after the file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 i = 40 - vim_strsize(IObuff);
3410 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 IObuff[len++] = ' ';
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003412 while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar52410572019-10-27 05:12:45 +01003413#ifdef FEAT_VIMINFO
3414 if (vim_strchr(eap->arg, 't') && buf->b_last_used)
3415 add_time(IObuff + len, (size_t)(IOSIZE - len), buf->b_last_used);
3416 else
3417#endif
3418 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3419 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003420 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 msg_outtrans(IObuff);
Bram Moolenaarc667da52019-11-30 20:52:27 +01003422 out_flush(); // output one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 ui_breakcheck();
3424 }
Bram Moolenaar52410572019-10-27 05:12:45 +01003425
3426#ifdef FEAT_VIMINFO
3427 if (buflist_data)
3428 ga_clear(&buflist);
3429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431
3432/*
3433 * Get file name and line number for file 'fnum'.
3434 * Used by DoOneCmd() for translating '%' and '#'.
3435 * Used by insert_reg() and cmdline_paste() for '#' register.
3436 * Return FAIL if not found, OK for success.
3437 */
3438 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003439buflist_name_nr(
3440 int fnum,
3441 char_u **fname,
3442 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443{
3444 buf_T *buf;
3445
3446 buf = buflist_findnr(fnum);
3447 if (buf == NULL || buf->b_fname == NULL)
3448 return FAIL;
3449
3450 *fname = buf->b_fname;
3451 *lnum = buflist_findlnum(buf);
3452
3453 return OK;
3454}
3455
3456/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003457 * Set the file name for "buf"' to "ffname_arg", short file name to
3458 * "sfname_arg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 * The file name with the full path is also remembered, for when :cd is used.
3460 * Returns FAIL for failure (file name already in use by other buffer)
3461 * OK otherwise.
3462 */
3463 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003464setfname(
3465 buf_T *buf,
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003466 char_u *ffname_arg,
3467 char_u *sfname_arg,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003468 int message) // give message when buffer already exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003470 char_u *ffname = ffname_arg;
3471 char_u *sfname = sfname_arg;
Bram Moolenaar81695252004-12-29 20:58:21 +00003472 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003474 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475#endif
3476
3477 if (ffname == NULL || *ffname == NUL)
3478 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003479 // Removing the name.
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003480 if (buf->b_sfname != buf->b_ffname)
3481 VIM_CLEAR(buf->b_sfname);
3482 else
3483 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003484 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485#ifdef UNIX
3486 st.st_dev = (dev_T)-1;
3487#endif
3488 }
3489 else
3490 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003491 fname_expand(buf, &ffname, &sfname); // will allocate ffname
3492 if (ffname == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 return FAIL;
3494
3495 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01003496 * If the file name is already used in another buffer:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 * - if the buffer is loaded, fail
3498 * - if the buffer is not loaded, delete it from the list
3499 */
3500#ifdef UNIX
3501 if (mch_stat((char *)ffname, &st) < 0)
3502 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003503#endif
3504 if (!(buf->b_flags & BF_DUMMY))
3505#ifdef UNIX
3506 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003508 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509#endif
3510 if (obuf != NULL && obuf != buf)
3511 {
Bram Moolenaard3710cf2021-10-04 23:13:13 +01003512 win_T *win;
3513 tabpage_T *tab;
3514 int in_use = FALSE;
3515
3516 // during startup a window may use a buffer that is not loaded yet
3517 FOR_ALL_TAB_WINDOWS(tab, win)
3518 if (win->w_buffer == obuf)
3519 in_use = TRUE;
3520
3521 // it's loaded or used in a window, fail
3522 if (obuf->b_ml.ml_mfp != NULL || in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 {
3524 if (message)
Bram Moolenaare1242042021-12-16 20:56:57 +00003525 emsg(_(e_buffer_with_this_name_already_exists));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 vim_free(ffname);
3527 return FAIL;
3528 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003529 // delete from the list
Bram Moolenaara6e8f882019-12-14 16:18:15 +01003530 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 }
3532 sfname = vim_strsave(sfname);
3533 if (ffname == NULL || sfname == NULL)
3534 {
3535 vim_free(sfname);
3536 vim_free(ffname);
3537 return FAIL;
3538 }
3539#ifdef USE_FNAME_CASE
Bram Moolenaarc667da52019-11-30 20:52:27 +01003540 fname_case(sfname, 0); // set correct case for short file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541#endif
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003542 if (buf->b_sfname != buf->b_ffname)
3543 vim_free(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 vim_free(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 buf->b_ffname = ffname;
3546 buf->b_sfname = sfname;
3547 }
3548 buf->b_fname = buf->b_sfname;
3549#ifdef UNIX
3550 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003551 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 else
3553 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003554 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 buf->b_dev = st.st_dev;
3556 buf->b_ino = st.st_ino;
3557 }
3558#endif
3559
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561
3562 buf_name_changed(buf);
3563 return OK;
3564}
3565
3566/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003567 * Crude way of changing the name of a buffer. Use with care!
3568 * The name should be relative to the current directory.
3569 */
3570 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003571buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003572{
3573 buf_T *buf;
3574
3575 buf = buflist_findnr(fnum);
3576 if (buf != NULL)
3577 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003578 if (buf->b_sfname != buf->b_ffname)
3579 vim_free(buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003580 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003581 buf->b_ffname = vim_strsave(name);
3582 buf->b_sfname = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01003583 // Allocate ffname and expand into full path. Also resolves .lnk
3584 // files on Win32.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003585 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003586 buf->b_fname = buf->b_sfname;
3587 }
3588}
3589
3590/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 * Take care of what needs to be done when the name of buffer "buf" has
3592 * changed.
3593 */
3594 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003595buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596{
3597 /*
3598 * If the file name changed, also change the name of the swapfile
3599 */
3600 if (buf->b_ml.ml_mfp != NULL)
3601 ml_setname(buf);
3602
Bram Moolenaar3ad69532021-11-19 17:01:08 +00003603#ifdef FEAT_TERMINAL
3604 if (buf->b_term != NULL)
3605 term_clear_status_text(buf->b_term);
3606#endif
3607
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 if (curwin->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003609 check_arg_idx(curwin); // check file name for arg list
Bram Moolenaarc667da52019-11-30 20:52:27 +01003610 maketitle(); // set window title
Bram Moolenaarc667da52019-11-30 20:52:27 +01003611 status_redraw_all(); // status lines need to be redrawn
3612 fmarks_check_names(buf); // check named file marks
3613 ml_timestamp(buf); // reset timestamp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614}
3615
3616/*
3617 * set alternate file name for current window
3618 *
3619 * Used by do_one_cmd(), do_write() and do_ecmd().
3620 * Return the buffer.
3621 */
3622 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003623setaltfname(
3624 char_u *ffname,
3625 char_u *sfname,
3626 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627{
3628 buf_T *buf;
3629
Bram Moolenaarc667da52019-11-30 20:52:27 +01003630 // Create a buffer. 'buflisted' is not set if it's a new buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaare1004402020-10-24 20:49:43 +02003632 if (buf != NULL && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 curwin->w_alt_fnum = buf->b_fnum;
3634 return buf;
3635}
3636
3637/*
3638 * Get alternate file name for current window.
3639 * Return NULL if there isn't any, and give error message if requested.
3640 */
3641 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003642getaltfname(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003643 int errmsg) // give error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644{
3645 char_u *fname;
3646 linenr_T dummy;
3647
3648 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3649 {
3650 if (errmsg)
Bram Moolenaar108010a2021-06-27 22:03:33 +02003651 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 return NULL;
3653 }
3654 return fname;
3655}
3656
3657/*
3658 * Add a file name to the buflist and return its number.
3659 * Uses same flags as buflist_new(), except BLN_DUMMY.
3660 *
3661 * used by qf_init(), main() and doarglist()
3662 */
3663 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003664buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665{
3666 buf_T *buf;
3667
3668 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3669 if (buf != NULL)
3670 return buf->b_fnum;
3671 return 0;
3672}
3673
3674#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3675/*
3676 * Adjust slashes in file names. Called after 'shellslash' was set.
3677 */
3678 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003679buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680{
3681 buf_T *bp;
3682
Bram Moolenaar29323592016-07-24 22:04:11 +02003683 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 {
3685 if (bp->b_ffname != NULL)
3686 slash_adjust(bp->b_ffname);
3687 if (bp->b_sfname != NULL)
3688 slash_adjust(bp->b_sfname);
3689 }
3690}
3691#endif
3692
3693/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003694 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 * Also save the local window option values.
3696 */
3697 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003698buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003700 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701}
3702
3703/*
3704 * Return TRUE if 'ffname' is not the same file as current file.
3705 * Fname must have a full path (expanded by mch_FullName()).
3706 */
3707 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003708otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709{
3710 return otherfile_buf(curbuf, ffname
3711#ifdef UNIX
3712 , NULL
3713#endif
3714 );
3715}
3716
3717 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003718otherfile_buf(
3719 buf_T *buf,
3720 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003722 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003724 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725{
Bram Moolenaarc667da52019-11-30 20:52:27 +01003726 // no name is different
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3728 return TRUE;
3729 if (fnamecmp(ffname, buf->b_ffname) == 0)
3730 return FALSE;
3731#ifdef UNIX
3732 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003733 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734
Bram Moolenaarc667da52019-11-30 20:52:27 +01003735 // If no stat_T given, get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 if (stp == NULL)
3737 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003738 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 st.st_dev = (dev_T)-1;
3740 stp = &st;
3741 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003742 // Use dev/ino to check if the files are the same, even when the names
3743 // are different (possible with links). Still need to compare the
3744 // name above, for when the file doesn't exist yet.
3745 // Problem: The dev/ino changes when a file is deleted (and created
3746 // again) and remains the same when renamed/moved. We don't want to
3747 // mch_stat() each buffer each time, that would be too slow. Get the
3748 // dev/ino again when they appear to match, but not when they appear
3749 // to be different: Could skip a buffer when it's actually the same
3750 // file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 if (buf_same_ino(buf, stp))
3752 {
3753 buf_setino(buf);
3754 if (buf_same_ino(buf, stp))
3755 return FALSE;
3756 }
3757 }
3758#endif
3759 return TRUE;
3760}
3761
3762#if defined(UNIX) || defined(PROTO)
3763/*
3764 * Set inode and device number for a buffer.
3765 * Must always be called when b_fname is changed!.
3766 */
3767 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003768buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003770 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771
3772 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3773 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003774 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 buf->b_dev = st.st_dev;
3776 buf->b_ino = st.st_ino;
3777 }
3778 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003779 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780}
3781
3782/*
3783 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3784 */
3785 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003786buf_same_ino(
3787 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003788 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003790 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 && stp->st_dev == buf->b_dev
3792 && stp->st_ino == buf->b_ino);
3793}
3794#endif
3795
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003796/*
3797 * Print info about the current buffer.
3798 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003800fileinfo(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003801 int fullname, // when non-zero print full path
Bram Moolenaar7454a062016-01-30 15:14:10 +01003802 int shorthelp,
3803 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804{
3805 char_u *name;
3806 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003807 char *p;
3808 char *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003809 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003811 buffer = alloc(IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 if (buffer == NULL)
3813 return;
3814
Bram Moolenaarc667da52019-11-30 20:52:27 +01003815 if (fullname > 1) // 2 CTRL-G: include buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003817 vim_snprintf(buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 p = buffer + STRLEN(buffer);
3819 }
3820 else
3821 p = buffer;
3822
3823 *p++ = '"';
3824 if (buf_spname(curbuf) != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003825 vim_strncpy((char_u *)p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 else
3827 {
3828 if (!fullname && curbuf->b_fname != NULL)
3829 name = curbuf->b_fname;
3830 else
3831 name = curbuf->b_ffname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003832 home_replace(shorthelp ? curbuf : NULL, name, (char_u *)p,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 (int)(IOSIZE - (p - buffer)), TRUE);
3834 }
3835
Bram Moolenaar32526b32019-01-19 17:43:09 +01003836 vim_snprintf_add(buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 curbufIsChanged() ? (shortmess(SHM_MOD)
3838 ? " [+]" : _(" [Modified]")) : " ",
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01003839 (curbuf->b_flags & BF_NOTEDITED) && !bt_dontwrite(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 ? _("[Not edited]") : "",
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01003841 (curbuf->b_flags & BF_NEW) && !bt_dontwrite(curbuf)
Bram Moolenaar722e5052020-06-12 22:31:00 +02003842 ? new_file_message() : "",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003844 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 : _("[readonly]")) : "",
3846 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3847 || curbuf->b_p_ro) ?
3848 " " : "");
Bram Moolenaarc667da52019-11-30 20:52:27 +01003849 // With 32 bit longs and more than 21,474,836 lines multiplying by 100
3850 // causes an overflow, thus for large numbers divide instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 if (curwin->w_cursor.lnum > 1000000L)
3852 n = (int)(((long)curwin->w_cursor.lnum) /
3853 ((long)curbuf->b_ml.ml_line_count / 100L));
3854 else
3855 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3856 (long)curbuf->b_ml.ml_line_count);
3857 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003858 vim_snprintf_add(buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 else if (p_ru)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003860 // Current line and column are already on the screen -- webb
Bram Moolenaar32526b32019-01-19 17:43:09 +01003861 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003862 NGETTEXT("%ld line --%d%%--", "%ld lines --%d%%--",
3863 curbuf->b_ml.ml_line_count),
3864 (long)curbuf->b_ml.ml_line_count, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 else
3866 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003867 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 _("line %ld of %ld --%d%%-- col "),
3869 (long)curwin->w_cursor.lnum,
3870 (long)curbuf->b_ml.ml_line_count,
3871 n);
3872 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003873 len = STRLEN(buffer);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003874 col_print((char_u *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3876 }
3877
Bram Moolenaar32526b32019-01-19 17:43:09 +01003878 (void)append_arg_number(curwin, (char_u *)buffer, IOSIZE,
3879 !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880
3881 if (dont_truncate)
3882 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003883 // Temporarily set msg_scroll to avoid the message being truncated.
3884 // First call msg_start() to get the message in the right place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 msg_start();
3886 n = msg_scroll;
3887 msg_scroll = TRUE;
3888 msg(buffer);
3889 msg_scroll = n;
3890 }
3891 else
3892 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003893 p = msg_trunc_attr(buffer, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaarc667da52019-11-30 20:52:27 +01003895 // Need to repeat the message after redrawing when:
3896 // - When restart_edit is set (otherwise there will be a delay
3897 // before redrawing).
3898 // - When the screen was scrolled but there is no wait-return
3899 // prompt.
Bram Moolenaar32526b32019-01-19 17:43:09 +01003900 set_keep_msg((char_u *)p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 }
3902
3903 vim_free(buffer);
3904}
3905
3906 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003907col_print(
3908 char_u *buf,
3909 size_t buflen,
3910 int col,
3911 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912{
3913 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003914 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003916 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917}
3918
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919static char_u *lasttitle = NULL;
3920static char_u *lasticon = NULL;
3921
Bram Moolenaar84a93082018-06-16 22:58:15 +02003922/*
3923 * Put the file name in the title bar and icon of the window.
3924 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003926maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927{
3928 char_u *p;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003929 char_u *title_str = NULL;
3930 char_u *icon_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 int maxlen = 0;
3932 int len;
3933 int mustset;
3934 char_u buf[IOSIZE];
3935 int off;
3936
3937 if (!redrawing())
3938 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003939 // Postpone updating the title when 'lazyredraw' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 need_maketitle = TRUE;
3941 return;
3942 }
3943
3944 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003945 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003946 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947
3948 if (p_title)
3949 {
3950 if (p_titlelen > 0)
3951 {
3952 maxlen = p_titlelen * Columns / 100;
3953 if (maxlen < 10)
3954 maxlen = 10;
3955 }
3956
Bram Moolenaar84a93082018-06-16 22:58:15 +02003957 title_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 if (*p_titlestring != NUL)
3959 {
3960#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003961 if (stl_syntax & STL_IN_TITLE)
3962 {
3963 int use_sandbox = FALSE;
Bram Moolenaar53989552019-12-23 22:59:18 +01003964 int called_emsg_before = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003965
3966# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003967 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003968# endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003969 build_stl_str_hl(curwin, title_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003970 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003971 0, maxlen, NULL, NULL);
Bram Moolenaar53989552019-12-23 22:59:18 +01003972 if (called_emsg > called_emsg_before)
Bram Moolenaard3667a22006-03-16 21:35:52 +00003973 set_string_option_direct((char_u *)"titlestring", -1,
3974 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 else
3977#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003978 title_str = p_titlestring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 }
3980 else
3981 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003982 // format: "fname + (path) (1 of 2) - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983
Bram Moolenaar2c666692012-09-05 13:30:40 +02003984#define SPACE_FOR_FNAME (IOSIZE - 100)
3985#define SPACE_FOR_DIR (IOSIZE - 20)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003986#define SPACE_FOR_ARGNR (IOSIZE - 10) // at least room for " - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003988 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar21554412017-07-24 21:44:43 +02003989#ifdef FEAT_TERMINAL
3990 else if (curbuf->b_term != NULL)
3991 {
3992 vim_strncpy(buf, term_get_status_text(curbuf->b_term),
3993 SPACE_FOR_FNAME);
3994 }
3995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 else
3997 {
3998 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003999 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 }
4002
Bram Moolenaar21554412017-07-24 21:44:43 +02004003#ifdef FEAT_TERMINAL
4004 if (curbuf->b_term == NULL)
4005#endif
4006 switch (bufIsChanged(curbuf)
4007 + (curbuf->b_p_ro * 2)
4008 + (!curbuf->b_p_ma * 4))
4009 {
4010 case 1: STRCAT(buf, " +"); break;
4011 case 2: STRCAT(buf, " ="); break;
4012 case 3: STRCAT(buf, " =+"); break;
4013 case 4:
4014 case 6: STRCAT(buf, " -"); break;
4015 case 5:
4016 case 7: STRCAT(buf, " -+"); break;
4017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018
Bram Moolenaar21554412017-07-24 21:44:43 +02004019 if (curbuf->b_fname != NULL
4020#ifdef FEAT_TERMINAL
4021 && curbuf->b_term == NULL
4022#endif
4023 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004025 // Get path of file, replace home dir with ~
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 off = (int)STRLEN(buf);
4027 buf[off++] = ' ';
4028 buf[off++] = '(';
4029 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02004030 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc667da52019-11-30 20:52:27 +01004032 // avoid "c:/name" to be reduced to "c"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 if (isalpha(buf[off]) && buf[off + 1] == ':')
4034 off += 2;
4035#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01004036 // remove the file name
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004037 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 if (p == buf + off)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02004039 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004040 // must be a help buffer
Bram Moolenaar21554412017-07-24 21:44:43 +02004041 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02004042 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02004043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004046
Bram Moolenaarc667da52019-11-30 20:52:27 +01004047 // Translate unprintable chars and concatenate. Keep some
4048 // room for the server name. When there is no room (very long
4049 // file name) use (...).
Bram Moolenaar2c666692012-09-05 13:30:40 +02004050 if (off < SPACE_FOR_DIR)
4051 {
4052 p = transstr(buf + off);
4053 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
4054 vim_free(p);
4055 }
4056 else
4057 {
4058 vim_strncpy(buf + off, (char_u *)"...",
4059 (size_t)(SPACE_FOR_ARGNR - off));
4060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 STRCAT(buf, ")");
4062 }
4063
Bram Moolenaar2c666692012-09-05 13:30:40 +02004064 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065
4066#if defined(FEAT_CLIENTSERVER)
4067 if (serverName != NULL)
4068 {
4069 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02004070 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 }
4072 else
4073#endif
4074 STRCAT(buf, " - VIM");
4075
4076 if (maxlen > 0)
4077 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004078 // make it shorter by removing a bit in the middle
Bram Moolenaarf31b7642012-01-20 20:44:43 +01004079 if (vim_strsize(buf) > maxlen)
4080 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 }
4082 }
4083 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02004084 mustset = value_changed(title_str, &lasttitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085
4086 if (p_icon)
4087 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02004088 icon_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 if (*p_iconstring != NUL)
4090 {
4091#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00004092 if (stl_syntax & STL_IN_ICON)
4093 {
4094 int use_sandbox = FALSE;
Bram Moolenaar53989552019-12-23 22:59:18 +01004095 int called_emsg_before = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004096
4097# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00004098 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004099# endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02004100 build_stl_str_hl(curwin, icon_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004101 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004102 0, 0, NULL, NULL);
Bram Moolenaar53989552019-12-23 22:59:18 +01004103 if (called_emsg > called_emsg_before)
Bram Moolenaard3667a22006-03-16 21:35:52 +00004104 set_string_option_direct((char_u *)"iconstring", -1,
4105 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaard3667a22006-03-16 21:35:52 +00004106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 else
4108#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02004109 icon_str = p_iconstring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 }
4111 else
4112 {
4113 if (buf_spname(curbuf) != NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004114 p = buf_spname(curbuf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004115 else // use file name only in icon
Bram Moolenaar84a93082018-06-16 22:58:15 +02004116 p = gettail(curbuf->b_ffname);
4117 *icon_str = NUL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004118 // Truncate name at 100 bytes.
Bram Moolenaar84a93082018-06-16 22:58:15 +02004119 len = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 if (len > 100)
4121 {
4122 len -= 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 if (has_mbyte)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004124 len += (*mb_tail_off)(p, p + len) + 1;
Bram Moolenaar84a93082018-06-16 22:58:15 +02004125 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02004127 STRCPY(icon_str, p);
4128 trans_characters(icon_str, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 }
4130 }
4131
Bram Moolenaar84a93082018-06-16 22:58:15 +02004132 mustset |= value_changed(icon_str, &lasticon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133
4134 if (mustset)
4135 resettitle();
4136}
4137
4138/*
4139 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
4140 * from "str" if it does.
Bram Moolenaar84a93082018-06-16 22:58:15 +02004141 * Return TRUE if resettitle() is to be called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 */
4143 static int
Bram Moolenaar84a93082018-06-16 22:58:15 +02004144value_changed(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145{
4146 if ((str == NULL) != (*last == NULL)
4147 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
4148 {
4149 vim_free(*last);
4150 if (str == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004151 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 *last = NULL;
Bram Moolenaar40385db2018-08-07 22:31:44 +02004153 mch_restore_title(
4154 last == &lasttitle ? SAVE_RESTORE_TITLE : SAVE_RESTORE_ICON);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 else
Bram Moolenaar84a93082018-06-16 22:58:15 +02004157 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 *last = vim_strsave(str);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004159 return TRUE;
4160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 }
4162 return FALSE;
4163}
4164
4165/*
4166 * Put current window title back (used after calling a shell)
4167 */
4168 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004169resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170{
4171 mch_settitle(lasttitle, lasticon);
4172}
Bram Moolenaarea408852005-06-25 22:49:46 +00004173
4174# if defined(EXITFREE) || defined(PROTO)
4175 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004176free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00004177{
4178 vim_free(lasttitle);
4179 vim_free(lasticon);
4180}
4181# endif
4182
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004184#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004185
4186/*
4187 * Used for building in the status line.
4188 */
4189typedef struct
4190{
4191 char_u *stl_start;
4192 int stl_minwid;
4193 int stl_maxwid;
4194 enum {
4195 Normal,
4196 Empty,
4197 Group,
4198 Middle,
4199 Highlight,
4200 TabPage,
4201 Trunc
4202 } stl_type;
4203} stl_item_T;
4204
4205static size_t stl_items_len = 20; // Initial value, grows as needed.
4206static stl_item_T *stl_items = NULL;
4207static int *stl_groupitem = NULL;
4208static stl_hlrec_T *stl_hltab = NULL;
4209static stl_hlrec_T *stl_tabtab = NULL;
4210
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004212 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 * Return length of string in screen cells.
4214 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004215 * Normally works for window "wp", except when working for 'tabline' then it
4216 * is "curwin".
4217 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 * Items are drawn interspersed with the text that surrounds it
4219 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
4220 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
4221 *
4222 * If maxwidth is not zero, the string will be filled at any middle marker
4223 * or truncated if too long, fillchar is used for all whitespace.
4224 */
4225 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004226build_stl_str_hl(
4227 win_T *wp,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004228 char_u *out, // buffer to write into != NameBuff
4229 size_t outlen, // length of out[]
Bram Moolenaar7454a062016-01-30 15:14:10 +01004230 char_u *fmt,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004231 int use_sandbox UNUSED, // "fmt" was set insecurely, use sandbox
Bram Moolenaar7454a062016-01-30 15:14:10 +01004232 int fillchar,
4233 int maxwidth,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004234 stl_hlrec_T **hltab, // return: HL attributes (can be NULL)
4235 stl_hlrec_T **tabtab) // return: tab page nrs (can be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236{
Bram Moolenaar10772302019-01-20 18:25:54 +01004237 linenr_T lnum;
4238 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 char_u *p;
4240 char_u *s;
4241 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004242 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243#ifdef FEAT_EVAL
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004244 win_T *save_curwin;
4245 buf_T *save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004246 int save_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247#endif
4248 int empty_line;
4249 colnr_T virtcol;
4250 long l;
4251 long n;
4252 int prevchar_isflag;
4253 int prevchar_isitem;
4254 int itemisflag;
4255 int fillable;
4256 char_u *str;
4257 long num;
4258 int width;
4259 int itemcnt;
4260 int curitem;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004261 int group_end_userhl;
4262 int group_start_userhl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 int groupdepth;
shadmansaleh30e3de22021-05-15 17:23:28 +02004264#ifdef FEAT_EVAL
4265 int evaldepth;
4266#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 int minwid;
4268 int maxwid;
4269 int zeropad;
4270 char_u base;
4271 char_u opt;
4272#define TMPLEN 70
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004273 char_u buf_tmp[TMPLEN];
4274 char_u win_tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004275 char_u *usefmt = fmt;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004276 stl_hlrec_T *sp;
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01004277 int save_redraw_not_allowed = redraw_not_allowed;
Bram Moolenaar0e1f36f2022-02-15 16:17:44 +00004278 int save_KeyTyped = KeyTyped;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004279
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01004280 // When inside update_screen() we do not want redrawing a statusline,
4281 // ruler, title, etc. to trigger another redraw, it may cause an endless
4282 // loop.
4283 if (updating_screen)
4284 redraw_not_allowed = TRUE;
4285
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004286 if (stl_items == NULL)
4287 {
4288 stl_items = ALLOC_MULT(stl_item_T, stl_items_len);
4289 stl_groupitem = ALLOC_MULT(int, stl_items_len);
Brandon Richardsona493b652022-02-19 11:45:03 +00004290
4291 // Allocate one more, because the last element is used to indicate the
4292 // end of the list.
4293 stl_hltab = ALLOC_MULT(stl_hlrec_T, stl_items_len + 1);
4294 stl_tabtab = ALLOC_MULT(stl_hlrec_T, stl_items_len + 1);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004295 }
4296
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004297#ifdef FEAT_EVAL
4298 /*
4299 * When the format starts with "%!" then evaluate it as an expression and
4300 * use the result as the actual format string.
4301 */
4302 if (fmt[0] == '%' && fmt[1] == '!')
4303 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004304 typval_T tv;
4305
4306 tv.v_type = VAR_NUMBER;
4307 tv.vval.v_number = wp->w_id;
4308 set_var((char_u *)"g:statusline_winid", &tv, FALSE);
4309
Bram Moolenaara4e0b972022-10-01 19:43:52 +01004310 usefmt = eval_to_string_safe(fmt + 2, use_sandbox, FALSE, FALSE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004311 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00004312 usefmt = fmt;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004313
4314 do_unlet((char_u *)"g:statusline_winid", TRUE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004315 }
4316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317
4318 if (fillchar == 0)
4319 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004320
Bram Moolenaar10772302019-01-20 18:25:54 +01004321 // The cursor in windows other than the current one isn't always
4322 // up-to-date, esp. because of autocommands and timers.
4323 lnum = wp->w_cursor.lnum;
4324 if (lnum > wp->w_buffer->b_ml.ml_line_count)
4325 {
4326 lnum = wp->w_buffer->b_ml.ml_line_count;
4327 wp->w_cursor.lnum = lnum;
4328 }
4329
4330 // Get line & check if empty (cursorpos will show "0-1"). Note that
4331 // p will become invalid when getting another buffer line.
4332 p = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar567199b2013-04-24 16:52:36 +02004333 empty_line = (*p == NUL);
4334
Bram Moolenaar10772302019-01-20 18:25:54 +01004335 // Get the byte value now, in case we need it below. This is more efficient
4336 // than making a copy of the line.
4337 len = STRLEN(p);
4338 if (wp->w_cursor.col > (colnr_T)len)
4339 {
4340 // Line may have changed since checking the cursor column, or the lnum
4341 // was adjusted above.
4342 wp->w_cursor.col = (colnr_T)len;
Bram Moolenaar10772302019-01-20 18:25:54 +01004343 wp->w_cursor.coladd = 0;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004344 byteval = 0;
Bram Moolenaar10772302019-01-20 18:25:54 +01004345 }
Bram Moolenaar567199b2013-04-24 16:52:36 +02004346 else
Bram Moolenaar567199b2013-04-24 16:52:36 +02004347 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348
4349 groupdepth = 0;
shadmansaleh30e3de22021-05-15 17:23:28 +02004350#ifdef FEAT_EVAL
4351 evaldepth = 0;
4352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 p = out;
4354 curitem = 0;
4355 prevchar_isflag = TRUE;
4356 prevchar_isitem = FALSE;
zeertzjq122dea72022-07-27 15:48:45 +01004357 for (s = usefmt; *s != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004359 if (curitem == (int)stl_items_len)
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004360 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004361 size_t new_len = stl_items_len * 3 / 2;
4362 stl_item_T *new_items;
4363 int *new_groupitem;
4364 stl_hlrec_T *new_hlrec;
4365
4366 new_items = vim_realloc(stl_items, sizeof(stl_item_T) * new_len);
4367 if (new_items == NULL)
4368 break;
4369 stl_items = new_items;
4370 new_groupitem = vim_realloc(stl_groupitem, sizeof(int) * new_len);
4371 if (new_groupitem == NULL)
4372 break;
4373 stl_groupitem = new_groupitem;
Brandon Richardsona493b652022-02-19 11:45:03 +00004374 new_hlrec = vim_realloc(stl_hltab,
4375 sizeof(stl_hlrec_T) * (new_len + 1));
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004376 if (new_hlrec == NULL)
4377 break;
4378 stl_hltab = new_hlrec;
Brandon Richardsona493b652022-02-19 11:45:03 +00004379 new_hlrec = vim_realloc(stl_tabtab,
4380 sizeof(stl_hlrec_T) * (new_len + 1));
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004381 if (new_hlrec == NULL)
4382 break;
4383 stl_tabtab = new_hlrec;
4384 stl_items_len = new_len;
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004385 }
4386
zeertzjq122dea72022-07-27 15:48:45 +01004387 if (*s != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 prevchar_isflag = prevchar_isitem = FALSE;
4389
4390 /*
4391 * Handle up to the next '%' or the end.
4392 */
4393 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
4394 *p++ = *s++;
4395 if (*s == NUL || p + 1 >= out + outlen)
4396 break;
4397
4398 /*
4399 * Handle one '%' item.
4400 */
4401 s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004402 if (*s == NUL) // ignore trailing %
Bram Moolenaar1d87f512011-02-01 21:55:01 +01004403 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 if (*s == '%')
4405 {
4406 if (p + 1 >= out + outlen)
4407 break;
4408 *p++ = *s++;
4409 prevchar_isflag = prevchar_isitem = FALSE;
4410 continue;
4411 }
4412 if (*s == STL_MIDDLEMARK)
4413 {
4414 s++;
4415 if (groupdepth > 0)
4416 continue;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004417 stl_items[curitem].stl_type = Middle;
4418 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 continue;
4420 }
4421 if (*s == STL_TRUNCMARK)
4422 {
4423 s++;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004424 stl_items[curitem].stl_type = Trunc;
4425 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 continue;
4427 }
4428 if (*s == ')')
4429 {
4430 s++;
4431 if (groupdepth < 1)
4432 continue;
4433 groupdepth--;
4434
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004435 t = stl_items[stl_groupitem[groupdepth]].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 *p = NUL;
4437 l = vim_strsize(t);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004438 if (curitem > stl_groupitem[groupdepth] + 1
4439 && stl_items[stl_groupitem[groupdepth]].stl_minwid == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004441 // remove group if all items are empty and highlight group
4442 // doesn't change
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004443 group_start_userhl = group_end_userhl = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004444 for (n = stl_groupitem[groupdepth] - 1; n >= 0; n--)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004445 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004446 if (stl_items[n].stl_type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004447 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004448 group_start_userhl = group_end_userhl =
4449 stl_items[n].stl_minwid;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004450 break;
4451 }
4452 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004453 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004454 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004455 if (stl_items[n].stl_type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 break;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004457 if (stl_items[n].stl_type == Highlight)
4458 group_end_userhl = stl_items[n].stl_minwid;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004459 }
4460 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 {
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004462 // empty group
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 p = t;
4464 l = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004465 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004466 {
4467 // do not use the highlighting from the removed group
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004468 if (stl_items[n].stl_type == Highlight)
4469 stl_items[n].stl_type = Empty;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004470 // adjust the start position of TabPage to the next
4471 // item position
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004472 if (stl_items[n].stl_type == TabPage)
4473 stl_items[n].stl_start = p;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 }
4476 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004477 if (l > stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004479 // truncate, remove n bytes of text at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 if (has_mbyte)
4481 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004482 // Find the first character that should be included.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 n = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004484 while (l >= stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 {
4486 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004487 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 }
4489 }
4490 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004491 n = (long)(p - t) - stl_items[stl_groupitem[groupdepth]]
4492 .stl_maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493
4494 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004495 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 p = p - n + 1;
Bram Moolenaar13505972019-01-24 15:04:48 +01004497
4498 // Fill up space left over by half a double-wide char.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004499 while (++l < stl_items[stl_groupitem[groupdepth]].stl_minwid)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004500 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501
Bram Moolenaarc667da52019-11-30 20:52:27 +01004502 // correct the start of the items for the truncation
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004503 for (l = stl_groupitem[groupdepth] + 1; l < curitem; l++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 {
LemonBoy57ff5262022-05-09 21:03:47 +01004505 // Minus one for the leading '<' added above.
4506 stl_items[l].stl_start -= n - 1;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004507 if (stl_items[l].stl_start < t)
4508 stl_items[l].stl_start = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 }
4510 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004511 else if (abs(stl_items[stl_groupitem[groupdepth]].stl_minwid) > l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004513 // fill
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004514 n = stl_items[stl_groupitem[groupdepth]].stl_minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 if (n < 0)
4516 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004517 // fill by appending characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 n = 0 - n;
4519 while (l++ < n && p + 1 < out + outlen)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004520 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 }
4522 else
4523 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004524 // fill by inserting characters
Bram Moolenaar008bff92021-03-04 21:55:58 +01004525 l = (n - l) * MB_CHAR2LEN(fillchar);
4526 mch_memmove(t + l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004528 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 p += l;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004530 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
4531 stl_items[n].stl_start += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 for ( ; l > 0; l--)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004533 MB_CHAR2BYTES(fillchar, t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 }
4535 }
4536 continue;
4537 }
4538 minwid = 0;
4539 maxwid = 9999;
4540 zeropad = FALSE;
4541 l = 1;
4542 if (*s == '0')
4543 {
4544 s++;
4545 zeropad = TRUE;
4546 }
4547 if (*s == '-')
4548 {
4549 s++;
4550 l = -1;
4551 }
4552 if (VIM_ISDIGIT(*s))
4553 {
4554 minwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004555 if (minwid < 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 minwid = 0;
4557 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004558 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004560 stl_items[curitem].stl_type = Highlight;
4561 stl_items[curitem].stl_start = p;
4562 stl_items[curitem].stl_minwid = minwid > 9 ? 1 : minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 s++;
4564 curitem++;
4565 continue;
4566 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004567 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4568 {
4569 if (*s == STL_TABCLOSENR)
4570 {
4571 if (minwid == 0)
4572 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004573 // %X ends the close label, go back to the previously
4574 // define tab label nr.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004575 for (n = curitem - 1; n >= 0; --n)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004576 if (stl_items[n].stl_type == TabPage
4577 && stl_items[n].stl_minwid >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004578 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004579 minwid = stl_items[n].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004580 break;
4581 }
4582 }
4583 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01004584 // close nrs are stored as negative values
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004585 minwid = - minwid;
4586 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004587 stl_items[curitem].stl_type = TabPage;
4588 stl_items[curitem].stl_start = p;
4589 stl_items[curitem].stl_minwid = minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004590 s++;
4591 curitem++;
4592 continue;
4593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 if (*s == '.')
4595 {
4596 s++;
4597 if (VIM_ISDIGIT(*s))
4598 {
4599 maxwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004600 if (maxwid <= 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 maxwid = 50;
4602 }
4603 }
4604 minwid = (minwid > 50 ? 50 : minwid) * l;
4605 if (*s == '(')
4606 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004607 stl_groupitem[groupdepth++] = curitem;
4608 stl_items[curitem].stl_type = Group;
4609 stl_items[curitem].stl_start = p;
4610 stl_items[curitem].stl_minwid = minwid;
4611 stl_items[curitem].stl_maxwid = maxwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 s++;
4613 curitem++;
4614 continue;
4615 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004616#ifdef FEAT_EVAL
4617 // Denotes end of expanded %{} block
4618 if (*s == '}' && evaldepth > 0)
4619 {
4620 s++;
4621 evaldepth--;
4622 continue;
4623 }
4624#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 if (vim_strchr(STL_ALL, *s) == NULL)
4626 {
4627 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
4785 case STL_ARGLISTSTAT:
4786 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004787 buf_tmp[0] = 0;
4788 if (append_arg_number(wp, buf_tmp, (int)sizeof(buf_tmp), FALSE))
4789 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 break;
4791
4792 case STL_KEYMAP:
4793 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004794 if (get_keymap_str(wp, (char_u *)"<%s>", buf_tmp, TMPLEN))
4795 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 break;
4797 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004798#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004799 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800#else
4801 num = 0;
4802#endif
4803 break;
4804
4805 case STL_BUFNO:
4806 num = wp->w_buffer->b_fnum;
4807 break;
4808
4809 case STL_OFFSET_X:
4810 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004811 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 case STL_OFFSET:
4813#ifdef FEAT_BYTEOFF
4814 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
Bram Moolenaar24959102022-05-07 20:01:16 +01004815 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0
4816 ? 0L : l + 1 + ((State & MODE_INSERT) == 0 && empty_line
4817 ? 0 : (int)wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818#endif
4819 break;
4820
4821 case STL_BYTEVAL_X:
4822 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004823 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004825 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 if (num == NL)
4827 num = 0;
4828 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4829 num = NL;
4830 break;
4831
4832 case STL_ROFLAG:
4833 case STL_ROFLAG_ALT:
4834 itemisflag = TRUE;
4835 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004836 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 break;
4838
4839 case STL_HELPFLAG:
4840 case STL_HELPFLAG_ALT:
4841 itemisflag = TRUE;
4842 if (wp->w_buffer->b_help)
4843 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004844 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 break;
4846
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 case STL_FILETYPE:
4848 if (*wp->w_buffer->b_p_ft != NUL
4849 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4850 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004851 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), "[%s]",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004852 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004853 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 }
4855 break;
4856
4857 case STL_FILETYPE_ALT:
4858 itemisflag = TRUE;
4859 if (*wp->w_buffer->b_p_ft != NUL
4860 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4861 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004862 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), ",%s",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004863 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004864 for (t = buf_tmp; *t != 0; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 *t = TOUPPER_LOC(*t);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004866 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 }
4868 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869
Bram Moolenaar4033c552017-09-16 20:54:51 +02004870#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 case STL_PREVIEWFLAG:
4872 case STL_PREVIEWFLAG_ALT:
4873 itemisflag = TRUE;
4874 if (wp->w_p_pvw)
4875 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4876 : _("[Preview]"));
4877 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004878
4879 case STL_QUICKFIX:
4880 if (bt_quickfix(wp->w_buffer))
4881 str = (char_u *)(wp->w_llist_ref
4882 ? _(msg_loclist)
4883 : _(msg_qflist));
4884 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885#endif
4886
4887 case STL_MODIFIED:
4888 case STL_MODIFIED_ALT:
4889 itemisflag = TRUE;
4890 switch ((opt == STL_MODIFIED_ALT)
4891 + bufIsChanged(wp->w_buffer) * 2
4892 + (!wp->w_buffer->b_p_ma) * 4)
4893 {
4894 case 2: str = (char_u *)"[+]"; break;
4895 case 3: str = (char_u *)",+"; break;
4896 case 4: str = (char_u *)"[-]"; break;
4897 case 5: str = (char_u *)",-"; break;
4898 case 6: str = (char_u *)"[+-]"; break;
4899 case 7: str = (char_u *)",+-"; break;
4900 }
4901 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004902
4903 case STL_HIGHLIGHT:
4904 t = s;
4905 while (*s != '#' && *s != NUL)
4906 ++s;
4907 if (*s == '#')
4908 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004909 stl_items[curitem].stl_type = Highlight;
4910 stl_items[curitem].stl_start = p;
4911 stl_items[curitem].stl_minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004912 curitem++;
4913 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004914 if (*s != NUL)
4915 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004916 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 }
4918
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004919 stl_items[curitem].stl_start = p;
4920 stl_items[curitem].stl_type = Normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 if (str != NULL && *str)
4922 {
4923 t = str;
4924 if (itemisflag)
4925 {
4926 if ((t[0] && t[1])
4927 && ((!prevchar_isitem && *t == ',')
4928 || (prevchar_isflag && *t == ' ')))
4929 t++;
4930 prevchar_isflag = TRUE;
4931 }
4932 l = vim_strsize(t);
4933 if (l > 0)
4934 prevchar_isitem = TRUE;
4935 if (l > maxwid)
4936 {
4937 while (l >= maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 if (has_mbyte)
4939 {
4940 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004941 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 }
4943 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 l -= byte2cells(*t++);
4945 if (p + 1 >= out + outlen)
4946 break;
4947 *p++ = '<';
4948 }
4949 if (minwid > 0)
4950 {
4951 for (; l < minwid && p + 1 < out + outlen; l++)
4952 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004953 // Don't put a "-" in front of a digit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4955 *p++ = ' ';
4956 else
Bram Moolenaar008bff92021-03-04 21:55:58 +01004957 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 }
4959 minwid = 0;
4960 }
4961 else
4962 minwid *= -1;
Bram Moolenaar008bff92021-03-04 21:55:58 +01004963 for (; *t && p + 1 < out + outlen; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004965 // Change a space by fillchar, unless fillchar is '-' and a
4966 // digit follows.
Bram Moolenaar008bff92021-03-04 21:55:58 +01004967 if (fillable && *t == ' '
4968 && (!VIM_ISDIGIT(*(t + 1)) || fillchar != '-'))
4969 MB_CHAR2BYTES(fillchar, p);
4970 else
4971 *p++ = *t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 }
4973 for (; l < minwid && p + 1 < out + outlen; l++)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004974 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 }
4976 else if (num >= 0)
4977 {
4978 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4979 char_u nstr[20];
4980
4981 if (p + 20 >= out + outlen)
Bram Moolenaarc667da52019-11-30 20:52:27 +01004982 break; // not sufficient space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 prevchar_isitem = TRUE;
4984 t = nstr;
4985 if (opt == STL_VIRTCOL_ALT)
4986 {
4987 *t++ = '-';
4988 minwid--;
4989 }
4990 *t++ = '%';
4991 if (zeropad)
4992 *t++ = '0';
4993 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004994 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 *t = 0;
4996
4997 for (n = num, l = 1; n >= nbase; n /= nbase)
4998 l++;
4999 if (opt == STL_VIRTCOL_ALT)
5000 l++;
5001 if (l > maxwid)
5002 {
5003 l += 2;
5004 n = l - maxwid;
5005 while (l-- > maxwid)
5006 num /= nbase;
5007 *t++ = '>';
5008 *t++ = '%';
5009 *t = t[-3];
5010 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005011 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
5012 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 }
5014 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005015 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
5016 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 p += STRLEN(p);
5018 }
5019 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005020 stl_items[curitem].stl_type = Empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01005022 if (num >= 0 || (!itemisflag && str != NULL && *str != NUL))
5023 prevchar_isflag = FALSE; // Item not NULL, but not a flag
5024 //
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 if (opt == STL_VIM_EXPR)
5026 vim_free(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 curitem++;
5028 }
5029 *p = NUL;
5030 itemcnt = curitem;
5031
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005032#ifdef FEAT_EVAL
5033 if (usefmt != fmt)
5034 vim_free(usefmt);
5035#endif
5036
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 width = vim_strsize(out);
5038 if (maxwidth > 0 && width > maxwidth)
5039 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005040 // Result is too long, must truncate somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 l = 0;
5042 if (itemcnt == 0)
5043 s = out;
5044 else
5045 {
5046 for ( ; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005047 if (stl_items[l].stl_type == Trunc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005049 // Truncate at %< item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005050 s = stl_items[l].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 break;
5052 }
5053 if (l == itemcnt)
5054 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005055 // No %< item, truncate first item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005056 s = stl_items[0].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 l = 0;
5058 }
5059 }
5060
5061 if (width - vim_strsize(s) >= maxwidth)
5062 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005063 // Truncation mark is beyond max length
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 if (has_mbyte)
5065 {
5066 s = out;
5067 width = 0;
5068 for (;;)
5069 {
5070 width += ptr2cells(s);
5071 if (width >= maxwidth)
5072 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005073 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005075 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 while (++width < maxwidth)
Bram Moolenaar008bff92021-03-04 21:55:58 +01005077 MB_CHAR2BYTES(fillchar, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 }
5079 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 s = out + maxwidth - 1;
5081 for (l = 0; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005082 if (stl_items[l].stl_start > s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 break;
5084 itemcnt = l;
5085 *s++ = '>';
5086 *s = 0;
5087 }
5088 else
5089 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 if (has_mbyte)
5091 {
5092 n = 0;
5093 while (width >= maxwidth)
5094 {
5095 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005096 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 }
5098 }
5099 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 n = width - maxwidth + 1;
5101 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00005102 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 *s = '<';
5104
Bram Moolenaarc667da52019-11-30 20:52:27 +01005105 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 while (++width < maxwidth)
5107 {
5108 s = s + STRLEN(s);
Bram Moolenaar008bff92021-03-04 21:55:58 +01005109 MB_CHAR2BYTES(fillchar, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 *s = NUL;
5111 }
5112
Bram Moolenaarc667da52019-11-30 20:52:27 +01005113 --n; // count the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 for (; l < itemcnt; l++)
5115 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005116 if (stl_items[l].stl_start - n >= s)
5117 stl_items[l].stl_start -= n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005119 stl_items[l].stl_start = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 }
5121 }
5122 width = maxwidth;
5123 }
5124 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
5125 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005126 // Apply STL_MIDDLE if any
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 for (l = 0; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005128 if (stl_items[l].stl_type == Middle)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 break;
5130 if (l < itemcnt)
5131 {
Bram Moolenaar008bff92021-03-04 21:55:58 +01005132 int middlelength = (maxwidth - width) * MB_CHAR2LEN(fillchar);
5133 p = stl_items[l].stl_start + middlelength;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005134 STRMOVE(p, stl_items[l].stl_start);
Bram Moolenaar008bff92021-03-04 21:55:58 +01005135 for (s = stl_items[l].stl_start; s < p;)
5136 MB_CHAR2BYTES(fillchar, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 for (l++; l < itemcnt; l++)
Bram Moolenaar008bff92021-03-04 21:55:58 +01005138 stl_items[l].stl_start += middlelength;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 width = maxwidth;
5140 }
5141 }
5142
Bram Moolenaarc667da52019-11-30 20:52:27 +01005143 // Store the info about highlighting.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005144 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005146 *hltab = stl_hltab;
5147 sp = stl_hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 for (l = 0; l < itemcnt; l++)
5149 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005150 if (stl_items[l].stl_type == Highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005152 sp->start = stl_items[l].stl_start;
5153 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005154 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 }
5156 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005157 sp->start = NULL;
5158 sp->userhl = 0;
5159 }
5160
Bram Moolenaarc667da52019-11-30 20:52:27 +01005161 // Store the info about tab pages labels.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005162 if (tabtab != NULL)
5163 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005164 *tabtab = stl_tabtab;
5165 sp = stl_tabtab;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005166 for (l = 0; l < itemcnt; l++)
5167 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005168 if (stl_items[l].stl_type == TabPage)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005169 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005170 sp->start = stl_items[l].stl_start;
5171 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005172 sp++;
5173 }
5174 }
5175 sp->start = NULL;
5176 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 }
5178
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01005179 redraw_not_allowed = save_redraw_not_allowed;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02005180
Bram Moolenaar0e1f36f2022-02-15 16:17:44 +00005181 // A user function may reset KeyTyped, restore it.
5182 KeyTyped = save_KeyTyped;
5183
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 return width;
5185}
Bram Moolenaarc667da52019-11-30 20:52:27 +01005186#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005189 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
5190 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 */
5192 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005193get_rel_pos(
5194 win_T *wp,
5195 char_u *buf,
5196 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005198 long above; // number of lines above window
5199 long below; // number of lines below window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200
Bram Moolenaarc667da52019-11-30 20:52:27 +01005201 if (buflen < 3) // need at least 3 chars for writing
Bram Moolenaar0027c212015-01-07 13:31:52 +01005202 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 above = wp->w_topline - 1;
5204#ifdef FEAT_DIFF
5205 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02005206 if (wp->w_topline == 1 && wp->w_topfill >= 1)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005207 above = 0; // All buffer lines are displayed and there is an
5208 // indication of filler lines, that can be considered
5209 // seeing all lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210#endif
5211 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
5212 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005213 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
5214 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005216 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005218 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 ? (int)(above / ((above + below) / 100L))
5220 : (int)(above * 100L / (above + below)));
5221}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222
5223/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005224 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 * Return TRUE if it was appended.
5226 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005227 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005228append_arg_number(
5229 win_T *wp,
5230 char_u *buf,
5231 int buflen,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005232 int add_file) // Add "file" before the arg number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233{
5234 char_u *p;
5235
Bram Moolenaarc667da52019-11-30 20:52:27 +01005236 if (ARGCOUNT <= 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 return FALSE;
5238
Bram Moolenaarc667da52019-11-30 20:52:27 +01005239 p = buf + STRLEN(buf); // go to the end of the buffer
5240 if (p - buf + 35 >= buflen) // getting too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 return FALSE;
5242 *p++ = ' ';
5243 *p++ = '(';
5244 if (add_file)
5245 {
5246 STRCPY(p, "file ");
5247 p += 5;
5248 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005249 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
5250 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
5252 return TRUE;
5253}
5254
5255/*
5256 * If fname is not a full path, make it a full path.
5257 * Returns pointer to allocated memory (NULL for failure).
5258 */
5259 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005260fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261{
5262 /*
5263 * Force expanding the path always for Unix, because symbolic links may
5264 * mess up the full path name, even though it starts with a '/'.
5265 * Also expand when there is ".." in the file name, try to remove it,
5266 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00005267 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 * For MS-Windows also expand names like "longna~1" to "longname".
5269 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00005270#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 return FullName_save(fname, TRUE);
5272#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00005273 if (!vim_isAbsName(fname)
5274 || strstr((char *)fname, "..") != NULL
5275 || strstr((char *)fname, "//") != NULL
5276# ifdef BACKSLASH_IN_FILENAME
5277 || strstr((char *)fname, "\\\\") != NULL
5278# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005279# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00005281# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 )
5283 return FullName_save(fname, FALSE);
5284
5285 fname = vim_strsave(fname);
5286
Bram Moolenaar9b942202007-10-03 12:31:33 +00005287# ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01005288 if (fname != NULL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005289 fname_case(fname, 0); // set correct case for file name
Bram Moolenaar9b942202007-10-03 12:31:33 +00005290# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291
5292 return fname;
5293#endif
5294}
5295
5296/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005297 * Make "*ffname" a full file name, set "*sfname" to "*ffname" if not NULL.
5298 * "*ffname" becomes a pointer to allocated memory (or NULL).
5299 * When resolving a link both "*sfname" and "*ffname" will point to the same
5300 * allocated memory.
5301 * The "*ffname" and "*sfname" pointer values on call will not be freed.
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005302 * Note that the resulting "*ffname" pointer should be considered not allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005305fname_expand(
5306 buf_T *buf UNUSED,
5307 char_u **ffname,
5308 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005310 if (*ffname == NULL) // no file name given, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 return;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005312 if (*sfname == NULL) // no short file name given, use ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 *sfname = *ffname;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005314 *ffname = fix_fname(*ffname); // expand to full path
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315
5316#ifdef FEAT_SHORTCUT
5317 if (!buf->b_p_bin)
5318 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005319 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005321 // If the file name is a shortcut file, use the file it links to.
Bram Moolenaardce1e892019-02-10 23:18:53 +01005322 rfname = mch_resolve_path(*ffname, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005323 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 {
5325 vim_free(*ffname);
5326 *ffname = rfname;
5327 *sfname = rfname;
5328 }
5329 }
5330#endif
5331}
5332
5333/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 * Open a window for a number of buffers.
5335 */
5336 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005337ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338{
5339 buf_T *buf;
5340 win_T *wp, *wpnext;
5341 int split_ret = OK;
5342 int p_ea_save;
5343 int open_wins = 0;
5344 int r;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005345 int count; // Maximum number of windows to open.
5346 int all; // When TRUE also load inactive buffers.
Bram Moolenaare1004402020-10-24 20:49:43 +02005347 int had_tab = cmdmod.cmod_tab;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005348 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349
Bram Moolenaarc667da52019-11-30 20:52:27 +01005350 if (eap->addr_count == 0) // make as many windows as possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 count = 9999;
5352 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01005353 count = eap->line2; // make as many windows as specified
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5355 all = FALSE;
5356 else
5357 all = TRUE;
5358
5359 setpcmark();
5360
5361#ifdef FEAT_GUI
5362 need_mouse_correct = TRUE;
5363#endif
5364
5365 /*
5366 * Close superfluous windows (two windows for the same buffer).
5367 * Also close windows that are not full-width.
5368 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005369 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005370 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005371 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005373 tpnext = curtab->tp_next;
5374 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005376 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005377 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005378 || ((cmdmod.cmod_split & WSP_VERT)
5379 ? wp->w_height + wp->w_status_height < Rows - p_ch
5380 - tabline_height()
5381 : wp->w_width != Columns)
5382 || (had_tab > 0 && wp != firstwin))
5383 && !ONE_WINDOW
5384 && !(wp->w_closing || wp->w_buffer->b_locked > 0)
5385 && !win_unlisted(wp))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005386 {
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005387 if (win_close(wp, FALSE) == FAIL)
5388 break;
5389 // Just in case an autocommand does something strange with
5390 // windows: start all over...
5391 wpnext = firstwin;
5392 tpnext = first_tabpage;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005393 open_wins = 0;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005394 }
5395 else
5396 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005398
Bram Moolenaarc667da52019-11-30 20:52:27 +01005399 // Without the ":tab" modifier only do the current tab page.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005400 if (had_tab == 0 || tpnext == NULL)
5401 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005402 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 }
5404
5405 /*
5406 * Go through the buffer list. When a buffer doesn't have a window yet,
5407 * open one. Otherwise move the window to the right position.
5408 * Watch out for autocommands that delete buffers or windows!
5409 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01005410 // Don't execute Win/Buf Enter/Leave autocommands here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 ++autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 win_enter(lastwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5415 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005416 // Check if this buffer needs a window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5418 continue;
5419
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005420 if (had_tab != 0)
5421 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005422 // With the ":tab" modifier don't move the window.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005423 if (buf->b_nwindows > 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005424 wp = lastwin; // buffer has a window, skip it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005425 else
5426 wp = NULL;
5427 }
5428 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005429 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005430 // Check if this buffer already has a window
Bram Moolenaar29323592016-07-24 22:04:11 +02005431 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005432 if (wp->w_buffer == buf)
5433 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005434 // If the buffer already has a window, move it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005435 if (wp != NULL)
5436 win_move_after(wp, curwin);
5437 }
5438
5439 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005441 bufref_T bufref;
5442
5443 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005444
Bram Moolenaarc667da52019-11-30 20:52:27 +01005445 // Split the window and put the buffer in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 p_ea_save = p_ea;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005447 p_ea = TRUE; // use space from all windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5449 ++open_wins;
5450 p_ea = p_ea_save;
5451 if (split_ret == FAIL)
5452 continue;
5453
Bram Moolenaarc667da52019-11-30 20:52:27 +01005454 // Open the buffer in this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005457 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005459 // autocommands deleted the buffer!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 swap_exists_action = SEA_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 break;
5462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 if (swap_exists_action == SEA_QUIT)
5464 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005465#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005466 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005467
Bram Moolenaarc667da52019-11-30 20:52:27 +01005468 // Reset the error/interrupt/exception state here so that
5469 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005470 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005471#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005472
Bram Moolenaarc667da52019-11-30 20:52:27 +01005473 // User selected Quit at ATTENTION prompt; close this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 win_close(curwin, TRUE);
5475 --open_wins;
5476 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005477 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005478
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005479#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005480 // Restore the error/interrupt/exception state if not
5481 // discarded by a new aborting error, interrupt, or uncaught
5482 // exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005483 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 }
5486 else
5487 handle_swap_exists(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 }
5489
5490 ui_breakcheck();
5491 if (got_int)
5492 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005493 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 break;
5495 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005496#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01005497 // Autocommands deleted the buffer or aborted script processing!!!
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005498 if (aborting())
5499 break;
5500#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01005501 // When ":tab" was used open a new tab for a new window repeatedly.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005502 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
Bram Moolenaare1004402020-10-24 20:49:43 +02005503 cmdmod.cmod_tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 --autocmd_no_enter;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005506 win_enter(firstwin, FALSE); // back to first window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508
5509 /*
5510 * Close superfluous windows.
5511 */
5512 for (wp = lastwin; open_wins > count; )
5513 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005514 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 || autowrite(wp->w_buffer, FALSE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 if (!win_valid(wp))
5517 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005518 // BufWrite Autocommands made the window invalid, start over
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 wp = lastwin;
5520 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005521 else if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005523 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 --open_wins;
5525 wp = lastwin;
5526 }
5527 else
5528 {
5529 wp = wp->w_prev;
5530 if (wp == NULL)
5531 break;
5532 }
5533 }
5534}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005537static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005538
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539/*
5540 * do_modelines() - process mode lines for the current file
5541 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005542 * "flags" can be:
5543 * OPT_WINONLY only set options local to window
5544 * OPT_NOWIN don't set options local to window
5545 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 * Returns immediately if the "ml" option isn't set.
5547 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005549do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005551 linenr_T lnum;
5552 int nmlines;
5553 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554
5555 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5556 return;
5557
Bram Moolenaarc667da52019-11-30 20:52:27 +01005558 // Disallow recursive entry here. Can happen when executing a modeline
5559 // triggers an autocommand, which reloads modelines with a ":do".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 if (entered)
5561 return;
5562
5563 ++entered;
Hu Jialun9dcd3492021-08-28 20:42:50 +02005564 for (lnum = 1; curbuf->b_p_ml && lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005566 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 nmlines = 0;
5568
Hu Jialun9dcd3492021-08-28 20:42:50 +02005569 for (lnum = curbuf->b_ml.ml_line_count; curbuf->b_p_ml && lnum > 0 && lnum > nmlines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005571 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 nmlines = 0;
5573 --entered;
5574}
5575
Bram Moolenaarc667da52019-11-30 20:52:27 +01005576#include "version.h" // for version number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577
5578/*
5579 * chk_modeline() - check a single line for a mode string
5580 * Return FAIL if an error encountered.
5581 */
5582 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005583chk_modeline(
5584 linenr_T lnum,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005585 int flags) // Same as for do_modelines().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586{
5587 char_u *s;
5588 char_u *e;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005589 char_u *linecopy; // local copy of any modeline found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 int prev;
5591 int vers;
5592 int end;
5593 int retval = OK;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005594 sctx_T save_current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005595
Bram Moolenaare31ee862020-01-07 20:59:34 +01005596 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597
5598 prev = -1;
5599 for (s = ml_get(lnum); *s != NUL; ++s)
5600 {
5601 if (prev == -1 || vim_isspace(prev))
5602 {
5603 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5604 || STRNCMP(s, "vi:", (size_t)3) == 0)
5605 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005606 // Accept both "vim" and "Vim".
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005607 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 {
5609 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5610 e = s + 4;
5611 else
5612 e = s + 3;
5613 vers = getdigits(&e);
5614 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005615 && (s[0] != 'V'
5616 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 && (s[3] == ':'
5618 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5619 || (VIM_VERSION_100 < vers && s[3] == '<')
5620 || (VIM_VERSION_100 > vers && s[3] == '>')
5621 || (VIM_VERSION_100 == vers && s[3] == '=')))
5622 break;
5623 }
5624 }
5625 prev = *s;
5626 }
5627
5628 if (*s)
5629 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005630 do // skip over "ex:", "vi:" or "vim:"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 ++s;
5632 while (s[-1] != ':');
5633
Bram Moolenaarc667da52019-11-30 20:52:27 +01005634 s = linecopy = vim_strsave(s); // copy the line, it will change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 if (linecopy == NULL)
5636 return FAIL;
5637
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005638 // prepare for emsg()
5639 estack_push(ETYPE_MODELINE, (char_u *)"modelines", lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01005640 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641
5642 end = FALSE;
5643 while (end == FALSE)
5644 {
5645 s = skipwhite(s);
5646 if (*s == NUL)
5647 break;
5648
5649 /*
5650 * Find end of set command: ':' or end of line.
5651 * Skip over "\:", replacing it with ":".
5652 */
5653 for (e = s; *e != ':' && *e != NUL; ++e)
5654 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005655 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 if (*e == NUL)
5657 end = TRUE;
5658
5659 /*
5660 * If there is a "set" command, require a terminating ':' and
5661 * ignore the stuff after the ':'.
5662 * "vi:set opt opt opt: foo" -- foo not interpreted
5663 * "vi:opt opt opt: foo" -- foo interpreted
5664 * Accept "se" for compatibility with Elvis.
5665 */
5666 if (STRNCMP(s, "set ", (size_t)4) == 0
5667 || STRNCMP(s, "se ", (size_t)3) == 0)
5668 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005669 if (*e != ':') // no terminating ':'?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 break;
5671 end = TRUE;
5672 s = vim_strchr(s, ' ') + 1;
5673 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005674 *e = NUL; // truncate the set command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675
Bram Moolenaarc667da52019-11-30 20:52:27 +01005676 if (*s != NUL) // skip over an empty "::"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 {
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005678 int secure_save = secure;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005679
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005680 save_current_sctx = current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005681 current_sctx.sc_version = 1;
5682#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005683 current_sctx.sc_sid = SID_MODELINE;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005684 current_sctx.sc_seq = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005685 current_sctx.sc_lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005687
Bram Moolenaar5958f952018-11-20 04:25:21 +01005688 // Make sure no risky things are executed as a side effect.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01005689 secure = 1;
Bram Moolenaar5958f952018-11-20 04:25:21 +01005690
Bram Moolenaara3227e22006-03-08 21:32:40 +00005691 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar5958f952018-11-20 04:25:21 +01005692
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005693 secure = secure_save;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005694 current_sctx = save_current_sctx;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005695 if (retval == FAIL) // stop if error found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 break;
5697 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005698 s = e + 1; // advance to next part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 }
5700
Bram Moolenaare31ee862020-01-07 20:59:34 +01005701 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005702 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 vim_free(linecopy);
5704 }
5705 return retval;
5706}
5707
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005708/*
Bram Moolenaar91335e52018-08-01 17:53:12 +02005709 * Return TRUE if "buf" is a normal buffer, 'buftype' is empty.
5710 */
5711 int
5712bt_normal(buf_T *buf)
5713{
5714 return buf != NULL && buf->b_p_bt[0] == NUL;
5715}
5716
5717/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005718 * Return TRUE if "buf" is the quickfix buffer.
5719 */
5720 int
Bram Moolenaar340dafd2022-08-25 16:16:45 +01005721bt_quickfix(buf_T *buf UNUSED)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005722{
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005723#ifdef FEAT_QUICKFIX
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005724 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005725#else
5726 return FALSE;
Bram Moolenaar113e1072019-01-20 15:30:40 +01005727#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005728}
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005729
5730/*
5731 * Return TRUE if "buf" is a terminal buffer.
5732 */
5733 int
Bram Moolenaar340dafd2022-08-25 16:16:45 +01005734bt_terminal(buf_T *buf UNUSED)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005735{
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005736#if defined(FEAT_TERMINAL)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005737 return buf != NULL && buf->b_p_bt[0] == 't';
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005738#else
5739 return FALSE;
Bram Moolenaar113e1072019-01-20 15:30:40 +01005740#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005741}
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005742
5743/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005744 * Return TRUE if "buf" is a help buffer.
5745 */
5746 int
5747bt_help(buf_T *buf)
5748{
5749 return buf != NULL && buf->b_help;
5750}
5751
5752/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02005753 * Return TRUE if "buf" is a prompt buffer.
5754 */
5755 int
5756bt_prompt(buf_T *buf)
5757{
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005758 return buf != NULL && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'r';
5759}
5760
Dominique Pelle748b3082022-01-08 12:41:16 +00005761#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005762/*
5763 * Return TRUE if "buf" is a buffer for a popup window.
5764 */
5765 int
5766bt_popup(buf_T *buf)
5767{
5768 return buf != NULL && buf->b_p_bt != NULL
5769 && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'o';
Bram Moolenaarf2732452018-06-03 14:47:35 +02005770}
Dominique Pelle748b3082022-01-08 12:41:16 +00005771#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005772
5773/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005774 * Return TRUE if "buf" is a "nofile", "acwrite", "terminal" or "prompt"
Bram Moolenaarc3126192022-08-26 12:58:17 +01005775 * buffer. This means the buffer name may not be a file name, at least not for
5776 * writing the buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005777 */
5778 int
Bram Moolenaar26910de2019-06-15 19:37:15 +02005779bt_nofilename(buf_T *buf)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005780{
5781 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5782 || buf->b_p_bt[0] == 'a'
Bram Moolenaarf2732452018-06-03 14:47:35 +02005783 || buf->b_p_bt[0] == 't'
5784 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005785}
5786
Bram Moolenaarc3126192022-08-26 12:58:17 +01005787/*
5788 * Return TRUE if "buf" is a "nofile", "quickfix", "terminal" or "prompt"
5789 * buffer. This means the buffer is not to be read from a file.
5790 */
5791 static int
5792bt_nofileread(buf_T *buf)
5793{
5794 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5795 || buf->b_p_bt[0] == 't'
5796 || buf->b_p_bt[0] == 'q'
5797 || buf->b_p_bt[0] == 'p');
5798}
5799
Dominique Pelle748b3082022-01-08 12:41:16 +00005800#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005801/*
Bram Moolenaar26910de2019-06-15 19:37:15 +02005802 * Return TRUE if "buf" has 'buftype' set to "nofile".
5803 */
5804 int
5805bt_nofile(buf_T *buf)
5806{
5807 return buf != NULL && buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f';
5808}
Dominique Pelle748b3082022-01-08 12:41:16 +00005809#endif
Bram Moolenaar26910de2019-06-15 19:37:15 +02005810
5811/*
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01005812 * Return TRUE if "buf" is a "nowrite", "nofile", "terminal", "prompt", or
5813 * "popup" buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005814 */
5815 int
5816bt_dontwrite(buf_T *buf)
5817{
Bram Moolenaarf2732452018-06-03 14:47:35 +02005818 return buf != NULL && (buf->b_p_bt[0] == 'n'
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005819 || buf->b_p_bt[0] == 't'
5820 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005821}
5822
5823 int
5824bt_dontwrite_msg(buf_T *buf)
5825{
5826 if (bt_dontwrite(buf))
5827 {
Bram Moolenaarf1474d82021-12-31 19:59:55 +00005828 emsg(_(e_cannot_write_buftype_option_is_set));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005829 return TRUE;
5830 }
5831 return FALSE;
5832}
5833
5834/*
5835 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
5836 * and 'bufhidden'.
5837 */
5838 int
5839buf_hide(buf_T *buf)
5840{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005841 // 'bufhidden' overrules 'hidden' and ":hide", check it first
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005842 switch (buf->b_p_bh[0])
5843 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005844 case 'u': // "unload"
5845 case 'w': // "wipe"
5846 case 'd': return FALSE; // "delete"
5847 case 'h': return TRUE; // "hide"
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005848 }
Bram Moolenaare1004402020-10-24 20:49:43 +02005849 return (p_hid || (cmdmod.cmod_flags & CMOD_HIDE));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005850}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851
5852/*
5853 * Return special buffer name.
5854 * Returns NULL when the buffer has a normal file name.
5855 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005856 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005857buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858{
Bram Moolenaar4033c552017-09-16 20:54:51 +02005859#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005861 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005862 /*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005863 * Differentiate between the quickfix and location list buffers using
5864 * the buffer number stored in the global quickfix stack.
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005865 */
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005866 if (buf->b_fnum == qf_stack_get_bufnr())
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005867 return (char_u *)_(msg_qflist);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005868 else
5869 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02005872
Bram Moolenaarc667da52019-11-30 20:52:27 +01005873 // There is no _file_ when 'buftype' is "nofile", b_sfname
5874 // contains the name as specified by the user.
Bram Moolenaar26910de2019-06-15 19:37:15 +02005875 if (bt_nofilename(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 {
Bram Moolenaar21554412017-07-24 21:44:43 +02005877#ifdef FEAT_TERMINAL
5878 if (buf->b_term != NULL)
5879 return term_get_status_text(buf->b_term);
5880#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02005881 if (buf->b_fname != NULL)
5882 return buf->b_fname;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02005883#ifdef FEAT_JOB_CHANNEL
5884 if (bt_prompt(buf))
5885 return (char_u *)_("[Prompt]");
5886#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005887#ifdef FEAT_PROP_POPUP
Bram Moolenaarc6896e22019-05-30 22:32:34 +02005888 if (bt_popup(buf))
5889 return (char_u *)_("[Popup]");
5890#endif
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005891 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 }
Bram Moolenaar21554412017-07-24 21:44:43 +02005893
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 if (buf->b_fname == NULL)
Bram Moolenaar00806bc2020-11-05 19:36:38 +01005895 return buf_get_fname(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 return NULL;
5897}
5898
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899/*
Bram Moolenaar00806bc2020-11-05 19:36:38 +01005900 * Get "buf->b_fname", use "[No Name]" if it is NULL.
5901 */
5902 char_u *
5903buf_get_fname(buf_T *buf)
5904{
5905 if (buf->b_fname == NULL)
5906 return (char_u *)_("[No Name]");
5907 return buf->b_fname;
5908}
5909
5910/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5912 */
5913 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005914set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915{
5916 if (on != curbuf->b_p_bl)
5917 {
5918 curbuf->b_p_bl = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 if (on)
5920 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5921 else
5922 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 }
5924}
5925
5926/*
5927 * Read the file for "buf" again and check if the contents changed.
5928 * Return TRUE if it changed or this could not be checked.
5929 */
5930 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005931buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932{
5933 buf_T *newbuf;
5934 int differ = TRUE;
5935 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 exarg_T ea;
5938
Bram Moolenaarc667da52019-11-30 20:52:27 +01005939 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5941 if (newbuf == NULL)
5942 return TRUE;
5943
Bram Moolenaarc667da52019-11-30 20:52:27 +01005944 // Force the 'fileencoding' and 'fileformat' to be equal.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 if (prep_exarg(&ea, buf) == FAIL)
5946 {
5947 wipe_buffer(newbuf, FALSE);
5948 return TRUE;
5949 }
5950
Bram Moolenaarc667da52019-11-30 20:52:27 +01005951 // set curwin/curbuf to buf and save a few things
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953
Bram Moolenaar4770d092006-01-12 23:22:24 +00005954 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 && readfile(buf->b_ffname, buf->b_fname,
5956 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5957 &ea, READ_NEW | READ_DUMMY) == OK)
5958 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005959 // compare the two files line by line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5961 {
5962 differ = FALSE;
5963 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5964 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5965 {
5966 differ = TRUE;
5967 break;
5968 }
5969 }
5970 }
5971 vim_free(ea.cmd);
5972
Bram Moolenaarc667da52019-11-30 20:52:27 +01005973 // restore curwin/curbuf and a few other things
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975
Bram Moolenaarc667da52019-11-30 20:52:27 +01005976 if (curbuf != newbuf) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 wipe_buffer(newbuf, FALSE);
5978
5979 return differ;
5980}
5981
5982/*
5983 * Wipe out a buffer and decrement the last buffer number if it was used for
5984 * this buffer. Call this to wipe out a temp buffer that does not contain any
5985 * marks.
5986 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005988wipe_buffer(
5989 buf_T *buf,
Bram Moolenaara6e8f882019-12-14 16:18:15 +01005990 int aucmd) // When TRUE trigger autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991{
5992 if (buf->b_fnum == top_file_num - 1)
5993 --top_file_num;
5994
Bram Moolenaarc667da52019-11-30 20:52:27 +01005995 if (!aucmd) // Don't trigger BufDelete autocommands here.
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005996 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005997
Bram Moolenaara6e8f882019-12-14 16:18:15 +01005998 close_buffer(NULL, buf, DOBUF_WIPE, FALSE, TRUE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005999
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006001 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002}