blob: 5a4825feb283baed7889beac2b84c38debf6e391 [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 Moolenaar071d4272004-06-13 20:20:40 +0000793 buf->b_p_eol = TRUE;
794 buf->b_start_eol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000796 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 buf->b_ml.ml_mfp = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +0100798 buf->b_ml.ml_flags = ML_EMPTY; // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799#ifdef FEAT_NETBEANS_INTG
800 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801#endif
802}
803
804/*
805 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200806 * the file. Careful: get here with "curwin" NULL when exiting.
807 * flags:
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100808 * BFA_DEL buffer is going to be deleted
809 * BFA_WIPE buffer is going to be wiped out
810 * BFA_KEEP_UNDO do not free undo information
811 * BFA_IGNORE_ABORT don't abort even when aborting() returns TRUE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100814buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200817 bufref_T bufref;
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200818 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaar5a497892016-09-03 16:29:04 +0200819 win_T *the_curwin = curwin;
820 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821
Bram Moolenaarc667da52019-11-30 20:52:27 +0100822 // Make sure the buffer isn't closed by autocommands.
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200823 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100824 ++buf->b_locked_split;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200825 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200826 if (buf->b_ml.ml_mfp != NULL)
827 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200828 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
829 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200830 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100831 // autocommands deleted the buffer
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200832 return;
833 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200834 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200836 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
837 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200838 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100839 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 return;
841 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200842 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200844 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
845 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200846 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100847 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 return;
849 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200850 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100851 --buf->b_locked_split;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200852
Bram Moolenaarc667da52019-11-30 20:52:27 +0100853 // If the buffer was in curwin and the window has changed, go back to that
854 // window, if it still exists. This avoids that ":edit x" triggering a
855 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaar5a497892016-09-03 16:29:04 +0200856 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
857 {
858 block_autocmds();
859 goto_tabpage_win(the_curtab, the_curwin);
860 unblock_autocmds();
861 }
Bram Moolenaar5a497892016-09-03 16:29:04 +0200862
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100863#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100864 // autocmds may abort script processing
865 if ((flags & BFA_IGNORE_ABORT) == 0 && aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100869 // It's possible that autocommands change curbuf to the one being deleted.
870 // This might cause curbuf to be deleted unexpectedly. But in some cases
871 // it's OK to delete the curbuf, because a new one is obtained anyway.
872 // Therefore only return if curbuf changed to the deleted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 if (buf == curbuf && !is_curbuf)
874 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875#ifdef FEAT_DIFF
Bram Moolenaarc667da52019-11-30 20:52:27 +0100876 diff_buf_delete(buf); // Can't use 'diff' for unloaded buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200878#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100879 // Remove any ownsyntax, unless exiting.
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200880 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaar89c71222011-11-30 15:40:56 +0100881 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200882#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000883
884#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +0100885 // No folds in an empty buffer.
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000886 {
887 win_T *win;
888 tabpage_T *tp;
889
890 FOR_ALL_TAB_WINDOWS(tp, win)
891 if (win->w_buffer == buf)
892 clearFolding(win);
893 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000894#endif
895
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896#ifdef FEAT_TCL
897 tcl_buffer_free(buf);
898#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100899 ml_close(buf, TRUE); // close and delete the memline/memfile
900 buf->b_ml.ml_line_count = 0; // no lines in buffer
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200901 if ((flags & BFA_KEEP_UNDO) == 0)
902 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100903 u_blockfree(buf); // free the memory allocated for undo
904 u_clearall(buf); // reset all undo information
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100907 syntax_clear(&buf->b_s); // reset syntax info
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100909#ifdef FEAT_PROP_POPUP
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100910 clear_buf_prop_types(buf);
911#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100912 buf->b_flags &= ~BF_READERR; // a read error is no longer relevant
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913}
914
915/*
916 * Free a buffer structure and the things it contains related to the buffer
917 * itself (not the file, that must have been done already).
918 */
919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100920free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200922 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200924#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100925 // b:changedtick uses an item in buf_T, remove it now
Bram Moolenaarb2c87502017-10-14 21:15:58 +0200926 dictitem_remove(buf->b_vars, (dictitem_T *)&buf->b_ct_di);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200927 unref_var_dict(buf->b_vars);
Bram Moolenaar86173482019-10-01 17:02:16 +0200928 remove_listeners(buf);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200929#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200930#ifdef FEAT_LUA
931 lua_buffer_free(buf);
932#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000933#ifdef FEAT_MZSCHEME
934 mzscheme_buffer_free(buf);
935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936#ifdef FEAT_PERL
937 perl_buf_free(buf);
938#endif
939#ifdef FEAT_PYTHON
940 python_buffer_free(buf);
941#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200942#ifdef FEAT_PYTHON3
943 python3_buffer_free(buf);
944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945#ifdef FEAT_RUBY
946 ruby_buffer_free(buf);
947#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200948#ifdef FEAT_JOB_CHANNEL
949 channel_buffer_free(buf);
950#endif
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200951#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +0200952 free_terminal(buf);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200953#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +0200954#ifdef FEAT_JOB_CHANNEL
955 vim_free(buf->b_prompt_text);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200956 free_callback(&buf->b_prompt_callback);
Bram Moolenaar86173482019-10-01 17:02:16 +0200957 free_callback(&buf->b_prompt_interrupt);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200958#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200959
960 buf_hashtab_remove(buf);
961
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200962 aubuflocal_remove(buf);
963
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200964 if (autocmd_busy)
965 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100966 // Do not free the buffer structure while autocommands are executing,
967 // it's still needed. Free it when autocmd_busy is reset.
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200968 buf->b_next = au_pending_free_buf;
969 au_pending_free_buf = buf;
970 }
971 else
Bram Moolenaarcee52202020-03-11 14:19:58 +0100972 {
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200973 vim_free(buf);
Bram Moolenaarcee52202020-03-11 14:19:58 +0100974 if (curbuf == buf)
975 curbuf = NULL; // make clear it's not to be used
976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977}
978
979/*
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100980 * Initializes b:changedtick.
Bram Moolenaar79518e22017-02-17 16:31:35 +0100981 */
982 static void
983init_changedtick(buf_T *buf)
984{
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100985 dictitem_T *di = (dictitem_T *)&buf->b_ct_di;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100986
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100987 di->di_flags = DI_FLAGS_FIX | DI_FLAGS_RO;
988 di->di_tv.v_type = VAR_NUMBER;
989 di->di_tv.v_lock = VAR_FIXED;
990 di->di_tv.vval.v_number = 0;
991
Bram Moolenaar92769c32017-02-25 15:41:37 +0100992#ifdef FEAT_EVAL
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100993 STRCPY(buf->b_ct_di.di_key, "changedtick");
994 (void)dict_add(buf->b_vars, di);
Bram Moolenaar92769c32017-02-25 15:41:37 +0100995#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +0100996}
997
998/*
Bram Moolenaarc3126192022-08-26 12:58:17 +0100999 * Free the b_wininfo list for buffer "buf".
1000 */
1001 static void
1002clear_wininfo(buf_T *buf)
1003{
1004 wininfo_T *wip;
1005
1006 while (buf->b_wininfo != NULL)
1007 {
1008 wip = buf->b_wininfo;
1009 buf->b_wininfo = wip->wi_next;
1010 free_wininfo(wip);
1011 }
1012}
1013
1014/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
1016 */
1017 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001018free_buffer_stuff(
1019 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001020 int free_options) // free options as well
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021{
1022 if (free_options)
1023 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001024 clear_wininfo(buf); // including window-local options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +02001026#ifdef FEAT_SPELL
1027 ga_clear(&buf->b_s.b_langp);
1028#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 }
1030#ifdef FEAT_EVAL
Bram Moolenaar79518e22017-02-17 16:31:35 +01001031 {
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001032 varnumber_T tick = CHANGEDTICK(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001033
Bram Moolenaarc667da52019-11-30 20:52:27 +01001034 vars_clear(&buf->b_vars->dv_hashtab); // free all buffer variables
Bram Moolenaar79518e22017-02-17 16:31:35 +01001035 hash_init(&buf->b_vars->dv_hashtab);
1036 init_changedtick(buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001037 CHANGEDTICK(buf) = tick;
Bram Moolenaarf10997a2020-01-03 21:00:02 +01001038 remove_listeners(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040#endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001041 uc_clear(&buf->b_ucmds); // clear local user commands
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042#ifdef FEAT_SIGNS
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001043 buf_delete_signs(buf, (char_u *)"*"); // delete any signs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00001045#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001046 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00001047#endif
Bram Moolenaar0c740e72022-07-25 19:07:04 +01001048#ifdef FEAT_PROP_POPUP
1049 ga_clear_strings(&buf->b_textprop_text);
1050#endif
zeertzjqc207fd22022-06-29 10:37:40 +01001051 map_clear_mode(buf, MAP_ALL_MODES, TRUE, FALSE); // clear local mappings
1052 map_clear_mode(buf, MAP_ALL_MODES, TRUE, TRUE); // clear local abbrevs
Bram Moolenaard23a8232018-02-10 18:45:26 +01001053 VIM_CLEAR(buf->b_start_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054}
1055
1056/*
Bram Moolenaar4882d982020-10-25 17:55:09 +01001057 * Free one wininfo_T.
1058 */
1059 void
1060free_wininfo(wininfo_T *wip)
1061{
1062 if (wip->wi_optset)
1063 {
1064 clear_winopt(&wip->wi_opt);
1065#ifdef FEAT_FOLDING
1066 deleteFoldRecurse(&wip->wi_folds);
1067#endif
1068 }
1069 vim_free(wip);
1070}
1071
1072/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 * Go to another buffer. Handles the result of the ATTENTION dialog.
1074 */
1075 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001076goto_buffer(
1077 exarg_T *eap,
1078 int start,
1079 int dir,
1080 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001082 bufref_T old_curbuf;
Bram Moolenaar188639d2022-04-04 16:57:21 +01001083 int save_sea = swap_exists_action;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001084
1085 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086
Bram Moolenaar188639d2022-04-04 16:57:21 +01001087 if (swap_exists_action == SEA_NONE)
1088 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
1090 start, dir, count, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
1092 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001093#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001094 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001095
Bram Moolenaarc667da52019-11-30 20:52:27 +01001096 // Reset the error/interrupt/exception state here so that
1097 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001098 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001099#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001100
Bram Moolenaarc667da52019-11-30 20:52:27 +01001101 // Quitting means closing the split window, nothing else.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 win_close(curwin, TRUE);
Bram Moolenaar188639d2022-04-04 16:57:21 +01001103 swap_exists_action = save_sea;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001104 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001105
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001106#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001107 // Restore the error/interrupt/exception state if not discarded by a
1108 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001109 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001110#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 }
1112 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001113 handle_swap_exists(&old_curbuf);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001114}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116/*
1117 * Handle the situation of swap_exists_action being set.
1118 * It is allowed for "old_curbuf" to be NULL or invalid.
1119 */
1120 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001121handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122{
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001123#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001124 cleanup_T cs;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001125#endif
1126#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001127 long old_tw = curbuf->b_p_tw;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001128#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001129 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001130
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 if (swap_exists_action == SEA_QUIT)
1132 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001133#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001134 // Reset the error/interrupt/exception state here so that
1135 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001136 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001137#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001138
Bram Moolenaarc667da52019-11-30 20:52:27 +01001139 // User selected Quit at ATTENTION prompt. Go back to previous
1140 // buffer. If that buffer is gone or the same as the current one,
1141 // open a new, empty buffer.
1142 swap_exists_action = SEA_NONE; // don't want it again
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001143 swap_exists_did_quit = TRUE;
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001144 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001145 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1146 || old_curbuf->br_buf == curbuf)
Bram Moolenaar1d97efc2021-07-04 13:27:11 +02001147 {
1148 // Block autocommands here because curwin->w_buffer is NULL.
1149 block_autocmds();
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001150 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar1d97efc2021-07-04 13:27:11 +02001151 unblock_autocmds();
1152 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001153 else
1154 buf = old_curbuf->br_buf;
1155 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001156 {
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001157 int old_msg_silent = msg_silent;
1158
1159 if (shortmess(SHM_FILEINFO))
1160 msg_silent = 1; // prevent fileinfo message
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001161 enter_buffer(buf);
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001162 // restore msg_silent, so that the command line will be shown
1163 msg_silent = old_msg_silent;
1164
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001165#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001166 if (old_tw != curbuf->b_p_tw)
1167 check_colorcolumn(curwin);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001168#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001169 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001170 // If "old_curbuf" is NULL we are in big trouble here...
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001171
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001172#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001173 // Restore the error/interrupt/exception state if not discarded by a
1174 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001175 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 }
1178 else if (swap_exists_action == SEA_RECOVER)
1179 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001180#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001181 // Reset the error/interrupt/exception state here so that
1182 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001183 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001184#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001185
Bram Moolenaarc667da52019-11-30 20:52:27 +01001186 // User selected Recover at ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 msg_scroll = TRUE;
Bram Moolenaar99499b12019-05-23 21:35:48 +02001188 ml_recover(FALSE);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001189 msg_puts("\n"); // don't overwrite the last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001191 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001192
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001193#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001194 // Restore the error/interrupt/exception state if not discarded by a
1195 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001196 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 }
1199 swap_exists_action = SEA_NONE;
1200}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202/*
Bram Moolenaara02471e2014-01-10 16:43:14 +01001203 * Make the current buffer empty.
1204 * Used when it is wiped out and it's the last buffer.
1205 */
1206 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001207empty_curbuf(
1208 int close_others,
1209 int forceit,
1210 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001211{
1212 int retval;
1213 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001214 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001215
1216 if (action == DOBUF_UNLOAD)
1217 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001218 emsg(_(e_cannot_unload_last_buffer));
Bram Moolenaara02471e2014-01-10 16:43:14 +01001219 return FAIL;
1220 }
1221
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001222 set_bufref(&bufref, buf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001223 if (close_others)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001224 // Close any other windows on this buffer, then make it empty.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001225 close_windows(buf, TRUE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001226
1227 setpcmark();
1228 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1229 forceit ? ECMD_FORCEIT : 0, curwin);
1230
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001231 // do_ecmd() may create a new buffer, then we have to delete
1232 // the old one. But do_ecmd() may have done that already, check
1233 // if the buffer still exists.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001234 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001235 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001236 if (!close_others)
1237 need_fileinfo = FALSE;
1238 return retval;
1239}
Bram Moolenaar94f01952018-09-01 15:30:03 +02001240
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241/*
1242 * Implementation of the commands for the buffer list.
1243 *
1244 * action == DOBUF_GOTO go to specified buffer
1245 * action == DOBUF_SPLIT split window and go to specified buffer
1246 * action == DOBUF_UNLOAD unload specified buffer(s)
1247 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1248 * action == DOBUF_WIPE delete specified buffer(s) really
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001249 * action == DOBUF_WIPE_REUSE idem, and add number to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 *
1251 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1252 * start == DOBUF_FIRST go to "count" buffer from first buffer
1253 * start == DOBUF_LAST go to "count" buffer from last buffer
1254 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1255 *
1256 * Return FAIL or OK.
1257 */
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001258 static int
1259do_buffer_ext(
Bram Moolenaar7454a062016-01-30 15:14:10 +01001260 int action,
1261 int start,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001262 int dir, // FORWARD or BACKWARD
1263 int count, // buffer number or number of buffers
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001264 int flags) // DOBUF_FORCEIT etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265{
1266 buf_T *buf;
1267 buf_T *bp;
1268 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001269 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270
1271 switch (start)
1272 {
1273 case DOBUF_FIRST: buf = firstbuf; break;
1274 case DOBUF_LAST: buf = lastbuf; break;
1275 default: buf = curbuf; break;
1276 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001277 if (start == DOBUF_MOD) // find next modified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 {
1279 while (count-- > 0)
1280 {
1281 do
1282 {
1283 buf = buf->b_next;
1284 if (buf == NULL)
1285 buf = firstbuf;
1286 }
1287 while (buf != curbuf && !bufIsChanged(buf));
1288 }
1289 if (!bufIsChanged(buf))
1290 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001291 emsg(_(e_no_modified_buffer_found));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 return FAIL;
1293 }
1294 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001295 else if (start == DOBUF_FIRST && count) // find specified buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 {
1297 while (buf != NULL && buf->b_fnum != count)
1298 buf = buf->b_next;
1299 }
1300 else
1301 {
1302 bp = NULL;
1303 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1304 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001305 // remember the buffer where we start, we come back there when all
1306 // buffers are unlisted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 if (bp == NULL)
1308 bp = buf;
1309 if (dir == FORWARD)
1310 {
1311 buf = buf->b_next;
1312 if (buf == NULL)
1313 buf = firstbuf;
1314 }
1315 else
1316 {
1317 buf = buf->b_prev;
1318 if (buf == NULL)
1319 buf = lastbuf;
1320 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001321 // don't count unlisted buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 if (unload || buf->b_p_bl)
1323 {
1324 --count;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001325 bp = NULL; // use this buffer as new starting point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 }
1327 if (bp == buf)
1328 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001329 // back where we started, didn't find anything.
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001330 emsg(_(e_there_is_no_listed_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 return FAIL;
1332 }
1333 }
1334 }
1335
Bram Moolenaarc667da52019-11-30 20:52:27 +01001336 if (buf == NULL) // could not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 {
1338 if (start == DOBUF_FIRST)
1339 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001340 // don't warn when deleting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 if (!unload)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001342 semsg(_(e_buffer_nr_does_not_exist), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 }
1344 else if (dir == FORWARD)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001345 emsg(_(e_cannot_go_beyond_last_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 else
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001347 emsg(_(e_cannot_go_before_first_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 return FAIL;
1349 }
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001350#ifdef FEAT_PROP_POPUP
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001351 if ((flags & DOBUF_NOPOPUP) && bt_popup(buf) && !bt_terminal(buf))
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001352 return OK;
1353#endif
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01001354 if ((action == DOBUF_GOTO || action == DOBUF_SPLIT)
1355 && (buf->b_flags & BF_DUMMY))
1356 {
1357 // disallow navigating to the dummy buffer
1358 semsg(_(e_buffer_nr_does_not_exist), count);
1359 return FAIL;
1360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361
1362#ifdef FEAT_GUI
1363 need_mouse_correct = TRUE;
1364#endif
1365
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001367 * delete buffer "buf" from memory and/or the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 */
1369 if (unload)
1370 {
1371 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001372 bufref_T bufref;
1373
Bram Moolenaar94f01952018-09-01 15:30:03 +02001374 if (!can_unload_buffer(buf))
Bram Moolenaara997b452018-04-17 23:24:06 +02001375 return FAIL;
Bram Moolenaara997b452018-04-17 23:24:06 +02001376
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001377 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378
Bram Moolenaarc667da52019-11-30 20:52:27 +01001379 // When unloading or deleting a buffer that's already unloaded and
1380 // unlisted: fail silently.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001381 if (action != DOBUF_WIPE && action != DOBUF_WIPE_REUSE
1382 && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 return FAIL;
1384
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001385 if ((flags & DOBUF_FORCEIT) == 0 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001387#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001388 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001390# ifdef FEAT_TERMINAL
1391 if (term_job_running(buf->b_term))
1392 {
1393 if (term_confirm_stop(buf) == FAIL)
1394 return FAIL;
1395 }
1396 else
1397# endif
1398 {
1399 dialog_changed(buf, FALSE);
1400 if (!bufref_valid(&bufref))
1401 // Autocommand deleted buffer, oops! It's not changed
1402 // now.
1403 return FAIL;
1404 // If it's still changed fail silently, the dialog already
1405 // mentioned why it fails.
1406 if (bufIsChanged(buf))
1407 return FAIL;
1408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001410 else
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001413 no_write_message_buf(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 return FAIL;
1415 }
1416 }
1417
Bram Moolenaarc667da52019-11-30 20:52:27 +01001418 // When closing the current buffer stop Visual mode.
Bram Moolenaar4930a762016-09-11 14:39:53 +02001419 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001420 end_visual_mode();
1421
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001422 // If deleting the last (listed) buffer, make it empty.
1423 // The last (listed) buffer cannot be unloaded.
Bram Moolenaar29323592016-07-24 22:04:11 +02001424 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 if (bp->b_p_bl && bp != buf)
1426 break;
1427 if (bp == NULL && buf == curbuf)
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001428 return empty_curbuf(TRUE, (flags & DOBUF_FORCEIT), action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001430 // If the deleted buffer is the current one, close the current window
1431 // (unless it's the only window). Repeat this so long as we end up in
1432 // a window with this buffer.
Bram Moolenaarf740b292006-02-16 22:11:02 +00001433 while (buf == curbuf
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001434 && !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
Bram Moolenaar459ca562016-11-10 18:16:33 +01001435 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001436 {
1437 if (win_close(curwin, FALSE) == FAIL)
1438 break;
1439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001441 // If the buffer to be deleted is not the current one, delete it here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 if (buf != curbuf)
1443 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001444 close_windows(buf, FALSE);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001445 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows <= 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001446 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 return OK;
1448 }
1449
1450 /*
1451 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001452 * There should be another, otherwise it would have been handled
1453 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001454 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 * Then prefer the buffer we most recently visited.
1456 * Else try to find one that is loaded, after the current buffer,
1457 * then before the current buffer.
1458 * Finally use any buffer.
1459 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001460 buf = NULL; // selected buffer
1461 bp = NULL; // used when no loaded buffer found
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001462 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1463 buf = au_new_curbuf.br_buf;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001464 else if (curwin->w_jumplistlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 {
1466 int jumpidx;
1467
1468 jumpidx = curwin->w_jumplistidx - 1;
1469 if (jumpidx < 0)
1470 jumpidx = curwin->w_jumplistlen - 1;
1471
1472 forward = jumpidx;
1473 while (jumpidx != curwin->w_jumplistidx)
1474 {
1475 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1476 if (buf != NULL)
1477 {
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001478 // Skip current and unlisted bufs. Also skip a quickfix
1479 // buffer, it might be deleted soon.
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001480 if (buf == curbuf || !buf->b_p_bl || bt_quickfix(buf))
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001481 buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 else if (buf->b_ml.ml_mfp == NULL)
1483 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001484 // skip unloaded buf, but may keep it for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 if (bp == NULL)
1486 bp = buf;
1487 buf = NULL;
1488 }
1489 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001490 if (buf != NULL) // found a valid buffer: stop searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001492 // advance to older entry in jump list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1494 break;
1495 if (--jumpidx < 0)
1496 jumpidx = curwin->w_jumplistlen - 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001497 if (jumpidx == forward) // List exhausted for sure
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 break;
1499 }
1500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501
Bram Moolenaarc667da52019-11-30 20:52:27 +01001502 if (buf == NULL) // No previous buffer, Try 2'nd approach
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 {
1504 forward = TRUE;
1505 buf = curbuf->b_next;
1506 for (;;)
1507 {
1508 if (buf == NULL)
1509 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001510 if (!forward) // tried both directions
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 break;
1512 buf = curbuf->b_prev;
1513 forward = FALSE;
1514 continue;
1515 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001516 // in non-help buffer, try to skip help buffers, and vv
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001517 if (buf->b_help == curbuf->b_help && buf->b_p_bl
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001518 && !bt_quickfix(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001520 if (buf->b_ml.ml_mfp != NULL) // found loaded buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001522 if (bp == NULL) // remember unloaded buf for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 bp = buf;
1524 }
1525 if (forward)
1526 buf = buf->b_next;
1527 else
1528 buf = buf->b_prev;
1529 }
1530 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001531 if (buf == NULL) // No loaded buffer, use unloaded one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 buf = bp;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001533 if (buf == NULL) // No loaded buffer, find listed one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001535 FOR_ALL_BUFFERS(buf)
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001536 if (buf->b_p_bl && buf != curbuf && !bt_quickfix(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 break;
1538 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001539 if (buf == NULL) // Still no buffer, just take one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 {
1541 if (curbuf->b_next != NULL)
1542 buf = curbuf->b_next;
1543 else
1544 buf = curbuf->b_prev;
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001545 if (bt_quickfix(buf))
1546 buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 }
1548 }
1549
Bram Moolenaara02471e2014-01-10 16:43:14 +01001550 if (buf == NULL)
1551 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001552 // Autocommands must have wiped out all other buffers. Only option
1553 // now is to make the current buffer empty.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001554 return empty_curbuf(FALSE, (flags & DOBUF_FORCEIT), action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001555 }
1556
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001558 * make "buf" the current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001560 if (action == DOBUF_SPLIT) // split window first
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001562 // If 'switchbuf' contains "useopen": jump to first window containing
1563 // "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00001564 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001565 return OK;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001566 // If 'switchbuf' contains "usetab": jump to first window in any tab
1567 // page containing "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00001568 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 return OK;
1570 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 return FAIL;
1572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573
Bram Moolenaarc667da52019-11-30 20:52:27 +01001574 // go to current buffer - nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 if (buf == curbuf)
1576 return OK;
1577
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001578 // Check if the current buffer may be abandoned.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001579 if (action == DOBUF_GOTO && !can_abandon(curbuf, (flags & DOBUF_FORCEIT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 {
1581#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001582 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001584# ifdef FEAT_TERMINAL
1585 if (term_job_running(curbuf->b_term))
1586 {
1587 if (term_confirm_stop(curbuf) == FAIL)
1588 return FAIL;
1589 // Manually kill the terminal here because this command will
1590 // hide it otherwise.
1591 free_terminal(curbuf);
1592 }
1593 else
1594# endif
1595 {
1596 bufref_T bufref;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001597
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001598 set_bufref(&bufref, buf);
1599 dialog_changed(curbuf, FALSE);
1600 if (!bufref_valid(&bufref))
1601 // Autocommand deleted buffer, oops!
1602 return FAIL;
1603
1604 if (bufIsChanged(curbuf))
1605 {
1606 no_write_message();
1607 return FAIL;
1608 }
1609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 }
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001611 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612#endif
1613 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001614 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 return FAIL;
1616 }
1617 }
1618
Bram Moolenaarc667da52019-11-30 20:52:27 +01001619 // Go to the other buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 set_curbuf(buf, action);
1621
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 if (action == DOBUF_SPLIT)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001623 RESET_BINDING(curwin); // reset 'scrollbind' and 'cursorbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001625#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001626 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 return FAIL;
1628#endif
1629
1630 return OK;
1631}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001633 int
1634do_buffer(
1635 int action,
1636 int start,
1637 int dir, // FORWARD or BACKWARD
1638 int count, // buffer number or number of buffers
1639 int forceit) // TRUE when using !
1640{
1641 return do_buffer_ext(action, start, dir, count,
1642 forceit ? DOBUF_FORCEIT : 0);
1643}
1644
1645/*
1646 * do_bufdel() - delete or unload buffer(s)
1647 *
1648 * addr_count == 0: ":bdel" - delete current buffer
1649 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1650 * buffer "end_bnr", then any other arguments.
1651 * addr_count == 2: ":N,N bdel" - delete buffers in range
1652 *
1653 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1654 * DOBUF_DEL (":bdel")
1655 *
1656 * Returns error message or NULL
1657 */
1658 char *
1659do_bufdel(
1660 int command,
1661 char_u *arg, // pointer to extra arguments
1662 int addr_count,
1663 int start_bnr, // first buffer number in a range
1664 int end_bnr, // buffer nr or last buffer nr in a range
1665 int forceit)
1666{
1667 int do_current = 0; // delete current buffer?
1668 int deleted = 0; // number of buffers deleted
1669 char *errormsg = NULL; // return value
1670 int bnr; // buffer number
1671 char_u *p;
1672
1673 if (addr_count == 0)
1674 {
1675 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1676 }
1677 else
1678 {
1679 if (addr_count == 2)
1680 {
1681 if (*arg) // both range and argument is not allowed
Bram Moolenaar74409f62022-01-01 15:58:22 +00001682 return ex_errmsg(e_trailing_characters_str, arg);
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001683 bnr = start_bnr;
1684 }
1685 else // addr_count == 1
1686 bnr = end_bnr;
1687
1688 for ( ;!got_int; ui_breakcheck())
1689 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001690 // Delete the current buffer last, otherwise when the
1691 // current buffer is deleted, the next buffer becomes
1692 // the current one and will be loaded, which may then
1693 // also be deleted, etc.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001694 if (bnr == curbuf->b_fnum)
1695 do_current = bnr;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00001696 else if (do_buffer_ext(command, DOBUF_FIRST, FORWARD, bnr,
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001697 DOBUF_NOPOPUP | (forceit ? DOBUF_FORCEIT : 0)) == OK)
1698 ++deleted;
1699
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001700 // find next buffer number to delete/unload
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001701 if (addr_count == 2)
1702 {
1703 if (++bnr > end_bnr)
1704 break;
1705 }
1706 else // addr_count == 1
1707 {
1708 arg = skipwhite(arg);
1709 if (*arg == NUL)
1710 break;
1711 if (!VIM_ISDIGIT(*arg))
1712 {
1713 p = skiptowhite_esc(arg);
1714 bnr = buflist_findpat(arg, p,
1715 command == DOBUF_WIPE || command == DOBUF_WIPE_REUSE,
1716 FALSE, FALSE);
1717 if (bnr < 0) // failed
1718 break;
1719 arg = p;
1720 }
1721 else
1722 bnr = getdigits(&arg);
1723 }
1724 }
1725 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1726 FORWARD, do_current, forceit) == OK)
1727 ++deleted;
1728
1729 if (deleted == 0)
1730 {
1731 if (command == DOBUF_UNLOAD)
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001732 STRCPY(IObuff, _(e_no_buffers_were_unloaded));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001733 else if (command == DOBUF_DEL)
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001734 STRCPY(IObuff, _(e_no_buffers_were_deleted));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001735 else
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001736 STRCPY(IObuff, _(e_no_buffers_were_wiped_out));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001737 errormsg = (char *)IObuff;
1738 }
1739 else if (deleted >= p_report)
1740 {
1741 if (command == DOBUF_UNLOAD)
1742 smsg(NGETTEXT("%d buffer unloaded",
1743 "%d buffers unloaded", deleted), deleted);
1744 else if (command == DOBUF_DEL)
1745 smsg(NGETTEXT("%d buffer deleted",
1746 "%d buffers deleted", deleted), deleted);
1747 else
1748 smsg(NGETTEXT("%d buffer wiped out",
1749 "%d buffers wiped out", deleted), deleted);
1750 }
1751 }
1752
1753
1754 return errormsg;
1755}
1756
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757/*
1758 * Set current buffer to "buf". Executes autocommands and closes current
1759 * buffer. "action" tells how to close the current buffer:
1760 * DOBUF_GOTO free or hide it
1761 * DOBUF_SPLIT nothing
1762 * DOBUF_UNLOAD unload it
1763 * DOBUF_DEL delete it
1764 * DOBUF_WIPE wipe it out
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001765 * DOBUF_WIPE_REUSE wipe it out and add to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 */
1767 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001768set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769{
1770 buf_T *prevbuf;
1771 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001772 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001773#ifdef FEAT_SYN_HL
1774 long old_tw = curbuf->b_p_tw;
1775#endif
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001776 bufref_T newbufref;
1777 bufref_T prevbufref;
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001778 int valid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779
1780 setpcmark();
Bram Moolenaare1004402020-10-24 20:49:43 +02001781 if ((cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001782 curwin->w_alt_fnum = curbuf->b_fnum; // remember alternate file
1783 buflist_altfpos(curwin); // remember curpos
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784
Bram Moolenaarc667da52019-11-30 20:52:27 +01001785 // Don't restart Select mode after switching to another buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787
Bram Moolenaarc667da52019-11-30 20:52:27 +01001788 // close_windows() or apply_autocmds() may change curbuf and wipe out "buf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 prevbuf = curbuf;
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001790 set_bufref(&prevbufref, prevbuf);
1791 set_bufref(&newbufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792
Bram Moolenaar983d83f2021-02-07 12:12:43 +01001793 // Autocommands may delete the current buffer and/or the buffer we want to
1794 // go to. In those cases don't close the buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001795 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001796 || (bufref_valid(&prevbufref)
1797 && bufref_valid(&newbufref)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001798#ifdef FEAT_EVAL
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001799 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001801 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001803#ifdef FEAT_SYN_HL
1804 if (prevbuf == curwin->w_buffer)
1805 reset_synblock(curwin);
1806#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001808 close_windows(prevbuf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001809#if defined(FEAT_EVAL)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001810 if (bufref_valid(&prevbufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811#else
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001812 if (bufref_valid(&prevbufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001814 {
Bram Moolenaare25865a2012-07-06 16:22:02 +02001815 win_T *previouswin = curwin;
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001816
Bram Moolenaare615db02022-01-20 21:00:54 +00001817 // Do not sync when in Insert mode and the buffer is open in
1818 // another window, might be a timer doing something in another
1819 // window.
1820 if (prevbuf == curbuf
Bram Moolenaar24959102022-05-07 20:01:16 +01001821 && ((State & MODE_INSERT) == 0 || curbuf->b_nwindows <= 1))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001822 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1824 unload ? action : (action == DOBUF_GOTO
Bram Moolenaareb44a682017-08-03 22:44:55 +02001825 && !buf_hide(prevbuf)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001826 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0,
1827 FALSE, FALSE);
Bram Moolenaare25865a2012-07-06 16:22:02 +02001828 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001829 // autocommands changed curwin, Grr!
Bram Moolenaare25865a2012-07-06 16:22:02 +02001830 curwin = previouswin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001833 // An autocommand may have deleted "buf", already entered it (e.g., when
1834 // it did ":bunload") or aborted the script processing.
1835 // If curwin->w_buffer is null, enter_buffer() will make it valid again
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001836 valid = buf_valid(buf);
1837 if ((valid && buf != curbuf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001838#ifdef FEAT_EVAL
Bram Moolenaar4033c552017-09-16 20:54:51 +02001839 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001841 ) || curwin->w_buffer == NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001842 {
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001843 // If the buffer is not valid but curwin->w_buffer is NULL we must
1844 // enter some buffer. Using the last one is hopefully OK.
1845 if (!valid)
1846 enter_buffer(lastbuf);
1847 else
1848 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001849#ifdef FEAT_SYN_HL
1850 if (old_tw != curbuf->b_p_tw)
1851 check_colorcolumn(curwin);
1852#endif
1853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854}
1855
1856/*
1857 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001858 * Old curbuf must have been abandoned already! This also means "curbuf" may
1859 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001862enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863{
Bram Moolenaarcfeb8a52022-08-13 14:09:44 +01001864 // when closing the current buffer stop Visual mode
1865 if (VIsual_active
1866#if defined(EXITFREE)
1867 && !entered_free_all_mem
1868#endif
1869 )
1870 end_visual_mode();
1871
Bram Moolenaar010ee962019-09-25 20:37:36 +02001872 // Get the buffer in the current window.
1873 curwin->w_buffer = buf;
1874 curbuf = buf;
1875 ++curbuf->b_nwindows;
1876
1877 // Copy buffer and window local option values. Not for a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1879 if (!buf->b_help)
1880 get_winopts(buf);
1881#ifdef FEAT_FOLDING
1882 else
Bram Moolenaar010ee962019-09-25 20:37:36 +02001883 // Remove all folds in the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 clearFolding(curwin);
Bram Moolenaar010ee962019-09-25 20:37:36 +02001885 foldUpdateAll(curwin); // update folds (later).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886#endif
1887
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001889 if (curwin->w_p_diff)
1890 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891#endif
1892
Bram Moolenaara971b822011-09-14 14:43:25 +02001893#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001894 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001895#endif
1896
Bram Moolenaarc667da52019-11-30 20:52:27 +01001897 // Cursor on first line by default.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 curwin->w_cursor.lnum = 1;
1899 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001902 curwin->w_topline_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903
Bram Moolenaarc667da52019-11-30 20:52:27 +01001904 // mark cursor position as being invalid
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001905 curwin->w_valid = 0;
1906
Bram Moolenaar9cea87c2018-09-21 16:59:45 +02001907 buflist_setfpos(curbuf, curwin, curbuf->b_last_cursor.lnum,
1908 curbuf->b_last_cursor.col, TRUE);
1909
Bram Moolenaarc667da52019-11-30 20:52:27 +01001910 // Make sure the buffer is loaded.
1911 if (curbuf->b_ml.ml_mfp == NULL) // need to load the file
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001912 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001913 // If there is no filetype, allow for detecting one. Esp. useful for
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001914 // ":ball" used in an autocommand. If there already is a filetype we
Bram Moolenaarc667da52019-11-30 20:52:27 +01001915 // might prefer to keep it.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001916 if (*curbuf->b_p_ft == NUL)
1917 did_filetype = FALSE;
1918
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001919 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 else
1922 {
Bram Moolenaareda65222019-05-16 20:29:44 +02001923 if (!msg_silent && !shortmess(SHM_FILEINFO))
1924 need_fileinfo = TRUE; // display file info after redraw
1925
1926 // check if file changed
1927 (void)buf_check_timestamp(curbuf, FALSE);
1928
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001930#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001932#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1934 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 }
1936
Bram Moolenaarc667da52019-11-30 20:52:27 +01001937 // If autocommands did not change the cursor position, restore cursor lnum
1938 // and possibly cursor col.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 if (curwin->w_cursor.lnum == 1 && inindent(0))
1940 buflist_getfpos();
1941
Bram Moolenaarc667da52019-11-30 20:52:27 +01001942 check_arg_idx(curwin); // check for valid arg_idx
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 maketitle();
Bram Moolenaarc667da52019-11-30 20:52:27 +01001944 // when autocmds didn't change it
Bram Moolenaard4153d42008-11-15 15:06:17 +00001945 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001946 scroll_cursor_halfway(FALSE); // redisplay at correct position
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947
Bram Moolenaar009b2592004-10-24 19:18:58 +00001948#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarc667da52019-11-30 20:52:27 +01001949 // Send fileOpened event because we've changed buffers.
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001950 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001951#endif
1952
Bram Moolenaarc667da52019-11-30 20:52:27 +01001953 // Change directories when the 'acd' option is set.
Bram Moolenaar6f470022018-04-10 18:47:20 +02001954 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955
1956#ifdef FEAT_KEYMAP
1957 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001958 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001960#ifdef FEAT_SPELL
Bram Moolenaarc667da52019-11-30 20:52:27 +01001961 // May need to set the spell language. Can only do this after the buffer
1962 // has been properly setup.
Bram Moolenaar860cae12010-06-05 23:22:07 +02001963 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1964 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001965#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001966#ifdef FEAT_VIMINFO
1967 curbuf->b_last_used = vim_time();
1968#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001969
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001970 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971}
1972
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001973#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1974/*
1975 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001976 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001977 */
1978 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001979do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001980{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001981 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001982 && curbuf->b_ffname != NULL
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001983 && vim_chdirfile(curbuf->b_ffname, "auto") == OK)
Bram Moolenaar05268152021-11-18 18:53:45 +00001984 {
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001985 shorten_fnames(TRUE);
Bram Moolenaar05268152021-11-18 18:53:45 +00001986 last_chdir_reason = "autochdir";
1987 }
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001988}
1989#endif
1990
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001991 static void
1992no_write_message_buf(buf_T *buf UNUSED)
1993{
1994#ifdef FEAT_TERMINAL
1995 if (term_job_running(buf->b_term))
1996 emsg(_(e_job_still_running_add_bang_to_end_the_job));
1997 else
1998#endif
1999 semsg(_(e_no_write_since_last_change_for_buffer_nr_add_bang_to_override),
2000 buf->b_fnum);
2001}
2002
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002003 void
2004no_write_message(void)
2005{
2006#ifdef FEAT_TERMINAL
2007 if (term_job_running(curbuf->b_term))
Bram Moolenaarf1474d82021-12-31 19:59:55 +00002008 emsg(_(e_job_still_running_add_bang_to_end_the_job));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002009 else
2010#endif
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002011 emsg(_(e_no_write_since_last_change_add_bang_to_override));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002012}
2013
2014 void
Bram Moolenaar7a760922018-02-19 23:10:02 +01002015no_write_message_nobang(buf_T *buf UNUSED)
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002016{
2017#ifdef FEAT_TERMINAL
Bram Moolenaar7a760922018-02-19 23:10:02 +01002018 if (term_job_running(buf->b_term))
Bram Moolenaarf1474d82021-12-31 19:59:55 +00002019 emsg(_(e_job_still_running));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002020 else
2021#endif
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002022 emsg(_(e_no_write_since_last_change));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002023}
2024
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025/*
2026 * functions for dealing with the buffer list
2027 */
2028
2029/*
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002030 * Return TRUE if the current buffer is empty, unnamed, unmodified and used in
2031 * only one window. That means it can be re-used.
2032 */
2033 int
2034curbuf_reusable(void)
2035{
2036 return (curbuf != NULL
2037 && curbuf->b_ffname == NULL
2038 && curbuf->b_nwindows <= 1
2039 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY())
Bram Moolenaar39803d82019-04-07 12:04:51 +02002040 && !bt_quickfix(curbuf)
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002041 && !curbufIsChanged());
2042}
2043
2044/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 * Add a file name to the buffer list. Return a pointer to the buffer.
2046 * If the same file name already exists return a pointer to that buffer.
2047 * If it does not exist, or if fname == NULL, a new entry is created.
2048 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
2049 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
2050 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02002051 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02002052 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
2053 * if the buffer already exists.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002054 * If (flags & BLN_REUSE) is TRUE, may use buffer number from "buf_reuse".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 * This is the ONLY way to create a new buffer.
2056 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002058buflist_new(
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002059 char_u *ffname_arg, // full path of fname or relative
2060 char_u *sfname_arg, // short fname or NULL
2061 linenr_T lnum, // preferred cursor line
2062 int flags) // BLN_ defines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002064 char_u *ffname = ffname_arg;
2065 char_u *sfname = sfname_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 buf_T *buf;
2067#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002068 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069#endif
2070
Bram Moolenaar480778b2016-07-14 22:09:39 +02002071 if (top_file_num == 1)
2072 hash_init(&buf_hashtab);
2073
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002074 fname_expand(curbuf, &ffname, &sfname); // will allocate ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075
2076 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002077 * If the file name already exists in the list, update the entry.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 */
2079#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002080 // On Unix we can use inode numbers when the file exists. Works better
2081 // for hard links.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
2083 st.st_dev = (dev_T)-1;
2084#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02002085 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086#ifdef UNIX
2087 buflist_findname_stat(ffname, &st)
2088#else
2089 buflist_findname(ffname)
2090#endif
2091 ) != NULL)
2092 {
2093 vim_free(ffname);
2094 if (lnum != 0)
Bram Moolenaar89b693e2020-10-25 17:09:50 +01002095 buflist_setfpos(buf, (flags & BLN_NOCURWIN) ? NULL : curwin,
2096 lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02002097
2098 if ((flags & BLN_NOOPT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002099 // copy the options now, if 'cpo' doesn't have 's' and not done
2100 // already
Bram Moolenaar82404332016-07-10 17:00:38 +02002101 buf_copy_options(buf, 0);
2102
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 if ((flags & BLN_LISTED) && !buf->b_p_bl)
2104 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002105 bufref_T bufref;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002106
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 buf->b_p_bl = TRUE;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002108 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002110 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002111 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002112 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002113 return NULL;
2114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 }
2116 return buf;
2117 }
2118
2119 /*
2120 * If the current buffer has no name and no contents, use the current
2121 * buffer. Otherwise: Need to allocate a new buffer structure.
2122 *
2123 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00002124 * (A spell file buffer is allocated in spell.c, but that's not a normal
2125 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 */
2127 buf = NULL;
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002128 if ((flags & BLN_CURBUF) && curbuf_reusable())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 {
2130 buf = curbuf;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002131 // It's like this buffer is deleted. Watch out for autocommands that
2132 // change curbuf! If that happens, allocate a new buffer anyway.
Charlie Grovesfef44852022-04-19 16:24:12 +01002133 buf_freeall(buf, BFA_WIPE | BFA_DEL);
2134 if (buf != curbuf) // autocommands deleted the buffer!
2135 return NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002136#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002137 if (aborting()) // autocmds may abort script processing
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002138 {
2139 vim_free(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 return NULL;
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 }
2144 if (buf != curbuf || curbuf == NULL)
2145 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002146 buf = ALLOC_CLEAR_ONE(buf_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 if (buf == NULL)
2148 {
2149 vim_free(ffname);
2150 return NULL;
2151 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02002152#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002153 // init b: variables
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01002154 buf->b_vars = dict_alloc_id(aid_newbuf_bvars);
Bram Moolenaar429fa852013-04-15 12:27:36 +02002155 if (buf->b_vars == NULL)
2156 {
2157 vim_free(ffname);
2158 vim_free(buf);
2159 return NULL;
2160 }
2161 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
2162#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01002163 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 }
2165
2166 if (ffname != NULL)
2167 {
2168 buf->b_ffname = ffname;
2169 buf->b_sfname = vim_strsave(sfname);
2170 }
2171
2172 clear_wininfo(buf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002173 buf->b_wininfo = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174
2175 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
2176 || buf->b_wininfo == NULL)
2177 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002178 if (buf->b_sfname != buf->b_ffname)
2179 VIM_CLEAR(buf->b_sfname);
2180 else
2181 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01002182 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 if (buf != curbuf)
2184 free_buffer(buf);
2185 return NULL;
2186 }
2187
2188 if (buf == curbuf)
2189 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002190 free_buffer_stuff(buf, FALSE); // delete local variables et al.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002191
Bram Moolenaarc667da52019-11-30 20:52:27 +01002192 // Init the options.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002193 buf->b_p_initialized = FALSE;
2194 buf_copy_options(buf, BCO_ENTER);
2195
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196#ifdef FEAT_KEYMAP
Bram Moolenaarc667da52019-11-30 20:52:27 +01002197 // need to reload lmaps and set b:keymap_name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 curbuf->b_kmap_state |= KEYMAP_INIT;
2199#endif
2200 }
2201 else
2202 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002203 // put the new buffer at the end of the buffer list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 buf->b_next = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002205 if (firstbuf == NULL) // buffer list is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 {
2207 buf->b_prev = NULL;
2208 firstbuf = buf;
2209 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002210 else // append new buffer at end of list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 {
2212 lastbuf->b_next = buf;
2213 buf->b_prev = lastbuf;
2214 }
2215 lastbuf = buf;
2216
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002217 if ((flags & BLN_REUSE) && buf_reuse.ga_len > 0)
2218 {
2219 // Recycle a previously used buffer number. Used for buffers which
2220 // are normally hidden, e.g. in a popup window. Avoids that the
2221 // buffer number grows rapidly.
2222 --buf_reuse.ga_len;
2223 buf->b_fnum = ((int *)buf_reuse.ga_data)[buf_reuse.ga_len];
Bram Moolenaar99ebf222019-12-10 23:44:48 +01002224
2225 // Move buffer to the right place in the buffer list.
2226 while (buf->b_prev != NULL && buf->b_fnum < buf->b_prev->b_fnum)
2227 {
2228 buf_T *prev = buf->b_prev;
2229
2230 prev->b_next = buf->b_next;
2231 if (prev->b_next != NULL)
2232 prev->b_next->b_prev = prev;
2233 buf->b_next = prev;
2234 buf->b_prev = prev->b_prev;
2235 if (buf->b_prev != NULL)
2236 buf->b_prev->b_next = buf;
2237 prev->b_prev = buf;
2238 if (lastbuf == buf)
2239 lastbuf = prev;
2240 if (firstbuf == prev)
2241 firstbuf = buf;
2242 }
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002243 }
2244 else
2245 buf->b_fnum = top_file_num++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002246 if (top_file_num < 0) // wrap around (may cause duplicates)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002248 emsg(_("W14: Warning: List of file names overflow"));
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002249 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 {
2251 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002252 ui_delay(3001L, TRUE); // make sure it is noticed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 }
2254 top_file_num = 1;
2255 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002256 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002258 // Always copy the options from the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 buf_copy_options(buf, BCO_ALWAYS);
2260 }
2261
2262 buf->b_wininfo->wi_fpos.lnum = lnum;
2263 buf->b_wininfo->wi_win = curwin;
2264
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002265#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002266 hash_init(&buf->b_s.b_keywtab);
2267 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268#endif
2269
2270 buf->b_fname = buf->b_sfname;
2271#ifdef UNIX
2272 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002273 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 else
2275 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002276 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 buf->b_dev = st.st_dev;
2278 buf->b_ino = st.st_ino;
2279 }
2280#endif
2281 buf->b_u_synced = TRUE;
2282 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002283 if (flags & BLN_DUMMY)
2284 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 buf_clear_file(buf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002286 clrallmarks(buf); // clear marks
2287 fmarks_check_names(buf); // check file marks for this file
2288 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; // init 'buflisted'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 if (!(flags & BLN_DUMMY))
2290 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002291 bufref_T bufref;
2292
Bram Moolenaarc667da52019-11-30 20:52:27 +01002293 // Tricky: these autocommands may change the buffer list. They could
2294 // also split the window with re-using the one empty buffer. This may
2295 // result in unexpectedly losing the empty buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002296 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002297 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002298 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002299 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002301 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002302 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002303 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002304 return NULL;
2305 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002306#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002307 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311
2312 return buf;
2313}
2314
2315/*
2316 * Free the memory for the options of a buffer.
2317 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2318 * 'fileencoding'.
2319 */
2320 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002321free_buf_options(
2322 buf_T *buf,
2323 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324{
2325 if (free_p_ff)
2326 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 clear_string_option(&buf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 clear_string_option(&buf->b_p_bh);
2330 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 }
2332#ifdef FEAT_FIND_ID
2333 clear_string_option(&buf->b_p_def);
2334 clear_string_option(&buf->b_p_inc);
2335# ifdef FEAT_EVAL
2336 clear_string_option(&buf->b_p_inex);
2337# endif
2338#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002339#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 clear_string_option(&buf->b_p_inde);
2341 clear_string_option(&buf->b_p_indk);
2342#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002343#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2344 clear_string_option(&buf->b_p_bexpr);
2345#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002346#if defined(FEAT_CRYPT)
2347 clear_string_option(&buf->b_p_cm);
2348#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002349 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002350#if defined(FEAT_EVAL)
2351 clear_string_option(&buf->b_p_fex);
2352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353#ifdef FEAT_CRYPT
Bram Moolenaar131530a2021-07-29 20:37:49 +02002354# ifdef FEAT_SODIUM
K.Takata1a8825d2022-01-19 13:32:57 +00002355 if ((buf->b_p_key != NULL) && (*buf->b_p_key != NUL) &&
2356 (crypt_get_method_nr(buf) == CRYPT_M_SOD))
2357 crypt_sodium_munlock(buf->b_p_key, STRLEN(buf->b_p_key));
Bram Moolenaar131530a2021-07-29 20:37:49 +02002358# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 clear_string_option(&buf->b_p_key);
2360#endif
2361 clear_string_option(&buf->b_p_kp);
2362 clear_string_option(&buf->b_p_mps);
2363 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002364 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 clear_string_option(&buf->b_p_isk);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002366#ifdef FEAT_VARTABS
2367 clear_string_option(&buf->b_p_vsts);
Bram Moolenaarbdace832019-03-02 10:13:42 +01002368 vim_free(buf->b_p_vsts_nopaste);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002369 buf->b_p_vsts_nopaste = NULL;
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00002370 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002371 clear_string_option(&buf->b_p_vts);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002372 VIM_CLEAR(buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002373#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374#ifdef FEAT_KEYMAP
2375 clear_string_option(&buf->b_p_keymap);
Bram Moolenaar50138322018-01-28 17:05:16 +01002376 keymap_clear(&buf->b_kmap_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 ga_clear(&buf->b_kmap_ga);
2378#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 clear_string_option(&buf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380#ifdef FEAT_FOLDING
2381 clear_string_option(&buf->b_p_cms);
2382#endif
2383 clear_string_option(&buf->b_p_nf);
2384#ifdef FEAT_SYN_HL
2385 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002386 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002387#endif
2388#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002389 clear_string_option(&buf->b_s.b_p_spc);
2390 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002391 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002392 buf->b_s.b_cap_prog = NULL;
2393 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02002394 clear_string_option(&buf->b_s.b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 clear_string_option(&buf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 clear_string_option(&buf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 clear_string_option(&buf->b_p_cink);
2399 clear_string_option(&buf->b_p_cino);
Bram Moolenaar49846fb2022-10-15 16:05:33 +01002400 clear_string_option(&buf->b_p_lop);
Tom Praschan3506cf32022-04-07 12:39:08 +01002401 clear_string_option(&buf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 clear_string_option(&buf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 clear_string_option(&buf->b_p_cpt);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002404#ifdef FEAT_COMPL_FUNC
2405 clear_string_option(&buf->b_p_cfu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002406 free_callback(&buf->b_cfu_cb);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002407 clear_string_option(&buf->b_p_ofu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002408 free_callback(&buf->b_ofu_cb);
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +01002409 clear_string_option(&buf->b_p_tsrfu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002410 free_callback(&buf->b_tsrfu_cb);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412#ifdef FEAT_QUICKFIX
2413 clear_string_option(&buf->b_p_gp);
2414 clear_string_option(&buf->b_p_mp);
2415 clear_string_option(&buf->b_p_efm);
2416#endif
2417 clear_string_option(&buf->b_p_ep);
2418 clear_string_option(&buf->b_p_path);
2419 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002420 clear_string_option(&buf->b_p_tc);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002421#ifdef FEAT_EVAL
2422 clear_string_option(&buf->b_p_tfu);
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +00002423 free_callback(&buf->b_tfu_cb);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002424#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 clear_string_option(&buf->b_p_dict);
2426 clear_string_option(&buf->b_p_tsr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002427 clear_string_option(&buf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002429 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002430 clear_string_option(&buf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002431 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002432 clear_string_option(&buf->b_p_menc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433}
2434
2435/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002436 * Get alternate file "n".
2437 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2438 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 * if (options & GETF_SETMARK) call setpcmark()
2440 * if (options & GETF_ALT) we are jumping to an alternate file.
2441 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2442 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002443 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 */
2445 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002446buflist_getfile(
2447 int n,
2448 linenr_T lnum,
2449 int options,
2450 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451{
2452 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 pos_T *fpos;
2455 colnr_T col;
2456
2457 buf = buflist_findnr(n);
2458 if (buf == NULL)
2459 {
2460 if ((options & GETF_ALT) && n == 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02002461 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 else
Bram Moolenaare1242042021-12-16 20:56:57 +00002463 semsg(_(e_buffer_nr_not_found), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 return FAIL;
2465 }
2466
Bram Moolenaarc667da52019-11-30 20:52:27 +01002467 // if alternate file is the current buffer, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 if (buf == curbuf)
2469 return OK;
2470
Bram Moolenaar71223e22022-05-30 15:23:09 +01002471 if (text_or_buf_locked())
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002472 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473
Bram Moolenaarc667da52019-11-30 20:52:27 +01002474 // altfpos may be changed by getfile(), get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 if (lnum == 0)
2476 {
2477 fpos = buflist_findfpos(buf);
2478 lnum = fpos->lnum;
2479 col = fpos->col;
2480 }
2481 else
2482 col = 0;
2483
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 if (options & GETF_SWITCH)
2485 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002486 // If 'switchbuf' contains "useopen": jump to first window containing
2487 // "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00002488 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002490
Bram Moolenaarc667da52019-11-30 20:52:27 +01002491 // If 'switchbuf' contains "usetab": jump to first window in any tab
2492 // page containing "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00002493 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002494 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002495
Bram Moolenaarc667da52019-11-30 20:52:27 +01002496 // If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2497 // current buffer isn't empty: open new tab or window
Bram Moolenaara594d772015-06-19 14:41:49 +02002498 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002499 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002501 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002502 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002503 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2504 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002506 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 }
2508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509
2510 ++RedrawingDisabled;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002511 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2512 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 {
2514 --RedrawingDisabled;
2515
Bram Moolenaarc667da52019-11-30 20:52:27 +01002516 // cursor is at to BOL and w_cursor.lnum is checked due to getfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 if (!p_sol && col != 0)
2518 {
2519 curwin->w_cursor.col = col;
2520 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 curwin->w_set_curswant = TRUE;
2523 }
2524 return OK;
2525 }
2526 --RedrawingDisabled;
2527 return FAIL;
2528}
2529
2530/*
2531 * go to the last know line number for the current buffer
2532 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002533 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002534buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535{
2536 pos_T *fpos;
2537
2538 fpos = buflist_findfpos(curbuf);
2539
2540 curwin->w_cursor.lnum = fpos->lnum;
2541 check_cursor_lnum();
2542
2543 if (p_sol)
2544 curwin->w_cursor.col = 0;
2545 else
2546 {
2547 curwin->w_cursor.col = fpos->col;
2548 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 curwin->w_set_curswant = TRUE;
2551 }
2552}
2553
Martin Tournoijba43e762022-10-13 22:12:15 +01002554#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar81695252004-12-29 20:58:21 +00002555/*
2556 * Find file in buffer list by name (it has to be for the current window).
2557 * Returns NULL if not found.
2558 */
2559 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002560buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002561{
2562 char_u *ffname;
2563 buf_T *buf = NULL;
2564
Bram Moolenaarc667da52019-11-30 20:52:27 +01002565 // First make the name into a full path name
Bram Moolenaar81695252004-12-29 20:58:21 +00002566 ffname = FullName_save(fname,
2567#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002568 TRUE // force expansion, get rid of symbolic links
Bram Moolenaar81695252004-12-29 20:58:21 +00002569#else
2570 FALSE
2571#endif
2572 );
2573 if (ffname != NULL)
2574 {
2575 buf = buflist_findname(ffname);
2576 vim_free(ffname);
2577 }
2578 return buf;
2579}
2580#endif
2581
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582/*
2583 * Find file in buffer list by name (it has to be for the current window).
2584 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002585 * Skips dummy buffers.
2586 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 */
2588 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002589buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590{
2591#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002592 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593
2594 if (mch_stat((char *)ffname, &st) < 0)
2595 st.st_dev = (dev_T)-1;
2596 return buflist_findname_stat(ffname, &st);
2597}
2598
2599/*
2600 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2601 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002602 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 */
2604 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002605buflist_findname_stat(
2606 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002607 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608{
2609#endif
2610 buf_T *buf;
2611
Bram Moolenaarc667da52019-11-30 20:52:27 +01002612 // Start at the last buffer, expect to find a match sooner.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002613 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaar81695252004-12-29 20:58:21 +00002614 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615#ifdef UNIX
2616 , stp
2617#endif
2618 ))
2619 return buf;
2620 return NULL;
2621}
2622
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623/*
2624 * Find file in buffer list by a regexp pattern.
2625 * Return fnum of the found buffer.
2626 * Return < 0 for error.
2627 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002629buflist_findpat(
2630 char_u *pattern,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002631 char_u *pattern_end, // pointer to first char after pattern
2632 int unlisted, // find unlisted buffers
2633 int diffmode UNUSED, // find diff-mode buffers only
2634 int curtab_only) // find buffers in current tab only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635{
2636 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 int match = -1;
2638 int find_listed;
2639 char_u *pat;
2640 char_u *patend;
2641 int attempt;
2642 char_u *p;
2643 int toggledollar;
2644
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002645 // "%" is current file, "%%" or "#" is alternate file
2646 if ((pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2647 || (in_vim9script() && pattern_end == pattern + 2
2648 && pattern[0] == '%' && pattern[1] == '%'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 {
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002650 if (*pattern == '#' || pattern_end == pattern + 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 match = curwin->w_alt_fnum;
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002652 else
2653 match = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654#ifdef FEAT_DIFF
2655 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2656 match = -1;
2657#endif
2658 }
2659
2660 /*
2661 * Try four ways of matching a listed buffer:
2662 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002663 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 * attempt == 2: with '$' at end (only match at end)
2665 * attempt == 3: with '^' at start and '$' at end (only full match)
2666 * Repeat this for finding an unlisted buffer if there was no matching
2667 * listed buffer.
2668 */
2669 else
2670 {
2671 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2672 if (pat == NULL)
2673 return -1;
2674 patend = pat + STRLEN(pat) - 1;
2675 toggledollar = (patend > pat && *patend == '$');
2676
Bram Moolenaarc667da52019-11-30 20:52:27 +01002677 // First try finding a listed buffer. If not found and "unlisted"
2678 // is TRUE, try finding an unlisted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 find_listed = TRUE;
2680 for (;;)
2681 {
2682 for (attempt = 0; attempt <= 3; ++attempt)
2683 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002684 regmatch_T regmatch;
2685
Bram Moolenaarc667da52019-11-30 20:52:27 +01002686 // may add '^' and '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 if (toggledollar)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002688 *patend = (attempt < 2) ? NUL : '$'; // add/remove '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 p = pat;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002690 if (*p == '^' && !(attempt & 1)) // add/remove '^'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 ++p;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01002692 regmatch.regprog = vim_regcomp(p, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002694 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002695 {
2696 if (regmatch.regprog == NULL)
2697 {
2698 // invalid pattern, possibly after switching engine
2699 vim_free(pat);
2700 return -1;
2701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 if (buf->b_p_bl == find_listed
2703#ifdef FEAT_DIFF
2704 && (!diffmode || diff_mode_buf(buf))
2705#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002706 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002708 if (curtab_only)
2709 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002710 // Ignore the match if the buffer is not open in
2711 // the current tab.
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002712 win_T *wp;
2713
Bram Moolenaar29323592016-07-24 22:04:11 +02002714 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002715 if (wp->w_buffer == buf)
2716 break;
2717 if (wp == NULL)
2718 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002719 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002720 if (match >= 0) // already found a match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 {
2722 match = -2;
2723 break;
2724 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002725 match = buf->b_fnum; // remember first match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 }
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002729 vim_regfree(regmatch.regprog);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002730 if (match >= 0) // found one match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 break;
2732 }
2733
Bram Moolenaarc667da52019-11-30 20:52:27 +01002734 // Only search for unlisted buffers if there was no match with
2735 // a listed buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 if (!unlisted || !find_listed || match != -1)
2737 break;
2738 find_listed = FALSE;
2739 }
2740
2741 vim_free(pat);
2742 }
2743
2744 if (match == -2)
Bram Moolenaare1242042021-12-16 20:56:57 +00002745 semsg(_(e_more_than_one_match_for_str), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 else if (match < 0)
Bram Moolenaare1242042021-12-16 20:56:57 +00002747 semsg(_(e_no_matching_buffer_for_str), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 return match;
2749}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750
Bram Moolenaar52410572019-10-27 05:12:45 +01002751#ifdef FEAT_VIMINFO
2752typedef struct {
2753 buf_T *buf;
2754 char_u *match;
2755} bufmatch_T;
2756#endif
2757
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758/*
2759 * Find all buffer names that match.
2760 * For command line expansion of ":buf" and ":sbuf".
2761 * Return OK if matches found, FAIL otherwise.
2762 */
2763 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002764ExpandBufnames(
2765 char_u *pat,
2766 int *num_file,
2767 char_u ***file,
2768 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769{
2770 int count = 0;
2771 buf_T *buf;
2772 int round;
2773 char_u *p;
2774 int attempt;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002775 char_u *patc = NULL;
Bram Moolenaar52410572019-10-27 05:12:45 +01002776#ifdef FEAT_VIMINFO
2777 bufmatch_T *matches = NULL;
2778#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002779 int fuzzy;
2780 fuzmatch_str_T *fuzmatch = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781
Bram Moolenaarc667da52019-11-30 20:52:27 +01002782 *num_file = 0; // return values in case of FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 *file = NULL;
2784
Bram Moolenaarefcc3292019-12-30 21:59:03 +01002785#ifdef FEAT_DIFF
2786 if ((options & BUF_DIFF_FILTER) && !curwin->w_p_diff)
2787 return FAIL;
2788#endif
2789
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002790 fuzzy = cmdline_fuzzy_complete(pat);
2791
2792 // Make a copy of "pat" and change "^" to "\(^\|[\/]\)" (if doing regular
2793 // expression matching)
2794 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002796 if (*pat == '^')
2797 {
2798 patc = alloc(STRLEN(pat) + 11);
2799 if (patc == NULL)
2800 return FAIL;
2801 STRCPY(patc, "\\(^\\|[\\/]\\)");
2802 STRCPY(patc + 11, pat + 1);
2803 }
2804 else
2805 patc = pat;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002806 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00002807
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002808 // attempt == 0: try match with '\<', match at start of word
2809 // attempt == 1: try match without '\<', match anywhere
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002810 for (attempt = 0; attempt <= (fuzzy ? 0 : 1); ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002811 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002812 regmatch_T regmatch;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002813 int score = 0;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002814
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002815 if (!fuzzy)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002816 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002817 if (attempt > 0 && patc == pat)
2818 break; // there was no anchor, no need to try again
2819 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002822 // round == 1: Count the matches.
2823 // round == 2: Build the array to keep the matches.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 for (round = 1; round <= 2; ++round)
2825 {
2826 count = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002827 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002829 if (!buf->b_p_bl) // skip unlisted buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 continue;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002831#ifdef FEAT_DIFF
2832 if (options & BUF_DIFF_FILTER)
2833 // Skip buffers not suitable for
2834 // :diffget or :diffput completion.
Bram Moolenaarefcc3292019-12-30 21:59:03 +01002835 if (buf == curbuf || !diff_mode_buf(buf))
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002836 continue;
2837#endif
2838
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002839 if (!fuzzy)
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002840 {
2841 if (regmatch.regprog == NULL)
2842 {
2843 // invalid pattern, possibly after recompiling
2844 if (patc != pat)
2845 vim_free(patc);
2846 return FAIL;
2847 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002848 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002849 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002850 else
2851 {
2852 p = NULL;
2853 // first try matching with the short file name
2854 if ((score = fuzzy_match_str(buf->b_sfname, pat)) != 0)
2855 p = buf->b_sfname;
2856 if (p == NULL)
2857 {
2858 // next try matching with the full path file name
2859 if ((score = fuzzy_match_str(buf->b_ffname, pat)) != 0)
2860 p = buf->b_ffname;
2861 }
2862 }
2863
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002864 if (p == NULL)
2865 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002866
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002867 if (round == 1)
2868 {
2869 ++count;
2870 continue;
2871 }
2872
2873 if (options & WILD_HOME_REPLACE)
2874 p = home_replace_save(buf, p);
2875 else
2876 p = vim_strsave(p);
2877
2878 if (!fuzzy)
2879 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002880#ifdef FEAT_VIMINFO
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002881 if (matches != NULL)
2882 {
2883 matches[count].buf = buf;
2884 matches[count].match = p;
2885 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002887 else
2888#endif
2889 (*file)[count++] = p;
2890 }
2891 else
2892 {
2893 fuzmatch[count].idx = count;
2894 fuzmatch[count].str = p;
2895 fuzmatch[count].score = score;
2896 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 }
2898 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002899 if (count == 0) // no match found, break here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 break;
2901 if (round == 1)
2902 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002903 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002905 *file = ALLOC_MULT(char_u *, count);
2906 if (*file == NULL)
2907 {
2908 vim_regfree(regmatch.regprog);
2909 if (patc != pat)
2910 vim_free(patc);
2911 return FAIL;
2912 }
Bram Moolenaar52410572019-10-27 05:12:45 +01002913#ifdef FEAT_VIMINFO
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002914 if (options & WILD_BUFLASTUSED)
2915 matches = ALLOC_MULT(bufmatch_T, count);
Bram Moolenaar52410572019-10-27 05:12:45 +01002916#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002917 }
2918 else
2919 {
2920 fuzmatch = ALLOC_MULT(fuzmatch_str_T, count);
2921 if (fuzmatch == NULL)
2922 {
2923 *num_file = 0;
2924 *file = NULL;
2925 return FAIL;
2926 }
2927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 }
2929 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002930
2931 if (!fuzzy)
2932 {
2933 vim_regfree(regmatch.regprog);
2934 if (count) // match(es) found, break here
2935 break;
2936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 }
2938
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002939 if (!fuzzy && patc != pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002940 vim_free(patc);
2941
Bram Moolenaar52410572019-10-27 05:12:45 +01002942#ifdef FEAT_VIMINFO
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002943 if (!fuzzy)
Bram Moolenaar52410572019-10-27 05:12:45 +01002944 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002945 if (matches != NULL)
Bram Moolenaar52410572019-10-27 05:12:45 +01002946 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002947 int i;
2948 if (count > 1)
2949 qsort(matches, count, sizeof(bufmatch_T), buf_compare);
2950 // if the current buffer is first in the list, place it at the end
2951 if (matches[0].buf == curbuf)
2952 {
2953 for (i = 1; i < count; i++)
2954 (*file)[i-1] = matches[i].match;
2955 (*file)[count-1] = matches[0].match;
2956 }
2957 else
2958 {
2959 for (i = 0; i < count; i++)
2960 (*file)[i] = matches[i].match;
2961 }
2962 vim_free(matches);
Bram Moolenaar52410572019-10-27 05:12:45 +01002963 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002964 }
2965 else
2966 {
2967 if (fuzzymatches_to_strmatches(fuzmatch, file, count, FALSE) == FAIL)
2968 return FAIL;
Bram Moolenaar52410572019-10-27 05:12:45 +01002969 }
2970#endif
2971
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 *num_file = count;
2973 return (count == 0 ? FAIL : OK);
2974}
2975
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976/*
2977 * Check for a match on the file name for buffer "buf" with regprog "prog".
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002978 * Note that rmp->regprog may become NULL when switching regexp engine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 */
2980 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002981buflist_match(
2982 regmatch_T *rmp,
2983 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002984 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985{
2986 char_u *match;
2987
Bram Moolenaarc667da52019-11-30 20:52:27 +01002988 // First try the short file name, then the long file name.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002989 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaara59f2df2022-05-11 11:42:28 +01002990 if (match == NULL && rmp->regprog != NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002991 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992
2993 return match;
2994}
2995
2996/*
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002997 * Try matching the regexp in "rmp->regprog" with file name "name".
2998 * Note that rmp->regprog may become NULL when switching regexp engine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 * Return "name" when there is a match, NULL when not.
3000 */
3001 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003002fname_match(
3003 regmatch_T *rmp,
3004 char_u *name,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003005 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006{
3007 char_u *match = NULL;
3008 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01003010 // extra check for valid arguments
3011 if (name != NULL && rmp->regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003013 // Ignore case when 'fileignorecase' or the argument is set.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01003014 rmp->rm_ic = p_fic || ignore_case;
3015 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 match = name;
Bram Moolenaar8e4b76d2022-05-07 11:28:06 +01003017 else if (rmp->regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003019 // Replace $(HOME) with '~' and try matching again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01003021 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 match = name;
3023 vim_free(p);
3024 }
3025 }
3026
3027 return match;
3028}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029
3030/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02003031 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 */
3033 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003034buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035{
Bram Moolenaar480778b2016-07-14 22:09:39 +02003036 char_u key[VIM_SIZEOF_INT * 2 + 1];
3037 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038
3039 if (nr == 0)
3040 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02003041 sprintf((char *)key, "%x", nr);
3042 hi = hash_find(&buf_hashtab, key);
3043
3044 if (!HASHITEM_EMPTY(hi))
3045 return (buf_T *)(hi->hi_key
3046 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 return NULL;
3048}
3049
3050/*
3051 * Get name of file 'n' in the buffer list.
3052 * When the file has no name an empty string is returned.
3053 * home_replace() is used to shorten the file name (used for marks).
3054 * Returns a pointer to allocated memory, of NULL when failed.
3055 */
3056 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003057buflist_nr2name(
3058 int n,
3059 int fullname,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003060 int helptail) // for help buffers return tail only
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061{
3062 buf_T *buf;
3063
3064 buf = buflist_findnr(n);
3065 if (buf == NULL)
3066 return NULL;
3067 return home_replace_save(helptail ? buf : NULL,
3068 fullname ? buf->b_ffname : buf->b_fname);
3069}
3070
3071/*
3072 * Set the "lnum" and "col" for the buffer "buf" and the current window.
3073 * When "copy_options" is TRUE save the local window option values.
3074 * When "lnum" is 0 only do the options.
3075 */
Bram Moolenaardefa0672019-07-21 19:25:37 +02003076 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003077buflist_setfpos(
3078 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003079 win_T *win, // may be NULL when using :badd
Bram Moolenaar7454a062016-01-30 15:14:10 +01003080 linenr_T lnum,
3081 colnr_T col,
3082 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083{
3084 wininfo_T *wip;
3085
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003086 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 if (wip->wi_win == win)
3088 break;
3089 if (wip == NULL)
3090 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003091 // allocate a new entry
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003092 wip = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 if (wip == NULL)
3094 return;
3095 wip->wi_win = win;
Bram Moolenaarc667da52019-11-30 20:52:27 +01003096 if (lnum == 0) // set lnum even when it's 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 lnum = 1;
3098 }
3099 else
3100 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003101 // remove the entry from the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 if (wip->wi_prev)
3103 wip->wi_prev->wi_next = wip->wi_next;
3104 else
3105 buf->b_wininfo = wip->wi_next;
3106 if (wip->wi_next)
3107 wip->wi_next->wi_prev = wip->wi_prev;
3108 if (copy_options && wip->wi_optset)
3109 {
3110 clear_winopt(&wip->wi_opt);
3111#ifdef FEAT_FOLDING
3112 deleteFoldRecurse(&wip->wi_folds);
3113#endif
3114 }
3115 }
3116 if (lnum != 0)
3117 {
3118 wip->wi_fpos.lnum = lnum;
3119 wip->wi_fpos.col = col;
3120 }
LemonBoydb0ea7f2022-04-10 17:59:26 +01003121 if (win != NULL)
3122 wip->wi_changelistidx = win->w_changelistidx;
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003123 if (copy_options && win != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003125 // Save the window-specific option values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
3127#ifdef FEAT_FOLDING
3128 wip->wi_fold_manual = win->w_fold_manual;
3129 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
3130#endif
3131 wip->wi_optset = TRUE;
3132 }
3133
Bram Moolenaarc667da52019-11-30 20:52:27 +01003134 // insert the entry in front of the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 wip->wi_next = buf->b_wininfo;
3136 buf->b_wininfo = wip;
3137 wip->wi_prev = NULL;
3138 if (wip->wi_next)
3139 wip->wi_next->wi_prev = wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140}
3141
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003142#ifdef FEAT_DIFF
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003143/*
3144 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
3145 * page. That's because a diff is local to a tab page.
3146 */
3147 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003148wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003149{
3150 win_T *wp;
3151
3152 if (wip->wi_opt.wo_diff)
3153 {
Bram Moolenaar29323592016-07-24 22:04:11 +02003154 FOR_ALL_WINDOWS(wp)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003155 // return FALSE when it's a window in the current tab page, thus
3156 // the buffer was in diff mode here
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003157 if (wip->wi_win == wp)
3158 return FALSE;
3159 return TRUE;
3160 }
3161 return FALSE;
3162}
3163#endif
3164
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165/*
3166 * Find info for the current window in buffer "buf".
3167 * If not found, return the info for the most recently used window.
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003168 * When "need_options" is TRUE skip entries where wi_optset is FALSE.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003169 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
3170 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 * Returns NULL when there isn't any info.
3172 */
3173 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003174find_wininfo(
3175 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003176 int need_options,
Bram Moolenaar7454a062016-01-30 15:14:10 +01003177 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178{
3179 wininfo_T *wip;
3180
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003181 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003182 if (wip->wi_win == curwin
3183#ifdef FEAT_DIFF
3184 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
3185#endif
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003186
3187 && (!need_options || wip->wi_optset))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003189
Bram Moolenaarc667da52019-11-30 20:52:27 +01003190 // If no wininfo for curwin, use the first in the list (that doesn't have
3191 // 'diff' set and is in another tab page).
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003192 // If "need_options" is TRUE skip entries that don't have options set,
3193 // unless the window is editing "buf", so we can copy from the window
3194 // itself.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003195 if (wip == NULL)
3196 {
3197#ifdef FEAT_DIFF
3198 if (skip_diff_buffer)
3199 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003200 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003201 if (!wininfo_other_tab_diff(wip)
3202 && (!need_options || wip->wi_optset
3203 || (wip->wi_win != NULL
3204 && wip->wi_win->w_buffer == buf)))
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003205 break;
3206 }
3207 else
3208#endif
3209 wip = buf->b_wininfo;
3210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 return wip;
3212}
3213
3214/*
3215 * Reset the local window options to the values last used in this window.
3216 * If the buffer wasn't used in this window before, use the values from
3217 * the most recently used window. If the values were never set, use the
3218 * global values for the window.
3219 */
3220 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003221get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222{
3223 wininfo_T *wip;
3224
3225 clear_winopt(&curwin->w_onebuf_opt);
3226#ifdef FEAT_FOLDING
3227 clearFolding(curwin);
3228#endif
3229
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003230 wip = find_wininfo(buf, TRUE, TRUE);
Bram Moolenaar25782a72018-05-13 18:05:33 +02003231 if (wip != NULL && wip->wi_win != NULL
3232 && wip->wi_win != curwin && wip->wi_win->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003234 // The buffer is currently displayed in the window: use the actual
3235 // option values instead of the saved (possibly outdated) values.
Bram Moolenaar25782a72018-05-13 18:05:33 +02003236 win_T *wp = wip->wi_win;
3237
3238 copy_winopt(&wp->w_onebuf_opt, &curwin->w_onebuf_opt);
3239#ifdef FEAT_FOLDING
3240 curwin->w_fold_manual = wp->w_fold_manual;
3241 curwin->w_foldinvalid = TRUE;
3242 cloneFoldGrowArray(&wp->w_folds, &curwin->w_folds);
3243#endif
3244 }
3245 else if (wip != NULL && wip->wi_optset)
3246 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003247 // the buffer was displayed in the current window earlier
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
3249#ifdef FEAT_FOLDING
3250 curwin->w_fold_manual = wip->wi_fold_manual;
3251 curwin->w_foldinvalid = TRUE;
3252 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
3253#endif
3254 }
3255 else
3256 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
LemonBoydb0ea7f2022-04-10 17:59:26 +01003257 if (wip != NULL)
3258 curwin->w_changelistidx = wip->wi_changelistidx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259
3260#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +01003261 // Set 'foldlevel' to 'foldlevelstart' if it's not negative.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 if (p_fdls >= 0)
3263 curwin->w_p_fdl = p_fdls;
3264#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02003265 after_copy_winopt(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266}
3267
3268/*
3269 * Find the position (lnum and col) for the buffer 'buf' for the current
3270 * window.
3271 * Returns a pointer to no_position if no position is found.
3272 */
3273 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003274buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275{
3276 wininfo_T *wip;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003277 static pos_T no_position = {1, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003279 wip = find_wininfo(buf, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 if (wip != NULL)
3281 return &(wip->wi_fpos);
3282 else
3283 return &no_position;
3284}
3285
3286/*
3287 * Find the lnum for the buffer 'buf' for the current window.
3288 */
3289 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01003290buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291{
3292 return buflist_findfpos(buf)->lnum;
3293}
3294
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02003296 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003299buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300{
Bram Moolenaar52410572019-10-27 05:12:45 +01003301 buf_T *buf = firstbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 int len;
3303 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003304 int ro_char;
3305 int changed_char;
Bram Moolenaar0751f512018-03-29 16:37:16 +02003306#ifdef FEAT_TERMINAL
3307 int job_running;
3308 int job_none_open;
3309#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310
Bram Moolenaar52410572019-10-27 05:12:45 +01003311#ifdef FEAT_VIMINFO
3312 garray_T buflist;
3313 buf_T **buflist_data = NULL, **p;
3314
3315 if (vim_strchr(eap->arg, 't'))
3316 {
3317 ga_init2(&buflist, sizeof(buf_T *), 50);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003318 FOR_ALL_BUFFERS(buf)
Bram Moolenaar52410572019-10-27 05:12:45 +01003319 {
3320 if (ga_grow(&buflist, 1) == OK)
3321 ((buf_T **)buflist.ga_data)[buflist.ga_len++] = buf;
3322 }
3323
3324 qsort(buflist.ga_data, (size_t)buflist.ga_len,
3325 sizeof(buf_T *), buf_compare);
3326
Bram Moolenaar3b991522019-11-06 23:26:20 +01003327 buflist_data = (buf_T **)buflist.ga_data;
3328 buf = *buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003329 }
Bram Moolenaar3b991522019-11-06 23:26:20 +01003330 p = buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003331
Bram Moolenaar3b991522019-11-06 23:26:20 +01003332 for (; buf != NULL && !got_int; buf = buflist_data != NULL
Bram Moolenaar52410572019-10-27 05:12:45 +01003333 ? (++p < buflist_data + buflist.ga_len ? *p : NULL)
3334 : buf->b_next)
3335#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
Bram Moolenaar52410572019-10-27 05:12:45 +01003337#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 {
Bram Moolenaar0751f512018-03-29 16:37:16 +02003339#ifdef FEAT_TERMINAL
3340 job_running = term_job_running(buf->b_term);
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003341 job_none_open = term_none_open(buf->b_term);
Bram Moolenaar0751f512018-03-29 16:37:16 +02003342#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003343 // skip unlisted buffers, unless ! was used
Bram Moolenaard51cb702015-07-21 15:03:06 +02003344 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
3345 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
3346 || (vim_strchr(eap->arg, '+')
3347 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
3348 || (vim_strchr(eap->arg, 'a')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003349 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
Bram Moolenaard51cb702015-07-21 15:03:06 +02003350 || (vim_strchr(eap->arg, 'h')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003351 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3352#ifdef FEAT_TERMINAL
3353 || (vim_strchr(eap->arg, 'R')
3354 && (!job_running || (job_running && job_none_open)))
3355 || (vim_strchr(eap->arg, '?')
3356 && (!job_running || (job_running && !job_none_open)))
3357 || (vim_strchr(eap->arg, 'F')
3358 && (job_running || buf->b_term == NULL))
3359#endif
Bram Moolenaard51cb702015-07-21 15:03:06 +02003360 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3361 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3362 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3363 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3364 || (vim_strchr(eap->arg, '#')
3365 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003368 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 else
3370 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003371 if (message_filtered(NameBuff))
3372 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003374 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3375 : (bufIsChanged(buf) ? '+' : ' ');
3376#ifdef FEAT_TERMINAL
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003377 if (job_running)
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003378 {
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003379 if (job_none_open)
Bram Moolenaar4033c552017-09-16 20:54:51 +02003380 ro_char = '?';
3381 else
3382 ro_char = 'R';
Bram Moolenaarc667da52019-11-30 20:52:27 +01003383 changed_char = ' '; // bufIsChanged() returns TRUE to avoid
3384 // closing, but it's not actually changed.
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003385 }
3386 else if (buf->b_term != NULL)
3387 ro_char = 'F';
3388 else
3389#endif
3390 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3391
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003392 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00003393 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 buf->b_fnum,
3395 buf->b_p_bl ? ' ' : 'u',
3396 buf == curbuf ? '%' :
3397 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3398 buf->b_ml.ml_mfp == NULL ? ' ' :
3399 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003400 ro_char,
3401 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003402 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003403 if (len > IOSIZE - 20)
3404 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405
Bram Moolenaarc667da52019-11-30 20:52:27 +01003406 // put "line 999" in column 40 or after the file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 i = 40 - vim_strsize(IObuff);
3408 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 IObuff[len++] = ' ';
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003410 while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar52410572019-10-27 05:12:45 +01003411#ifdef FEAT_VIMINFO
3412 if (vim_strchr(eap->arg, 't') && buf->b_last_used)
3413 add_time(IObuff + len, (size_t)(IOSIZE - len), buf->b_last_used);
3414 else
3415#endif
3416 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3417 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003418 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 msg_outtrans(IObuff);
Bram Moolenaarc667da52019-11-30 20:52:27 +01003420 out_flush(); // output one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 ui_breakcheck();
3422 }
Bram Moolenaar52410572019-10-27 05:12:45 +01003423
3424#ifdef FEAT_VIMINFO
3425 if (buflist_data)
3426 ga_clear(&buflist);
3427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429
3430/*
3431 * Get file name and line number for file 'fnum'.
3432 * Used by DoOneCmd() for translating '%' and '#'.
3433 * Used by insert_reg() and cmdline_paste() for '#' register.
3434 * Return FAIL if not found, OK for success.
3435 */
3436 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003437buflist_name_nr(
3438 int fnum,
3439 char_u **fname,
3440 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441{
3442 buf_T *buf;
3443
3444 buf = buflist_findnr(fnum);
3445 if (buf == NULL || buf->b_fname == NULL)
3446 return FAIL;
3447
3448 *fname = buf->b_fname;
3449 *lnum = buflist_findlnum(buf);
3450
3451 return OK;
3452}
3453
3454/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003455 * Set the file name for "buf"' to "ffname_arg", short file name to
3456 * "sfname_arg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 * The file name with the full path is also remembered, for when :cd is used.
3458 * Returns FAIL for failure (file name already in use by other buffer)
3459 * OK otherwise.
3460 */
3461 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003462setfname(
3463 buf_T *buf,
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003464 char_u *ffname_arg,
3465 char_u *sfname_arg,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003466 int message) // give message when buffer already exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003468 char_u *ffname = ffname_arg;
3469 char_u *sfname = sfname_arg;
Bram Moolenaar81695252004-12-29 20:58:21 +00003470 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003472 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473#endif
3474
3475 if (ffname == NULL || *ffname == NUL)
3476 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003477 // Removing the name.
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003478 if (buf->b_sfname != buf->b_ffname)
3479 VIM_CLEAR(buf->b_sfname);
3480 else
3481 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003482 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483#ifdef UNIX
3484 st.st_dev = (dev_T)-1;
3485#endif
3486 }
3487 else
3488 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003489 fname_expand(buf, &ffname, &sfname); // will allocate ffname
3490 if (ffname == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 return FAIL;
3492
3493 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01003494 * If the file name is already used in another buffer:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 * - if the buffer is loaded, fail
3496 * - if the buffer is not loaded, delete it from the list
3497 */
3498#ifdef UNIX
3499 if (mch_stat((char *)ffname, &st) < 0)
3500 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003501#endif
3502 if (!(buf->b_flags & BF_DUMMY))
3503#ifdef UNIX
3504 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003506 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507#endif
3508 if (obuf != NULL && obuf != buf)
3509 {
Bram Moolenaard3710cf2021-10-04 23:13:13 +01003510 win_T *win;
3511 tabpage_T *tab;
3512 int in_use = FALSE;
3513
3514 // during startup a window may use a buffer that is not loaded yet
3515 FOR_ALL_TAB_WINDOWS(tab, win)
3516 if (win->w_buffer == obuf)
3517 in_use = TRUE;
3518
3519 // it's loaded or used in a window, fail
3520 if (obuf->b_ml.ml_mfp != NULL || in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 {
3522 if (message)
Bram Moolenaare1242042021-12-16 20:56:57 +00003523 emsg(_(e_buffer_with_this_name_already_exists));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 vim_free(ffname);
3525 return FAIL;
3526 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003527 // delete from the list
Bram Moolenaara6e8f882019-12-14 16:18:15 +01003528 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 }
3530 sfname = vim_strsave(sfname);
3531 if (ffname == NULL || sfname == NULL)
3532 {
3533 vim_free(sfname);
3534 vim_free(ffname);
3535 return FAIL;
3536 }
3537#ifdef USE_FNAME_CASE
Bram Moolenaarc667da52019-11-30 20:52:27 +01003538 fname_case(sfname, 0); // set correct case for short file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539#endif
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003540 if (buf->b_sfname != buf->b_ffname)
3541 vim_free(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 vim_free(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 buf->b_ffname = ffname;
3544 buf->b_sfname = sfname;
3545 }
3546 buf->b_fname = buf->b_sfname;
3547#ifdef UNIX
3548 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003549 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 else
3551 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003552 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 buf->b_dev = st.st_dev;
3554 buf->b_ino = st.st_ino;
3555 }
3556#endif
3557
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559
3560 buf_name_changed(buf);
3561 return OK;
3562}
3563
3564/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003565 * Crude way of changing the name of a buffer. Use with care!
3566 * The name should be relative to the current directory.
3567 */
3568 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003569buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003570{
3571 buf_T *buf;
3572
3573 buf = buflist_findnr(fnum);
3574 if (buf != NULL)
3575 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003576 if (buf->b_sfname != buf->b_ffname)
3577 vim_free(buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003578 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003579 buf->b_ffname = vim_strsave(name);
3580 buf->b_sfname = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01003581 // Allocate ffname and expand into full path. Also resolves .lnk
3582 // files on Win32.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003583 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003584 buf->b_fname = buf->b_sfname;
3585 }
3586}
3587
3588/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 * Take care of what needs to be done when the name of buffer "buf" has
3590 * changed.
3591 */
3592 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003593buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594{
3595 /*
3596 * If the file name changed, also change the name of the swapfile
3597 */
3598 if (buf->b_ml.ml_mfp != NULL)
3599 ml_setname(buf);
3600
Bram Moolenaar3ad69532021-11-19 17:01:08 +00003601#ifdef FEAT_TERMINAL
3602 if (buf->b_term != NULL)
3603 term_clear_status_text(buf->b_term);
3604#endif
3605
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 if (curwin->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003607 check_arg_idx(curwin); // check file name for arg list
Bram Moolenaarc667da52019-11-30 20:52:27 +01003608 maketitle(); // set window title
Bram Moolenaarc667da52019-11-30 20:52:27 +01003609 status_redraw_all(); // status lines need to be redrawn
3610 fmarks_check_names(buf); // check named file marks
3611 ml_timestamp(buf); // reset timestamp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612}
3613
3614/*
3615 * set alternate file name for current window
3616 *
3617 * Used by do_one_cmd(), do_write() and do_ecmd().
3618 * Return the buffer.
3619 */
3620 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003621setaltfname(
3622 char_u *ffname,
3623 char_u *sfname,
3624 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625{
3626 buf_T *buf;
3627
Bram Moolenaarc667da52019-11-30 20:52:27 +01003628 // Create a buffer. 'buflisted' is not set if it's a new buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaare1004402020-10-24 20:49:43 +02003630 if (buf != NULL && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 curwin->w_alt_fnum = buf->b_fnum;
3632 return buf;
3633}
3634
3635/*
3636 * Get alternate file name for current window.
3637 * Return NULL if there isn't any, and give error message if requested.
3638 */
3639 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003640getaltfname(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003641 int errmsg) // give error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642{
3643 char_u *fname;
3644 linenr_T dummy;
3645
3646 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3647 {
3648 if (errmsg)
Bram Moolenaar108010a2021-06-27 22:03:33 +02003649 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 return NULL;
3651 }
3652 return fname;
3653}
3654
3655/*
3656 * Add a file name to the buflist and return its number.
3657 * Uses same flags as buflist_new(), except BLN_DUMMY.
3658 *
3659 * used by qf_init(), main() and doarglist()
3660 */
3661 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003662buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663{
3664 buf_T *buf;
3665
3666 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3667 if (buf != NULL)
3668 return buf->b_fnum;
3669 return 0;
3670}
3671
3672#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3673/*
3674 * Adjust slashes in file names. Called after 'shellslash' was set.
3675 */
3676 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003677buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678{
3679 buf_T *bp;
3680
Bram Moolenaar29323592016-07-24 22:04:11 +02003681 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 {
3683 if (bp->b_ffname != NULL)
3684 slash_adjust(bp->b_ffname);
3685 if (bp->b_sfname != NULL)
3686 slash_adjust(bp->b_sfname);
3687 }
3688}
3689#endif
3690
3691/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003692 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 * Also save the local window option values.
3694 */
3695 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003696buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003698 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699}
3700
3701/*
3702 * Return TRUE if 'ffname' is not the same file as current file.
3703 * Fname must have a full path (expanded by mch_FullName()).
3704 */
3705 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003706otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707{
3708 return otherfile_buf(curbuf, ffname
3709#ifdef UNIX
3710 , NULL
3711#endif
3712 );
3713}
3714
3715 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003716otherfile_buf(
3717 buf_T *buf,
3718 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003720 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003722 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723{
Bram Moolenaarc667da52019-11-30 20:52:27 +01003724 // no name is different
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3726 return TRUE;
3727 if (fnamecmp(ffname, buf->b_ffname) == 0)
3728 return FALSE;
3729#ifdef UNIX
3730 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003731 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732
Bram Moolenaarc667da52019-11-30 20:52:27 +01003733 // If no stat_T given, get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 if (stp == NULL)
3735 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003736 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 st.st_dev = (dev_T)-1;
3738 stp = &st;
3739 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003740 // Use dev/ino to check if the files are the same, even when the names
3741 // are different (possible with links). Still need to compare the
3742 // name above, for when the file doesn't exist yet.
3743 // Problem: The dev/ino changes when a file is deleted (and created
3744 // again) and remains the same when renamed/moved. We don't want to
3745 // mch_stat() each buffer each time, that would be too slow. Get the
3746 // dev/ino again when they appear to match, but not when they appear
3747 // to be different: Could skip a buffer when it's actually the same
3748 // file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 if (buf_same_ino(buf, stp))
3750 {
3751 buf_setino(buf);
3752 if (buf_same_ino(buf, stp))
3753 return FALSE;
3754 }
3755 }
3756#endif
3757 return TRUE;
3758}
3759
3760#if defined(UNIX) || defined(PROTO)
3761/*
3762 * Set inode and device number for a buffer.
3763 * Must always be called when b_fname is changed!.
3764 */
3765 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003766buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003768 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769
3770 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3771 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003772 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 buf->b_dev = st.st_dev;
3774 buf->b_ino = st.st_ino;
3775 }
3776 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003777 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778}
3779
3780/*
3781 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3782 */
3783 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003784buf_same_ino(
3785 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003786 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003788 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 && stp->st_dev == buf->b_dev
3790 && stp->st_ino == buf->b_ino);
3791}
3792#endif
3793
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003794/*
3795 * Print info about the current buffer.
3796 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003798fileinfo(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003799 int fullname, // when non-zero print full path
Bram Moolenaar7454a062016-01-30 15:14:10 +01003800 int shorthelp,
3801 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802{
3803 char_u *name;
3804 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003805 char *p;
3806 char *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003807 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003809 buffer = alloc(IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 if (buffer == NULL)
3811 return;
3812
Bram Moolenaarc667da52019-11-30 20:52:27 +01003813 if (fullname > 1) // 2 CTRL-G: include buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003815 vim_snprintf(buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 p = buffer + STRLEN(buffer);
3817 }
3818 else
3819 p = buffer;
3820
3821 *p++ = '"';
3822 if (buf_spname(curbuf) != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003823 vim_strncpy((char_u *)p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 else
3825 {
3826 if (!fullname && curbuf->b_fname != NULL)
3827 name = curbuf->b_fname;
3828 else
3829 name = curbuf->b_ffname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003830 home_replace(shorthelp ? curbuf : NULL, name, (char_u *)p,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 (int)(IOSIZE - (p - buffer)), TRUE);
3832 }
3833
Bram Moolenaar32526b32019-01-19 17:43:09 +01003834 vim_snprintf_add(buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 curbufIsChanged() ? (shortmess(SHM_MOD)
3836 ? " [+]" : _(" [Modified]")) : " ",
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01003837 (curbuf->b_flags & BF_NOTEDITED) && !bt_dontwrite(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 ? _("[Not edited]") : "",
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01003839 (curbuf->b_flags & BF_NEW) && !bt_dontwrite(curbuf)
Bram Moolenaar722e5052020-06-12 22:31:00 +02003840 ? new_file_message() : "",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003842 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 : _("[readonly]")) : "",
3844 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3845 || curbuf->b_p_ro) ?
3846 " " : "");
Bram Moolenaarc667da52019-11-30 20:52:27 +01003847 // With 32 bit longs and more than 21,474,836 lines multiplying by 100
3848 // causes an overflow, thus for large numbers divide instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 if (curwin->w_cursor.lnum > 1000000L)
3850 n = (int)(((long)curwin->w_cursor.lnum) /
3851 ((long)curbuf->b_ml.ml_line_count / 100L));
3852 else
3853 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3854 (long)curbuf->b_ml.ml_line_count);
3855 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003856 vim_snprintf_add(buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 else if (p_ru)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003858 // Current line and column are already on the screen -- webb
Bram Moolenaar32526b32019-01-19 17:43:09 +01003859 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003860 NGETTEXT("%ld line --%d%%--", "%ld lines --%d%%--",
3861 curbuf->b_ml.ml_line_count),
3862 (long)curbuf->b_ml.ml_line_count, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 else
3864 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003865 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 _("line %ld of %ld --%d%%-- col "),
3867 (long)curwin->w_cursor.lnum,
3868 (long)curbuf->b_ml.ml_line_count,
3869 n);
3870 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003871 len = STRLEN(buffer);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003872 col_print((char_u *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3874 }
3875
Bram Moolenaar32526b32019-01-19 17:43:09 +01003876 (void)append_arg_number(curwin, (char_u *)buffer, IOSIZE,
3877 !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878
3879 if (dont_truncate)
3880 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003881 // Temporarily set msg_scroll to avoid the message being truncated.
3882 // First call msg_start() to get the message in the right place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 msg_start();
3884 n = msg_scroll;
3885 msg_scroll = TRUE;
3886 msg(buffer);
3887 msg_scroll = n;
3888 }
3889 else
3890 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003891 p = msg_trunc_attr(buffer, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaarc667da52019-11-30 20:52:27 +01003893 // Need to repeat the message after redrawing when:
3894 // - When restart_edit is set (otherwise there will be a delay
3895 // before redrawing).
3896 // - When the screen was scrolled but there is no wait-return
3897 // prompt.
Bram Moolenaar32526b32019-01-19 17:43:09 +01003898 set_keep_msg((char_u *)p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 }
3900
3901 vim_free(buffer);
3902}
3903
3904 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003905col_print(
3906 char_u *buf,
3907 size_t buflen,
3908 int col,
3909 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910{
3911 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003912 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003914 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915}
3916
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917static char_u *lasttitle = NULL;
3918static char_u *lasticon = NULL;
3919
Bram Moolenaar84a93082018-06-16 22:58:15 +02003920/*
3921 * Put the file name in the title bar and icon of the window.
3922 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003924maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925{
3926 char_u *p;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003927 char_u *title_str = NULL;
3928 char_u *icon_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 int maxlen = 0;
3930 int len;
3931 int mustset;
3932 char_u buf[IOSIZE];
3933 int off;
3934
3935 if (!redrawing())
3936 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003937 // Postpone updating the title when 'lazyredraw' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 need_maketitle = TRUE;
3939 return;
3940 }
3941
3942 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003943 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003944 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945
3946 if (p_title)
3947 {
3948 if (p_titlelen > 0)
3949 {
3950 maxlen = p_titlelen * Columns / 100;
3951 if (maxlen < 10)
3952 maxlen = 10;
3953 }
3954
Bram Moolenaar84a93082018-06-16 22:58:15 +02003955 title_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 if (*p_titlestring != NUL)
3957 {
3958#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003959 if (stl_syntax & STL_IN_TITLE)
3960 {
3961 int use_sandbox = FALSE;
Bram Moolenaar53989552019-12-23 22:59:18 +01003962 int called_emsg_before = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003963
3964# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003965 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003966# endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003967 build_stl_str_hl(curwin, title_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003968 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003969 0, maxlen, NULL, NULL);
Bram Moolenaar53989552019-12-23 22:59:18 +01003970 if (called_emsg > called_emsg_before)
Bram Moolenaard3667a22006-03-16 21:35:52 +00003971 set_string_option_direct((char_u *)"titlestring", -1,
3972 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 else
3975#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003976 title_str = p_titlestring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 }
3978 else
3979 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003980 // format: "fname + (path) (1 of 2) - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981
Bram Moolenaar2c666692012-09-05 13:30:40 +02003982#define SPACE_FOR_FNAME (IOSIZE - 100)
3983#define SPACE_FOR_DIR (IOSIZE - 20)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003984#define SPACE_FOR_ARGNR (IOSIZE - 10) // at least room for " - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003986 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar21554412017-07-24 21:44:43 +02003987#ifdef FEAT_TERMINAL
3988 else if (curbuf->b_term != NULL)
3989 {
3990 vim_strncpy(buf, term_get_status_text(curbuf->b_term),
3991 SPACE_FOR_FNAME);
3992 }
3993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 else
3995 {
3996 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003997 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 }
4000
Bram Moolenaar21554412017-07-24 21:44:43 +02004001#ifdef FEAT_TERMINAL
4002 if (curbuf->b_term == NULL)
4003#endif
4004 switch (bufIsChanged(curbuf)
4005 + (curbuf->b_p_ro * 2)
4006 + (!curbuf->b_p_ma * 4))
4007 {
4008 case 1: STRCAT(buf, " +"); break;
4009 case 2: STRCAT(buf, " ="); break;
4010 case 3: STRCAT(buf, " =+"); break;
4011 case 4:
4012 case 6: STRCAT(buf, " -"); break;
4013 case 5:
4014 case 7: STRCAT(buf, " -+"); break;
4015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016
Bram Moolenaar21554412017-07-24 21:44:43 +02004017 if (curbuf->b_fname != NULL
4018#ifdef FEAT_TERMINAL
4019 && curbuf->b_term == NULL
4020#endif
4021 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004023 // Get path of file, replace home dir with ~
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 off = (int)STRLEN(buf);
4025 buf[off++] = ' ';
4026 buf[off++] = '(';
4027 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02004028 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc667da52019-11-30 20:52:27 +01004030 // avoid "c:/name" to be reduced to "c"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 if (isalpha(buf[off]) && buf[off + 1] == ':')
4032 off += 2;
4033#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01004034 // remove the file name
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004035 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 if (p == buf + off)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02004037 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004038 // must be a help buffer
Bram Moolenaar21554412017-07-24 21:44:43 +02004039 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02004040 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02004041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004044
Bram Moolenaarc667da52019-11-30 20:52:27 +01004045 // Translate unprintable chars and concatenate. Keep some
4046 // room for the server name. When there is no room (very long
4047 // file name) use (...).
Bram Moolenaar2c666692012-09-05 13:30:40 +02004048 if (off < SPACE_FOR_DIR)
4049 {
4050 p = transstr(buf + off);
4051 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
4052 vim_free(p);
4053 }
4054 else
4055 {
4056 vim_strncpy(buf + off, (char_u *)"...",
4057 (size_t)(SPACE_FOR_ARGNR - off));
4058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 STRCAT(buf, ")");
4060 }
4061
Bram Moolenaar2c666692012-09-05 13:30:40 +02004062 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063
4064#if defined(FEAT_CLIENTSERVER)
4065 if (serverName != NULL)
4066 {
4067 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02004068 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 }
4070 else
4071#endif
4072 STRCAT(buf, " - VIM");
4073
4074 if (maxlen > 0)
4075 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004076 // make it shorter by removing a bit in the middle
Bram Moolenaarf31b7642012-01-20 20:44:43 +01004077 if (vim_strsize(buf) > maxlen)
4078 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 }
4080 }
4081 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02004082 mustset = value_changed(title_str, &lasttitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083
4084 if (p_icon)
4085 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02004086 icon_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 if (*p_iconstring != NUL)
4088 {
4089#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00004090 if (stl_syntax & STL_IN_ICON)
4091 {
4092 int use_sandbox = FALSE;
Bram Moolenaar53989552019-12-23 22:59:18 +01004093 int called_emsg_before = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004094
4095# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00004096 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004097# endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02004098 build_stl_str_hl(curwin, icon_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004099 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004100 0, 0, NULL, NULL);
Bram Moolenaar53989552019-12-23 22:59:18 +01004101 if (called_emsg > called_emsg_before)
Bram Moolenaard3667a22006-03-16 21:35:52 +00004102 set_string_option_direct((char_u *)"iconstring", -1,
4103 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaard3667a22006-03-16 21:35:52 +00004104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 else
4106#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02004107 icon_str = p_iconstring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 }
4109 else
4110 {
4111 if (buf_spname(curbuf) != NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004112 p = buf_spname(curbuf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004113 else // use file name only in icon
Bram Moolenaar84a93082018-06-16 22:58:15 +02004114 p = gettail(curbuf->b_ffname);
4115 *icon_str = NUL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004116 // Truncate name at 100 bytes.
Bram Moolenaar84a93082018-06-16 22:58:15 +02004117 len = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 if (len > 100)
4119 {
4120 len -= 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 if (has_mbyte)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004122 len += (*mb_tail_off)(p, p + len) + 1;
Bram Moolenaar84a93082018-06-16 22:58:15 +02004123 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02004125 STRCPY(icon_str, p);
4126 trans_characters(icon_str, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 }
4128 }
4129
Bram Moolenaar84a93082018-06-16 22:58:15 +02004130 mustset |= value_changed(icon_str, &lasticon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131
4132 if (mustset)
4133 resettitle();
4134}
4135
4136/*
4137 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
4138 * from "str" if it does.
Bram Moolenaar84a93082018-06-16 22:58:15 +02004139 * Return TRUE if resettitle() is to be called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 */
4141 static int
Bram Moolenaar84a93082018-06-16 22:58:15 +02004142value_changed(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143{
4144 if ((str == NULL) != (*last == NULL)
4145 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
4146 {
4147 vim_free(*last);
4148 if (str == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004149 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 *last = NULL;
Bram Moolenaar40385db2018-08-07 22:31:44 +02004151 mch_restore_title(
4152 last == &lasttitle ? SAVE_RESTORE_TITLE : SAVE_RESTORE_ICON);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 else
Bram Moolenaar84a93082018-06-16 22:58:15 +02004155 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 *last = vim_strsave(str);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004157 return TRUE;
4158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 }
4160 return FALSE;
4161}
4162
4163/*
4164 * Put current window title back (used after calling a shell)
4165 */
4166 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004167resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168{
4169 mch_settitle(lasttitle, lasticon);
4170}
Bram Moolenaarea408852005-06-25 22:49:46 +00004171
4172# if defined(EXITFREE) || defined(PROTO)
4173 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004174free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00004175{
4176 vim_free(lasttitle);
4177 vim_free(lasticon);
4178}
4179# endif
4180
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004182#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004183
4184/*
4185 * Used for building in the status line.
4186 */
4187typedef struct
4188{
4189 char_u *stl_start;
4190 int stl_minwid;
4191 int stl_maxwid;
4192 enum {
4193 Normal,
4194 Empty,
4195 Group,
4196 Middle,
4197 Highlight,
4198 TabPage,
4199 Trunc
4200 } stl_type;
4201} stl_item_T;
4202
4203static size_t stl_items_len = 20; // Initial value, grows as needed.
4204static stl_item_T *stl_items = NULL;
4205static int *stl_groupitem = NULL;
4206static stl_hlrec_T *stl_hltab = NULL;
4207static stl_hlrec_T *stl_tabtab = NULL;
4208
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004210 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 * Return length of string in screen cells.
4212 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004213 * Normally works for window "wp", except when working for 'tabline' then it
4214 * is "curwin".
4215 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 * Items are drawn interspersed with the text that surrounds it
4217 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
4218 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
4219 *
4220 * If maxwidth is not zero, the string will be filled at any middle marker
4221 * or truncated if too long, fillchar is used for all whitespace.
4222 */
4223 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004224build_stl_str_hl(
4225 win_T *wp,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004226 char_u *out, // buffer to write into != NameBuff
4227 size_t outlen, // length of out[]
Bram Moolenaar7454a062016-01-30 15:14:10 +01004228 char_u *fmt,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004229 int use_sandbox UNUSED, // "fmt" was set insecurely, use sandbox
Bram Moolenaar7454a062016-01-30 15:14:10 +01004230 int fillchar,
4231 int maxwidth,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004232 stl_hlrec_T **hltab, // return: HL attributes (can be NULL)
4233 stl_hlrec_T **tabtab) // return: tab page nrs (can be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234{
Bram Moolenaar10772302019-01-20 18:25:54 +01004235 linenr_T lnum;
4236 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 char_u *p;
4238 char_u *s;
4239 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004240 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241#ifdef FEAT_EVAL
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004242 win_T *save_curwin;
4243 buf_T *save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004244 int save_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245#endif
4246 int empty_line;
4247 colnr_T virtcol;
4248 long l;
4249 long n;
4250 int prevchar_isflag;
4251 int prevchar_isitem;
4252 int itemisflag;
4253 int fillable;
4254 char_u *str;
4255 long num;
4256 int width;
4257 int itemcnt;
4258 int curitem;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004259 int group_end_userhl;
4260 int group_start_userhl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 int groupdepth;
shadmansaleh30e3de22021-05-15 17:23:28 +02004262#ifdef FEAT_EVAL
4263 int evaldepth;
4264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 int minwid;
4266 int maxwid;
4267 int zeropad;
4268 char_u base;
4269 char_u opt;
4270#define TMPLEN 70
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004271 char_u buf_tmp[TMPLEN];
4272 char_u win_tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004273 char_u *usefmt = fmt;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004274 stl_hlrec_T *sp;
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01004275 int save_redraw_not_allowed = redraw_not_allowed;
Bram Moolenaar0e1f36f2022-02-15 16:17:44 +00004276 int save_KeyTyped = KeyTyped;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004277
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01004278 // When inside update_screen() we do not want redrawing a statusline,
4279 // ruler, title, etc. to trigger another redraw, it may cause an endless
4280 // loop.
4281 if (updating_screen)
4282 redraw_not_allowed = TRUE;
4283
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004284 if (stl_items == NULL)
4285 {
4286 stl_items = ALLOC_MULT(stl_item_T, stl_items_len);
4287 stl_groupitem = ALLOC_MULT(int, stl_items_len);
Brandon Richardsona493b652022-02-19 11:45:03 +00004288
4289 // Allocate one more, because the last element is used to indicate the
4290 // end of the list.
4291 stl_hltab = ALLOC_MULT(stl_hlrec_T, stl_items_len + 1);
4292 stl_tabtab = ALLOC_MULT(stl_hlrec_T, stl_items_len + 1);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004293 }
4294
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004295#ifdef FEAT_EVAL
4296 /*
4297 * When the format starts with "%!" then evaluate it as an expression and
4298 * use the result as the actual format string.
4299 */
4300 if (fmt[0] == '%' && fmt[1] == '!')
4301 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004302 typval_T tv;
4303
4304 tv.v_type = VAR_NUMBER;
4305 tv.vval.v_number = wp->w_id;
4306 set_var((char_u *)"g:statusline_winid", &tv, FALSE);
4307
Bram Moolenaara4e0b972022-10-01 19:43:52 +01004308 usefmt = eval_to_string_safe(fmt + 2, use_sandbox, FALSE, FALSE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004309 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00004310 usefmt = fmt;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004311
4312 do_unlet((char_u *)"g:statusline_winid", TRUE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004313 }
4314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315
4316 if (fillchar == 0)
4317 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004318
Bram Moolenaar10772302019-01-20 18:25:54 +01004319 // The cursor in windows other than the current one isn't always
4320 // up-to-date, esp. because of autocommands and timers.
4321 lnum = wp->w_cursor.lnum;
4322 if (lnum > wp->w_buffer->b_ml.ml_line_count)
4323 {
4324 lnum = wp->w_buffer->b_ml.ml_line_count;
4325 wp->w_cursor.lnum = lnum;
4326 }
4327
4328 // Get line & check if empty (cursorpos will show "0-1"). Note that
4329 // p will become invalid when getting another buffer line.
4330 p = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar567199b2013-04-24 16:52:36 +02004331 empty_line = (*p == NUL);
4332
Bram Moolenaar10772302019-01-20 18:25:54 +01004333 // Get the byte value now, in case we need it below. This is more efficient
4334 // than making a copy of the line.
4335 len = STRLEN(p);
4336 if (wp->w_cursor.col > (colnr_T)len)
4337 {
4338 // Line may have changed since checking the cursor column, or the lnum
4339 // was adjusted above.
4340 wp->w_cursor.col = (colnr_T)len;
Bram Moolenaar10772302019-01-20 18:25:54 +01004341 wp->w_cursor.coladd = 0;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004342 byteval = 0;
Bram Moolenaar10772302019-01-20 18:25:54 +01004343 }
Bram Moolenaar567199b2013-04-24 16:52:36 +02004344 else
Bram Moolenaar567199b2013-04-24 16:52:36 +02004345 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346
4347 groupdepth = 0;
shadmansaleh30e3de22021-05-15 17:23:28 +02004348#ifdef FEAT_EVAL
4349 evaldepth = 0;
4350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 p = out;
4352 curitem = 0;
4353 prevchar_isflag = TRUE;
4354 prevchar_isitem = FALSE;
zeertzjq122dea72022-07-27 15:48:45 +01004355 for (s = usefmt; *s != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004357 if (curitem == (int)stl_items_len)
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004358 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004359 size_t new_len = stl_items_len * 3 / 2;
4360 stl_item_T *new_items;
4361 int *new_groupitem;
4362 stl_hlrec_T *new_hlrec;
4363
4364 new_items = vim_realloc(stl_items, sizeof(stl_item_T) * new_len);
4365 if (new_items == NULL)
4366 break;
4367 stl_items = new_items;
4368 new_groupitem = vim_realloc(stl_groupitem, sizeof(int) * new_len);
4369 if (new_groupitem == NULL)
4370 break;
4371 stl_groupitem = new_groupitem;
Brandon Richardsona493b652022-02-19 11:45:03 +00004372 new_hlrec = vim_realloc(stl_hltab,
4373 sizeof(stl_hlrec_T) * (new_len + 1));
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004374 if (new_hlrec == NULL)
4375 break;
4376 stl_hltab = new_hlrec;
Brandon Richardsona493b652022-02-19 11:45:03 +00004377 new_hlrec = vim_realloc(stl_tabtab,
4378 sizeof(stl_hlrec_T) * (new_len + 1));
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004379 if (new_hlrec == NULL)
4380 break;
4381 stl_tabtab = new_hlrec;
4382 stl_items_len = new_len;
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004383 }
4384
zeertzjq122dea72022-07-27 15:48:45 +01004385 if (*s != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 prevchar_isflag = prevchar_isitem = FALSE;
4387
4388 /*
4389 * Handle up to the next '%' or the end.
4390 */
4391 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
4392 *p++ = *s++;
4393 if (*s == NUL || p + 1 >= out + outlen)
4394 break;
4395
4396 /*
4397 * Handle one '%' item.
4398 */
4399 s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004400 if (*s == NUL) // ignore trailing %
Bram Moolenaar1d87f512011-02-01 21:55:01 +01004401 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 if (*s == '%')
4403 {
4404 if (p + 1 >= out + outlen)
4405 break;
4406 *p++ = *s++;
4407 prevchar_isflag = prevchar_isitem = FALSE;
4408 continue;
4409 }
4410 if (*s == STL_MIDDLEMARK)
4411 {
4412 s++;
4413 if (groupdepth > 0)
4414 continue;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004415 stl_items[curitem].stl_type = Middle;
4416 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 continue;
4418 }
4419 if (*s == STL_TRUNCMARK)
4420 {
4421 s++;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004422 stl_items[curitem].stl_type = Trunc;
4423 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 continue;
4425 }
4426 if (*s == ')')
4427 {
4428 s++;
4429 if (groupdepth < 1)
4430 continue;
4431 groupdepth--;
4432
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004433 t = stl_items[stl_groupitem[groupdepth]].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 *p = NUL;
4435 l = vim_strsize(t);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004436 if (curitem > stl_groupitem[groupdepth] + 1
4437 && stl_items[stl_groupitem[groupdepth]].stl_minwid == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004439 // remove group if all items are empty and highlight group
4440 // doesn't change
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004441 group_start_userhl = group_end_userhl = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004442 for (n = stl_groupitem[groupdepth] - 1; n >= 0; n--)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004443 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004444 if (stl_items[n].stl_type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004445 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004446 group_start_userhl = group_end_userhl =
4447 stl_items[n].stl_minwid;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004448 break;
4449 }
4450 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004451 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004452 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004453 if (stl_items[n].stl_type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 break;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004455 if (stl_items[n].stl_type == Highlight)
4456 group_end_userhl = stl_items[n].stl_minwid;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004457 }
4458 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 {
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004460 // empty group
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 p = t;
4462 l = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004463 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004464 {
4465 // do not use the highlighting from the removed group
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004466 if (stl_items[n].stl_type == Highlight)
4467 stl_items[n].stl_type = Empty;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004468 // adjust the start position of TabPage to the next
4469 // item position
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004470 if (stl_items[n].stl_type == TabPage)
4471 stl_items[n].stl_start = p;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 }
4474 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004475 if (l > stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004477 // truncate, remove n bytes of text at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 if (has_mbyte)
4479 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004480 // Find the first character that should be included.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 n = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004482 while (l >= stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 {
4484 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004485 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 }
4487 }
4488 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004489 n = (long)(p - t) - stl_items[stl_groupitem[groupdepth]]
4490 .stl_maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491
4492 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004493 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 p = p - n + 1;
Bram Moolenaar13505972019-01-24 15:04:48 +01004495
4496 // Fill up space left over by half a double-wide char.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004497 while (++l < stl_items[stl_groupitem[groupdepth]].stl_minwid)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004498 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499
Bram Moolenaarc667da52019-11-30 20:52:27 +01004500 // correct the start of the items for the truncation
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004501 for (l = stl_groupitem[groupdepth] + 1; l < curitem; l++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 {
LemonBoy57ff5262022-05-09 21:03:47 +01004503 // Minus one for the leading '<' added above.
4504 stl_items[l].stl_start -= n - 1;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004505 if (stl_items[l].stl_start < t)
4506 stl_items[l].stl_start = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 }
4508 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004509 else if (abs(stl_items[stl_groupitem[groupdepth]].stl_minwid) > l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004511 // fill
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004512 n = stl_items[stl_groupitem[groupdepth]].stl_minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 if (n < 0)
4514 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004515 // fill by appending characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 n = 0 - n;
4517 while (l++ < n && p + 1 < out + outlen)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004518 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 }
4520 else
4521 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004522 // fill by inserting characters
Bram Moolenaar008bff92021-03-04 21:55:58 +01004523 l = (n - l) * MB_CHAR2LEN(fillchar);
4524 mch_memmove(t + l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004526 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 p += l;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004528 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
4529 stl_items[n].stl_start += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 for ( ; l > 0; l--)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004531 MB_CHAR2BYTES(fillchar, t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 }
4533 }
4534 continue;
4535 }
4536 minwid = 0;
4537 maxwid = 9999;
4538 zeropad = FALSE;
4539 l = 1;
4540 if (*s == '0')
4541 {
4542 s++;
4543 zeropad = TRUE;
4544 }
4545 if (*s == '-')
4546 {
4547 s++;
4548 l = -1;
4549 }
4550 if (VIM_ISDIGIT(*s))
4551 {
4552 minwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004553 if (minwid < 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 minwid = 0;
4555 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004556 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004558 stl_items[curitem].stl_type = Highlight;
4559 stl_items[curitem].stl_start = p;
4560 stl_items[curitem].stl_minwid = minwid > 9 ? 1 : minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 s++;
4562 curitem++;
4563 continue;
4564 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004565 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4566 {
4567 if (*s == STL_TABCLOSENR)
4568 {
4569 if (minwid == 0)
4570 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004571 // %X ends the close label, go back to the previously
4572 // define tab label nr.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004573 for (n = curitem - 1; n >= 0; --n)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004574 if (stl_items[n].stl_type == TabPage
4575 && stl_items[n].stl_minwid >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004576 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004577 minwid = stl_items[n].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004578 break;
4579 }
4580 }
4581 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01004582 // close nrs are stored as negative values
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004583 minwid = - minwid;
4584 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004585 stl_items[curitem].stl_type = TabPage;
4586 stl_items[curitem].stl_start = p;
4587 stl_items[curitem].stl_minwid = minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004588 s++;
4589 curitem++;
4590 continue;
4591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 if (*s == '.')
4593 {
4594 s++;
4595 if (VIM_ISDIGIT(*s))
4596 {
4597 maxwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004598 if (maxwid <= 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 maxwid = 50;
4600 }
4601 }
4602 minwid = (minwid > 50 ? 50 : minwid) * l;
4603 if (*s == '(')
4604 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004605 stl_groupitem[groupdepth++] = curitem;
4606 stl_items[curitem].stl_type = Group;
4607 stl_items[curitem].stl_start = p;
4608 stl_items[curitem].stl_minwid = minwid;
4609 stl_items[curitem].stl_maxwid = maxwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 s++;
4611 curitem++;
4612 continue;
4613 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004614#ifdef FEAT_EVAL
4615 // Denotes end of expanded %{} block
4616 if (*s == '}' && evaldepth > 0)
4617 {
4618 s++;
4619 evaldepth--;
4620 continue;
4621 }
4622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 if (vim_strchr(STL_ALL, *s) == NULL)
4624 {
4625 s++;
4626 continue;
4627 }
4628 opt = *s++;
4629
Bram Moolenaarc667da52019-11-30 20:52:27 +01004630 // OK - now for the real work
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 base = 'D';
4632 itemisflag = FALSE;
4633 fillable = TRUE;
4634 num = -1;
4635 str = NULL;
4636 switch (opt)
4637 {
4638 case STL_FILEPATH:
4639 case STL_FULLPATH:
4640 case STL_FILENAME:
Bram Moolenaarc667da52019-11-30 20:52:27 +01004641 fillable = FALSE; // don't change ' ' to fillchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004643 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 else
4645 {
4646 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004647 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4649 }
4650 trans_characters(NameBuff, MAXPATHL);
4651 if (opt != STL_FILENAME)
4652 str = NameBuff;
4653 else
4654 str = gettail(NameBuff);
4655 break;
4656
Bram Moolenaarc667da52019-11-30 20:52:27 +01004657 case STL_VIM_EXPR: // '{'
shadmansaleh30e3de22021-05-15 17:23:28 +02004658 {
4659#ifdef FEAT_EVAL
4660 char_u *block_start = s - 1;
4661#endif
4662 int reevaluate = (*s == '%');
4663
4664 if (reevaluate)
4665 s++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 itemisflag = TRUE;
4667 t = p;
shadmansaleh30e3de22021-05-15 17:23:28 +02004668 while ((*s != '}' || (reevaluate && s[-1] != '%'))
4669 && *s != NUL && p + 1 < out + outlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 *p++ = *s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004671 if (*s != '}') // missing '}' or out of space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 break;
4673 s++;
shadmansaleh30e3de22021-05-15 17:23:28 +02004674 if (reevaluate)
4675 p[-1] = 0; // remove the % at the end of %{% expr %}
4676 else
4677 *p = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 p = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679#ifdef FEAT_EVAL
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004680 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp),
4681 "%d", curbuf->b_fnum);
4682 set_internal_string_var((char_u *)"g:actual_curbuf", buf_tmp);
4683 vim_snprintf((char *)win_tmp, sizeof(win_tmp), "%d", curwin->w_id);
4684 set_internal_string_var((char_u *)"g:actual_curwin", win_tmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004686 save_curbuf = curbuf;
4687 save_curwin = curwin;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004688 save_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 curwin = wp;
4690 curbuf = wp->w_buffer;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004691 // Visual mode is only valid in the current window.
4692 if (curwin != save_curwin)
4693 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694
Bram Moolenaara4e0b972022-10-01 19:43:52 +01004695 str = eval_to_string_safe(p, use_sandbox, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004697 curwin = save_curwin;
4698 curbuf = save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004699 VIsual_active = save_VIsual_active;
Bram Moolenaar01824652005-01-31 18:58:23 +00004700 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004701 do_unlet((char_u *)"g:actual_curwin", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702
4703 if (str != NULL && *str != 0)
4704 {
4705 if (*skipdigits(str) == NUL)
4706 {
4707 num = atoi((char *)str);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004708 VIM_CLEAR(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 itemisflag = FALSE;
4710 }
4711 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004712
4713 // If the output of the expression needs to be evaluated
4714 // replace the %{} block with the result of evaluation
4715 if (reevaluate && str != NULL && *str != 0
4716 && strchr((const char *)str, '%') != NULL
4717 && evaldepth < MAX_STL_EVAL_DEPTH)
4718 {
4719 size_t parsed_usefmt = (size_t)(block_start - usefmt);
4720 size_t str_length = strlen((const char *)str);
4721 size_t fmt_length = strlen((const char *)s);
4722 size_t new_fmt_len = parsed_usefmt
4723 + str_length + fmt_length + 3;
4724 char_u *new_fmt = (char_u *)alloc(new_fmt_len * sizeof(char_u));
4725 char_u *new_fmt_p = new_fmt;
4726
4727 new_fmt_p = (char_u *)memcpy(new_fmt_p, usefmt, parsed_usefmt)
4728 + parsed_usefmt;
4729 new_fmt_p = (char_u *)memcpy(new_fmt_p , str, str_length)
4730 + str_length;
4731 new_fmt_p = (char_u *)memcpy(new_fmt_p, "%}", 2) + 2;
4732 new_fmt_p = (char_u *)memcpy(new_fmt_p , s, fmt_length)
4733 + fmt_length;
4734 *new_fmt_p = 0;
4735 new_fmt_p = NULL;
4736
4737 if (usefmt != fmt)
4738 vim_free(usefmt);
4739 VIM_CLEAR(str);
4740 usefmt = new_fmt;
4741 s = usefmt + parsed_usefmt;
4742 evaldepth++;
4743 continue;
4744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745#endif
4746 break;
shadmansaleh30e3de22021-05-15 17:23:28 +02004747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 case STL_LINE:
4749 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4750 ? 0L : (long)(wp->w_cursor.lnum);
4751 break;
4752
4753 case STL_NUMLINES:
4754 num = wp->w_buffer->b_ml.ml_line_count;
4755 break;
4756
4757 case STL_COLUMN:
Bram Moolenaar24959102022-05-07 20:01:16 +01004758 num = (State & MODE_INSERT) == 0 && empty_line
4759 ? 0 : (int)wp->w_cursor.col + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 break;
4761
4762 case STL_VIRTCOL:
4763 case STL_VIRTCOL_ALT:
zeertzjq0f112052022-01-14 20:11:38 +00004764 virtcol = wp->w_virtcol + 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004765 // Don't display %V if it's the same as %c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 if (opt == STL_VIRTCOL_ALT
Bram Moolenaar24959102022-05-07 20:01:16 +01004767 && (virtcol == (colnr_T)((State & MODE_INSERT) == 0
4768 && empty_line ? 0 : (int)wp->w_cursor.col + 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 break;
4770 num = (long)virtcol;
4771 break;
4772
4773 case STL_PERCENTAGE:
4774 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4775 (long)wp->w_buffer->b_ml.ml_line_count);
4776 break;
4777
4778 case STL_ALTPERCENT:
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004779 str = buf_tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004780 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 break;
4782
4783 case STL_ARGLISTSTAT:
4784 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004785 buf_tmp[0] = 0;
4786 if (append_arg_number(wp, buf_tmp, (int)sizeof(buf_tmp), FALSE))
4787 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 break;
4789
4790 case STL_KEYMAP:
4791 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004792 if (get_keymap_str(wp, (char_u *)"<%s>", buf_tmp, TMPLEN))
4793 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 break;
4795 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004796#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004797 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798#else
4799 num = 0;
4800#endif
4801 break;
4802
4803 case STL_BUFNO:
4804 num = wp->w_buffer->b_fnum;
4805 break;
4806
4807 case STL_OFFSET_X:
4808 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004809 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 case STL_OFFSET:
4811#ifdef FEAT_BYTEOFF
4812 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
Bram Moolenaar24959102022-05-07 20:01:16 +01004813 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0
4814 ? 0L : l + 1 + ((State & MODE_INSERT) == 0 && empty_line
4815 ? 0 : (int)wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816#endif
4817 break;
4818
4819 case STL_BYTEVAL_X:
4820 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004821 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004823 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 if (num == NL)
4825 num = 0;
4826 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4827 num = NL;
4828 break;
4829
4830 case STL_ROFLAG:
4831 case STL_ROFLAG_ALT:
4832 itemisflag = TRUE;
4833 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004834 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 break;
4836
4837 case STL_HELPFLAG:
4838 case STL_HELPFLAG_ALT:
4839 itemisflag = TRUE;
4840 if (wp->w_buffer->b_help)
4841 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004842 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 break;
4844
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 case STL_FILETYPE:
4846 if (*wp->w_buffer->b_p_ft != NUL
4847 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4848 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004849 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), "[%s]",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004850 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004851 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 }
4853 break;
4854
4855 case STL_FILETYPE_ALT:
4856 itemisflag = TRUE;
4857 if (*wp->w_buffer->b_p_ft != NUL
4858 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4859 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004860 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), ",%s",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004861 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004862 for (t = buf_tmp; *t != 0; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 *t = TOUPPER_LOC(*t);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004864 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 }
4866 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867
Bram Moolenaar4033c552017-09-16 20:54:51 +02004868#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 case STL_PREVIEWFLAG:
4870 case STL_PREVIEWFLAG_ALT:
4871 itemisflag = TRUE;
4872 if (wp->w_p_pvw)
4873 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4874 : _("[Preview]"));
4875 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004876
4877 case STL_QUICKFIX:
4878 if (bt_quickfix(wp->w_buffer))
4879 str = (char_u *)(wp->w_llist_ref
4880 ? _(msg_loclist)
4881 : _(msg_qflist));
4882 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883#endif
4884
4885 case STL_MODIFIED:
4886 case STL_MODIFIED_ALT:
4887 itemisflag = TRUE;
4888 switch ((opt == STL_MODIFIED_ALT)
4889 + bufIsChanged(wp->w_buffer) * 2
4890 + (!wp->w_buffer->b_p_ma) * 4)
4891 {
4892 case 2: str = (char_u *)"[+]"; break;
4893 case 3: str = (char_u *)",+"; break;
4894 case 4: str = (char_u *)"[-]"; break;
4895 case 5: str = (char_u *)",-"; break;
4896 case 6: str = (char_u *)"[+-]"; break;
4897 case 7: str = (char_u *)",+-"; break;
4898 }
4899 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004900
4901 case STL_HIGHLIGHT:
4902 t = s;
4903 while (*s != '#' && *s != NUL)
4904 ++s;
4905 if (*s == '#')
4906 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004907 stl_items[curitem].stl_type = Highlight;
4908 stl_items[curitem].stl_start = p;
4909 stl_items[curitem].stl_minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004910 curitem++;
4911 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004912 if (*s != NUL)
4913 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004914 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 }
4916
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004917 stl_items[curitem].stl_start = p;
4918 stl_items[curitem].stl_type = Normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 if (str != NULL && *str)
4920 {
4921 t = str;
4922 if (itemisflag)
4923 {
4924 if ((t[0] && t[1])
4925 && ((!prevchar_isitem && *t == ',')
4926 || (prevchar_isflag && *t == ' ')))
4927 t++;
4928 prevchar_isflag = TRUE;
4929 }
4930 l = vim_strsize(t);
4931 if (l > 0)
4932 prevchar_isitem = TRUE;
4933 if (l > maxwid)
4934 {
4935 while (l >= maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 if (has_mbyte)
4937 {
4938 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004939 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 }
4941 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 l -= byte2cells(*t++);
4943 if (p + 1 >= out + outlen)
4944 break;
4945 *p++ = '<';
4946 }
4947 if (minwid > 0)
4948 {
4949 for (; l < minwid && p + 1 < out + outlen; l++)
4950 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004951 // Don't put a "-" in front of a digit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4953 *p++ = ' ';
4954 else
Bram Moolenaar008bff92021-03-04 21:55:58 +01004955 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 }
4957 minwid = 0;
4958 }
4959 else
4960 minwid *= -1;
Bram Moolenaar008bff92021-03-04 21:55:58 +01004961 for (; *t && p + 1 < out + outlen; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004963 // Change a space by fillchar, unless fillchar is '-' and a
4964 // digit follows.
Bram Moolenaar008bff92021-03-04 21:55:58 +01004965 if (fillable && *t == ' '
4966 && (!VIM_ISDIGIT(*(t + 1)) || fillchar != '-'))
4967 MB_CHAR2BYTES(fillchar, p);
4968 else
4969 *p++ = *t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 }
4971 for (; l < minwid && p + 1 < out + outlen; l++)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004972 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 }
4974 else if (num >= 0)
4975 {
4976 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4977 char_u nstr[20];
4978
4979 if (p + 20 >= out + outlen)
Bram Moolenaarc667da52019-11-30 20:52:27 +01004980 break; // not sufficient space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 prevchar_isitem = TRUE;
4982 t = nstr;
4983 if (opt == STL_VIRTCOL_ALT)
4984 {
4985 *t++ = '-';
4986 minwid--;
4987 }
4988 *t++ = '%';
4989 if (zeropad)
4990 *t++ = '0';
4991 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004992 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 *t = 0;
4994
4995 for (n = num, l = 1; n >= nbase; n /= nbase)
4996 l++;
4997 if (opt == STL_VIRTCOL_ALT)
4998 l++;
4999 if (l > maxwid)
5000 {
5001 l += 2;
5002 n = l - maxwid;
5003 while (l-- > maxwid)
5004 num /= nbase;
5005 *t++ = '>';
5006 *t++ = '%';
5007 *t = t[-3];
5008 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005009 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
5010 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 }
5012 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005013 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
5014 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 p += STRLEN(p);
5016 }
5017 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005018 stl_items[curitem].stl_type = Empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01005020 if (num >= 0 || (!itemisflag && str != NULL && *str != NUL))
5021 prevchar_isflag = FALSE; // Item not NULL, but not a flag
5022 //
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 if (opt == STL_VIM_EXPR)
5024 vim_free(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 curitem++;
5026 }
5027 *p = NUL;
5028 itemcnt = curitem;
5029
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005030#ifdef FEAT_EVAL
5031 if (usefmt != fmt)
5032 vim_free(usefmt);
5033#endif
5034
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 width = vim_strsize(out);
5036 if (maxwidth > 0 && width > maxwidth)
5037 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005038 // Result is too long, must truncate somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 l = 0;
5040 if (itemcnt == 0)
5041 s = out;
5042 else
5043 {
5044 for ( ; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005045 if (stl_items[l].stl_type == Trunc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005047 // Truncate at %< item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005048 s = stl_items[l].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 break;
5050 }
5051 if (l == itemcnt)
5052 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005053 // No %< item, truncate first item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005054 s = stl_items[0].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 l = 0;
5056 }
5057 }
5058
5059 if (width - vim_strsize(s) >= maxwidth)
5060 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005061 // Truncation mark is beyond max length
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 if (has_mbyte)
5063 {
5064 s = out;
5065 width = 0;
5066 for (;;)
5067 {
5068 width += ptr2cells(s);
5069 if (width >= maxwidth)
5070 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005071 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005073 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 while (++width < maxwidth)
Bram Moolenaar008bff92021-03-04 21:55:58 +01005075 MB_CHAR2BYTES(fillchar, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 }
5077 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 s = out + maxwidth - 1;
5079 for (l = 0; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005080 if (stl_items[l].stl_start > s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 break;
5082 itemcnt = l;
5083 *s++ = '>';
5084 *s = 0;
5085 }
5086 else
5087 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 if (has_mbyte)
5089 {
5090 n = 0;
5091 while (width >= maxwidth)
5092 {
5093 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005094 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 }
5096 }
5097 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 n = width - maxwidth + 1;
5099 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00005100 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 *s = '<';
5102
Bram Moolenaarc667da52019-11-30 20:52:27 +01005103 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 while (++width < maxwidth)
5105 {
5106 s = s + STRLEN(s);
Bram Moolenaar008bff92021-03-04 21:55:58 +01005107 MB_CHAR2BYTES(fillchar, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 *s = NUL;
5109 }
5110
Bram Moolenaarc667da52019-11-30 20:52:27 +01005111 --n; // count the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 for (; l < itemcnt; l++)
5113 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005114 if (stl_items[l].stl_start - n >= s)
5115 stl_items[l].stl_start -= n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005117 stl_items[l].stl_start = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 }
5119 }
5120 width = maxwidth;
5121 }
5122 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
5123 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005124 // Apply STL_MIDDLE if any
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 for (l = 0; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005126 if (stl_items[l].stl_type == Middle)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 break;
5128 if (l < itemcnt)
5129 {
Bram Moolenaar008bff92021-03-04 21:55:58 +01005130 int middlelength = (maxwidth - width) * MB_CHAR2LEN(fillchar);
5131 p = stl_items[l].stl_start + middlelength;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005132 STRMOVE(p, stl_items[l].stl_start);
Bram Moolenaar008bff92021-03-04 21:55:58 +01005133 for (s = stl_items[l].stl_start; s < p;)
5134 MB_CHAR2BYTES(fillchar, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 for (l++; l < itemcnt; l++)
Bram Moolenaar008bff92021-03-04 21:55:58 +01005136 stl_items[l].stl_start += middlelength;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 width = maxwidth;
5138 }
5139 }
5140
Bram Moolenaarc667da52019-11-30 20:52:27 +01005141 // Store the info about highlighting.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005142 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005144 *hltab = stl_hltab;
5145 sp = stl_hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 for (l = 0; l < itemcnt; l++)
5147 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005148 if (stl_items[l].stl_type == Highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005150 sp->start = stl_items[l].stl_start;
5151 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005152 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 }
5154 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005155 sp->start = NULL;
5156 sp->userhl = 0;
5157 }
5158
Bram Moolenaarc667da52019-11-30 20:52:27 +01005159 // Store the info about tab pages labels.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005160 if (tabtab != NULL)
5161 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005162 *tabtab = stl_tabtab;
5163 sp = stl_tabtab;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005164 for (l = 0; l < itemcnt; l++)
5165 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005166 if (stl_items[l].stl_type == TabPage)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005167 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005168 sp->start = stl_items[l].stl_start;
5169 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005170 sp++;
5171 }
5172 }
5173 sp->start = NULL;
5174 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 }
5176
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01005177 redraw_not_allowed = save_redraw_not_allowed;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02005178
Bram Moolenaar0e1f36f2022-02-15 16:17:44 +00005179 // A user function may reset KeyTyped, restore it.
5180 KeyTyped = save_KeyTyped;
5181
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 return width;
5183}
Bram Moolenaarc667da52019-11-30 20:52:27 +01005184#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005187 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
5188 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 */
5190 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005191get_rel_pos(
5192 win_T *wp,
5193 char_u *buf,
5194 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005196 long above; // number of lines above window
5197 long below; // number of lines below window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198
Bram Moolenaarc667da52019-11-30 20:52:27 +01005199 if (buflen < 3) // need at least 3 chars for writing
Bram Moolenaar0027c212015-01-07 13:31:52 +01005200 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 above = wp->w_topline - 1;
5202#ifdef FEAT_DIFF
5203 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02005204 if (wp->w_topline == 1 && wp->w_topfill >= 1)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005205 above = 0; // All buffer lines are displayed and there is an
5206 // indication of filler lines, that can be considered
5207 // seeing all lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208#endif
5209 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
5210 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005211 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
5212 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005214 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005216 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 ? (int)(above / ((above + below) / 100L))
5218 : (int)(above * 100L / (above + below)));
5219}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220
5221/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005222 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 * Return TRUE if it was appended.
5224 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005225 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005226append_arg_number(
5227 win_T *wp,
5228 char_u *buf,
5229 int buflen,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005230 int add_file) // Add "file" before the arg number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231{
5232 char_u *p;
5233
Bram Moolenaarc667da52019-11-30 20:52:27 +01005234 if (ARGCOUNT <= 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 return FALSE;
5236
Bram Moolenaarc667da52019-11-30 20:52:27 +01005237 p = buf + STRLEN(buf); // go to the end of the buffer
5238 if (p - buf + 35 >= buflen) // getting too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 return FALSE;
5240 *p++ = ' ';
5241 *p++ = '(';
5242 if (add_file)
5243 {
5244 STRCPY(p, "file ");
5245 p += 5;
5246 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005247 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
5248 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
5250 return TRUE;
5251}
5252
5253/*
5254 * If fname is not a full path, make it a full path.
5255 * Returns pointer to allocated memory (NULL for failure).
5256 */
5257 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005258fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259{
5260 /*
5261 * Force expanding the path always for Unix, because symbolic links may
5262 * mess up the full path name, even though it starts with a '/'.
5263 * Also expand when there is ".." in the file name, try to remove it,
5264 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00005265 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 * For MS-Windows also expand names like "longna~1" to "longname".
5267 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00005268#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 return FullName_save(fname, TRUE);
5270#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00005271 if (!vim_isAbsName(fname)
5272 || strstr((char *)fname, "..") != NULL
5273 || strstr((char *)fname, "//") != NULL
5274# ifdef BACKSLASH_IN_FILENAME
5275 || strstr((char *)fname, "\\\\") != NULL
5276# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005277# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00005279# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 )
5281 return FullName_save(fname, FALSE);
5282
5283 fname = vim_strsave(fname);
5284
Bram Moolenaar9b942202007-10-03 12:31:33 +00005285# ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01005286 if (fname != NULL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005287 fname_case(fname, 0); // set correct case for file name
Bram Moolenaar9b942202007-10-03 12:31:33 +00005288# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289
5290 return fname;
5291#endif
5292}
5293
5294/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005295 * Make "*ffname" a full file name, set "*sfname" to "*ffname" if not NULL.
5296 * "*ffname" becomes a pointer to allocated memory (or NULL).
5297 * When resolving a link both "*sfname" and "*ffname" will point to the same
5298 * allocated memory.
5299 * The "*ffname" and "*sfname" pointer values on call will not be freed.
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005300 * Note that the resulting "*ffname" pointer should be considered not allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005303fname_expand(
5304 buf_T *buf UNUSED,
5305 char_u **ffname,
5306 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005308 if (*ffname == NULL) // no file name given, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 return;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005310 if (*sfname == NULL) // no short file name given, use ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 *sfname = *ffname;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005312 *ffname = fix_fname(*ffname); // expand to full path
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313
5314#ifdef FEAT_SHORTCUT
5315 if (!buf->b_p_bin)
5316 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005317 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005319 // If the file name is a shortcut file, use the file it links to.
Bram Moolenaardce1e892019-02-10 23:18:53 +01005320 rfname = mch_resolve_path(*ffname, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005321 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 {
5323 vim_free(*ffname);
5324 *ffname = rfname;
5325 *sfname = rfname;
5326 }
5327 }
5328#endif
5329}
5330
5331/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 * Open a window for a number of buffers.
5333 */
5334 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005335ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336{
5337 buf_T *buf;
5338 win_T *wp, *wpnext;
5339 int split_ret = OK;
5340 int p_ea_save;
5341 int open_wins = 0;
5342 int r;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005343 int count; // Maximum number of windows to open.
5344 int all; // When TRUE also load inactive buffers.
Bram Moolenaare1004402020-10-24 20:49:43 +02005345 int had_tab = cmdmod.cmod_tab;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005346 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347
Bram Moolenaarc667da52019-11-30 20:52:27 +01005348 if (eap->addr_count == 0) // make as many windows as possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 count = 9999;
5350 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01005351 count = eap->line2; // make as many windows as specified
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5353 all = FALSE;
5354 else
5355 all = TRUE;
5356
5357 setpcmark();
5358
5359#ifdef FEAT_GUI
5360 need_mouse_correct = TRUE;
5361#endif
5362
5363 /*
5364 * Close superfluous windows (two windows for the same buffer).
5365 * Also close windows that are not full-width.
5366 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005367 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005368 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005369 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005371 tpnext = curtab->tp_next;
5372 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005374 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005375 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005376 || ((cmdmod.cmod_split & WSP_VERT)
5377 ? wp->w_height + wp->w_status_height < Rows - p_ch
5378 - tabline_height()
5379 : wp->w_width != Columns)
5380 || (had_tab > 0 && wp != firstwin))
5381 && !ONE_WINDOW
5382 && !(wp->w_closing || wp->w_buffer->b_locked > 0)
5383 && !win_unlisted(wp))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005384 {
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005385 if (win_close(wp, FALSE) == FAIL)
5386 break;
5387 // Just in case an autocommand does something strange with
5388 // windows: start all over...
5389 wpnext = firstwin;
5390 tpnext = first_tabpage;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005391 open_wins = 0;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005392 }
5393 else
5394 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005396
Bram Moolenaarc667da52019-11-30 20:52:27 +01005397 // Without the ":tab" modifier only do the current tab page.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005398 if (had_tab == 0 || tpnext == NULL)
5399 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005400 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 }
5402
5403 /*
5404 * Go through the buffer list. When a buffer doesn't have a window yet,
5405 * open one. Otherwise move the window to the right position.
5406 * Watch out for autocommands that delete buffers or windows!
5407 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01005408 // Don't execute Win/Buf Enter/Leave autocommands here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 ++autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 win_enter(lastwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5413 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005414 // Check if this buffer needs a window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5416 continue;
5417
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005418 if (had_tab != 0)
5419 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005420 // With the ":tab" modifier don't move the window.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005421 if (buf->b_nwindows > 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005422 wp = lastwin; // buffer has a window, skip it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005423 else
5424 wp = NULL;
5425 }
5426 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005427 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005428 // Check if this buffer already has a window
Bram Moolenaar29323592016-07-24 22:04:11 +02005429 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005430 if (wp->w_buffer == buf)
5431 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005432 // If the buffer already has a window, move it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005433 if (wp != NULL)
5434 win_move_after(wp, curwin);
5435 }
5436
5437 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005439 bufref_T bufref;
5440
5441 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005442
Bram Moolenaarc667da52019-11-30 20:52:27 +01005443 // Split the window and put the buffer in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 p_ea_save = p_ea;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005445 p_ea = TRUE; // use space from all windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5447 ++open_wins;
5448 p_ea = p_ea_save;
5449 if (split_ret == FAIL)
5450 continue;
5451
Bram Moolenaarc667da52019-11-30 20:52:27 +01005452 // Open the buffer in this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005455 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005457 // autocommands deleted the buffer!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 swap_exists_action = SEA_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 break;
5460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 if (swap_exists_action == SEA_QUIT)
5462 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005463#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005464 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005465
Bram Moolenaarc667da52019-11-30 20:52:27 +01005466 // Reset the error/interrupt/exception state here so that
5467 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005468 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005469#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005470
Bram Moolenaarc667da52019-11-30 20:52:27 +01005471 // User selected Quit at ATTENTION prompt; close this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 win_close(curwin, TRUE);
5473 --open_wins;
5474 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005475 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005476
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005477#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005478 // Restore the error/interrupt/exception state if not
5479 // discarded by a new aborting error, interrupt, or uncaught
5480 // exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005481 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 }
5484 else
5485 handle_swap_exists(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 }
5487
5488 ui_breakcheck();
5489 if (got_int)
5490 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005491 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 break;
5493 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005494#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01005495 // Autocommands deleted the buffer or aborted script processing!!!
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005496 if (aborting())
5497 break;
5498#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01005499 // When ":tab" was used open a new tab for a new window repeatedly.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005500 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
Bram Moolenaare1004402020-10-24 20:49:43 +02005501 cmdmod.cmod_tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 --autocmd_no_enter;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005504 win_enter(firstwin, FALSE); // back to first window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506
5507 /*
5508 * Close superfluous windows.
5509 */
5510 for (wp = lastwin; open_wins > count; )
5511 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005512 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 || autowrite(wp->w_buffer, FALSE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 if (!win_valid(wp))
5515 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005516 // BufWrite Autocommands made the window invalid, start over
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 wp = lastwin;
5518 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005519 else if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005521 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 --open_wins;
5523 wp = lastwin;
5524 }
5525 else
5526 {
5527 wp = wp->w_prev;
5528 if (wp == NULL)
5529 break;
5530 }
5531 }
5532}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005535static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005536
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537/*
5538 * do_modelines() - process mode lines for the current file
5539 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005540 * "flags" can be:
5541 * OPT_WINONLY only set options local to window
5542 * OPT_NOWIN don't set options local to window
5543 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 * Returns immediately if the "ml" option isn't set.
5545 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005547do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005549 linenr_T lnum;
5550 int nmlines;
5551 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552
5553 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5554 return;
5555
Bram Moolenaarc667da52019-11-30 20:52:27 +01005556 // Disallow recursive entry here. Can happen when executing a modeline
5557 // triggers an autocommand, which reloads modelines with a ":do".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 if (entered)
5559 return;
5560
5561 ++entered;
Hu Jialun9dcd3492021-08-28 20:42:50 +02005562 for (lnum = 1; curbuf->b_p_ml && lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005564 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 nmlines = 0;
5566
Hu Jialun9dcd3492021-08-28 20:42:50 +02005567 for (lnum = curbuf->b_ml.ml_line_count; curbuf->b_p_ml && lnum > 0 && lnum > nmlines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005569 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 nmlines = 0;
5571 --entered;
5572}
5573
Bram Moolenaarc667da52019-11-30 20:52:27 +01005574#include "version.h" // for version number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575
5576/*
5577 * chk_modeline() - check a single line for a mode string
5578 * Return FAIL if an error encountered.
5579 */
5580 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005581chk_modeline(
5582 linenr_T lnum,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005583 int flags) // Same as for do_modelines().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584{
5585 char_u *s;
5586 char_u *e;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005587 char_u *linecopy; // local copy of any modeline found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 int prev;
5589 int vers;
5590 int end;
5591 int retval = OK;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005592 sctx_T save_current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005593
Bram Moolenaare31ee862020-01-07 20:59:34 +01005594 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595
5596 prev = -1;
5597 for (s = ml_get(lnum); *s != NUL; ++s)
5598 {
5599 if (prev == -1 || vim_isspace(prev))
5600 {
5601 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5602 || STRNCMP(s, "vi:", (size_t)3) == 0)
5603 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005604 // Accept both "vim" and "Vim".
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005605 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 {
5607 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5608 e = s + 4;
5609 else
5610 e = s + 3;
5611 vers = getdigits(&e);
5612 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005613 && (s[0] != 'V'
5614 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 && (s[3] == ':'
5616 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5617 || (VIM_VERSION_100 < vers && s[3] == '<')
5618 || (VIM_VERSION_100 > vers && s[3] == '>')
5619 || (VIM_VERSION_100 == vers && s[3] == '=')))
5620 break;
5621 }
5622 }
5623 prev = *s;
5624 }
5625
5626 if (*s)
5627 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005628 do // skip over "ex:", "vi:" or "vim:"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 ++s;
5630 while (s[-1] != ':');
5631
Bram Moolenaarc667da52019-11-30 20:52:27 +01005632 s = linecopy = vim_strsave(s); // copy the line, it will change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 if (linecopy == NULL)
5634 return FAIL;
5635
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005636 // prepare for emsg()
5637 estack_push(ETYPE_MODELINE, (char_u *)"modelines", lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01005638 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639
5640 end = FALSE;
5641 while (end == FALSE)
5642 {
5643 s = skipwhite(s);
5644 if (*s == NUL)
5645 break;
5646
5647 /*
5648 * Find end of set command: ':' or end of line.
5649 * Skip over "\:", replacing it with ":".
5650 */
5651 for (e = s; *e != ':' && *e != NUL; ++e)
5652 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005653 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 if (*e == NUL)
5655 end = TRUE;
5656
5657 /*
5658 * If there is a "set" command, require a terminating ':' and
5659 * ignore the stuff after the ':'.
5660 * "vi:set opt opt opt: foo" -- foo not interpreted
5661 * "vi:opt opt opt: foo" -- foo interpreted
5662 * Accept "se" for compatibility with Elvis.
5663 */
5664 if (STRNCMP(s, "set ", (size_t)4) == 0
5665 || STRNCMP(s, "se ", (size_t)3) == 0)
5666 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005667 if (*e != ':') // no terminating ':'?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 break;
5669 end = TRUE;
5670 s = vim_strchr(s, ' ') + 1;
5671 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005672 *e = NUL; // truncate the set command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673
Bram Moolenaarc667da52019-11-30 20:52:27 +01005674 if (*s != NUL) // skip over an empty "::"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 {
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005676 int secure_save = secure;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005677
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005678 save_current_sctx = current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005679 current_sctx.sc_version = 1;
5680#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005681 current_sctx.sc_sid = SID_MODELINE;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005682 current_sctx.sc_seq = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005683 current_sctx.sc_lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005685
Bram Moolenaar5958f952018-11-20 04:25:21 +01005686 // Make sure no risky things are executed as a side effect.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01005687 secure = 1;
Bram Moolenaar5958f952018-11-20 04:25:21 +01005688
Bram Moolenaara3227e22006-03-08 21:32:40 +00005689 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar5958f952018-11-20 04:25:21 +01005690
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005691 secure = secure_save;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005692 current_sctx = save_current_sctx;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005693 if (retval == FAIL) // stop if error found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 break;
5695 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005696 s = e + 1; // advance to next part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 }
5698
Bram Moolenaare31ee862020-01-07 20:59:34 +01005699 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005700 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 vim_free(linecopy);
5702 }
5703 return retval;
5704}
5705
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005706/*
Bram Moolenaar91335e52018-08-01 17:53:12 +02005707 * Return TRUE if "buf" is a normal buffer, 'buftype' is empty.
5708 */
5709 int
5710bt_normal(buf_T *buf)
5711{
5712 return buf != NULL && buf->b_p_bt[0] == NUL;
5713}
5714
5715/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005716 * Return TRUE if "buf" is the quickfix buffer.
5717 */
5718 int
Bram Moolenaar340dafd2022-08-25 16:16:45 +01005719bt_quickfix(buf_T *buf UNUSED)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005720{
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005721#ifdef FEAT_QUICKFIX
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005722 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005723#else
5724 return FALSE;
Bram Moolenaar113e1072019-01-20 15:30:40 +01005725#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005726}
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005727
5728/*
5729 * Return TRUE if "buf" is a terminal buffer.
5730 */
5731 int
Bram Moolenaar340dafd2022-08-25 16:16:45 +01005732bt_terminal(buf_T *buf UNUSED)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005733{
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005734#if defined(FEAT_TERMINAL)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005735 return buf != NULL && buf->b_p_bt[0] == 't';
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005736#else
5737 return FALSE;
Bram Moolenaar113e1072019-01-20 15:30:40 +01005738#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005739}
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005740
5741/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005742 * Return TRUE if "buf" is a help buffer.
5743 */
5744 int
5745bt_help(buf_T *buf)
5746{
5747 return buf != NULL && buf->b_help;
5748}
5749
5750/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02005751 * Return TRUE if "buf" is a prompt buffer.
5752 */
5753 int
5754bt_prompt(buf_T *buf)
5755{
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005756 return buf != NULL && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'r';
5757}
5758
Dominique Pelle748b3082022-01-08 12:41:16 +00005759#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005760/*
5761 * Return TRUE if "buf" is a buffer for a popup window.
5762 */
5763 int
5764bt_popup(buf_T *buf)
5765{
5766 return buf != NULL && buf->b_p_bt != NULL
5767 && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'o';
Bram Moolenaarf2732452018-06-03 14:47:35 +02005768}
Dominique Pelle748b3082022-01-08 12:41:16 +00005769#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005770
5771/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005772 * Return TRUE if "buf" is a "nofile", "acwrite", "terminal" or "prompt"
Bram Moolenaarc3126192022-08-26 12:58:17 +01005773 * buffer. This means the buffer name may not be a file name, at least not for
5774 * writing the buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005775 */
5776 int
Bram Moolenaar26910de2019-06-15 19:37:15 +02005777bt_nofilename(buf_T *buf)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005778{
5779 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5780 || buf->b_p_bt[0] == 'a'
Bram Moolenaarf2732452018-06-03 14:47:35 +02005781 || buf->b_p_bt[0] == 't'
5782 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005783}
5784
Bram Moolenaarc3126192022-08-26 12:58:17 +01005785/*
5786 * Return TRUE if "buf" is a "nofile", "quickfix", "terminal" or "prompt"
5787 * buffer. This means the buffer is not to be read from a file.
5788 */
5789 static int
5790bt_nofileread(buf_T *buf)
5791{
5792 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5793 || buf->b_p_bt[0] == 't'
5794 || buf->b_p_bt[0] == 'q'
5795 || buf->b_p_bt[0] == 'p');
5796}
5797
Dominique Pelle748b3082022-01-08 12:41:16 +00005798#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005799/*
Bram Moolenaar26910de2019-06-15 19:37:15 +02005800 * Return TRUE if "buf" has 'buftype' set to "nofile".
5801 */
5802 int
5803bt_nofile(buf_T *buf)
5804{
5805 return buf != NULL && buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f';
5806}
Dominique Pelle748b3082022-01-08 12:41:16 +00005807#endif
Bram Moolenaar26910de2019-06-15 19:37:15 +02005808
5809/*
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01005810 * Return TRUE if "buf" is a "nowrite", "nofile", "terminal", "prompt", or
5811 * "popup" buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005812 */
5813 int
5814bt_dontwrite(buf_T *buf)
5815{
Bram Moolenaarf2732452018-06-03 14:47:35 +02005816 return buf != NULL && (buf->b_p_bt[0] == 'n'
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005817 || buf->b_p_bt[0] == 't'
5818 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005819}
5820
5821 int
5822bt_dontwrite_msg(buf_T *buf)
5823{
5824 if (bt_dontwrite(buf))
5825 {
Bram Moolenaarf1474d82021-12-31 19:59:55 +00005826 emsg(_(e_cannot_write_buftype_option_is_set));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005827 return TRUE;
5828 }
5829 return FALSE;
5830}
5831
5832/*
5833 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
5834 * and 'bufhidden'.
5835 */
5836 int
5837buf_hide(buf_T *buf)
5838{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005839 // 'bufhidden' overrules 'hidden' and ":hide", check it first
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005840 switch (buf->b_p_bh[0])
5841 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005842 case 'u': // "unload"
5843 case 'w': // "wipe"
5844 case 'd': return FALSE; // "delete"
5845 case 'h': return TRUE; // "hide"
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005846 }
Bram Moolenaare1004402020-10-24 20:49:43 +02005847 return (p_hid || (cmdmod.cmod_flags & CMOD_HIDE));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005848}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849
5850/*
5851 * Return special buffer name.
5852 * Returns NULL when the buffer has a normal file name.
5853 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005854 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005855buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856{
Bram Moolenaar4033c552017-09-16 20:54:51 +02005857#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005859 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005860 /*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005861 * Differentiate between the quickfix and location list buffers using
5862 * the buffer number stored in the global quickfix stack.
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005863 */
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005864 if (buf->b_fnum == qf_stack_get_bufnr())
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005865 return (char_u *)_(msg_qflist);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005866 else
5867 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02005870
Bram Moolenaarc667da52019-11-30 20:52:27 +01005871 // There is no _file_ when 'buftype' is "nofile", b_sfname
5872 // contains the name as specified by the user.
Bram Moolenaar26910de2019-06-15 19:37:15 +02005873 if (bt_nofilename(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 {
Bram Moolenaar21554412017-07-24 21:44:43 +02005875#ifdef FEAT_TERMINAL
5876 if (buf->b_term != NULL)
5877 return term_get_status_text(buf->b_term);
5878#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02005879 if (buf->b_fname != NULL)
5880 return buf->b_fname;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02005881#ifdef FEAT_JOB_CHANNEL
5882 if (bt_prompt(buf))
5883 return (char_u *)_("[Prompt]");
5884#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005885#ifdef FEAT_PROP_POPUP
Bram Moolenaarc6896e22019-05-30 22:32:34 +02005886 if (bt_popup(buf))
5887 return (char_u *)_("[Popup]");
5888#endif
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005889 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 }
Bram Moolenaar21554412017-07-24 21:44:43 +02005891
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 if (buf->b_fname == NULL)
Bram Moolenaar00806bc2020-11-05 19:36:38 +01005893 return buf_get_fname(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 return NULL;
5895}
5896
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897/*
Bram Moolenaar00806bc2020-11-05 19:36:38 +01005898 * Get "buf->b_fname", use "[No Name]" if it is NULL.
5899 */
5900 char_u *
5901buf_get_fname(buf_T *buf)
5902{
5903 if (buf->b_fname == NULL)
5904 return (char_u *)_("[No Name]");
5905 return buf->b_fname;
5906}
5907
5908/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5910 */
5911 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005912set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913{
5914 if (on != curbuf->b_p_bl)
5915 {
5916 curbuf->b_p_bl = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 if (on)
5918 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5919 else
5920 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 }
5922}
5923
5924/*
5925 * Read the file for "buf" again and check if the contents changed.
5926 * Return TRUE if it changed or this could not be checked.
5927 */
5928 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005929buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930{
5931 buf_T *newbuf;
5932 int differ = TRUE;
5933 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 exarg_T ea;
5936
Bram Moolenaarc667da52019-11-30 20:52:27 +01005937 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5939 if (newbuf == NULL)
5940 return TRUE;
5941
Bram Moolenaarc667da52019-11-30 20:52:27 +01005942 // Force the 'fileencoding' and 'fileformat' to be equal.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 if (prep_exarg(&ea, buf) == FAIL)
5944 {
5945 wipe_buffer(newbuf, FALSE);
5946 return TRUE;
5947 }
5948
Bram Moolenaarc667da52019-11-30 20:52:27 +01005949 // set curwin/curbuf to buf and save a few things
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951
Bram Moolenaar4770d092006-01-12 23:22:24 +00005952 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 && readfile(buf->b_ffname, buf->b_fname,
5954 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5955 &ea, READ_NEW | READ_DUMMY) == OK)
5956 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005957 // compare the two files line by line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5959 {
5960 differ = FALSE;
5961 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5962 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5963 {
5964 differ = TRUE;
5965 break;
5966 }
5967 }
5968 }
5969 vim_free(ea.cmd);
5970
Bram Moolenaarc667da52019-11-30 20:52:27 +01005971 // restore curwin/curbuf and a few other things
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973
Bram Moolenaarc667da52019-11-30 20:52:27 +01005974 if (curbuf != newbuf) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 wipe_buffer(newbuf, FALSE);
5976
5977 return differ;
5978}
5979
5980/*
5981 * Wipe out a buffer and decrement the last buffer number if it was used for
5982 * this buffer. Call this to wipe out a temp buffer that does not contain any
5983 * marks.
5984 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005986wipe_buffer(
5987 buf_T *buf,
Bram Moolenaara6e8f882019-12-14 16:18:15 +01005988 int aucmd) // When TRUE trigger autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989{
5990 if (buf->b_fnum == top_file_num - 1)
5991 --top_file_num;
5992
Bram Moolenaarc667da52019-11-30 20:52:27 +01005993 if (!aucmd) // Don't trigger BufDelete autocommands here.
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005994 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005995
Bram Moolenaara6e8f882019-12-14 16:18:15 +01005996 close_buffer(NULL, buf, DOBUF_WIPE, FALSE, TRUE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005997
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005999 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000}