blob: 97b41e0a89c510d32ffabcb1bb71f654e6e06923 [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
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200541 if (bt_terminal(buf) && (buf->b_nwindows == 1 || del_buf))
Bram Moolenaar94053a52017-08-01 21:44:33 +0200542 {
Bram Moolenaarcee52202020-03-11 14:19:58 +0100543 CHECK_CURBUF;
Bram Moolenaar94053a52017-08-01 21:44:33 +0200544 if (term_job_running(buf->b_term))
545 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200546 if (wipe_buf || unload_buf)
Bram Moolenaara997b452018-04-17 23:24:06 +0200547 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200548 if (!can_unload_buffer(buf))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100549 return FALSE;
Bram Moolenaar94f01952018-09-01 15:30:03 +0200550
Bram Moolenaarc667da52019-11-30 20:52:27 +0100551 // Wiping out or unloading a terminal buffer kills the job.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200552 free_terminal(buf);
Bram Moolenaara997b452018-04-17 23:24:06 +0200553 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200554 else
555 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100556 // The job keeps running, hide the buffer.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200557 del_buf = FALSE;
558 unload_buf = FALSE;
559 }
560 }
Bram Moolenaarc9f8b842020-11-24 19:36:16 +0100561 else if (buf->b_p_bh[0] == 'h' && !del_buf)
562 {
563 // Hide a terminal buffer.
564 unload_buf = FALSE;
565 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200566 else
567 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100568 // A terminal buffer is wiped out if the job has finished.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200569 del_buf = TRUE;
570 unload_buf = TRUE;
571 wipe_buf = TRUE;
572 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100573 CHECK_CURBUF;
Bram Moolenaar94053a52017-08-01 21:44:33 +0200574 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200575#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576
Bram Moolenaarc667da52019-11-30 20:52:27 +0100577 // Disallow deleting the buffer when it is locked (already being closed or
578 // halfway a command that relies on it). Unloading is allowed.
Bram Moolenaar94f01952018-09-01 15:30:03 +0200579 if ((del_buf || wipe_buf) && !can_unload_buffer(buf))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100580 return FALSE;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200581
Bram Moolenaarc667da52019-11-30 20:52:27 +0100582 // check no autocommands closed the window
Bram Moolenaar4033c552017-09-16 20:54:51 +0200583 if (win != NULL && win_valid_any_tab(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100585 // Set b_last_cursor when closing the last window for the buffer.
586 // Remember the last cursor position and window options of the buffer.
587 // This used to be only for the current window, but then options like
588 // 'foldmethod' may be lost with a ":only" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 if (buf->b_nwindows == 1)
590 set_last_cursor(win);
591 buflist_setfpos(buf, win,
592 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
593 win->w_cursor.col, TRUE);
594 }
595
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200596 set_bufref(&bufref, buf);
597
Bram Moolenaarc667da52019-11-30 20:52:27 +0100598 // When the buffer is no longer in a window, trigger BufWinLeave
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 if (buf->b_nwindows == 1)
600 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200601 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100602 ++buf->b_locked_split;
Bram Moolenaar82404332016-07-10 17:00:38 +0200603 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
604 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200605 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100606 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100607 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200608aucmd_abort:
Bram Moolenaarf1474d82021-12-31 19:59:55 +0000609 emsg(_(e_autocommands_caused_command_to_abort));
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100610 return FALSE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100611 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200612 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100613 --buf->b_locked_split;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200614 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100615 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200616 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617
Bram Moolenaarc667da52019-11-30 20:52:27 +0100618 // When the buffer becomes hidden, but is not unloaded, trigger
619 // BufHidden
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 if (!unload_buf)
621 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200622 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100623 ++buf->b_locked_split;
Bram Moolenaar82404332016-07-10 17:00:38 +0200624 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
625 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200626 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100627 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200628 goto aucmd_abort;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200629 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100630 --buf->b_locked_split;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200631 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100632 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200633 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100635#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100636 // autocmds may abort script processing
637 if (!ignore_abort && aborting())
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100638 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200641
Bram Moolenaarc667da52019-11-30 20:52:27 +0100642 // If the buffer was in curwin and the window has changed, go back to that
643 // window, if it still exists. This avoids that ":edit x" triggering a
644 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200645 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
646 {
647 block_autocmds();
648 goto_tabpage_win(the_curtab, the_curwin);
649 unblock_autocmds();
650 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200651
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 nwindows = buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653
Bram Moolenaarc667da52019-11-30 20:52:27 +0100654 // decrease the link count from windows (unless not in any window)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 if (buf->b_nwindows > 0)
656 --buf->b_nwindows;
657
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100658#ifdef FEAT_DIFF
659 if (diffopt_hiddenoff() && !unload_buf && buf->b_nwindows == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100660 diff_buf_delete(buf); // Clear 'diff' for hidden buffer.
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100661#endif
662
Bram Moolenaarc667da52019-11-30 20:52:27 +0100663 // Return when a window is displaying the buffer or when it's not
664 // unloaded.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100666 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667
Bram Moolenaarc667da52019-11-30 20:52:27 +0100668 // Always remove the buffer when there is no file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 if (buf->b_ffname == NULL)
670 del_buf = TRUE;
671
Bram Moolenaarc667da52019-11-30 20:52:27 +0100672 // When closing the current buffer stop Visual mode before freeing
673 // anything.
Bram Moolenaar4930a762016-09-11 14:39:53 +0200674 if (buf == curbuf && VIsual_active
Bram Moolenaar9a27c7f2016-09-09 12:57:09 +0200675#if defined(EXITFREE)
676 && !entered_free_all_mem
677#endif
678 )
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200679 end_visual_mode();
680
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100681 // Free all things allocated for this buffer.
682 // Also calls the "BufDelete" autocommands when del_buf is TRUE.
683 //
Bram Moolenaarc667da52019-11-30 20:52:27 +0100684 // Remember if we are closing the current buffer. Restore the number of
685 // windows, so that autocommands in buf_freeall() don't get confused.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 is_curbuf = (buf == curbuf);
687 buf->b_nwindows = nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100689 buf_freeall(buf, (del_buf ? BFA_DEL : 0)
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100690 + (wipe_buf ? BFA_WIPE : 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100691 + (ignore_abort ? BFA_IGNORE_ABORT : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692
Bram Moolenaarc667da52019-11-30 20:52:27 +0100693 // Autocommands may have deleted the buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200694 if (!bufref_valid(&bufref))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100695 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100696#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100697 // autocmds may abort script processing
698 if (!ignore_abort && aborting())
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100699 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100700#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100702 // It's possible that autocommands change curbuf to the one being deleted.
703 // This might cause the previous curbuf to be deleted unexpectedly. But
704 // in some cases it's OK to delete the curbuf, because a new one is
705 // obtained anyway. Therefore only return if curbuf changed to the
706 // deleted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 if (buf == curbuf && !is_curbuf)
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100708 return FALSE;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200709
Bram Moolenaar4033c552017-09-16 20:54:51 +0200710 if (win_valid_any_tab(win) && win->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100711 win->w_buffer = NULL; // make sure we don't use the buffer now
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200712
Bram Moolenaarc667da52019-11-30 20:52:27 +0100713 // Autocommands may have opened or closed windows for this buffer.
714 // Decrement the count for the close we do here.
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200715 if (buf->b_nwindows > 0)
716 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 /*
719 * Remove the buffer from the list.
720 */
721 if (wipe_buf)
722 {
Bram Moolenaar347538f2022-03-26 16:28:06 +0000723 // Do not wipe out the buffer if it is used in a window.
724 if (buf->b_nwindows > 0)
725 return FALSE;
726
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200727 if (action == DOBUF_WIPE_REUSE)
728 {
729 // we can re-use this buffer number, store it
730 if (buf_reuse.ga_itemsize == 0)
731 ga_init2(&buf_reuse, sizeof(int), 50);
732 if (ga_grow(&buf_reuse, 1) == OK)
733 ((int *)buf_reuse.ga_data)[buf_reuse.ga_len++] = buf->b_fnum;
734 }
Bram Moolenaar3d6014f2018-10-11 19:27:47 +0200735 if (buf->b_sfname != buf->b_ffname)
736 VIM_CLEAR(buf->b_sfname);
737 else
738 buf->b_sfname = NULL;
739 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 if (buf->b_prev == NULL)
741 firstbuf = buf->b_next;
742 else
743 buf->b_prev->b_next = buf->b_next;
744 if (buf->b_next == NULL)
745 lastbuf = buf->b_prev;
746 else
747 buf->b_next->b_prev = buf->b_prev;
748 free_buffer(buf);
749 }
750 else
751 {
752 if (del_buf)
753 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100754 // Free all internal variables and reset option values, to make
755 // ":bdel" compatible with Vim 5.7.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 free_buffer_stuff(buf, TRUE);
757
Bram Moolenaarc667da52019-11-30 20:52:27 +0100758 // Make it look like a new buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
760
Bram Moolenaarc667da52019-11-30 20:52:27 +0100761 // Init the options when loaded again.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 buf->b_p_initialized = FALSE;
763 }
764 buf_clear_file(buf);
765 if (del_buf)
766 buf->b_p_bl = FALSE;
767 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100768 // NOTE: at this point "curbuf" may be invalid!
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100769 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770}
771
772/*
773 * Make buffer not contain a file.
774 */
775 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100776buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777{
778 buf->b_ml.ml_line_count = 1;
Bram Moolenaarc024b462019-06-08 18:07:21 +0200779 unchanged(buf, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 buf->b_p_eol = TRUE;
782 buf->b_start_eol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000784 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 buf->b_ml.ml_mfp = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +0100786 buf->b_ml.ml_flags = ML_EMPTY; // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787#ifdef FEAT_NETBEANS_INTG
788 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789#endif
790}
791
792/*
793 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200794 * the file. Careful: get here with "curwin" NULL when exiting.
795 * flags:
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100796 * BFA_DEL buffer is going to be deleted
797 * BFA_WIPE buffer is going to be wiped out
798 * BFA_KEEP_UNDO do not free undo information
799 * BFA_IGNORE_ABORT don't abort even when aborting() returns TRUE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100802buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200805 bufref_T bufref;
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200806 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaar5a497892016-09-03 16:29:04 +0200807 win_T *the_curwin = curwin;
808 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809
Bram Moolenaarc667da52019-11-30 20:52:27 +0100810 // Make sure the buffer isn't closed by autocommands.
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200811 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100812 ++buf->b_locked_split;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200813 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200814 if (buf->b_ml.ml_mfp != NULL)
815 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200816 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
817 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200818 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100819 // autocommands deleted the buffer
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200820 return;
821 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200822 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200824 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
825 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200826 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100827 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 return;
829 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200830 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200832 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
833 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200834 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100835 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 return;
837 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200838 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100839 --buf->b_locked_split;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200840
Bram Moolenaarc667da52019-11-30 20:52:27 +0100841 // If the buffer was in curwin and the window has changed, go back to that
842 // window, if it still exists. This avoids that ":edit x" triggering a
843 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaar5a497892016-09-03 16:29:04 +0200844 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
845 {
846 block_autocmds();
847 goto_tabpage_win(the_curtab, the_curwin);
848 unblock_autocmds();
849 }
Bram Moolenaar5a497892016-09-03 16:29:04 +0200850
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100851#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100852 // autocmds may abort script processing
853 if ((flags & BFA_IGNORE_ABORT) == 0 && aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100855#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100857 // It's possible that autocommands change curbuf to the one being deleted.
858 // This might cause curbuf to be deleted unexpectedly. But in some cases
859 // it's OK to delete the curbuf, because a new one is obtained anyway.
860 // Therefore only return if curbuf changed to the deleted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 if (buf == curbuf && !is_curbuf)
862 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863#ifdef FEAT_DIFF
Bram Moolenaarc667da52019-11-30 20:52:27 +0100864 diff_buf_delete(buf); // Can't use 'diff' for unloaded buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200866#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100867 // Remove any ownsyntax, unless exiting.
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200868 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaar89c71222011-11-30 15:40:56 +0100869 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200870#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000871
872#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +0100873 // No folds in an empty buffer.
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000874 {
875 win_T *win;
876 tabpage_T *tp;
877
878 FOR_ALL_TAB_WINDOWS(tp, win)
879 if (win->w_buffer == buf)
880 clearFolding(win);
881 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000882#endif
883
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884#ifdef FEAT_TCL
885 tcl_buffer_free(buf);
886#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100887 ml_close(buf, TRUE); // close and delete the memline/memfile
888 buf->b_ml.ml_line_count = 0; // no lines in buffer
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200889 if ((flags & BFA_KEEP_UNDO) == 0)
890 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100891 u_blockfree(buf); // free the memory allocated for undo
892 u_clearall(buf); // reset all undo information
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100895 syntax_clear(&buf->b_s); // reset syntax info
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100897#ifdef FEAT_PROP_POPUP
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100898 clear_buf_prop_types(buf);
899#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100900 buf->b_flags &= ~BF_READERR; // a read error is no longer relevant
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901}
902
903/*
904 * Free a buffer structure and the things it contains related to the buffer
905 * itself (not the file, that must have been done already).
906 */
907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100908free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200910 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200912#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100913 // b:changedtick uses an item in buf_T, remove it now
Bram Moolenaarb2c87502017-10-14 21:15:58 +0200914 dictitem_remove(buf->b_vars, (dictitem_T *)&buf->b_ct_di);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200915 unref_var_dict(buf->b_vars);
Bram Moolenaar86173482019-10-01 17:02:16 +0200916 remove_listeners(buf);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200917#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200918#ifdef FEAT_LUA
919 lua_buffer_free(buf);
920#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000921#ifdef FEAT_MZSCHEME
922 mzscheme_buffer_free(buf);
923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924#ifdef FEAT_PERL
925 perl_buf_free(buf);
926#endif
927#ifdef FEAT_PYTHON
928 python_buffer_free(buf);
929#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200930#ifdef FEAT_PYTHON3
931 python3_buffer_free(buf);
932#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933#ifdef FEAT_RUBY
934 ruby_buffer_free(buf);
935#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200936#ifdef FEAT_JOB_CHANNEL
937 channel_buffer_free(buf);
938#endif
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200939#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +0200940 free_terminal(buf);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200941#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +0200942#ifdef FEAT_JOB_CHANNEL
943 vim_free(buf->b_prompt_text);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200944 free_callback(&buf->b_prompt_callback);
Bram Moolenaar86173482019-10-01 17:02:16 +0200945 free_callback(&buf->b_prompt_interrupt);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200946#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200947
948 buf_hashtab_remove(buf);
949
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200950 aubuflocal_remove(buf);
951
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200952 if (autocmd_busy)
953 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100954 // Do not free the buffer structure while autocommands are executing,
955 // it's still needed. Free it when autocmd_busy is reset.
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200956 buf->b_next = au_pending_free_buf;
957 au_pending_free_buf = buf;
958 }
959 else
Bram Moolenaarcee52202020-03-11 14:19:58 +0100960 {
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200961 vim_free(buf);
Bram Moolenaarcee52202020-03-11 14:19:58 +0100962 if (curbuf == buf)
963 curbuf = NULL; // make clear it's not to be used
964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965}
966
967/*
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100968 * Initializes b:changedtick.
Bram Moolenaar79518e22017-02-17 16:31:35 +0100969 */
970 static void
971init_changedtick(buf_T *buf)
972{
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100973 dictitem_T *di = (dictitem_T *)&buf->b_ct_di;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100974
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100975 di->di_flags = DI_FLAGS_FIX | DI_FLAGS_RO;
976 di->di_tv.v_type = VAR_NUMBER;
977 di->di_tv.v_lock = VAR_FIXED;
978 di->di_tv.vval.v_number = 0;
979
Bram Moolenaar92769c32017-02-25 15:41:37 +0100980#ifdef FEAT_EVAL
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100981 STRCPY(buf->b_ct_di.di_key, "changedtick");
982 (void)dict_add(buf->b_vars, di);
Bram Moolenaar92769c32017-02-25 15:41:37 +0100983#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +0100984}
985
986/*
Bram Moolenaarc3126192022-08-26 12:58:17 +0100987 * Free the b_wininfo list for buffer "buf".
988 */
989 static void
990clear_wininfo(buf_T *buf)
991{
992 wininfo_T *wip;
993
994 while (buf->b_wininfo != NULL)
995 {
996 wip = buf->b_wininfo;
997 buf->b_wininfo = wip->wi_next;
998 free_wininfo(wip);
999 }
1000}
1001
1002/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
1004 */
1005 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001006free_buffer_stuff(
1007 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001008 int free_options) // free options as well
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009{
1010 if (free_options)
1011 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001012 clear_wininfo(buf); // including window-local options
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +02001014#ifdef FEAT_SPELL
1015 ga_clear(&buf->b_s.b_langp);
1016#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 }
1018#ifdef FEAT_EVAL
Bram Moolenaar79518e22017-02-17 16:31:35 +01001019 {
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001020 varnumber_T tick = CHANGEDTICK(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001021
Bram Moolenaarc667da52019-11-30 20:52:27 +01001022 vars_clear(&buf->b_vars->dv_hashtab); // free all buffer variables
Bram Moolenaar79518e22017-02-17 16:31:35 +01001023 hash_init(&buf->b_vars->dv_hashtab);
1024 init_changedtick(buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001025 CHANGEDTICK(buf) = tick;
Bram Moolenaarf10997a2020-01-03 21:00:02 +01001026 remove_listeners(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028#endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001029 uc_clear(&buf->b_ucmds); // clear local user commands
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030#ifdef FEAT_SIGNS
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001031 buf_delete_signs(buf, (char_u *)"*"); // delete any signs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00001033#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001034 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00001035#endif
Bram Moolenaar0c740e72022-07-25 19:07:04 +01001036#ifdef FEAT_PROP_POPUP
1037 ga_clear_strings(&buf->b_textprop_text);
1038#endif
zeertzjqc207fd22022-06-29 10:37:40 +01001039 map_clear_mode(buf, MAP_ALL_MODES, TRUE, FALSE); // clear local mappings
1040 map_clear_mode(buf, MAP_ALL_MODES, TRUE, TRUE); // clear local abbrevs
Bram Moolenaard23a8232018-02-10 18:45:26 +01001041 VIM_CLEAR(buf->b_start_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042}
1043
1044/*
Bram Moolenaar4882d982020-10-25 17:55:09 +01001045 * Free one wininfo_T.
1046 */
1047 void
1048free_wininfo(wininfo_T *wip)
1049{
1050 if (wip->wi_optset)
1051 {
1052 clear_winopt(&wip->wi_opt);
1053#ifdef FEAT_FOLDING
1054 deleteFoldRecurse(&wip->wi_folds);
1055#endif
1056 }
1057 vim_free(wip);
1058}
1059
1060/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 * Go to another buffer. Handles the result of the ATTENTION dialog.
1062 */
1063 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001064goto_buffer(
1065 exarg_T *eap,
1066 int start,
1067 int dir,
1068 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001070 bufref_T old_curbuf;
Bram Moolenaar188639d2022-04-04 16:57:21 +01001071 int save_sea = swap_exists_action;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001072
1073 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074
Bram Moolenaar188639d2022-04-04 16:57:21 +01001075 if (swap_exists_action == SEA_NONE)
1076 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
1078 start, dir, count, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
1080 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001081#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001082 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001083
Bram Moolenaarc667da52019-11-30 20:52:27 +01001084 // Reset the error/interrupt/exception state here so that
1085 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001086 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001087#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001088
Bram Moolenaarc667da52019-11-30 20:52:27 +01001089 // Quitting means closing the split window, nothing else.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 win_close(curwin, TRUE);
Bram Moolenaar188639d2022-04-04 16:57:21 +01001091 swap_exists_action = save_sea;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001092 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001093
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001094#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001095 // Restore the error/interrupt/exception state if not discarded by a
1096 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001097 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001098#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 }
1100 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001101 handle_swap_exists(&old_curbuf);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001102}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104/*
1105 * Handle the situation of swap_exists_action being set.
1106 * It is allowed for "old_curbuf" to be NULL or invalid.
1107 */
1108 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001109handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110{
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001111#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001112 cleanup_T cs;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001113#endif
1114#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001115 long old_tw = curbuf->b_p_tw;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001116#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001117 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001118
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 if (swap_exists_action == SEA_QUIT)
1120 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001121#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001122 // Reset the error/interrupt/exception state here so that
1123 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001124 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001125#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001126
Bram Moolenaarc667da52019-11-30 20:52:27 +01001127 // User selected Quit at ATTENTION prompt. Go back to previous
1128 // buffer. If that buffer is gone or the same as the current one,
1129 // open a new, empty buffer.
1130 swap_exists_action = SEA_NONE; // don't want it again
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001131 swap_exists_did_quit = TRUE;
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001132 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001133 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1134 || old_curbuf->br_buf == curbuf)
Bram Moolenaar1d97efc2021-07-04 13:27:11 +02001135 {
1136 // Block autocommands here because curwin->w_buffer is NULL.
1137 block_autocmds();
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001138 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar1d97efc2021-07-04 13:27:11 +02001139 unblock_autocmds();
1140 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001141 else
1142 buf = old_curbuf->br_buf;
1143 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001144 {
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001145 int old_msg_silent = msg_silent;
1146
1147 if (shortmess(SHM_FILEINFO))
1148 msg_silent = 1; // prevent fileinfo message
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001149 enter_buffer(buf);
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001150 // restore msg_silent, so that the command line will be shown
1151 msg_silent = old_msg_silent;
1152
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001153#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001154 if (old_tw != curbuf->b_p_tw)
1155 check_colorcolumn(curwin);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001156#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001157 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001158 // If "old_curbuf" is NULL we are in big trouble here...
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001159
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001160#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001161 // Restore the error/interrupt/exception state if not discarded by a
1162 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001163 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 }
1166 else if (swap_exists_action == SEA_RECOVER)
1167 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001168#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001169 // Reset the error/interrupt/exception state here so that
1170 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001171 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001172#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001173
Bram Moolenaarc667da52019-11-30 20:52:27 +01001174 // User selected Recover at ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 msg_scroll = TRUE;
Bram Moolenaar99499b12019-05-23 21:35:48 +02001176 ml_recover(FALSE);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001177 msg_puts("\n"); // don't overwrite the last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001179 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001180
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001181#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001182 // Restore the error/interrupt/exception state if not discarded by a
1183 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001184 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001185#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 }
1187 swap_exists_action = SEA_NONE;
1188}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190/*
Bram Moolenaara02471e2014-01-10 16:43:14 +01001191 * Make the current buffer empty.
1192 * Used when it is wiped out and it's the last buffer.
1193 */
1194 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001195empty_curbuf(
1196 int close_others,
1197 int forceit,
1198 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001199{
1200 int retval;
1201 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001202 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001203
1204 if (action == DOBUF_UNLOAD)
1205 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001206 emsg(_(e_cannot_unload_last_buffer));
Bram Moolenaara02471e2014-01-10 16:43:14 +01001207 return FAIL;
1208 }
1209
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001210 set_bufref(&bufref, buf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001211 if (close_others)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001212 // Close any other windows on this buffer, then make it empty.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001213 close_windows(buf, TRUE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001214
1215 setpcmark();
1216 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1217 forceit ? ECMD_FORCEIT : 0, curwin);
1218
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001219 // do_ecmd() may create a new buffer, then we have to delete
1220 // the old one. But do_ecmd() may have done that already, check
1221 // if the buffer still exists.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001222 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001223 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001224 if (!close_others)
1225 need_fileinfo = FALSE;
1226 return retval;
1227}
Bram Moolenaar94f01952018-09-01 15:30:03 +02001228
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229/*
1230 * Implementation of the commands for the buffer list.
1231 *
1232 * action == DOBUF_GOTO go to specified buffer
1233 * action == DOBUF_SPLIT split window and go to specified buffer
1234 * action == DOBUF_UNLOAD unload specified buffer(s)
1235 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1236 * action == DOBUF_WIPE delete specified buffer(s) really
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001237 * action == DOBUF_WIPE_REUSE idem, and add number to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 *
1239 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1240 * start == DOBUF_FIRST go to "count" buffer from first buffer
1241 * start == DOBUF_LAST go to "count" buffer from last buffer
1242 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1243 *
1244 * Return FAIL or OK.
1245 */
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001246 static int
1247do_buffer_ext(
Bram Moolenaar7454a062016-01-30 15:14:10 +01001248 int action,
1249 int start,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001250 int dir, // FORWARD or BACKWARD
1251 int count, // buffer number or number of buffers
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001252 int flags) // DOBUF_FORCEIT etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253{
1254 buf_T *buf;
1255 buf_T *bp;
1256 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001257 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258
1259 switch (start)
1260 {
1261 case DOBUF_FIRST: buf = firstbuf; break;
1262 case DOBUF_LAST: buf = lastbuf; break;
1263 default: buf = curbuf; break;
1264 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001265 if (start == DOBUF_MOD) // find next modified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 {
1267 while (count-- > 0)
1268 {
1269 do
1270 {
1271 buf = buf->b_next;
1272 if (buf == NULL)
1273 buf = firstbuf;
1274 }
1275 while (buf != curbuf && !bufIsChanged(buf));
1276 }
1277 if (!bufIsChanged(buf))
1278 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001279 emsg(_(e_no_modified_buffer_found));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 return FAIL;
1281 }
1282 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001283 else if (start == DOBUF_FIRST && count) // find specified buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 {
1285 while (buf != NULL && buf->b_fnum != count)
1286 buf = buf->b_next;
1287 }
1288 else
1289 {
1290 bp = NULL;
1291 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1292 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001293 // remember the buffer where we start, we come back there when all
1294 // buffers are unlisted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 if (bp == NULL)
1296 bp = buf;
1297 if (dir == FORWARD)
1298 {
1299 buf = buf->b_next;
1300 if (buf == NULL)
1301 buf = firstbuf;
1302 }
1303 else
1304 {
1305 buf = buf->b_prev;
1306 if (buf == NULL)
1307 buf = lastbuf;
1308 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001309 // don't count unlisted buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 if (unload || buf->b_p_bl)
1311 {
1312 --count;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001313 bp = NULL; // use this buffer as new starting point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 }
1315 if (bp == buf)
1316 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001317 // back where we started, didn't find anything.
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001318 emsg(_(e_there_is_no_listed_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 return FAIL;
1320 }
1321 }
1322 }
1323
Bram Moolenaarc667da52019-11-30 20:52:27 +01001324 if (buf == NULL) // could not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 {
1326 if (start == DOBUF_FIRST)
1327 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001328 // don't warn when deleting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 if (!unload)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001330 semsg(_(e_buffer_nr_does_not_exist), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 }
1332 else if (dir == FORWARD)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001333 emsg(_(e_cannot_go_beyond_last_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 else
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001335 emsg(_(e_cannot_go_before_first_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 return FAIL;
1337 }
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001338#ifdef FEAT_PROP_POPUP
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001339 if ((flags & DOBUF_NOPOPUP) && bt_popup(buf) && !bt_terminal(buf))
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001340 return OK;
1341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342
1343#ifdef FEAT_GUI
1344 need_mouse_correct = TRUE;
1345#endif
1346
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001348 * delete buffer "buf" from memory and/or the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 */
1350 if (unload)
1351 {
1352 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001353 bufref_T bufref;
1354
Bram Moolenaar94f01952018-09-01 15:30:03 +02001355 if (!can_unload_buffer(buf))
Bram Moolenaara997b452018-04-17 23:24:06 +02001356 return FAIL;
Bram Moolenaara997b452018-04-17 23:24:06 +02001357
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001358 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359
Bram Moolenaarc667da52019-11-30 20:52:27 +01001360 // When unloading or deleting a buffer that's already unloaded and
1361 // unlisted: fail silently.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001362 if (action != DOBUF_WIPE && action != DOBUF_WIPE_REUSE
1363 && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 return FAIL;
1365
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001366 if ((flags & DOBUF_FORCEIT) == 0 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001368#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001369 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001371# ifdef FEAT_TERMINAL
1372 if (term_job_running(buf->b_term))
1373 {
1374 if (term_confirm_stop(buf) == FAIL)
1375 return FAIL;
1376 }
1377 else
1378# endif
1379 {
1380 dialog_changed(buf, FALSE);
1381 if (!bufref_valid(&bufref))
1382 // Autocommand deleted buffer, oops! It's not changed
1383 // now.
1384 return FAIL;
1385 // If it's still changed fail silently, the dialog already
1386 // mentioned why it fails.
1387 if (bufIsChanged(buf))
1388 return FAIL;
1389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001391 else
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001394 no_write_message_buf(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 return FAIL;
1396 }
1397 }
1398
Bram Moolenaarc667da52019-11-30 20:52:27 +01001399 // When closing the current buffer stop Visual mode.
Bram Moolenaar4930a762016-09-11 14:39:53 +02001400 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001401 end_visual_mode();
1402
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001403 // If deleting the last (listed) buffer, make it empty.
1404 // The last (listed) buffer cannot be unloaded.
Bram Moolenaar29323592016-07-24 22:04:11 +02001405 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 if (bp->b_p_bl && bp != buf)
1407 break;
1408 if (bp == NULL && buf == curbuf)
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001409 return empty_curbuf(TRUE, (flags & DOBUF_FORCEIT), action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001411 // If the deleted buffer is the current one, close the current window
1412 // (unless it's the only window). Repeat this so long as we end up in
1413 // a window with this buffer.
Bram Moolenaarf740b292006-02-16 22:11:02 +00001414 while (buf == curbuf
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001415 && !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
Bram Moolenaar459ca562016-11-10 18:16:33 +01001416 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001417 {
1418 if (win_close(curwin, FALSE) == FAIL)
1419 break;
1420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001422 // If the buffer to be deleted is not the current one, delete it here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 if (buf != curbuf)
1424 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001425 close_windows(buf, FALSE);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001426 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows <= 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001427 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 return OK;
1429 }
1430
1431 /*
1432 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001433 * There should be another, otherwise it would have been handled
1434 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001435 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 * Then prefer the buffer we most recently visited.
1437 * Else try to find one that is loaded, after the current buffer,
1438 * then before the current buffer.
1439 * Finally use any buffer.
1440 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001441 buf = NULL; // selected buffer
1442 bp = NULL; // used when no loaded buffer found
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001443 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1444 buf = au_new_curbuf.br_buf;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001445 else if (curwin->w_jumplistlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 {
1447 int jumpidx;
1448
1449 jumpidx = curwin->w_jumplistidx - 1;
1450 if (jumpidx < 0)
1451 jumpidx = curwin->w_jumplistlen - 1;
1452
1453 forward = jumpidx;
1454 while (jumpidx != curwin->w_jumplistidx)
1455 {
1456 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1457 if (buf != NULL)
1458 {
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001459 // Skip current and unlisted bufs. Also skip a quickfix
1460 // buffer, it might be deleted soon.
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001461 if (buf == curbuf || !buf->b_p_bl || bt_quickfix(buf))
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001462 buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 else if (buf->b_ml.ml_mfp == NULL)
1464 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001465 // skip unloaded buf, but may keep it for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 if (bp == NULL)
1467 bp = buf;
1468 buf = NULL;
1469 }
1470 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001471 if (buf != NULL) // found a valid buffer: stop searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001473 // advance to older entry in jump list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1475 break;
1476 if (--jumpidx < 0)
1477 jumpidx = curwin->w_jumplistlen - 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001478 if (jumpidx == forward) // List exhausted for sure
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 break;
1480 }
1481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482
Bram Moolenaarc667da52019-11-30 20:52:27 +01001483 if (buf == NULL) // No previous buffer, Try 2'nd approach
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 {
1485 forward = TRUE;
1486 buf = curbuf->b_next;
1487 for (;;)
1488 {
1489 if (buf == NULL)
1490 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001491 if (!forward) // tried both directions
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 break;
1493 buf = curbuf->b_prev;
1494 forward = FALSE;
1495 continue;
1496 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001497 // in non-help buffer, try to skip help buffers, and vv
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001498 if (buf->b_help == curbuf->b_help && buf->b_p_bl
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001499 && !bt_quickfix(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001501 if (buf->b_ml.ml_mfp != NULL) // found loaded buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001503 if (bp == NULL) // remember unloaded buf for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 bp = buf;
1505 }
1506 if (forward)
1507 buf = buf->b_next;
1508 else
1509 buf = buf->b_prev;
1510 }
1511 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001512 if (buf == NULL) // No loaded buffer, use unloaded one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 buf = bp;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001514 if (buf == NULL) // No loaded buffer, find listed one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001516 FOR_ALL_BUFFERS(buf)
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01001517 if (buf->b_p_bl && buf != curbuf && !bt_quickfix(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 break;
1519 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001520 if (buf == NULL) // Still no buffer, just take one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 {
1522 if (curbuf->b_next != NULL)
1523 buf = curbuf->b_next;
1524 else
1525 buf = curbuf->b_prev;
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001526 if (bt_quickfix(buf))
1527 buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 }
1529 }
1530
Bram Moolenaara02471e2014-01-10 16:43:14 +01001531 if (buf == NULL)
1532 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001533 // Autocommands must have wiped out all other buffers. Only option
1534 // now is to make the current buffer empty.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001535 return empty_curbuf(FALSE, (flags & DOBUF_FORCEIT), action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001536 }
1537
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001539 * make "buf" the current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001541 if (action == DOBUF_SPLIT) // split window first
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001543 // If 'switchbuf' contains "useopen": jump to first window containing
1544 // "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00001545 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001546 return OK;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001547 // If 'switchbuf' contains "usetab": jump to first window in any tab
1548 // page containing "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00001549 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 return OK;
1551 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 return FAIL;
1553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554
Bram Moolenaarc667da52019-11-30 20:52:27 +01001555 // go to current buffer - nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 if (buf == curbuf)
1557 return OK;
1558
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001559 // Check if the current buffer may be abandoned.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001560 if (action == DOBUF_GOTO && !can_abandon(curbuf, (flags & DOBUF_FORCEIT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 {
1562#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001563 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 {
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001565# ifdef FEAT_TERMINAL
1566 if (term_job_running(curbuf->b_term))
1567 {
1568 if (term_confirm_stop(curbuf) == FAIL)
1569 return FAIL;
1570 // Manually kill the terminal here because this command will
1571 // hide it otherwise.
1572 free_terminal(curbuf);
1573 }
1574 else
1575# endif
1576 {
1577 bufref_T bufref;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001578
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001579 set_bufref(&bufref, buf);
1580 dialog_changed(curbuf, FALSE);
1581 if (!bufref_valid(&bufref))
1582 // Autocommand deleted buffer, oops!
1583 return FAIL;
1584
1585 if (bufIsChanged(curbuf))
1586 {
1587 no_write_message();
1588 return FAIL;
1589 }
1590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 }
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001592 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593#endif
1594 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001595 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 return FAIL;
1597 }
1598 }
1599
Bram Moolenaarc667da52019-11-30 20:52:27 +01001600 // Go to the other buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 set_curbuf(buf, action);
1602
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 if (action == DOBUF_SPLIT)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001604 RESET_BINDING(curwin); // reset 'scrollbind' and 'cursorbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001606#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001607 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 return FAIL;
1609#endif
1610
1611 return OK;
1612}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001614 int
1615do_buffer(
1616 int action,
1617 int start,
1618 int dir, // FORWARD or BACKWARD
1619 int count, // buffer number or number of buffers
1620 int forceit) // TRUE when using !
1621{
1622 return do_buffer_ext(action, start, dir, count,
1623 forceit ? DOBUF_FORCEIT : 0);
1624}
1625
1626/*
1627 * do_bufdel() - delete or unload buffer(s)
1628 *
1629 * addr_count == 0: ":bdel" - delete current buffer
1630 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1631 * buffer "end_bnr", then any other arguments.
1632 * addr_count == 2: ":N,N bdel" - delete buffers in range
1633 *
1634 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1635 * DOBUF_DEL (":bdel")
1636 *
1637 * Returns error message or NULL
1638 */
1639 char *
1640do_bufdel(
1641 int command,
1642 char_u *arg, // pointer to extra arguments
1643 int addr_count,
1644 int start_bnr, // first buffer number in a range
1645 int end_bnr, // buffer nr or last buffer nr in a range
1646 int forceit)
1647{
1648 int do_current = 0; // delete current buffer?
1649 int deleted = 0; // number of buffers deleted
1650 char *errormsg = NULL; // return value
1651 int bnr; // buffer number
1652 char_u *p;
1653
1654 if (addr_count == 0)
1655 {
1656 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1657 }
1658 else
1659 {
1660 if (addr_count == 2)
1661 {
1662 if (*arg) // both range and argument is not allowed
Bram Moolenaar74409f62022-01-01 15:58:22 +00001663 return ex_errmsg(e_trailing_characters_str, arg);
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001664 bnr = start_bnr;
1665 }
1666 else // addr_count == 1
1667 bnr = end_bnr;
1668
1669 for ( ;!got_int; ui_breakcheck())
1670 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001671 // Delete the current buffer last, otherwise when the
1672 // current buffer is deleted, the next buffer becomes
1673 // the current one and will be loaded, which may then
1674 // also be deleted, etc.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001675 if (bnr == curbuf->b_fnum)
1676 do_current = bnr;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00001677 else if (do_buffer_ext(command, DOBUF_FIRST, FORWARD, bnr,
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001678 DOBUF_NOPOPUP | (forceit ? DOBUF_FORCEIT : 0)) == OK)
1679 ++deleted;
1680
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001681 // find next buffer number to delete/unload
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001682 if (addr_count == 2)
1683 {
1684 if (++bnr > end_bnr)
1685 break;
1686 }
1687 else // addr_count == 1
1688 {
1689 arg = skipwhite(arg);
1690 if (*arg == NUL)
1691 break;
1692 if (!VIM_ISDIGIT(*arg))
1693 {
1694 p = skiptowhite_esc(arg);
1695 bnr = buflist_findpat(arg, p,
1696 command == DOBUF_WIPE || command == DOBUF_WIPE_REUSE,
1697 FALSE, FALSE);
1698 if (bnr < 0) // failed
1699 break;
1700 arg = p;
1701 }
1702 else
1703 bnr = getdigits(&arg);
1704 }
1705 }
1706 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1707 FORWARD, do_current, forceit) == OK)
1708 ++deleted;
1709
1710 if (deleted == 0)
1711 {
1712 if (command == DOBUF_UNLOAD)
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001713 STRCPY(IObuff, _(e_no_buffers_were_unloaded));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001714 else if (command == DOBUF_DEL)
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001715 STRCPY(IObuff, _(e_no_buffers_were_deleted));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001716 else
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001717 STRCPY(IObuff, _(e_no_buffers_were_wiped_out));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001718 errormsg = (char *)IObuff;
1719 }
1720 else if (deleted >= p_report)
1721 {
1722 if (command == DOBUF_UNLOAD)
1723 smsg(NGETTEXT("%d buffer unloaded",
1724 "%d buffers unloaded", deleted), deleted);
1725 else if (command == DOBUF_DEL)
1726 smsg(NGETTEXT("%d buffer deleted",
1727 "%d buffers deleted", deleted), deleted);
1728 else
1729 smsg(NGETTEXT("%d buffer wiped out",
1730 "%d buffers wiped out", deleted), deleted);
1731 }
1732 }
1733
1734
1735 return errormsg;
1736}
1737
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738/*
1739 * Set current buffer to "buf". Executes autocommands and closes current
1740 * buffer. "action" tells how to close the current buffer:
1741 * DOBUF_GOTO free or hide it
1742 * DOBUF_SPLIT nothing
1743 * DOBUF_UNLOAD unload it
1744 * DOBUF_DEL delete it
1745 * DOBUF_WIPE wipe it out
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001746 * DOBUF_WIPE_REUSE wipe it out and add to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 */
1748 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001749set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750{
1751 buf_T *prevbuf;
1752 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001753 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001754#ifdef FEAT_SYN_HL
1755 long old_tw = curbuf->b_p_tw;
1756#endif
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001757 bufref_T newbufref;
1758 bufref_T prevbufref;
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001759 int valid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760
1761 setpcmark();
Bram Moolenaare1004402020-10-24 20:49:43 +02001762 if ((cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001763 curwin->w_alt_fnum = curbuf->b_fnum; // remember alternate file
1764 buflist_altfpos(curwin); // remember curpos
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765
Bram Moolenaarc667da52019-11-30 20:52:27 +01001766 // Don't restart Select mode after switching to another buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768
Bram Moolenaarc667da52019-11-30 20:52:27 +01001769 // close_windows() or apply_autocmds() may change curbuf and wipe out "buf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 prevbuf = curbuf;
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001771 set_bufref(&prevbufref, prevbuf);
1772 set_bufref(&newbufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773
Bram Moolenaar983d83f2021-02-07 12:12:43 +01001774 // Autocommands may delete the current buffer and/or the buffer we want to
1775 // go to. In those cases don't close the buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001776 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001777 || (bufref_valid(&prevbufref)
1778 && bufref_valid(&newbufref)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001779#ifdef FEAT_EVAL
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001780 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001782 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001784#ifdef FEAT_SYN_HL
1785 if (prevbuf == curwin->w_buffer)
1786 reset_synblock(curwin);
1787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001789 close_windows(prevbuf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001790#if defined(FEAT_EVAL)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001791 if (bufref_valid(&prevbufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792#else
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001793 if (bufref_valid(&prevbufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001795 {
Bram Moolenaare25865a2012-07-06 16:22:02 +02001796 win_T *previouswin = curwin;
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001797
Bram Moolenaare615db02022-01-20 21:00:54 +00001798 // Do not sync when in Insert mode and the buffer is open in
1799 // another window, might be a timer doing something in another
1800 // window.
1801 if (prevbuf == curbuf
Bram Moolenaar24959102022-05-07 20:01:16 +01001802 && ((State & MODE_INSERT) == 0 || curbuf->b_nwindows <= 1))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001803 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1805 unload ? action : (action == DOBUF_GOTO
Bram Moolenaareb44a682017-08-03 22:44:55 +02001806 && !buf_hide(prevbuf)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001807 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0,
1808 FALSE, FALSE);
Bram Moolenaare25865a2012-07-06 16:22:02 +02001809 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001810 // autocommands changed curwin, Grr!
Bram Moolenaare25865a2012-07-06 16:22:02 +02001811 curwin = previouswin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001814 // An autocommand may have deleted "buf", already entered it (e.g., when
1815 // it did ":bunload") or aborted the script processing.
1816 // If curwin->w_buffer is null, enter_buffer() will make it valid again
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001817 valid = buf_valid(buf);
1818 if ((valid && buf != curbuf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001819#ifdef FEAT_EVAL
Bram Moolenaar4033c552017-09-16 20:54:51 +02001820 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001822 ) || curwin->w_buffer == NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001823 {
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001824 // If the buffer is not valid but curwin->w_buffer is NULL we must
1825 // enter some buffer. Using the last one is hopefully OK.
1826 if (!valid)
1827 enter_buffer(lastbuf);
1828 else
1829 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001830#ifdef FEAT_SYN_HL
1831 if (old_tw != curbuf->b_p_tw)
1832 check_colorcolumn(curwin);
1833#endif
1834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835}
1836
1837/*
1838 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001839 * Old curbuf must have been abandoned already! This also means "curbuf" may
1840 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001843enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844{
Bram Moolenaarcfeb8a52022-08-13 14:09:44 +01001845 // when closing the current buffer stop Visual mode
1846 if (VIsual_active
1847#if defined(EXITFREE)
1848 && !entered_free_all_mem
1849#endif
1850 )
1851 end_visual_mode();
1852
Bram Moolenaar010ee962019-09-25 20:37:36 +02001853 // Get the buffer in the current window.
1854 curwin->w_buffer = buf;
1855 curbuf = buf;
1856 ++curbuf->b_nwindows;
1857
1858 // Copy buffer and window local option values. Not for a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1860 if (!buf->b_help)
1861 get_winopts(buf);
1862#ifdef FEAT_FOLDING
1863 else
Bram Moolenaar010ee962019-09-25 20:37:36 +02001864 // Remove all folds in the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 clearFolding(curwin);
Bram Moolenaar010ee962019-09-25 20:37:36 +02001866 foldUpdateAll(curwin); // update folds (later).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867#endif
1868
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001870 if (curwin->w_p_diff)
1871 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872#endif
1873
Bram Moolenaara971b822011-09-14 14:43:25 +02001874#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001875 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001876#endif
1877
Bram Moolenaarc667da52019-11-30 20:52:27 +01001878 // Cursor on first line by default.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 curwin->w_cursor.lnum = 1;
1880 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001883 curwin->w_topline_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884
Bram Moolenaarc667da52019-11-30 20:52:27 +01001885 // mark cursor position as being invalid
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001886 curwin->w_valid = 0;
1887
Bram Moolenaar9cea87c2018-09-21 16:59:45 +02001888 buflist_setfpos(curbuf, curwin, curbuf->b_last_cursor.lnum,
1889 curbuf->b_last_cursor.col, TRUE);
1890
Bram Moolenaarc667da52019-11-30 20:52:27 +01001891 // Make sure the buffer is loaded.
1892 if (curbuf->b_ml.ml_mfp == NULL) // need to load the file
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001893 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001894 // If there is no filetype, allow for detecting one. Esp. useful for
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001895 // ":ball" used in an autocommand. If there already is a filetype we
Bram Moolenaarc667da52019-11-30 20:52:27 +01001896 // might prefer to keep it.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001897 if (*curbuf->b_p_ft == NUL)
1898 did_filetype = FALSE;
1899
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001900 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 else
1903 {
Bram Moolenaareda65222019-05-16 20:29:44 +02001904 if (!msg_silent && !shortmess(SHM_FILEINFO))
1905 need_fileinfo = TRUE; // display file info after redraw
1906
1907 // check if file changed
1908 (void)buf_check_timestamp(curbuf, FALSE);
1909
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001911#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1915 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 }
1917
Bram Moolenaarc667da52019-11-30 20:52:27 +01001918 // If autocommands did not change the cursor position, restore cursor lnum
1919 // and possibly cursor col.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 if (curwin->w_cursor.lnum == 1 && inindent(0))
1921 buflist_getfpos();
1922
Bram Moolenaarc667da52019-11-30 20:52:27 +01001923 check_arg_idx(curwin); // check for valid arg_idx
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 maketitle();
Bram Moolenaarc667da52019-11-30 20:52:27 +01001925 // when autocmds didn't change it
Bram Moolenaard4153d42008-11-15 15:06:17 +00001926 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001927 scroll_cursor_halfway(FALSE); // redisplay at correct position
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928
Bram Moolenaar009b2592004-10-24 19:18:58 +00001929#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarc667da52019-11-30 20:52:27 +01001930 // Send fileOpened event because we've changed buffers.
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001931 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001932#endif
1933
Bram Moolenaarc667da52019-11-30 20:52:27 +01001934 // Change directories when the 'acd' option is set.
Bram Moolenaar6f470022018-04-10 18:47:20 +02001935 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936
1937#ifdef FEAT_KEYMAP
1938 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001939 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001941#ifdef FEAT_SPELL
Bram Moolenaarc667da52019-11-30 20:52:27 +01001942 // May need to set the spell language. Can only do this after the buffer
1943 // has been properly setup.
Bram Moolenaar860cae12010-06-05 23:22:07 +02001944 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1945 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001946#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001947#ifdef FEAT_VIMINFO
1948 curbuf->b_last_used = vim_time();
1949#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001950
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001951 redraw_later(UPD_NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952}
1953
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001954#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1955/*
1956 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001957 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001958 */
1959 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001960do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001961{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001962 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001963 && curbuf->b_ffname != NULL
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001964 && vim_chdirfile(curbuf->b_ffname, "auto") == OK)
Bram Moolenaar05268152021-11-18 18:53:45 +00001965 {
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001966 shorten_fnames(TRUE);
Bram Moolenaar05268152021-11-18 18:53:45 +00001967 last_chdir_reason = "autochdir";
1968 }
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001969}
1970#endif
1971
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01001972 static void
1973no_write_message_buf(buf_T *buf UNUSED)
1974{
1975#ifdef FEAT_TERMINAL
1976 if (term_job_running(buf->b_term))
1977 emsg(_(e_job_still_running_add_bang_to_end_the_job));
1978 else
1979#endif
1980 semsg(_(e_no_write_since_last_change_for_buffer_nr_add_bang_to_override),
1981 buf->b_fnum);
1982}
1983
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001984 void
1985no_write_message(void)
1986{
1987#ifdef FEAT_TERMINAL
1988 if (term_job_running(curbuf->b_term))
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001989 emsg(_(e_job_still_running_add_bang_to_end_the_job));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001990 else
1991#endif
Bram Moolenaare29a27f2021-07-20 21:07:36 +02001992 emsg(_(e_no_write_since_last_change_add_bang_to_override));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001993}
1994
1995 void
Bram Moolenaar7a760922018-02-19 23:10:02 +01001996no_write_message_nobang(buf_T *buf UNUSED)
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001997{
1998#ifdef FEAT_TERMINAL
Bram Moolenaar7a760922018-02-19 23:10:02 +01001999 if (term_job_running(buf->b_term))
Bram Moolenaarf1474d82021-12-31 19:59:55 +00002000 emsg(_(e_job_still_running));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002001 else
2002#endif
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002003 emsg(_(e_no_write_since_last_change));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02002004}
2005
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006/*
2007 * functions for dealing with the buffer list
2008 */
2009
2010/*
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002011 * Return TRUE if the current buffer is empty, unnamed, unmodified and used in
2012 * only one window. That means it can be re-used.
2013 */
2014 int
2015curbuf_reusable(void)
2016{
2017 return (curbuf != NULL
2018 && curbuf->b_ffname == NULL
2019 && curbuf->b_nwindows <= 1
2020 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY())
Bram Moolenaar39803d82019-04-07 12:04:51 +02002021 && !bt_quickfix(curbuf)
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002022 && !curbufIsChanged());
2023}
2024
2025/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 * Add a file name to the buffer list. Return a pointer to the buffer.
2027 * If the same file name already exists return a pointer to that buffer.
2028 * If it does not exist, or if fname == NULL, a new entry is created.
2029 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
2030 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
2031 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02002032 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02002033 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
2034 * if the buffer already exists.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002035 * If (flags & BLN_REUSE) is TRUE, may use buffer number from "buf_reuse".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 * This is the ONLY way to create a new buffer.
2037 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002039buflist_new(
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002040 char_u *ffname_arg, // full path of fname or relative
2041 char_u *sfname_arg, // short fname or NULL
2042 linenr_T lnum, // preferred cursor line
2043 int flags) // BLN_ defines
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002045 char_u *ffname = ffname_arg;
2046 char_u *sfname = sfname_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 buf_T *buf;
2048#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002049 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050#endif
2051
Bram Moolenaar480778b2016-07-14 22:09:39 +02002052 if (top_file_num == 1)
2053 hash_init(&buf_hashtab);
2054
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002055 fname_expand(curbuf, &ffname, &sfname); // will allocate ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056
2057 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002058 * If the file name already exists in the list, update the entry.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 */
2060#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002061 // On Unix we can use inode numbers when the file exists. Works better
2062 // for hard links.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
2064 st.st_dev = (dev_T)-1;
2065#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02002066 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067#ifdef UNIX
2068 buflist_findname_stat(ffname, &st)
2069#else
2070 buflist_findname(ffname)
2071#endif
2072 ) != NULL)
2073 {
2074 vim_free(ffname);
2075 if (lnum != 0)
Bram Moolenaar89b693e2020-10-25 17:09:50 +01002076 buflist_setfpos(buf, (flags & BLN_NOCURWIN) ? NULL : curwin,
2077 lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02002078
2079 if ((flags & BLN_NOOPT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002080 // copy the options now, if 'cpo' doesn't have 's' and not done
2081 // already
Bram Moolenaar82404332016-07-10 17:00:38 +02002082 buf_copy_options(buf, 0);
2083
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 if ((flags & BLN_LISTED) && !buf->b_p_bl)
2085 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002086 bufref_T bufref;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002087
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 buf->b_p_bl = TRUE;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002089 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002091 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002092 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002093 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002094 return NULL;
2095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 }
2097 return buf;
2098 }
2099
2100 /*
2101 * If the current buffer has no name and no contents, use the current
2102 * buffer. Otherwise: Need to allocate a new buffer structure.
2103 *
2104 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00002105 * (A spell file buffer is allocated in spell.c, but that's not a normal
2106 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 */
2108 buf = NULL;
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002109 if ((flags & BLN_CURBUF) && curbuf_reusable())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 {
2111 buf = curbuf;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002112 // It's like this buffer is deleted. Watch out for autocommands that
2113 // change curbuf! If that happens, allocate a new buffer anyway.
Charlie Grovesfef44852022-04-19 16:24:12 +01002114 buf_freeall(buf, BFA_WIPE | BFA_DEL);
2115 if (buf != curbuf) // autocommands deleted the buffer!
2116 return NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002117#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002118 if (aborting()) // autocmds may abort script processing
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002119 {
2120 vim_free(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 return NULL;
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 }
2125 if (buf != curbuf || curbuf == NULL)
2126 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002127 buf = ALLOC_CLEAR_ONE(buf_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 if (buf == NULL)
2129 {
2130 vim_free(ffname);
2131 return NULL;
2132 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02002133#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002134 // init b: variables
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01002135 buf->b_vars = dict_alloc_id(aid_newbuf_bvars);
Bram Moolenaar429fa852013-04-15 12:27:36 +02002136 if (buf->b_vars == NULL)
2137 {
2138 vim_free(ffname);
2139 vim_free(buf);
2140 return NULL;
2141 }
2142 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
2143#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01002144 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 }
2146
2147 if (ffname != NULL)
2148 {
2149 buf->b_ffname = ffname;
2150 buf->b_sfname = vim_strsave(sfname);
2151 }
2152
2153 clear_wininfo(buf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002154 buf->b_wininfo = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155
2156 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
2157 || buf->b_wininfo == NULL)
2158 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002159 if (buf->b_sfname != buf->b_ffname)
2160 VIM_CLEAR(buf->b_sfname);
2161 else
2162 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01002163 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 if (buf != curbuf)
2165 free_buffer(buf);
2166 return NULL;
2167 }
2168
2169 if (buf == curbuf)
2170 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002171 free_buffer_stuff(buf, FALSE); // delete local variables et al.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002172
Bram Moolenaarc667da52019-11-30 20:52:27 +01002173 // Init the options.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002174 buf->b_p_initialized = FALSE;
2175 buf_copy_options(buf, BCO_ENTER);
2176
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177#ifdef FEAT_KEYMAP
Bram Moolenaarc667da52019-11-30 20:52:27 +01002178 // need to reload lmaps and set b:keymap_name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 curbuf->b_kmap_state |= KEYMAP_INIT;
2180#endif
2181 }
2182 else
2183 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002184 // put the new buffer at the end of the buffer list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 buf->b_next = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002186 if (firstbuf == NULL) // buffer list is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 {
2188 buf->b_prev = NULL;
2189 firstbuf = buf;
2190 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002191 else // append new buffer at end of list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 {
2193 lastbuf->b_next = buf;
2194 buf->b_prev = lastbuf;
2195 }
2196 lastbuf = buf;
2197
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002198 if ((flags & BLN_REUSE) && buf_reuse.ga_len > 0)
2199 {
2200 // Recycle a previously used buffer number. Used for buffers which
2201 // are normally hidden, e.g. in a popup window. Avoids that the
2202 // buffer number grows rapidly.
2203 --buf_reuse.ga_len;
2204 buf->b_fnum = ((int *)buf_reuse.ga_data)[buf_reuse.ga_len];
Bram Moolenaar99ebf222019-12-10 23:44:48 +01002205
2206 // Move buffer to the right place in the buffer list.
2207 while (buf->b_prev != NULL && buf->b_fnum < buf->b_prev->b_fnum)
2208 {
2209 buf_T *prev = buf->b_prev;
2210
2211 prev->b_next = buf->b_next;
2212 if (prev->b_next != NULL)
2213 prev->b_next->b_prev = prev;
2214 buf->b_next = prev;
2215 buf->b_prev = prev->b_prev;
2216 if (buf->b_prev != NULL)
2217 buf->b_prev->b_next = buf;
2218 prev->b_prev = buf;
2219 if (lastbuf == buf)
2220 lastbuf = prev;
2221 if (firstbuf == prev)
2222 firstbuf = buf;
2223 }
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002224 }
2225 else
2226 buf->b_fnum = top_file_num++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002227 if (top_file_num < 0) // wrap around (may cause duplicates)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002229 emsg(_("W14: Warning: List of file names overflow"));
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002230 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 {
2232 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002233 ui_delay(3001L, TRUE); // make sure it is noticed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 }
2235 top_file_num = 1;
2236 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002237 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002239 // Always copy the options from the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 buf_copy_options(buf, BCO_ALWAYS);
2241 }
2242
2243 buf->b_wininfo->wi_fpos.lnum = lnum;
2244 buf->b_wininfo->wi_win = curwin;
2245
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002246#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002247 hash_init(&buf->b_s.b_keywtab);
2248 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249#endif
2250
2251 buf->b_fname = buf->b_sfname;
2252#ifdef UNIX
2253 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002254 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 else
2256 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002257 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 buf->b_dev = st.st_dev;
2259 buf->b_ino = st.st_ino;
2260 }
2261#endif
2262 buf->b_u_synced = TRUE;
2263 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002264 if (flags & BLN_DUMMY)
2265 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 buf_clear_file(buf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002267 clrallmarks(buf); // clear marks
2268 fmarks_check_names(buf); // check file marks for this file
2269 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; // init 'buflisted'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 if (!(flags & BLN_DUMMY))
2271 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002272 bufref_T bufref;
2273
Bram Moolenaarc667da52019-11-30 20:52:27 +01002274 // Tricky: these autocommands may change the buffer list. They could
2275 // also split the window with re-using the one empty buffer. This may
2276 // result in unexpectedly losing the empty buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002277 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002278 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002279 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002280 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002282 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002283 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002284 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002285 return NULL;
2286 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002287#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002288 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292
2293 return buf;
2294}
2295
2296/*
2297 * Free the memory for the options of a buffer.
2298 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2299 * 'fileencoding'.
2300 */
2301 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002302free_buf_options(
2303 buf_T *buf,
2304 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305{
2306 if (free_p_ff)
2307 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 clear_string_option(&buf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 clear_string_option(&buf->b_p_bh);
2311 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 }
2313#ifdef FEAT_FIND_ID
2314 clear_string_option(&buf->b_p_def);
2315 clear_string_option(&buf->b_p_inc);
2316# ifdef FEAT_EVAL
2317 clear_string_option(&buf->b_p_inex);
2318# endif
2319#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002320#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 clear_string_option(&buf->b_p_inde);
2322 clear_string_option(&buf->b_p_indk);
2323#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002324#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2325 clear_string_option(&buf->b_p_bexpr);
2326#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002327#if defined(FEAT_CRYPT)
2328 clear_string_option(&buf->b_p_cm);
2329#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002330 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002331#if defined(FEAT_EVAL)
2332 clear_string_option(&buf->b_p_fex);
2333#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334#ifdef FEAT_CRYPT
Bram Moolenaar131530a2021-07-29 20:37:49 +02002335# ifdef FEAT_SODIUM
K.Takata1a8825d2022-01-19 13:32:57 +00002336 if ((buf->b_p_key != NULL) && (*buf->b_p_key != NUL) &&
2337 (crypt_get_method_nr(buf) == CRYPT_M_SOD))
2338 crypt_sodium_munlock(buf->b_p_key, STRLEN(buf->b_p_key));
Bram Moolenaar131530a2021-07-29 20:37:49 +02002339# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 clear_string_option(&buf->b_p_key);
2341#endif
2342 clear_string_option(&buf->b_p_kp);
2343 clear_string_option(&buf->b_p_mps);
2344 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002345 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 clear_string_option(&buf->b_p_isk);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002347#ifdef FEAT_VARTABS
2348 clear_string_option(&buf->b_p_vsts);
Bram Moolenaarbdace832019-03-02 10:13:42 +01002349 vim_free(buf->b_p_vsts_nopaste);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002350 buf->b_p_vsts_nopaste = NULL;
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00002351 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002352 clear_string_option(&buf->b_p_vts);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002353 VIM_CLEAR(buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355#ifdef FEAT_KEYMAP
2356 clear_string_option(&buf->b_p_keymap);
Bram Moolenaar50138322018-01-28 17:05:16 +01002357 keymap_clear(&buf->b_kmap_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 ga_clear(&buf->b_kmap_ga);
2359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 clear_string_option(&buf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361#ifdef FEAT_FOLDING
2362 clear_string_option(&buf->b_p_cms);
2363#endif
2364 clear_string_option(&buf->b_p_nf);
2365#ifdef FEAT_SYN_HL
2366 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002367 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002368#endif
2369#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002370 clear_string_option(&buf->b_s.b_p_spc);
2371 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002372 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002373 buf->b_s.b_cap_prog = NULL;
2374 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02002375 clear_string_option(&buf->b_s.b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 clear_string_option(&buf->b_p_sua);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 clear_string_option(&buf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 clear_string_option(&buf->b_p_cink);
2380 clear_string_option(&buf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +01002381 clear_string_option(&buf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 clear_string_option(&buf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 clear_string_option(&buf->b_p_cpt);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002384#ifdef FEAT_COMPL_FUNC
2385 clear_string_option(&buf->b_p_cfu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002386 free_callback(&buf->b_cfu_cb);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002387 clear_string_option(&buf->b_p_ofu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002388 free_callback(&buf->b_ofu_cb);
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +01002389 clear_string_option(&buf->b_p_tsrfu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002390 free_callback(&buf->b_tsrfu_cb);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002391#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392#ifdef FEAT_QUICKFIX
2393 clear_string_option(&buf->b_p_gp);
2394 clear_string_option(&buf->b_p_mp);
2395 clear_string_option(&buf->b_p_efm);
2396#endif
2397 clear_string_option(&buf->b_p_ep);
2398 clear_string_option(&buf->b_p_path);
2399 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002400 clear_string_option(&buf->b_p_tc);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002401#ifdef FEAT_EVAL
2402 clear_string_option(&buf->b_p_tfu);
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +00002403 free_callback(&buf->b_tfu_cb);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 clear_string_option(&buf->b_p_dict);
2406 clear_string_option(&buf->b_p_tsr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002407 clear_string_option(&buf->b_p_qe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002409 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002410 clear_string_option(&buf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002411 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002412 clear_string_option(&buf->b_p_menc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413}
2414
2415/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002416 * Get alternate file "n".
2417 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2418 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 * if (options & GETF_SETMARK) call setpcmark()
2420 * if (options & GETF_ALT) we are jumping to an alternate file.
2421 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2422 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002423 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 */
2425 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002426buflist_getfile(
2427 int n,
2428 linenr_T lnum,
2429 int options,
2430 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431{
2432 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 pos_T *fpos;
2435 colnr_T col;
2436
2437 buf = buflist_findnr(n);
2438 if (buf == NULL)
2439 {
2440 if ((options & GETF_ALT) && n == 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02002441 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 else
Bram Moolenaare1242042021-12-16 20:56:57 +00002443 semsg(_(e_buffer_nr_not_found), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 return FAIL;
2445 }
2446
Bram Moolenaarc667da52019-11-30 20:52:27 +01002447 // if alternate file is the current buffer, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 if (buf == curbuf)
2449 return OK;
2450
Bram Moolenaar71223e22022-05-30 15:23:09 +01002451 if (text_or_buf_locked())
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002452 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453
Bram Moolenaarc667da52019-11-30 20:52:27 +01002454 // altfpos may be changed by getfile(), get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 if (lnum == 0)
2456 {
2457 fpos = buflist_findfpos(buf);
2458 lnum = fpos->lnum;
2459 col = fpos->col;
2460 }
2461 else
2462 col = 0;
2463
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 if (options & GETF_SWITCH)
2465 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002466 // If 'switchbuf' contains "useopen": jump to first window containing
2467 // "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00002468 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002470
Bram Moolenaarc667da52019-11-30 20:52:27 +01002471 // If 'switchbuf' contains "usetab": jump to first window in any tab
2472 // page containing "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00002473 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002474 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002475
Bram Moolenaarc667da52019-11-30 20:52:27 +01002476 // If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2477 // current buffer isn't empty: open new tab or window
Bram Moolenaara594d772015-06-19 14:41:49 +02002478 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002479 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002481 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002482 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002483 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2484 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002486 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 }
2488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489
2490 ++RedrawingDisabled;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002491 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2492 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 {
2494 --RedrawingDisabled;
2495
Bram Moolenaarc667da52019-11-30 20:52:27 +01002496 // cursor is at to BOL and w_cursor.lnum is checked due to getfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 if (!p_sol && col != 0)
2498 {
2499 curwin->w_cursor.col = col;
2500 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 curwin->w_set_curswant = TRUE;
2503 }
2504 return OK;
2505 }
2506 --RedrawingDisabled;
2507 return FAIL;
2508}
2509
2510/*
2511 * go to the last know line number for the current buffer
2512 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002514buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515{
2516 pos_T *fpos;
2517
2518 fpos = buflist_findfpos(curbuf);
2519
2520 curwin->w_cursor.lnum = fpos->lnum;
2521 check_cursor_lnum();
2522
2523 if (p_sol)
2524 curwin->w_cursor.col = 0;
2525 else
2526 {
2527 curwin->w_cursor.col = fpos->col;
2528 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 curwin->w_set_curswant = TRUE;
2531 }
2532}
2533
Bram Moolenaar81695252004-12-29 20:58:21 +00002534#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2535/*
2536 * Find file in buffer list by name (it has to be for the current window).
2537 * Returns NULL if not found.
2538 */
2539 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002540buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002541{
2542 char_u *ffname;
2543 buf_T *buf = NULL;
2544
Bram Moolenaarc667da52019-11-30 20:52:27 +01002545 // First make the name into a full path name
Bram Moolenaar81695252004-12-29 20:58:21 +00002546 ffname = FullName_save(fname,
2547#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002548 TRUE // force expansion, get rid of symbolic links
Bram Moolenaar81695252004-12-29 20:58:21 +00002549#else
2550 FALSE
2551#endif
2552 );
2553 if (ffname != NULL)
2554 {
2555 buf = buflist_findname(ffname);
2556 vim_free(ffname);
2557 }
2558 return buf;
2559}
2560#endif
2561
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562/*
2563 * Find file in buffer list by name (it has to be for the current window).
2564 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002565 * Skips dummy buffers.
2566 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 */
2568 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002569buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570{
2571#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002572 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573
2574 if (mch_stat((char *)ffname, &st) < 0)
2575 st.st_dev = (dev_T)-1;
2576 return buflist_findname_stat(ffname, &st);
2577}
2578
2579/*
2580 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2581 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002582 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 */
2584 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002585buflist_findname_stat(
2586 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002587 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588{
2589#endif
2590 buf_T *buf;
2591
Bram Moolenaarc667da52019-11-30 20:52:27 +01002592 // Start at the last buffer, expect to find a match sooner.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002593 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaar81695252004-12-29 20:58:21 +00002594 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595#ifdef UNIX
2596 , stp
2597#endif
2598 ))
2599 return buf;
2600 return NULL;
2601}
2602
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603/*
2604 * Find file in buffer list by a regexp pattern.
2605 * Return fnum of the found buffer.
2606 * Return < 0 for error.
2607 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002609buflist_findpat(
2610 char_u *pattern,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002611 char_u *pattern_end, // pointer to first char after pattern
2612 int unlisted, // find unlisted buffers
2613 int diffmode UNUSED, // find diff-mode buffers only
2614 int curtab_only) // find buffers in current tab only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615{
2616 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 int match = -1;
2618 int find_listed;
2619 char_u *pat;
2620 char_u *patend;
2621 int attempt;
2622 char_u *p;
2623 int toggledollar;
2624
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002625 // "%" is current file, "%%" or "#" is alternate file
2626 if ((pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2627 || (in_vim9script() && pattern_end == pattern + 2
2628 && pattern[0] == '%' && pattern[1] == '%'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 {
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002630 if (*pattern == '#' || pattern_end == pattern + 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 match = curwin->w_alt_fnum;
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002632 else
2633 match = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634#ifdef FEAT_DIFF
2635 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2636 match = -1;
2637#endif
2638 }
2639
2640 /*
2641 * Try four ways of matching a listed buffer:
2642 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002643 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 * attempt == 2: with '$' at end (only match at end)
2645 * attempt == 3: with '^' at start and '$' at end (only full match)
2646 * Repeat this for finding an unlisted buffer if there was no matching
2647 * listed buffer.
2648 */
2649 else
2650 {
2651 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2652 if (pat == NULL)
2653 return -1;
2654 patend = pat + STRLEN(pat) - 1;
2655 toggledollar = (patend > pat && *patend == '$');
2656
Bram Moolenaarc667da52019-11-30 20:52:27 +01002657 // First try finding a listed buffer. If not found and "unlisted"
2658 // is TRUE, try finding an unlisted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 find_listed = TRUE;
2660 for (;;)
2661 {
2662 for (attempt = 0; attempt <= 3; ++attempt)
2663 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002664 regmatch_T regmatch;
2665
Bram Moolenaarc667da52019-11-30 20:52:27 +01002666 // may add '^' and '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 if (toggledollar)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002668 *patend = (attempt < 2) ? NUL : '$'; // add/remove '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 p = pat;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002670 if (*p == '^' && !(attempt & 1)) // add/remove '^'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 ++p;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01002672 regmatch.regprog = vim_regcomp(p, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002674 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002675 {
2676 if (regmatch.regprog == NULL)
2677 {
2678 // invalid pattern, possibly after switching engine
2679 vim_free(pat);
2680 return -1;
2681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 if (buf->b_p_bl == find_listed
2683#ifdef FEAT_DIFF
2684 && (!diffmode || diff_mode_buf(buf))
2685#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002686 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002688 if (curtab_only)
2689 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002690 // Ignore the match if the buffer is not open in
2691 // the current tab.
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002692 win_T *wp;
2693
Bram Moolenaar29323592016-07-24 22:04:11 +02002694 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002695 if (wp->w_buffer == buf)
2696 break;
2697 if (wp == NULL)
2698 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002699 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002700 if (match >= 0) // already found a match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 {
2702 match = -2;
2703 break;
2704 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002705 match = buf->b_fnum; // remember first match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 }
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002709 vim_regfree(regmatch.regprog);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002710 if (match >= 0) // found one match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 break;
2712 }
2713
Bram Moolenaarc667da52019-11-30 20:52:27 +01002714 // Only search for unlisted buffers if there was no match with
2715 // a listed buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 if (!unlisted || !find_listed || match != -1)
2717 break;
2718 find_listed = FALSE;
2719 }
2720
2721 vim_free(pat);
2722 }
2723
2724 if (match == -2)
Bram Moolenaare1242042021-12-16 20:56:57 +00002725 semsg(_(e_more_than_one_match_for_str), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 else if (match < 0)
Bram Moolenaare1242042021-12-16 20:56:57 +00002727 semsg(_(e_no_matching_buffer_for_str), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 return match;
2729}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730
Bram Moolenaar52410572019-10-27 05:12:45 +01002731#ifdef FEAT_VIMINFO
2732typedef struct {
2733 buf_T *buf;
2734 char_u *match;
2735} bufmatch_T;
2736#endif
2737
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738/*
2739 * Find all buffer names that match.
2740 * For command line expansion of ":buf" and ":sbuf".
2741 * Return OK if matches found, FAIL otherwise.
2742 */
2743 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002744ExpandBufnames(
2745 char_u *pat,
2746 int *num_file,
2747 char_u ***file,
2748 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749{
2750 int count = 0;
2751 buf_T *buf;
2752 int round;
2753 char_u *p;
2754 int attempt;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002755 char_u *patc = NULL;
Bram Moolenaar52410572019-10-27 05:12:45 +01002756#ifdef FEAT_VIMINFO
2757 bufmatch_T *matches = NULL;
2758#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002759 int fuzzy;
2760 fuzmatch_str_T *fuzmatch = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761
Bram Moolenaarc667da52019-11-30 20:52:27 +01002762 *num_file = 0; // return values in case of FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 *file = NULL;
2764
Bram Moolenaarefcc3292019-12-30 21:59:03 +01002765#ifdef FEAT_DIFF
2766 if ((options & BUF_DIFF_FILTER) && !curwin->w_p_diff)
2767 return FAIL;
2768#endif
2769
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002770 fuzzy = cmdline_fuzzy_complete(pat);
2771
2772 // Make a copy of "pat" and change "^" to "\(^\|[\/]\)" (if doing regular
2773 // expression matching)
2774 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002776 if (*pat == '^')
2777 {
2778 patc = alloc(STRLEN(pat) + 11);
2779 if (patc == NULL)
2780 return FAIL;
2781 STRCPY(patc, "\\(^\\|[\\/]\\)");
2782 STRCPY(patc + 11, pat + 1);
2783 }
2784 else
2785 patc = pat;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002786 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00002787
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002788 // attempt == 0: try match with '\<', match at start of word
2789 // attempt == 1: try match without '\<', match anywhere
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002790 for (attempt = 0; attempt <= (fuzzy ? 0 : 1); ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002791 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002792 regmatch_T regmatch;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002793 int score = 0;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002794
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002795 if (!fuzzy)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002796 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002797 if (attempt > 0 && patc == pat)
2798 break; // there was no anchor, no need to try again
2799 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002802 // round == 1: Count the matches.
2803 // round == 2: Build the array to keep the matches.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 for (round = 1; round <= 2; ++round)
2805 {
2806 count = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002807 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002809 if (!buf->b_p_bl) // skip unlisted buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 continue;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002811#ifdef FEAT_DIFF
2812 if (options & BUF_DIFF_FILTER)
2813 // Skip buffers not suitable for
2814 // :diffget or :diffput completion.
Bram Moolenaarefcc3292019-12-30 21:59:03 +01002815 if (buf == curbuf || !diff_mode_buf(buf))
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002816 continue;
2817#endif
2818
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002819 if (!fuzzy)
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002820 {
2821 if (regmatch.regprog == NULL)
2822 {
2823 // invalid pattern, possibly after recompiling
2824 if (patc != pat)
2825 vim_free(patc);
2826 return FAIL;
2827 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002828 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002829 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002830 else
2831 {
2832 p = NULL;
2833 // first try matching with the short file name
2834 if ((score = fuzzy_match_str(buf->b_sfname, pat)) != 0)
2835 p = buf->b_sfname;
2836 if (p == NULL)
2837 {
2838 // next try matching with the full path file name
2839 if ((score = fuzzy_match_str(buf->b_ffname, pat)) != 0)
2840 p = buf->b_ffname;
2841 }
2842 }
2843
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002844 if (p == NULL)
2845 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002846
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002847 if (round == 1)
2848 {
2849 ++count;
2850 continue;
2851 }
2852
2853 if (options & WILD_HOME_REPLACE)
2854 p = home_replace_save(buf, p);
2855 else
2856 p = vim_strsave(p);
2857
2858 if (!fuzzy)
2859 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002860#ifdef FEAT_VIMINFO
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002861 if (matches != NULL)
2862 {
2863 matches[count].buf = buf;
2864 matches[count].match = p;
2865 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002867 else
2868#endif
2869 (*file)[count++] = p;
2870 }
2871 else
2872 {
2873 fuzmatch[count].idx = count;
2874 fuzmatch[count].str = p;
2875 fuzmatch[count].score = score;
2876 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 }
2878 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002879 if (count == 0) // no match found, break here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 break;
2881 if (round == 1)
2882 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002883 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002885 *file = ALLOC_MULT(char_u *, count);
2886 if (*file == NULL)
2887 {
2888 vim_regfree(regmatch.regprog);
2889 if (patc != pat)
2890 vim_free(patc);
2891 return FAIL;
2892 }
Bram Moolenaar52410572019-10-27 05:12:45 +01002893#ifdef FEAT_VIMINFO
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002894 if (options & WILD_BUFLASTUSED)
2895 matches = ALLOC_MULT(bufmatch_T, count);
Bram Moolenaar52410572019-10-27 05:12:45 +01002896#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002897 }
2898 else
2899 {
2900 fuzmatch = ALLOC_MULT(fuzmatch_str_T, count);
2901 if (fuzmatch == NULL)
2902 {
2903 *num_file = 0;
2904 *file = NULL;
2905 return FAIL;
2906 }
2907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 }
2909 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002910
2911 if (!fuzzy)
2912 {
2913 vim_regfree(regmatch.regprog);
2914 if (count) // match(es) found, break here
2915 break;
2916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 }
2918
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002919 if (!fuzzy && patc != pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002920 vim_free(patc);
2921
Bram Moolenaar52410572019-10-27 05:12:45 +01002922#ifdef FEAT_VIMINFO
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002923 if (!fuzzy)
Bram Moolenaar52410572019-10-27 05:12:45 +01002924 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002925 if (matches != NULL)
Bram Moolenaar52410572019-10-27 05:12:45 +01002926 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002927 int i;
2928 if (count > 1)
2929 qsort(matches, count, sizeof(bufmatch_T), buf_compare);
2930 // if the current buffer is first in the list, place it at the end
2931 if (matches[0].buf == curbuf)
2932 {
2933 for (i = 1; i < count; i++)
2934 (*file)[i-1] = matches[i].match;
2935 (*file)[count-1] = matches[0].match;
2936 }
2937 else
2938 {
2939 for (i = 0; i < count; i++)
2940 (*file)[i] = matches[i].match;
2941 }
2942 vim_free(matches);
Bram Moolenaar52410572019-10-27 05:12:45 +01002943 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002944 }
2945 else
2946 {
2947 if (fuzzymatches_to_strmatches(fuzmatch, file, count, FALSE) == FAIL)
2948 return FAIL;
Bram Moolenaar52410572019-10-27 05:12:45 +01002949 }
2950#endif
2951
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 *num_file = count;
2953 return (count == 0 ? FAIL : OK);
2954}
2955
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956/*
2957 * Check for a match on the file name for buffer "buf" with regprog "prog".
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002958 * Note that rmp->regprog may become NULL when switching regexp engine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 */
2960 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002961buflist_match(
2962 regmatch_T *rmp,
2963 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002964 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965{
2966 char_u *match;
2967
Bram Moolenaarc667da52019-11-30 20:52:27 +01002968 // First try the short file name, then the long file name.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002969 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaara59f2df2022-05-11 11:42:28 +01002970 if (match == NULL && rmp->regprog != NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002971 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972
2973 return match;
2974}
2975
2976/*
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002977 * Try matching the regexp in "rmp->regprog" with file name "name".
2978 * Note that rmp->regprog may become NULL when switching regexp engine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 * Return "name" when there is a match, NULL when not.
2980 */
2981 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002982fname_match(
2983 regmatch_T *rmp,
2984 char_u *name,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002985 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986{
2987 char_u *match = NULL;
2988 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002990 // extra check for valid arguments
2991 if (name != NULL && rmp->regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002993 // Ignore case when 'fileignorecase' or the argument is set.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002994 rmp->rm_ic = p_fic || ignore_case;
2995 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 match = name;
Bram Moolenaar8e4b76d2022-05-07 11:28:06 +01002997 else if (rmp->regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002999 // Replace $(HOME) with '~' and try matching again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01003001 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 match = name;
3003 vim_free(p);
3004 }
3005 }
3006
3007 return match;
3008}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009
3010/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02003011 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 */
3013 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003014buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015{
Bram Moolenaar480778b2016-07-14 22:09:39 +02003016 char_u key[VIM_SIZEOF_INT * 2 + 1];
3017 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018
3019 if (nr == 0)
3020 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02003021 sprintf((char *)key, "%x", nr);
3022 hi = hash_find(&buf_hashtab, key);
3023
3024 if (!HASHITEM_EMPTY(hi))
3025 return (buf_T *)(hi->hi_key
3026 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 return NULL;
3028}
3029
3030/*
3031 * Get name of file 'n' in the buffer list.
3032 * When the file has no name an empty string is returned.
3033 * home_replace() is used to shorten the file name (used for marks).
3034 * Returns a pointer to allocated memory, of NULL when failed.
3035 */
3036 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003037buflist_nr2name(
3038 int n,
3039 int fullname,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003040 int helptail) // for help buffers return tail only
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041{
3042 buf_T *buf;
3043
3044 buf = buflist_findnr(n);
3045 if (buf == NULL)
3046 return NULL;
3047 return home_replace_save(helptail ? buf : NULL,
3048 fullname ? buf->b_ffname : buf->b_fname);
3049}
3050
3051/*
3052 * Set the "lnum" and "col" for the buffer "buf" and the current window.
3053 * When "copy_options" is TRUE save the local window option values.
3054 * When "lnum" is 0 only do the options.
3055 */
Bram Moolenaardefa0672019-07-21 19:25:37 +02003056 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003057buflist_setfpos(
3058 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003059 win_T *win, // may be NULL when using :badd
Bram Moolenaar7454a062016-01-30 15:14:10 +01003060 linenr_T lnum,
3061 colnr_T col,
3062 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063{
3064 wininfo_T *wip;
3065
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003066 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 if (wip->wi_win == win)
3068 break;
3069 if (wip == NULL)
3070 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003071 // allocate a new entry
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003072 wip = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 if (wip == NULL)
3074 return;
3075 wip->wi_win = win;
Bram Moolenaarc667da52019-11-30 20:52:27 +01003076 if (lnum == 0) // set lnum even when it's 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 lnum = 1;
3078 }
3079 else
3080 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003081 // remove the entry from the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 if (wip->wi_prev)
3083 wip->wi_prev->wi_next = wip->wi_next;
3084 else
3085 buf->b_wininfo = wip->wi_next;
3086 if (wip->wi_next)
3087 wip->wi_next->wi_prev = wip->wi_prev;
3088 if (copy_options && wip->wi_optset)
3089 {
3090 clear_winopt(&wip->wi_opt);
3091#ifdef FEAT_FOLDING
3092 deleteFoldRecurse(&wip->wi_folds);
3093#endif
3094 }
3095 }
3096 if (lnum != 0)
3097 {
3098 wip->wi_fpos.lnum = lnum;
3099 wip->wi_fpos.col = col;
3100 }
LemonBoydb0ea7f2022-04-10 17:59:26 +01003101 if (win != NULL)
3102 wip->wi_changelistidx = win->w_changelistidx;
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003103 if (copy_options && win != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003105 // Save the window-specific option values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
3107#ifdef FEAT_FOLDING
3108 wip->wi_fold_manual = win->w_fold_manual;
3109 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
3110#endif
3111 wip->wi_optset = TRUE;
3112 }
3113
Bram Moolenaarc667da52019-11-30 20:52:27 +01003114 // insert the entry in front of the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 wip->wi_next = buf->b_wininfo;
3116 buf->b_wininfo = wip;
3117 wip->wi_prev = NULL;
3118 if (wip->wi_next)
3119 wip->wi_next->wi_prev = wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120}
3121
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003122#ifdef FEAT_DIFF
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003123/*
3124 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
3125 * page. That's because a diff is local to a tab page.
3126 */
3127 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003128wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003129{
3130 win_T *wp;
3131
3132 if (wip->wi_opt.wo_diff)
3133 {
Bram Moolenaar29323592016-07-24 22:04:11 +02003134 FOR_ALL_WINDOWS(wp)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003135 // return FALSE when it's a window in the current tab page, thus
3136 // the buffer was in diff mode here
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003137 if (wip->wi_win == wp)
3138 return FALSE;
3139 return TRUE;
3140 }
3141 return FALSE;
3142}
3143#endif
3144
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145/*
3146 * Find info for the current window in buffer "buf".
3147 * If not found, return the info for the most recently used window.
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003148 * When "need_options" is TRUE skip entries where wi_optset is FALSE.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003149 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
3150 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 * Returns NULL when there isn't any info.
3152 */
3153 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003154find_wininfo(
3155 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003156 int need_options,
Bram Moolenaar7454a062016-01-30 15:14:10 +01003157 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158{
3159 wininfo_T *wip;
3160
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003161 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003162 if (wip->wi_win == curwin
3163#ifdef FEAT_DIFF
3164 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
3165#endif
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003166
3167 && (!need_options || wip->wi_optset))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003169
Bram Moolenaarc667da52019-11-30 20:52:27 +01003170 // If no wininfo for curwin, use the first in the list (that doesn't have
3171 // 'diff' set and is in another tab page).
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003172 // If "need_options" is TRUE skip entries that don't have options set,
3173 // unless the window is editing "buf", so we can copy from the window
3174 // itself.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003175 if (wip == NULL)
3176 {
3177#ifdef FEAT_DIFF
3178 if (skip_diff_buffer)
3179 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003180 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003181 if (!wininfo_other_tab_diff(wip)
3182 && (!need_options || wip->wi_optset
3183 || (wip->wi_win != NULL
3184 && wip->wi_win->w_buffer == buf)))
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003185 break;
3186 }
3187 else
3188#endif
3189 wip = buf->b_wininfo;
3190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 return wip;
3192}
3193
3194/*
3195 * Reset the local window options to the values last used in this window.
3196 * If the buffer wasn't used in this window before, use the values from
3197 * the most recently used window. If the values were never set, use the
3198 * global values for the window.
3199 */
3200 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003201get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202{
3203 wininfo_T *wip;
3204
3205 clear_winopt(&curwin->w_onebuf_opt);
3206#ifdef FEAT_FOLDING
3207 clearFolding(curwin);
3208#endif
3209
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003210 wip = find_wininfo(buf, TRUE, TRUE);
Bram Moolenaar25782a72018-05-13 18:05:33 +02003211 if (wip != NULL && wip->wi_win != NULL
3212 && wip->wi_win != curwin && wip->wi_win->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003214 // The buffer is currently displayed in the window: use the actual
3215 // option values instead of the saved (possibly outdated) values.
Bram Moolenaar25782a72018-05-13 18:05:33 +02003216 win_T *wp = wip->wi_win;
3217
3218 copy_winopt(&wp->w_onebuf_opt, &curwin->w_onebuf_opt);
3219#ifdef FEAT_FOLDING
3220 curwin->w_fold_manual = wp->w_fold_manual;
3221 curwin->w_foldinvalid = TRUE;
3222 cloneFoldGrowArray(&wp->w_folds, &curwin->w_folds);
3223#endif
3224 }
3225 else if (wip != NULL && wip->wi_optset)
3226 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003227 // the buffer was displayed in the current window earlier
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
3229#ifdef FEAT_FOLDING
3230 curwin->w_fold_manual = wip->wi_fold_manual;
3231 curwin->w_foldinvalid = TRUE;
3232 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
3233#endif
3234 }
3235 else
3236 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
LemonBoydb0ea7f2022-04-10 17:59:26 +01003237 if (wip != NULL)
3238 curwin->w_changelistidx = wip->wi_changelistidx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239
3240#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +01003241 // Set 'foldlevel' to 'foldlevelstart' if it's not negative.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 if (p_fdls >= 0)
3243 curwin->w_p_fdl = p_fdls;
3244#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02003245 after_copy_winopt(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246}
3247
3248/*
3249 * Find the position (lnum and col) for the buffer 'buf' for the current
3250 * window.
3251 * Returns a pointer to no_position if no position is found.
3252 */
3253 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003254buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255{
3256 wininfo_T *wip;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003257 static pos_T no_position = {1, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003259 wip = find_wininfo(buf, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 if (wip != NULL)
3261 return &(wip->wi_fpos);
3262 else
3263 return &no_position;
3264}
3265
3266/*
3267 * Find the lnum for the buffer 'buf' for the current window.
3268 */
3269 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01003270buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271{
3272 return buflist_findfpos(buf)->lnum;
3273}
3274
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02003276 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003279buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280{
Bram Moolenaar52410572019-10-27 05:12:45 +01003281 buf_T *buf = firstbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 int len;
3283 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003284 int ro_char;
3285 int changed_char;
Bram Moolenaar0751f512018-03-29 16:37:16 +02003286#ifdef FEAT_TERMINAL
3287 int job_running;
3288 int job_none_open;
3289#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290
Bram Moolenaar52410572019-10-27 05:12:45 +01003291#ifdef FEAT_VIMINFO
3292 garray_T buflist;
3293 buf_T **buflist_data = NULL, **p;
3294
3295 if (vim_strchr(eap->arg, 't'))
3296 {
3297 ga_init2(&buflist, sizeof(buf_T *), 50);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003298 FOR_ALL_BUFFERS(buf)
Bram Moolenaar52410572019-10-27 05:12:45 +01003299 {
3300 if (ga_grow(&buflist, 1) == OK)
3301 ((buf_T **)buflist.ga_data)[buflist.ga_len++] = buf;
3302 }
3303
3304 qsort(buflist.ga_data, (size_t)buflist.ga_len,
3305 sizeof(buf_T *), buf_compare);
3306
Bram Moolenaar3b991522019-11-06 23:26:20 +01003307 buflist_data = (buf_T **)buflist.ga_data;
3308 buf = *buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003309 }
Bram Moolenaar3b991522019-11-06 23:26:20 +01003310 p = buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003311
Bram Moolenaar3b991522019-11-06 23:26:20 +01003312 for (; buf != NULL && !got_int; buf = buflist_data != NULL
Bram Moolenaar52410572019-10-27 05:12:45 +01003313 ? (++p < buflist_data + buflist.ga_len ? *p : NULL)
3314 : buf->b_next)
3315#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
Bram Moolenaar52410572019-10-27 05:12:45 +01003317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 {
Bram Moolenaar0751f512018-03-29 16:37:16 +02003319#ifdef FEAT_TERMINAL
3320 job_running = term_job_running(buf->b_term);
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003321 job_none_open = term_none_open(buf->b_term);
Bram Moolenaar0751f512018-03-29 16:37:16 +02003322#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003323 // skip unlisted buffers, unless ! was used
Bram Moolenaard51cb702015-07-21 15:03:06 +02003324 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
3325 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
3326 || (vim_strchr(eap->arg, '+')
3327 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
3328 || (vim_strchr(eap->arg, 'a')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003329 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
Bram Moolenaard51cb702015-07-21 15:03:06 +02003330 || (vim_strchr(eap->arg, 'h')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003331 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3332#ifdef FEAT_TERMINAL
3333 || (vim_strchr(eap->arg, 'R')
3334 && (!job_running || (job_running && job_none_open)))
3335 || (vim_strchr(eap->arg, '?')
3336 && (!job_running || (job_running && !job_none_open)))
3337 || (vim_strchr(eap->arg, 'F')
3338 && (job_running || buf->b_term == NULL))
3339#endif
Bram Moolenaard51cb702015-07-21 15:03:06 +02003340 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3341 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3342 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3343 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3344 || (vim_strchr(eap->arg, '#')
3345 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003348 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 else
3350 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003351 if (message_filtered(NameBuff))
3352 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003354 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3355 : (bufIsChanged(buf) ? '+' : ' ');
3356#ifdef FEAT_TERMINAL
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003357 if (job_running)
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003358 {
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003359 if (job_none_open)
Bram Moolenaar4033c552017-09-16 20:54:51 +02003360 ro_char = '?';
3361 else
3362 ro_char = 'R';
Bram Moolenaarc667da52019-11-30 20:52:27 +01003363 changed_char = ' '; // bufIsChanged() returns TRUE to avoid
3364 // closing, but it's not actually changed.
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003365 }
3366 else if (buf->b_term != NULL)
3367 ro_char = 'F';
3368 else
3369#endif
3370 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3371
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003372 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00003373 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 buf->b_fnum,
3375 buf->b_p_bl ? ' ' : 'u',
3376 buf == curbuf ? '%' :
3377 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3378 buf->b_ml.ml_mfp == NULL ? ' ' :
3379 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003380 ro_char,
3381 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003382 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003383 if (len > IOSIZE - 20)
3384 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385
Bram Moolenaarc667da52019-11-30 20:52:27 +01003386 // put "line 999" in column 40 or after the file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 i = 40 - vim_strsize(IObuff);
3388 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 IObuff[len++] = ' ';
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003390 while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar52410572019-10-27 05:12:45 +01003391#ifdef FEAT_VIMINFO
3392 if (vim_strchr(eap->arg, 't') && buf->b_last_used)
3393 add_time(IObuff + len, (size_t)(IOSIZE - len), buf->b_last_used);
3394 else
3395#endif
3396 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3397 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003398 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 msg_outtrans(IObuff);
Bram Moolenaarc667da52019-11-30 20:52:27 +01003400 out_flush(); // output one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 ui_breakcheck();
3402 }
Bram Moolenaar52410572019-10-27 05:12:45 +01003403
3404#ifdef FEAT_VIMINFO
3405 if (buflist_data)
3406 ga_clear(&buflist);
3407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409
3410/*
3411 * Get file name and line number for file 'fnum'.
3412 * Used by DoOneCmd() for translating '%' and '#'.
3413 * Used by insert_reg() and cmdline_paste() for '#' register.
3414 * Return FAIL if not found, OK for success.
3415 */
3416 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003417buflist_name_nr(
3418 int fnum,
3419 char_u **fname,
3420 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421{
3422 buf_T *buf;
3423
3424 buf = buflist_findnr(fnum);
3425 if (buf == NULL || buf->b_fname == NULL)
3426 return FAIL;
3427
3428 *fname = buf->b_fname;
3429 *lnum = buflist_findlnum(buf);
3430
3431 return OK;
3432}
3433
3434/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003435 * Set the file name for "buf"' to "ffname_arg", short file name to
3436 * "sfname_arg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 * The file name with the full path is also remembered, for when :cd is used.
3438 * Returns FAIL for failure (file name already in use by other buffer)
3439 * OK otherwise.
3440 */
3441 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003442setfname(
3443 buf_T *buf,
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003444 char_u *ffname_arg,
3445 char_u *sfname_arg,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003446 int message) // give message when buffer already exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003448 char_u *ffname = ffname_arg;
3449 char_u *sfname = sfname_arg;
Bram Moolenaar81695252004-12-29 20:58:21 +00003450 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003452 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453#endif
3454
3455 if (ffname == NULL || *ffname == NUL)
3456 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003457 // Removing the name.
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003458 if (buf->b_sfname != buf->b_ffname)
3459 VIM_CLEAR(buf->b_sfname);
3460 else
3461 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003462 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463#ifdef UNIX
3464 st.st_dev = (dev_T)-1;
3465#endif
3466 }
3467 else
3468 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003469 fname_expand(buf, &ffname, &sfname); // will allocate ffname
3470 if (ffname == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 return FAIL;
3472
3473 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01003474 * If the file name is already used in another buffer:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 * - if the buffer is loaded, fail
3476 * - if the buffer is not loaded, delete it from the list
3477 */
3478#ifdef UNIX
3479 if (mch_stat((char *)ffname, &st) < 0)
3480 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003481#endif
3482 if (!(buf->b_flags & BF_DUMMY))
3483#ifdef UNIX
3484 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003486 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487#endif
3488 if (obuf != NULL && obuf != buf)
3489 {
Bram Moolenaard3710cf2021-10-04 23:13:13 +01003490 win_T *win;
3491 tabpage_T *tab;
3492 int in_use = FALSE;
3493
3494 // during startup a window may use a buffer that is not loaded yet
3495 FOR_ALL_TAB_WINDOWS(tab, win)
3496 if (win->w_buffer == obuf)
3497 in_use = TRUE;
3498
3499 // it's loaded or used in a window, fail
3500 if (obuf->b_ml.ml_mfp != NULL || in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 {
3502 if (message)
Bram Moolenaare1242042021-12-16 20:56:57 +00003503 emsg(_(e_buffer_with_this_name_already_exists));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 vim_free(ffname);
3505 return FAIL;
3506 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003507 // delete from the list
Bram Moolenaara6e8f882019-12-14 16:18:15 +01003508 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 }
3510 sfname = vim_strsave(sfname);
3511 if (ffname == NULL || sfname == NULL)
3512 {
3513 vim_free(sfname);
3514 vim_free(ffname);
3515 return FAIL;
3516 }
3517#ifdef USE_FNAME_CASE
Bram Moolenaarc667da52019-11-30 20:52:27 +01003518 fname_case(sfname, 0); // set correct case for short file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519#endif
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003520 if (buf->b_sfname != buf->b_ffname)
3521 vim_free(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 vim_free(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 buf->b_ffname = ffname;
3524 buf->b_sfname = sfname;
3525 }
3526 buf->b_fname = buf->b_sfname;
3527#ifdef UNIX
3528 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003529 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 else
3531 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003532 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 buf->b_dev = st.st_dev;
3534 buf->b_ino = st.st_ino;
3535 }
3536#endif
3537
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539
3540 buf_name_changed(buf);
3541 return OK;
3542}
3543
3544/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003545 * Crude way of changing the name of a buffer. Use with care!
3546 * The name should be relative to the current directory.
3547 */
3548 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003549buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003550{
3551 buf_T *buf;
3552
3553 buf = buflist_findnr(fnum);
3554 if (buf != NULL)
3555 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003556 if (buf->b_sfname != buf->b_ffname)
3557 vim_free(buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003558 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003559 buf->b_ffname = vim_strsave(name);
3560 buf->b_sfname = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01003561 // Allocate ffname and expand into full path. Also resolves .lnk
3562 // files on Win32.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003563 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003564 buf->b_fname = buf->b_sfname;
3565 }
3566}
3567
3568/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 * Take care of what needs to be done when the name of buffer "buf" has
3570 * changed.
3571 */
3572 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003573buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574{
3575 /*
3576 * If the file name changed, also change the name of the swapfile
3577 */
3578 if (buf->b_ml.ml_mfp != NULL)
3579 ml_setname(buf);
3580
Bram Moolenaar3ad69532021-11-19 17:01:08 +00003581#ifdef FEAT_TERMINAL
3582 if (buf->b_term != NULL)
3583 term_clear_status_text(buf->b_term);
3584#endif
3585
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 if (curwin->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003587 check_arg_idx(curwin); // check file name for arg list
Bram Moolenaarc667da52019-11-30 20:52:27 +01003588 maketitle(); // set window title
Bram Moolenaarc667da52019-11-30 20:52:27 +01003589 status_redraw_all(); // status lines need to be redrawn
3590 fmarks_check_names(buf); // check named file marks
3591 ml_timestamp(buf); // reset timestamp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592}
3593
3594/*
3595 * set alternate file name for current window
3596 *
3597 * Used by do_one_cmd(), do_write() and do_ecmd().
3598 * Return the buffer.
3599 */
3600 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003601setaltfname(
3602 char_u *ffname,
3603 char_u *sfname,
3604 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605{
3606 buf_T *buf;
3607
Bram Moolenaarc667da52019-11-30 20:52:27 +01003608 // Create a buffer. 'buflisted' is not set if it's a new buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaare1004402020-10-24 20:49:43 +02003610 if (buf != NULL && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 curwin->w_alt_fnum = buf->b_fnum;
3612 return buf;
3613}
3614
3615/*
3616 * Get alternate file name for current window.
3617 * Return NULL if there isn't any, and give error message if requested.
3618 */
3619 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003620getaltfname(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003621 int errmsg) // give error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622{
3623 char_u *fname;
3624 linenr_T dummy;
3625
3626 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3627 {
3628 if (errmsg)
Bram Moolenaar108010a2021-06-27 22:03:33 +02003629 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 return NULL;
3631 }
3632 return fname;
3633}
3634
3635/*
3636 * Add a file name to the buflist and return its number.
3637 * Uses same flags as buflist_new(), except BLN_DUMMY.
3638 *
3639 * used by qf_init(), main() and doarglist()
3640 */
3641 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003642buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643{
3644 buf_T *buf;
3645
3646 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3647 if (buf != NULL)
3648 return buf->b_fnum;
3649 return 0;
3650}
3651
3652#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3653/*
3654 * Adjust slashes in file names. Called after 'shellslash' was set.
3655 */
3656 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003657buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658{
3659 buf_T *bp;
3660
Bram Moolenaar29323592016-07-24 22:04:11 +02003661 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 {
3663 if (bp->b_ffname != NULL)
3664 slash_adjust(bp->b_ffname);
3665 if (bp->b_sfname != NULL)
3666 slash_adjust(bp->b_sfname);
3667 }
3668}
3669#endif
3670
3671/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003672 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 * Also save the local window option values.
3674 */
3675 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003676buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003678 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679}
3680
3681/*
3682 * Return TRUE if 'ffname' is not the same file as current file.
3683 * Fname must have a full path (expanded by mch_FullName()).
3684 */
3685 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003686otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687{
3688 return otherfile_buf(curbuf, ffname
3689#ifdef UNIX
3690 , NULL
3691#endif
3692 );
3693}
3694
3695 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003696otherfile_buf(
3697 buf_T *buf,
3698 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003700 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003702 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703{
Bram Moolenaarc667da52019-11-30 20:52:27 +01003704 // no name is different
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3706 return TRUE;
3707 if (fnamecmp(ffname, buf->b_ffname) == 0)
3708 return FALSE;
3709#ifdef UNIX
3710 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003711 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712
Bram Moolenaarc667da52019-11-30 20:52:27 +01003713 // If no stat_T given, get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 if (stp == NULL)
3715 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003716 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 st.st_dev = (dev_T)-1;
3718 stp = &st;
3719 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003720 // Use dev/ino to check if the files are the same, even when the names
3721 // are different (possible with links). Still need to compare the
3722 // name above, for when the file doesn't exist yet.
3723 // Problem: The dev/ino changes when a file is deleted (and created
3724 // again) and remains the same when renamed/moved. We don't want to
3725 // mch_stat() each buffer each time, that would be too slow. Get the
3726 // dev/ino again when they appear to match, but not when they appear
3727 // to be different: Could skip a buffer when it's actually the same
3728 // file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 if (buf_same_ino(buf, stp))
3730 {
3731 buf_setino(buf);
3732 if (buf_same_ino(buf, stp))
3733 return FALSE;
3734 }
3735 }
3736#endif
3737 return TRUE;
3738}
3739
3740#if defined(UNIX) || defined(PROTO)
3741/*
3742 * Set inode and device number for a buffer.
3743 * Must always be called when b_fname is changed!.
3744 */
3745 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003746buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003748 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749
3750 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3751 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003752 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 buf->b_dev = st.st_dev;
3754 buf->b_ino = st.st_ino;
3755 }
3756 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003757 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758}
3759
3760/*
3761 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3762 */
3763 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003764buf_same_ino(
3765 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003766 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003768 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 && stp->st_dev == buf->b_dev
3770 && stp->st_ino == buf->b_ino);
3771}
3772#endif
3773
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003774/*
3775 * Print info about the current buffer.
3776 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003778fileinfo(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003779 int fullname, // when non-zero print full path
Bram Moolenaar7454a062016-01-30 15:14:10 +01003780 int shorthelp,
3781 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782{
3783 char_u *name;
3784 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003785 char *p;
3786 char *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003787 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003789 buffer = alloc(IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 if (buffer == NULL)
3791 return;
3792
Bram Moolenaarc667da52019-11-30 20:52:27 +01003793 if (fullname > 1) // 2 CTRL-G: include buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003795 vim_snprintf(buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 p = buffer + STRLEN(buffer);
3797 }
3798 else
3799 p = buffer;
3800
3801 *p++ = '"';
3802 if (buf_spname(curbuf) != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003803 vim_strncpy((char_u *)p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 else
3805 {
3806 if (!fullname && curbuf->b_fname != NULL)
3807 name = curbuf->b_fname;
3808 else
3809 name = curbuf->b_ffname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003810 home_replace(shorthelp ? curbuf : NULL, name, (char_u *)p,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 (int)(IOSIZE - (p - buffer)), TRUE);
3812 }
3813
Bram Moolenaar32526b32019-01-19 17:43:09 +01003814 vim_snprintf_add(buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 curbufIsChanged() ? (shortmess(SHM_MOD)
3816 ? " [+]" : _(" [Modified]")) : " ",
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01003817 (curbuf->b_flags & BF_NOTEDITED) && !bt_dontwrite(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 ? _("[Not edited]") : "",
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01003819 (curbuf->b_flags & BF_NEW) && !bt_dontwrite(curbuf)
Bram Moolenaar722e5052020-06-12 22:31:00 +02003820 ? new_file_message() : "",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003822 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 : _("[readonly]")) : "",
3824 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3825 || curbuf->b_p_ro) ?
3826 " " : "");
Bram Moolenaarc667da52019-11-30 20:52:27 +01003827 // With 32 bit longs and more than 21,474,836 lines multiplying by 100
3828 // causes an overflow, thus for large numbers divide instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 if (curwin->w_cursor.lnum > 1000000L)
3830 n = (int)(((long)curwin->w_cursor.lnum) /
3831 ((long)curbuf->b_ml.ml_line_count / 100L));
3832 else
3833 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3834 (long)curbuf->b_ml.ml_line_count);
3835 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003836 vim_snprintf_add(buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837#ifdef FEAT_CMDL_INFO
3838 else if (p_ru)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003839 // Current line and column are already on the screen -- webb
Bram Moolenaar32526b32019-01-19 17:43:09 +01003840 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003841 NGETTEXT("%ld line --%d%%--", "%ld lines --%d%%--",
3842 curbuf->b_ml.ml_line_count),
3843 (long)curbuf->b_ml.ml_line_count, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844#endif
3845 else
3846 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003847 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 _("line %ld of %ld --%d%%-- col "),
3849 (long)curwin->w_cursor.lnum,
3850 (long)curbuf->b_ml.ml_line_count,
3851 n);
3852 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003853 len = STRLEN(buffer);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003854 col_print((char_u *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3856 }
3857
Bram Moolenaar32526b32019-01-19 17:43:09 +01003858 (void)append_arg_number(curwin, (char_u *)buffer, IOSIZE,
3859 !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860
3861 if (dont_truncate)
3862 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003863 // Temporarily set msg_scroll to avoid the message being truncated.
3864 // First call msg_start() to get the message in the right place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 msg_start();
3866 n = msg_scroll;
3867 msg_scroll = TRUE;
3868 msg(buffer);
3869 msg_scroll = n;
3870 }
3871 else
3872 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003873 p = msg_trunc_attr(buffer, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaarc667da52019-11-30 20:52:27 +01003875 // Need to repeat the message after redrawing when:
3876 // - When restart_edit is set (otherwise there will be a delay
3877 // before redrawing).
3878 // - When the screen was scrolled but there is no wait-return
3879 // prompt.
Bram Moolenaar32526b32019-01-19 17:43:09 +01003880 set_keep_msg((char_u *)p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 }
3882
3883 vim_free(buffer);
3884}
3885
3886 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003887col_print(
3888 char_u *buf,
3889 size_t buflen,
3890 int col,
3891 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892{
3893 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003894 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003896 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897}
3898
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899static char_u *lasttitle = NULL;
3900static char_u *lasticon = NULL;
3901
Bram Moolenaar84a93082018-06-16 22:58:15 +02003902/*
3903 * Put the file name in the title bar and icon of the window.
3904 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003906maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907{
3908 char_u *p;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003909 char_u *title_str = NULL;
3910 char_u *icon_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 int maxlen = 0;
3912 int len;
3913 int mustset;
3914 char_u buf[IOSIZE];
3915 int off;
3916
3917 if (!redrawing())
3918 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003919 // Postpone updating the title when 'lazyredraw' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 need_maketitle = TRUE;
3921 return;
3922 }
3923
3924 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003925 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003926 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927
3928 if (p_title)
3929 {
3930 if (p_titlelen > 0)
3931 {
3932 maxlen = p_titlelen * Columns / 100;
3933 if (maxlen < 10)
3934 maxlen = 10;
3935 }
3936
Bram Moolenaar84a93082018-06-16 22:58:15 +02003937 title_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 if (*p_titlestring != NUL)
3939 {
3940#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003941 if (stl_syntax & STL_IN_TITLE)
3942 {
3943 int use_sandbox = FALSE;
Bram Moolenaar53989552019-12-23 22:59:18 +01003944 int called_emsg_before = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003945
3946# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003947 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003948# endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003949 build_stl_str_hl(curwin, title_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003950 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003951 0, maxlen, NULL, NULL);
Bram Moolenaar53989552019-12-23 22:59:18 +01003952 if (called_emsg > called_emsg_before)
Bram Moolenaard3667a22006-03-16 21:35:52 +00003953 set_string_option_direct((char_u *)"titlestring", -1,
3954 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 else
3957#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003958 title_str = p_titlestring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 }
3960 else
3961 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003962 // format: "fname + (path) (1 of 2) - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963
Bram Moolenaar2c666692012-09-05 13:30:40 +02003964#define SPACE_FOR_FNAME (IOSIZE - 100)
3965#define SPACE_FOR_DIR (IOSIZE - 20)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003966#define SPACE_FOR_ARGNR (IOSIZE - 10) // at least room for " - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003968 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar21554412017-07-24 21:44:43 +02003969#ifdef FEAT_TERMINAL
3970 else if (curbuf->b_term != NULL)
3971 {
3972 vim_strncpy(buf, term_get_status_text(curbuf->b_term),
3973 SPACE_FOR_FNAME);
3974 }
3975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 else
3977 {
3978 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003979 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 }
3982
Bram Moolenaar21554412017-07-24 21:44:43 +02003983#ifdef FEAT_TERMINAL
3984 if (curbuf->b_term == NULL)
3985#endif
3986 switch (bufIsChanged(curbuf)
3987 + (curbuf->b_p_ro * 2)
3988 + (!curbuf->b_p_ma * 4))
3989 {
3990 case 1: STRCAT(buf, " +"); break;
3991 case 2: STRCAT(buf, " ="); break;
3992 case 3: STRCAT(buf, " =+"); break;
3993 case 4:
3994 case 6: STRCAT(buf, " -"); break;
3995 case 5:
3996 case 7: STRCAT(buf, " -+"); break;
3997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998
Bram Moolenaar21554412017-07-24 21:44:43 +02003999 if (curbuf->b_fname != NULL
4000#ifdef FEAT_TERMINAL
4001 && curbuf->b_term == NULL
4002#endif
4003 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004005 // Get path of file, replace home dir with ~
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 off = (int)STRLEN(buf);
4007 buf[off++] = ' ';
4008 buf[off++] = '(';
4009 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02004010 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc667da52019-11-30 20:52:27 +01004012 // avoid "c:/name" to be reduced to "c"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 if (isalpha(buf[off]) && buf[off + 1] == ':')
4014 off += 2;
4015#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01004016 // remove the file name
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004017 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 if (p == buf + off)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02004019 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004020 // must be a help buffer
Bram Moolenaar21554412017-07-24 21:44:43 +02004021 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02004022 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02004023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004026
Bram Moolenaarc667da52019-11-30 20:52:27 +01004027 // Translate unprintable chars and concatenate. Keep some
4028 // room for the server name. When there is no room (very long
4029 // file name) use (...).
Bram Moolenaar2c666692012-09-05 13:30:40 +02004030 if (off < SPACE_FOR_DIR)
4031 {
4032 p = transstr(buf + off);
4033 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
4034 vim_free(p);
4035 }
4036 else
4037 {
4038 vim_strncpy(buf + off, (char_u *)"...",
4039 (size_t)(SPACE_FOR_ARGNR - off));
4040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 STRCAT(buf, ")");
4042 }
4043
Bram Moolenaar2c666692012-09-05 13:30:40 +02004044 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045
4046#if defined(FEAT_CLIENTSERVER)
4047 if (serverName != NULL)
4048 {
4049 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02004050 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 }
4052 else
4053#endif
4054 STRCAT(buf, " - VIM");
4055
4056 if (maxlen > 0)
4057 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004058 // make it shorter by removing a bit in the middle
Bram Moolenaarf31b7642012-01-20 20:44:43 +01004059 if (vim_strsize(buf) > maxlen)
4060 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 }
4062 }
4063 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02004064 mustset = value_changed(title_str, &lasttitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065
4066 if (p_icon)
4067 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02004068 icon_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 if (*p_iconstring != NUL)
4070 {
4071#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00004072 if (stl_syntax & STL_IN_ICON)
4073 {
4074 int use_sandbox = FALSE;
Bram Moolenaar53989552019-12-23 22:59:18 +01004075 int called_emsg_before = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004076
4077# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00004078 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004079# endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02004080 build_stl_str_hl(curwin, icon_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004081 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004082 0, 0, NULL, NULL);
Bram Moolenaar53989552019-12-23 22:59:18 +01004083 if (called_emsg > called_emsg_before)
Bram Moolenaard3667a22006-03-16 21:35:52 +00004084 set_string_option_direct((char_u *)"iconstring", -1,
4085 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaard3667a22006-03-16 21:35:52 +00004086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 else
4088#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02004089 icon_str = p_iconstring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 }
4091 else
4092 {
4093 if (buf_spname(curbuf) != NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004094 p = buf_spname(curbuf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004095 else // use file name only in icon
Bram Moolenaar84a93082018-06-16 22:58:15 +02004096 p = gettail(curbuf->b_ffname);
4097 *icon_str = NUL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004098 // Truncate name at 100 bytes.
Bram Moolenaar84a93082018-06-16 22:58:15 +02004099 len = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 if (len > 100)
4101 {
4102 len -= 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 if (has_mbyte)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004104 len += (*mb_tail_off)(p, p + len) + 1;
Bram Moolenaar84a93082018-06-16 22:58:15 +02004105 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02004107 STRCPY(icon_str, p);
4108 trans_characters(icon_str, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 }
4110 }
4111
Bram Moolenaar84a93082018-06-16 22:58:15 +02004112 mustset |= value_changed(icon_str, &lasticon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113
4114 if (mustset)
4115 resettitle();
4116}
4117
4118/*
4119 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
4120 * from "str" if it does.
Bram Moolenaar84a93082018-06-16 22:58:15 +02004121 * Return TRUE if resettitle() is to be called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 */
4123 static int
Bram Moolenaar84a93082018-06-16 22:58:15 +02004124value_changed(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125{
4126 if ((str == NULL) != (*last == NULL)
4127 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
4128 {
4129 vim_free(*last);
4130 if (str == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004131 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 *last = NULL;
Bram Moolenaar40385db2018-08-07 22:31:44 +02004133 mch_restore_title(
4134 last == &lasttitle ? SAVE_RESTORE_TITLE : SAVE_RESTORE_ICON);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 else
Bram Moolenaar84a93082018-06-16 22:58:15 +02004137 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 *last = vim_strsave(str);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004139 return TRUE;
4140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 }
4142 return FALSE;
4143}
4144
4145/*
4146 * Put current window title back (used after calling a shell)
4147 */
4148 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004149resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150{
4151 mch_settitle(lasttitle, lasticon);
4152}
Bram Moolenaarea408852005-06-25 22:49:46 +00004153
4154# if defined(EXITFREE) || defined(PROTO)
4155 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004156free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00004157{
4158 vim_free(lasttitle);
4159 vim_free(lasticon);
4160}
4161# endif
4162
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004164#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004165
4166/*
4167 * Used for building in the status line.
4168 */
4169typedef struct
4170{
4171 char_u *stl_start;
4172 int stl_minwid;
4173 int stl_maxwid;
4174 enum {
4175 Normal,
4176 Empty,
4177 Group,
4178 Middle,
4179 Highlight,
4180 TabPage,
4181 Trunc
4182 } stl_type;
4183} stl_item_T;
4184
4185static size_t stl_items_len = 20; // Initial value, grows as needed.
4186static stl_item_T *stl_items = NULL;
4187static int *stl_groupitem = NULL;
4188static stl_hlrec_T *stl_hltab = NULL;
4189static stl_hlrec_T *stl_tabtab = NULL;
4190
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004192 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 * Return length of string in screen cells.
4194 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004195 * Normally works for window "wp", except when working for 'tabline' then it
4196 * is "curwin".
4197 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 * Items are drawn interspersed with the text that surrounds it
4199 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
4200 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
4201 *
4202 * If maxwidth is not zero, the string will be filled at any middle marker
4203 * or truncated if too long, fillchar is used for all whitespace.
4204 */
4205 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004206build_stl_str_hl(
4207 win_T *wp,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004208 char_u *out, // buffer to write into != NameBuff
4209 size_t outlen, // length of out[]
Bram Moolenaar7454a062016-01-30 15:14:10 +01004210 char_u *fmt,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004211 int use_sandbox UNUSED, // "fmt" was set insecurely, use sandbox
Bram Moolenaar7454a062016-01-30 15:14:10 +01004212 int fillchar,
4213 int maxwidth,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004214 stl_hlrec_T **hltab, // return: HL attributes (can be NULL)
4215 stl_hlrec_T **tabtab) // return: tab page nrs (can be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216{
Bram Moolenaar10772302019-01-20 18:25:54 +01004217 linenr_T lnum;
4218 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 char_u *p;
4220 char_u *s;
4221 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004222 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223#ifdef FEAT_EVAL
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004224 win_T *save_curwin;
4225 buf_T *save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004226 int save_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227#endif
4228 int empty_line;
4229 colnr_T virtcol;
4230 long l;
4231 long n;
4232 int prevchar_isflag;
4233 int prevchar_isitem;
4234 int itemisflag;
4235 int fillable;
4236 char_u *str;
4237 long num;
4238 int width;
4239 int itemcnt;
4240 int curitem;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004241 int group_end_userhl;
4242 int group_start_userhl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 int groupdepth;
shadmansaleh30e3de22021-05-15 17:23:28 +02004244#ifdef FEAT_EVAL
4245 int evaldepth;
4246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 int minwid;
4248 int maxwid;
4249 int zeropad;
4250 char_u base;
4251 char_u opt;
4252#define TMPLEN 70
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004253 char_u buf_tmp[TMPLEN];
4254 char_u win_tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004255 char_u *usefmt = fmt;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004256 stl_hlrec_T *sp;
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01004257 int save_redraw_not_allowed = redraw_not_allowed;
Bram Moolenaar0e1f36f2022-02-15 16:17:44 +00004258 int save_KeyTyped = KeyTyped;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004259
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01004260 // When inside update_screen() we do not want redrawing a statusline,
4261 // ruler, title, etc. to trigger another redraw, it may cause an endless
4262 // loop.
4263 if (updating_screen)
4264 redraw_not_allowed = TRUE;
4265
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004266 if (stl_items == NULL)
4267 {
4268 stl_items = ALLOC_MULT(stl_item_T, stl_items_len);
4269 stl_groupitem = ALLOC_MULT(int, stl_items_len);
Brandon Richardsona493b652022-02-19 11:45:03 +00004270
4271 // Allocate one more, because the last element is used to indicate the
4272 // end of the list.
4273 stl_hltab = ALLOC_MULT(stl_hlrec_T, stl_items_len + 1);
4274 stl_tabtab = ALLOC_MULT(stl_hlrec_T, stl_items_len + 1);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004275 }
4276
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004277#ifdef FEAT_EVAL
4278 /*
4279 * When the format starts with "%!" then evaluate it as an expression and
4280 * use the result as the actual format string.
4281 */
4282 if (fmt[0] == '%' && fmt[1] == '!')
4283 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004284 typval_T tv;
4285
4286 tv.v_type = VAR_NUMBER;
4287 tv.vval.v_number = wp->w_id;
4288 set_var((char_u *)"g:statusline_winid", &tv, FALSE);
4289
Bram Moolenaara4e0b972022-10-01 19:43:52 +01004290 usefmt = eval_to_string_safe(fmt + 2, use_sandbox, FALSE, FALSE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004291 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00004292 usefmt = fmt;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004293
4294 do_unlet((char_u *)"g:statusline_winid", TRUE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004295 }
4296#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297
4298 if (fillchar == 0)
4299 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004300
Bram Moolenaar10772302019-01-20 18:25:54 +01004301 // The cursor in windows other than the current one isn't always
4302 // up-to-date, esp. because of autocommands and timers.
4303 lnum = wp->w_cursor.lnum;
4304 if (lnum > wp->w_buffer->b_ml.ml_line_count)
4305 {
4306 lnum = wp->w_buffer->b_ml.ml_line_count;
4307 wp->w_cursor.lnum = lnum;
4308 }
4309
4310 // Get line & check if empty (cursorpos will show "0-1"). Note that
4311 // p will become invalid when getting another buffer line.
4312 p = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar567199b2013-04-24 16:52:36 +02004313 empty_line = (*p == NUL);
4314
Bram Moolenaar10772302019-01-20 18:25:54 +01004315 // Get the byte value now, in case we need it below. This is more efficient
4316 // than making a copy of the line.
4317 len = STRLEN(p);
4318 if (wp->w_cursor.col > (colnr_T)len)
4319 {
4320 // Line may have changed since checking the cursor column, or the lnum
4321 // was adjusted above.
4322 wp->w_cursor.col = (colnr_T)len;
Bram Moolenaar10772302019-01-20 18:25:54 +01004323 wp->w_cursor.coladd = 0;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004324 byteval = 0;
Bram Moolenaar10772302019-01-20 18:25:54 +01004325 }
Bram Moolenaar567199b2013-04-24 16:52:36 +02004326 else
Bram Moolenaar567199b2013-04-24 16:52:36 +02004327 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328
4329 groupdepth = 0;
shadmansaleh30e3de22021-05-15 17:23:28 +02004330#ifdef FEAT_EVAL
4331 evaldepth = 0;
4332#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 p = out;
4334 curitem = 0;
4335 prevchar_isflag = TRUE;
4336 prevchar_isitem = FALSE;
zeertzjq122dea72022-07-27 15:48:45 +01004337 for (s = usefmt; *s != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004339 if (curitem == (int)stl_items_len)
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004340 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004341 size_t new_len = stl_items_len * 3 / 2;
4342 stl_item_T *new_items;
4343 int *new_groupitem;
4344 stl_hlrec_T *new_hlrec;
4345
4346 new_items = vim_realloc(stl_items, sizeof(stl_item_T) * new_len);
4347 if (new_items == NULL)
4348 break;
4349 stl_items = new_items;
4350 new_groupitem = vim_realloc(stl_groupitem, sizeof(int) * new_len);
4351 if (new_groupitem == NULL)
4352 break;
4353 stl_groupitem = new_groupitem;
Brandon Richardsona493b652022-02-19 11:45:03 +00004354 new_hlrec = vim_realloc(stl_hltab,
4355 sizeof(stl_hlrec_T) * (new_len + 1));
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004356 if (new_hlrec == NULL)
4357 break;
4358 stl_hltab = new_hlrec;
Brandon Richardsona493b652022-02-19 11:45:03 +00004359 new_hlrec = vim_realloc(stl_tabtab,
4360 sizeof(stl_hlrec_T) * (new_len + 1));
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004361 if (new_hlrec == NULL)
4362 break;
4363 stl_tabtab = new_hlrec;
4364 stl_items_len = new_len;
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004365 }
4366
zeertzjq122dea72022-07-27 15:48:45 +01004367 if (*s != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 prevchar_isflag = prevchar_isitem = FALSE;
4369
4370 /*
4371 * Handle up to the next '%' or the end.
4372 */
4373 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
4374 *p++ = *s++;
4375 if (*s == NUL || p + 1 >= out + outlen)
4376 break;
4377
4378 /*
4379 * Handle one '%' item.
4380 */
4381 s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004382 if (*s == NUL) // ignore trailing %
Bram Moolenaar1d87f512011-02-01 21:55:01 +01004383 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 if (*s == '%')
4385 {
4386 if (p + 1 >= out + outlen)
4387 break;
4388 *p++ = *s++;
4389 prevchar_isflag = prevchar_isitem = FALSE;
4390 continue;
4391 }
4392 if (*s == STL_MIDDLEMARK)
4393 {
4394 s++;
4395 if (groupdepth > 0)
4396 continue;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004397 stl_items[curitem].stl_type = Middle;
4398 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 continue;
4400 }
4401 if (*s == STL_TRUNCMARK)
4402 {
4403 s++;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004404 stl_items[curitem].stl_type = Trunc;
4405 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 continue;
4407 }
4408 if (*s == ')')
4409 {
4410 s++;
4411 if (groupdepth < 1)
4412 continue;
4413 groupdepth--;
4414
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004415 t = stl_items[stl_groupitem[groupdepth]].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 *p = NUL;
4417 l = vim_strsize(t);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004418 if (curitem > stl_groupitem[groupdepth] + 1
4419 && stl_items[stl_groupitem[groupdepth]].stl_minwid == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004421 // remove group if all items are empty and highlight group
4422 // doesn't change
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004423 group_start_userhl = group_end_userhl = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004424 for (n = stl_groupitem[groupdepth] - 1; n >= 0; n--)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004425 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004426 if (stl_items[n].stl_type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004427 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004428 group_start_userhl = group_end_userhl =
4429 stl_items[n].stl_minwid;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004430 break;
4431 }
4432 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004433 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004434 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004435 if (stl_items[n].stl_type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 break;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004437 if (stl_items[n].stl_type == Highlight)
4438 group_end_userhl = stl_items[n].stl_minwid;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004439 }
4440 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 {
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004442 // empty group
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 p = t;
4444 l = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004445 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004446 {
4447 // do not use the highlighting from the removed group
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004448 if (stl_items[n].stl_type == Highlight)
4449 stl_items[n].stl_type = Empty;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004450 // adjust the start position of TabPage to the next
4451 // item position
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004452 if (stl_items[n].stl_type == TabPage)
4453 stl_items[n].stl_start = p;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 }
4456 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004457 if (l > stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004459 // truncate, remove n bytes of text at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 if (has_mbyte)
4461 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004462 // Find the first character that should be included.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 n = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004464 while (l >= stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 {
4466 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004467 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 }
4469 }
4470 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004471 n = (long)(p - t) - stl_items[stl_groupitem[groupdepth]]
4472 .stl_maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473
4474 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004475 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 p = p - n + 1;
Bram Moolenaar13505972019-01-24 15:04:48 +01004477
4478 // Fill up space left over by half a double-wide char.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004479 while (++l < stl_items[stl_groupitem[groupdepth]].stl_minwid)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004480 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481
Bram Moolenaarc667da52019-11-30 20:52:27 +01004482 // correct the start of the items for the truncation
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004483 for (l = stl_groupitem[groupdepth] + 1; l < curitem; l++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 {
LemonBoy57ff5262022-05-09 21:03:47 +01004485 // Minus one for the leading '<' added above.
4486 stl_items[l].stl_start -= n - 1;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004487 if (stl_items[l].stl_start < t)
4488 stl_items[l].stl_start = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 }
4490 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004491 else if (abs(stl_items[stl_groupitem[groupdepth]].stl_minwid) > l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004493 // fill
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004494 n = stl_items[stl_groupitem[groupdepth]].stl_minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 if (n < 0)
4496 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004497 // fill by appending characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 n = 0 - n;
4499 while (l++ < n && p + 1 < out + outlen)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004500 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 }
4502 else
4503 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004504 // fill by inserting characters
Bram Moolenaar008bff92021-03-04 21:55:58 +01004505 l = (n - l) * MB_CHAR2LEN(fillchar);
4506 mch_memmove(t + l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004508 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 p += l;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004510 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
4511 stl_items[n].stl_start += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 for ( ; l > 0; l--)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004513 MB_CHAR2BYTES(fillchar, t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 }
4515 }
4516 continue;
4517 }
4518 minwid = 0;
4519 maxwid = 9999;
4520 zeropad = FALSE;
4521 l = 1;
4522 if (*s == '0')
4523 {
4524 s++;
4525 zeropad = TRUE;
4526 }
4527 if (*s == '-')
4528 {
4529 s++;
4530 l = -1;
4531 }
4532 if (VIM_ISDIGIT(*s))
4533 {
4534 minwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004535 if (minwid < 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 minwid = 0;
4537 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004538 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004540 stl_items[curitem].stl_type = Highlight;
4541 stl_items[curitem].stl_start = p;
4542 stl_items[curitem].stl_minwid = minwid > 9 ? 1 : minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 s++;
4544 curitem++;
4545 continue;
4546 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004547 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4548 {
4549 if (*s == STL_TABCLOSENR)
4550 {
4551 if (minwid == 0)
4552 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004553 // %X ends the close label, go back to the previously
4554 // define tab label nr.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004555 for (n = curitem - 1; n >= 0; --n)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004556 if (stl_items[n].stl_type == TabPage
4557 && stl_items[n].stl_minwid >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004558 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004559 minwid = stl_items[n].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004560 break;
4561 }
4562 }
4563 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01004564 // close nrs are stored as negative values
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004565 minwid = - minwid;
4566 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004567 stl_items[curitem].stl_type = TabPage;
4568 stl_items[curitem].stl_start = p;
4569 stl_items[curitem].stl_minwid = minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004570 s++;
4571 curitem++;
4572 continue;
4573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 if (*s == '.')
4575 {
4576 s++;
4577 if (VIM_ISDIGIT(*s))
4578 {
4579 maxwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004580 if (maxwid <= 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 maxwid = 50;
4582 }
4583 }
4584 minwid = (minwid > 50 ? 50 : minwid) * l;
4585 if (*s == '(')
4586 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004587 stl_groupitem[groupdepth++] = curitem;
4588 stl_items[curitem].stl_type = Group;
4589 stl_items[curitem].stl_start = p;
4590 stl_items[curitem].stl_minwid = minwid;
4591 stl_items[curitem].stl_maxwid = maxwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 s++;
4593 curitem++;
4594 continue;
4595 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004596#ifdef FEAT_EVAL
4597 // Denotes end of expanded %{} block
4598 if (*s == '}' && evaldepth > 0)
4599 {
4600 s++;
4601 evaldepth--;
4602 continue;
4603 }
4604#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 if (vim_strchr(STL_ALL, *s) == NULL)
4606 {
4607 s++;
4608 continue;
4609 }
4610 opt = *s++;
4611
Bram Moolenaarc667da52019-11-30 20:52:27 +01004612 // OK - now for the real work
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 base = 'D';
4614 itemisflag = FALSE;
4615 fillable = TRUE;
4616 num = -1;
4617 str = NULL;
4618 switch (opt)
4619 {
4620 case STL_FILEPATH:
4621 case STL_FULLPATH:
4622 case STL_FILENAME:
Bram Moolenaarc667da52019-11-30 20:52:27 +01004623 fillable = FALSE; // don't change ' ' to fillchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004625 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 else
4627 {
4628 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004629 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4631 }
4632 trans_characters(NameBuff, MAXPATHL);
4633 if (opt != STL_FILENAME)
4634 str = NameBuff;
4635 else
4636 str = gettail(NameBuff);
4637 break;
4638
Bram Moolenaarc667da52019-11-30 20:52:27 +01004639 case STL_VIM_EXPR: // '{'
shadmansaleh30e3de22021-05-15 17:23:28 +02004640 {
4641#ifdef FEAT_EVAL
4642 char_u *block_start = s - 1;
4643#endif
4644 int reevaluate = (*s == '%');
4645
4646 if (reevaluate)
4647 s++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 itemisflag = TRUE;
4649 t = p;
shadmansaleh30e3de22021-05-15 17:23:28 +02004650 while ((*s != '}' || (reevaluate && s[-1] != '%'))
4651 && *s != NUL && p + 1 < out + outlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 *p++ = *s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004653 if (*s != '}') // missing '}' or out of space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 break;
4655 s++;
shadmansaleh30e3de22021-05-15 17:23:28 +02004656 if (reevaluate)
4657 p[-1] = 0; // remove the % at the end of %{% expr %}
4658 else
4659 *p = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 p = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661#ifdef FEAT_EVAL
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004662 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp),
4663 "%d", curbuf->b_fnum);
4664 set_internal_string_var((char_u *)"g:actual_curbuf", buf_tmp);
4665 vim_snprintf((char *)win_tmp, sizeof(win_tmp), "%d", curwin->w_id);
4666 set_internal_string_var((char_u *)"g:actual_curwin", win_tmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004668 save_curbuf = curbuf;
4669 save_curwin = curwin;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004670 save_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 curwin = wp;
4672 curbuf = wp->w_buffer;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004673 // Visual mode is only valid in the current window.
4674 if (curwin != save_curwin)
4675 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676
Bram Moolenaara4e0b972022-10-01 19:43:52 +01004677 str = eval_to_string_safe(p, use_sandbox, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004679 curwin = save_curwin;
4680 curbuf = save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004681 VIsual_active = save_VIsual_active;
Bram Moolenaar01824652005-01-31 18:58:23 +00004682 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004683 do_unlet((char_u *)"g:actual_curwin", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684
4685 if (str != NULL && *str != 0)
4686 {
4687 if (*skipdigits(str) == NUL)
4688 {
4689 num = atoi((char *)str);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004690 VIM_CLEAR(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 itemisflag = FALSE;
4692 }
4693 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004694
4695 // If the output of the expression needs to be evaluated
4696 // replace the %{} block with the result of evaluation
4697 if (reevaluate && str != NULL && *str != 0
4698 && strchr((const char *)str, '%') != NULL
4699 && evaldepth < MAX_STL_EVAL_DEPTH)
4700 {
4701 size_t parsed_usefmt = (size_t)(block_start - usefmt);
4702 size_t str_length = strlen((const char *)str);
4703 size_t fmt_length = strlen((const char *)s);
4704 size_t new_fmt_len = parsed_usefmt
4705 + str_length + fmt_length + 3;
4706 char_u *new_fmt = (char_u *)alloc(new_fmt_len * sizeof(char_u));
4707 char_u *new_fmt_p = new_fmt;
4708
4709 new_fmt_p = (char_u *)memcpy(new_fmt_p, usefmt, parsed_usefmt)
4710 + parsed_usefmt;
4711 new_fmt_p = (char_u *)memcpy(new_fmt_p , str, str_length)
4712 + str_length;
4713 new_fmt_p = (char_u *)memcpy(new_fmt_p, "%}", 2) + 2;
4714 new_fmt_p = (char_u *)memcpy(new_fmt_p , s, fmt_length)
4715 + fmt_length;
4716 *new_fmt_p = 0;
4717 new_fmt_p = NULL;
4718
4719 if (usefmt != fmt)
4720 vim_free(usefmt);
4721 VIM_CLEAR(str);
4722 usefmt = new_fmt;
4723 s = usefmt + parsed_usefmt;
4724 evaldepth++;
4725 continue;
4726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727#endif
4728 break;
shadmansaleh30e3de22021-05-15 17:23:28 +02004729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 case STL_LINE:
4731 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4732 ? 0L : (long)(wp->w_cursor.lnum);
4733 break;
4734
4735 case STL_NUMLINES:
4736 num = wp->w_buffer->b_ml.ml_line_count;
4737 break;
4738
4739 case STL_COLUMN:
Bram Moolenaar24959102022-05-07 20:01:16 +01004740 num = (State & MODE_INSERT) == 0 && empty_line
4741 ? 0 : (int)wp->w_cursor.col + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 break;
4743
4744 case STL_VIRTCOL:
4745 case STL_VIRTCOL_ALT:
zeertzjq0f112052022-01-14 20:11:38 +00004746 virtcol = wp->w_virtcol + 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004747 // Don't display %V if it's the same as %c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 if (opt == STL_VIRTCOL_ALT
Bram Moolenaar24959102022-05-07 20:01:16 +01004749 && (virtcol == (colnr_T)((State & MODE_INSERT) == 0
4750 && empty_line ? 0 : (int)wp->w_cursor.col + 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 break;
4752 num = (long)virtcol;
4753 break;
4754
4755 case STL_PERCENTAGE:
4756 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4757 (long)wp->w_buffer->b_ml.ml_line_count);
4758 break;
4759
4760 case STL_ALTPERCENT:
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004761 str = buf_tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004762 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 break;
4764
4765 case STL_ARGLISTSTAT:
4766 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004767 buf_tmp[0] = 0;
4768 if (append_arg_number(wp, buf_tmp, (int)sizeof(buf_tmp), FALSE))
4769 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 break;
4771
4772 case STL_KEYMAP:
4773 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004774 if (get_keymap_str(wp, (char_u *)"<%s>", buf_tmp, TMPLEN))
4775 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 break;
4777 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004778#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004779 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780#else
4781 num = 0;
4782#endif
4783 break;
4784
4785 case STL_BUFNO:
4786 num = wp->w_buffer->b_fnum;
4787 break;
4788
4789 case STL_OFFSET_X:
4790 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004791 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 case STL_OFFSET:
4793#ifdef FEAT_BYTEOFF
4794 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
Bram Moolenaar24959102022-05-07 20:01:16 +01004795 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0
4796 ? 0L : l + 1 + ((State & MODE_INSERT) == 0 && empty_line
4797 ? 0 : (int)wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798#endif
4799 break;
4800
4801 case STL_BYTEVAL_X:
4802 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004803 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004805 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 if (num == NL)
4807 num = 0;
4808 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4809 num = NL;
4810 break;
4811
4812 case STL_ROFLAG:
4813 case STL_ROFLAG_ALT:
4814 itemisflag = TRUE;
4815 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004816 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 break;
4818
4819 case STL_HELPFLAG:
4820 case STL_HELPFLAG_ALT:
4821 itemisflag = TRUE;
4822 if (wp->w_buffer->b_help)
4823 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004824 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 break;
4826
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 case STL_FILETYPE:
4828 if (*wp->w_buffer->b_p_ft != NUL
4829 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4830 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004831 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), "[%s]",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004832 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004833 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 }
4835 break;
4836
4837 case STL_FILETYPE_ALT:
4838 itemisflag = TRUE;
4839 if (*wp->w_buffer->b_p_ft != NUL
4840 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4841 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004842 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), ",%s",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004843 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004844 for (t = buf_tmp; *t != 0; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 *t = TOUPPER_LOC(*t);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004846 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 }
4848 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849
Bram Moolenaar4033c552017-09-16 20:54:51 +02004850#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 case STL_PREVIEWFLAG:
4852 case STL_PREVIEWFLAG_ALT:
4853 itemisflag = TRUE;
4854 if (wp->w_p_pvw)
4855 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4856 : _("[Preview]"));
4857 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004858
4859 case STL_QUICKFIX:
4860 if (bt_quickfix(wp->w_buffer))
4861 str = (char_u *)(wp->w_llist_ref
4862 ? _(msg_loclist)
4863 : _(msg_qflist));
4864 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865#endif
4866
4867 case STL_MODIFIED:
4868 case STL_MODIFIED_ALT:
4869 itemisflag = TRUE;
4870 switch ((opt == STL_MODIFIED_ALT)
4871 + bufIsChanged(wp->w_buffer) * 2
4872 + (!wp->w_buffer->b_p_ma) * 4)
4873 {
4874 case 2: str = (char_u *)"[+]"; break;
4875 case 3: str = (char_u *)",+"; break;
4876 case 4: str = (char_u *)"[-]"; break;
4877 case 5: str = (char_u *)",-"; break;
4878 case 6: str = (char_u *)"[+-]"; break;
4879 case 7: str = (char_u *)",+-"; break;
4880 }
4881 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004882
4883 case STL_HIGHLIGHT:
4884 t = s;
4885 while (*s != '#' && *s != NUL)
4886 ++s;
4887 if (*s == '#')
4888 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004889 stl_items[curitem].stl_type = Highlight;
4890 stl_items[curitem].stl_start = p;
4891 stl_items[curitem].stl_minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004892 curitem++;
4893 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004894 if (*s != NUL)
4895 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004896 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 }
4898
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004899 stl_items[curitem].stl_start = p;
4900 stl_items[curitem].stl_type = Normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 if (str != NULL && *str)
4902 {
4903 t = str;
4904 if (itemisflag)
4905 {
4906 if ((t[0] && t[1])
4907 && ((!prevchar_isitem && *t == ',')
4908 || (prevchar_isflag && *t == ' ')))
4909 t++;
4910 prevchar_isflag = TRUE;
4911 }
4912 l = vim_strsize(t);
4913 if (l > 0)
4914 prevchar_isitem = TRUE;
4915 if (l > maxwid)
4916 {
4917 while (l >= maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 if (has_mbyte)
4919 {
4920 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004921 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 }
4923 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 l -= byte2cells(*t++);
4925 if (p + 1 >= out + outlen)
4926 break;
4927 *p++ = '<';
4928 }
4929 if (minwid > 0)
4930 {
4931 for (; l < minwid && p + 1 < out + outlen; l++)
4932 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004933 // Don't put a "-" in front of a digit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4935 *p++ = ' ';
4936 else
Bram Moolenaar008bff92021-03-04 21:55:58 +01004937 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 }
4939 minwid = 0;
4940 }
4941 else
4942 minwid *= -1;
Bram Moolenaar008bff92021-03-04 21:55:58 +01004943 for (; *t && p + 1 < out + outlen; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004945 // Change a space by fillchar, unless fillchar is '-' and a
4946 // digit follows.
Bram Moolenaar008bff92021-03-04 21:55:58 +01004947 if (fillable && *t == ' '
4948 && (!VIM_ISDIGIT(*(t + 1)) || fillchar != '-'))
4949 MB_CHAR2BYTES(fillchar, p);
4950 else
4951 *p++ = *t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 }
4953 for (; l < minwid && p + 1 < out + outlen; l++)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004954 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 }
4956 else if (num >= 0)
4957 {
4958 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4959 char_u nstr[20];
4960
4961 if (p + 20 >= out + outlen)
Bram Moolenaarc667da52019-11-30 20:52:27 +01004962 break; // not sufficient space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 prevchar_isitem = TRUE;
4964 t = nstr;
4965 if (opt == STL_VIRTCOL_ALT)
4966 {
4967 *t++ = '-';
4968 minwid--;
4969 }
4970 *t++ = '%';
4971 if (zeropad)
4972 *t++ = '0';
4973 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004974 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 *t = 0;
4976
4977 for (n = num, l = 1; n >= nbase; n /= nbase)
4978 l++;
4979 if (opt == STL_VIRTCOL_ALT)
4980 l++;
4981 if (l > maxwid)
4982 {
4983 l += 2;
4984 n = l - maxwid;
4985 while (l-- > maxwid)
4986 num /= nbase;
4987 *t++ = '>';
4988 *t++ = '%';
4989 *t = t[-3];
4990 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004991 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4992 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 }
4994 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004995 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4996 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 p += STRLEN(p);
4998 }
4999 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005000 stl_items[curitem].stl_type = Empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01005002 if (num >= 0 || (!itemisflag && str != NULL && *str != NUL))
5003 prevchar_isflag = FALSE; // Item not NULL, but not a flag
5004 //
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 if (opt == STL_VIM_EXPR)
5006 vim_free(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 curitem++;
5008 }
5009 *p = NUL;
5010 itemcnt = curitem;
5011
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005012#ifdef FEAT_EVAL
5013 if (usefmt != fmt)
5014 vim_free(usefmt);
5015#endif
5016
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 width = vim_strsize(out);
5018 if (maxwidth > 0 && width > maxwidth)
5019 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005020 // Result is too long, must truncate somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 l = 0;
5022 if (itemcnt == 0)
5023 s = out;
5024 else
5025 {
5026 for ( ; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005027 if (stl_items[l].stl_type == Trunc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005029 // Truncate at %< item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005030 s = stl_items[l].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 break;
5032 }
5033 if (l == itemcnt)
5034 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005035 // No %< item, truncate first item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005036 s = stl_items[0].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 l = 0;
5038 }
5039 }
5040
5041 if (width - vim_strsize(s) >= maxwidth)
5042 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005043 // Truncation mark is beyond max length
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 if (has_mbyte)
5045 {
5046 s = out;
5047 width = 0;
5048 for (;;)
5049 {
5050 width += ptr2cells(s);
5051 if (width >= maxwidth)
5052 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005053 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005055 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 while (++width < maxwidth)
Bram Moolenaar008bff92021-03-04 21:55:58 +01005057 MB_CHAR2BYTES(fillchar, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 }
5059 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 s = out + maxwidth - 1;
5061 for (l = 0; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005062 if (stl_items[l].stl_start > s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 break;
5064 itemcnt = l;
5065 *s++ = '>';
5066 *s = 0;
5067 }
5068 else
5069 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 if (has_mbyte)
5071 {
5072 n = 0;
5073 while (width >= maxwidth)
5074 {
5075 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005076 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 }
5078 }
5079 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 n = width - maxwidth + 1;
5081 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00005082 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 *s = '<';
5084
Bram Moolenaarc667da52019-11-30 20:52:27 +01005085 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 while (++width < maxwidth)
5087 {
5088 s = s + STRLEN(s);
Bram Moolenaar008bff92021-03-04 21:55:58 +01005089 MB_CHAR2BYTES(fillchar, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 *s = NUL;
5091 }
5092
Bram Moolenaarc667da52019-11-30 20:52:27 +01005093 --n; // count the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 for (; l < itemcnt; l++)
5095 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005096 if (stl_items[l].stl_start - n >= s)
5097 stl_items[l].stl_start -= n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005099 stl_items[l].stl_start = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 }
5101 }
5102 width = maxwidth;
5103 }
5104 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
5105 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005106 // Apply STL_MIDDLE if any
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 for (l = 0; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005108 if (stl_items[l].stl_type == Middle)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 break;
5110 if (l < itemcnt)
5111 {
Bram Moolenaar008bff92021-03-04 21:55:58 +01005112 int middlelength = (maxwidth - width) * MB_CHAR2LEN(fillchar);
5113 p = stl_items[l].stl_start + middlelength;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005114 STRMOVE(p, stl_items[l].stl_start);
Bram Moolenaar008bff92021-03-04 21:55:58 +01005115 for (s = stl_items[l].stl_start; s < p;)
5116 MB_CHAR2BYTES(fillchar, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 for (l++; l < itemcnt; l++)
Bram Moolenaar008bff92021-03-04 21:55:58 +01005118 stl_items[l].stl_start += middlelength;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 width = maxwidth;
5120 }
5121 }
5122
Bram Moolenaarc667da52019-11-30 20:52:27 +01005123 // Store the info about highlighting.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005124 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005126 *hltab = stl_hltab;
5127 sp = stl_hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 for (l = 0; l < itemcnt; l++)
5129 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005130 if (stl_items[l].stl_type == Highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005132 sp->start = stl_items[l].stl_start;
5133 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005134 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 }
5136 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005137 sp->start = NULL;
5138 sp->userhl = 0;
5139 }
5140
Bram Moolenaarc667da52019-11-30 20:52:27 +01005141 // Store the info about tab pages labels.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005142 if (tabtab != NULL)
5143 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005144 *tabtab = stl_tabtab;
5145 sp = stl_tabtab;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005146 for (l = 0; l < itemcnt; l++)
5147 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005148 if (stl_items[l].stl_type == TabPage)
Bram Moolenaard1f56e62006-02-22 21:25:37 +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++;
5153 }
5154 }
5155 sp->start = NULL;
5156 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 }
5158
Bram Moolenaar471c0fa2022-08-22 15:19:16 +01005159 redraw_not_allowed = save_redraw_not_allowed;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02005160
Bram Moolenaar0e1f36f2022-02-15 16:17:44 +00005161 // A user function may reset KeyTyped, restore it.
5162 KeyTyped = save_KeyTyped;
5163
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 return width;
5165}
Bram Moolenaarc667da52019-11-30 20:52:27 +01005166#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005168#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
5169 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005171 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
5172 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 */
5174 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005175get_rel_pos(
5176 win_T *wp,
5177 char_u *buf,
5178 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005180 long above; // number of lines above window
5181 long below; // number of lines below window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182
Bram Moolenaarc667da52019-11-30 20:52:27 +01005183 if (buflen < 3) // need at least 3 chars for writing
Bram Moolenaar0027c212015-01-07 13:31:52 +01005184 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 above = wp->w_topline - 1;
5186#ifdef FEAT_DIFF
5187 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02005188 if (wp->w_topline == 1 && wp->w_topfill >= 1)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005189 above = 0; // All buffer lines are displayed and there is an
5190 // indication of filler lines, that can be considered
5191 // seeing all lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192#endif
5193 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
5194 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005195 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
5196 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005198 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005200 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 ? (int)(above / ((above + below) / 100L))
5202 : (int)(above * 100L / (above + below)));
5203}
5204#endif
5205
5206/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005207 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 * Return TRUE if it was appended.
5209 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005210 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005211append_arg_number(
5212 win_T *wp,
5213 char_u *buf,
5214 int buflen,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005215 int add_file) // Add "file" before the arg number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216{
5217 char_u *p;
5218
Bram Moolenaarc667da52019-11-30 20:52:27 +01005219 if (ARGCOUNT <= 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 return FALSE;
5221
Bram Moolenaarc667da52019-11-30 20:52:27 +01005222 p = buf + STRLEN(buf); // go to the end of the buffer
5223 if (p - buf + 35 >= buflen) // getting too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 return FALSE;
5225 *p++ = ' ';
5226 *p++ = '(';
5227 if (add_file)
5228 {
5229 STRCPY(p, "file ");
5230 p += 5;
5231 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005232 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
5233 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
5235 return TRUE;
5236}
5237
5238/*
5239 * If fname is not a full path, make it a full path.
5240 * Returns pointer to allocated memory (NULL for failure).
5241 */
5242 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005243fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244{
5245 /*
5246 * Force expanding the path always for Unix, because symbolic links may
5247 * mess up the full path name, even though it starts with a '/'.
5248 * Also expand when there is ".." in the file name, try to remove it,
5249 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00005250 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 * For MS-Windows also expand names like "longna~1" to "longname".
5252 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00005253#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 return FullName_save(fname, TRUE);
5255#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00005256 if (!vim_isAbsName(fname)
5257 || strstr((char *)fname, "..") != NULL
5258 || strstr((char *)fname, "//") != NULL
5259# ifdef BACKSLASH_IN_FILENAME
5260 || strstr((char *)fname, "\\\\") != NULL
5261# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005262# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00005264# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 )
5266 return FullName_save(fname, FALSE);
5267
5268 fname = vim_strsave(fname);
5269
Bram Moolenaar9b942202007-10-03 12:31:33 +00005270# ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01005271 if (fname != NULL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005272 fname_case(fname, 0); // set correct case for file name
Bram Moolenaar9b942202007-10-03 12:31:33 +00005273# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274
5275 return fname;
5276#endif
5277}
5278
5279/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005280 * Make "*ffname" a full file name, set "*sfname" to "*ffname" if not NULL.
5281 * "*ffname" becomes a pointer to allocated memory (or NULL).
5282 * When resolving a link both "*sfname" and "*ffname" will point to the same
5283 * allocated memory.
5284 * The "*ffname" and "*sfname" pointer values on call will not be freed.
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005285 * Note that the resulting "*ffname" pointer should be considered not allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005288fname_expand(
5289 buf_T *buf UNUSED,
5290 char_u **ffname,
5291 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005293 if (*ffname == NULL) // no file name given, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 return;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005295 if (*sfname == NULL) // no short file name given, use ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 *sfname = *ffname;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005297 *ffname = fix_fname(*ffname); // expand to full path
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298
5299#ifdef FEAT_SHORTCUT
5300 if (!buf->b_p_bin)
5301 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005302 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005304 // If the file name is a shortcut file, use the file it links to.
Bram Moolenaardce1e892019-02-10 23:18:53 +01005305 rfname = mch_resolve_path(*ffname, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005306 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 {
5308 vim_free(*ffname);
5309 *ffname = rfname;
5310 *sfname = rfname;
5311 }
5312 }
5313#endif
5314}
5315
5316/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 * Open a window for a number of buffers.
5318 */
5319 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005320ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321{
5322 buf_T *buf;
5323 win_T *wp, *wpnext;
5324 int split_ret = OK;
5325 int p_ea_save;
5326 int open_wins = 0;
5327 int r;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005328 int count; // Maximum number of windows to open.
5329 int all; // When TRUE also load inactive buffers.
Bram Moolenaare1004402020-10-24 20:49:43 +02005330 int had_tab = cmdmod.cmod_tab;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005331 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332
Bram Moolenaarc667da52019-11-30 20:52:27 +01005333 if (eap->addr_count == 0) // make as many windows as possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 count = 9999;
5335 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01005336 count = eap->line2; // make as many windows as specified
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5338 all = FALSE;
5339 else
5340 all = TRUE;
5341
5342 setpcmark();
5343
5344#ifdef FEAT_GUI
5345 need_mouse_correct = TRUE;
5346#endif
5347
5348 /*
5349 * Close superfluous windows (two windows for the same buffer).
5350 * Also close windows that are not full-width.
5351 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005352 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005353 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005354 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005356 tpnext = curtab->tp_next;
5357 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005359 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005360 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005361 || ((cmdmod.cmod_split & WSP_VERT)
5362 ? wp->w_height + wp->w_status_height < Rows - p_ch
5363 - tabline_height()
5364 : wp->w_width != Columns)
5365 || (had_tab > 0 && wp != firstwin))
5366 && !ONE_WINDOW
5367 && !(wp->w_closing || wp->w_buffer->b_locked > 0)
5368 && !win_unlisted(wp))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005369 {
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005370 if (win_close(wp, FALSE) == FAIL)
5371 break;
5372 // Just in case an autocommand does something strange with
5373 // windows: start all over...
5374 wpnext = firstwin;
5375 tpnext = first_tabpage;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005376 open_wins = 0;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005377 }
5378 else
5379 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005381
Bram Moolenaarc667da52019-11-30 20:52:27 +01005382 // Without the ":tab" modifier only do the current tab page.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005383 if (had_tab == 0 || tpnext == NULL)
5384 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005385 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 }
5387
5388 /*
5389 * Go through the buffer list. When a buffer doesn't have a window yet,
5390 * open one. Otherwise move the window to the right position.
5391 * Watch out for autocommands that delete buffers or windows!
5392 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01005393 // Don't execute Win/Buf Enter/Leave autocommands here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 ++autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 win_enter(lastwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5398 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005399 // Check if this buffer needs a window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5401 continue;
5402
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005403 if (had_tab != 0)
5404 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005405 // With the ":tab" modifier don't move the window.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005406 if (buf->b_nwindows > 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005407 wp = lastwin; // buffer has a window, skip it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005408 else
5409 wp = NULL;
5410 }
5411 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005412 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005413 // Check if this buffer already has a window
Bram Moolenaar29323592016-07-24 22:04:11 +02005414 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005415 if (wp->w_buffer == buf)
5416 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005417 // If the buffer already has a window, move it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005418 if (wp != NULL)
5419 win_move_after(wp, curwin);
5420 }
5421
5422 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005424 bufref_T bufref;
5425
5426 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005427
Bram Moolenaarc667da52019-11-30 20:52:27 +01005428 // Split the window and put the buffer in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 p_ea_save = p_ea;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005430 p_ea = TRUE; // use space from all windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5432 ++open_wins;
5433 p_ea = p_ea_save;
5434 if (split_ret == FAIL)
5435 continue;
5436
Bram Moolenaarc667da52019-11-30 20:52:27 +01005437 // Open the buffer in this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005440 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005442 // autocommands deleted the buffer!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 swap_exists_action = SEA_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 break;
5445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 if (swap_exists_action == SEA_QUIT)
5447 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005448#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005449 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005450
Bram Moolenaarc667da52019-11-30 20:52:27 +01005451 // Reset the error/interrupt/exception state here so that
5452 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005453 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005454#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005455
Bram Moolenaarc667da52019-11-30 20:52:27 +01005456 // User selected Quit at ATTENTION prompt; close this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 win_close(curwin, TRUE);
5458 --open_wins;
5459 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005460 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005461
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005462#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005463 // Restore the error/interrupt/exception state if not
5464 // discarded by a new aborting error, interrupt, or uncaught
5465 // exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005466 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 }
5469 else
5470 handle_swap_exists(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 }
5472
5473 ui_breakcheck();
5474 if (got_int)
5475 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005476 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 break;
5478 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005479#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01005480 // Autocommands deleted the buffer or aborted script processing!!!
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005481 if (aborting())
5482 break;
5483#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01005484 // When ":tab" was used open a new tab for a new window repeatedly.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005485 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
Bram Moolenaare1004402020-10-24 20:49:43 +02005486 cmdmod.cmod_tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 --autocmd_no_enter;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005489 win_enter(firstwin, FALSE); // back to first window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491
5492 /*
5493 * Close superfluous windows.
5494 */
5495 for (wp = lastwin; open_wins > count; )
5496 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005497 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 || autowrite(wp->w_buffer, FALSE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 if (!win_valid(wp))
5500 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005501 // BufWrite Autocommands made the window invalid, start over
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 wp = lastwin;
5503 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005504 else if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005506 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 --open_wins;
5508 wp = lastwin;
5509 }
5510 else
5511 {
5512 wp = wp->w_prev;
5513 if (wp == NULL)
5514 break;
5515 }
5516 }
5517}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005520static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005521
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522/*
5523 * do_modelines() - process mode lines for the current file
5524 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005525 * "flags" can be:
5526 * OPT_WINONLY only set options local to window
5527 * OPT_NOWIN don't set options local to window
5528 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 * Returns immediately if the "ml" option isn't set.
5530 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005532do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005534 linenr_T lnum;
5535 int nmlines;
5536 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537
5538 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5539 return;
5540
Bram Moolenaarc667da52019-11-30 20:52:27 +01005541 // Disallow recursive entry here. Can happen when executing a modeline
5542 // triggers an autocommand, which reloads modelines with a ":do".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 if (entered)
5544 return;
5545
5546 ++entered;
Hu Jialun9dcd3492021-08-28 20:42:50 +02005547 for (lnum = 1; curbuf->b_p_ml && lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005549 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 nmlines = 0;
5551
Hu Jialun9dcd3492021-08-28 20:42:50 +02005552 for (lnum = curbuf->b_ml.ml_line_count; curbuf->b_p_ml && lnum > 0 && lnum > nmlines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005554 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 nmlines = 0;
5556 --entered;
5557}
5558
Bram Moolenaarc667da52019-11-30 20:52:27 +01005559#include "version.h" // for version number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560
5561/*
5562 * chk_modeline() - check a single line for a mode string
5563 * Return FAIL if an error encountered.
5564 */
5565 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005566chk_modeline(
5567 linenr_T lnum,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005568 int flags) // Same as for do_modelines().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569{
5570 char_u *s;
5571 char_u *e;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005572 char_u *linecopy; // local copy of any modeline found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 int prev;
5574 int vers;
5575 int end;
5576 int retval = OK;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005577 sctx_T save_current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005578
Bram Moolenaare31ee862020-01-07 20:59:34 +01005579 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580
5581 prev = -1;
5582 for (s = ml_get(lnum); *s != NUL; ++s)
5583 {
5584 if (prev == -1 || vim_isspace(prev))
5585 {
5586 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5587 || STRNCMP(s, "vi:", (size_t)3) == 0)
5588 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005589 // Accept both "vim" and "Vim".
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005590 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 {
5592 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5593 e = s + 4;
5594 else
5595 e = s + 3;
5596 vers = getdigits(&e);
5597 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005598 && (s[0] != 'V'
5599 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 && (s[3] == ':'
5601 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5602 || (VIM_VERSION_100 < vers && s[3] == '<')
5603 || (VIM_VERSION_100 > vers && s[3] == '>')
5604 || (VIM_VERSION_100 == vers && s[3] == '=')))
5605 break;
5606 }
5607 }
5608 prev = *s;
5609 }
5610
5611 if (*s)
5612 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005613 do // skip over "ex:", "vi:" or "vim:"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 ++s;
5615 while (s[-1] != ':');
5616
Bram Moolenaarc667da52019-11-30 20:52:27 +01005617 s = linecopy = vim_strsave(s); // copy the line, it will change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 if (linecopy == NULL)
5619 return FAIL;
5620
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005621 // prepare for emsg()
5622 estack_push(ETYPE_MODELINE, (char_u *)"modelines", lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01005623 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624
5625 end = FALSE;
5626 while (end == FALSE)
5627 {
5628 s = skipwhite(s);
5629 if (*s == NUL)
5630 break;
5631
5632 /*
5633 * Find end of set command: ':' or end of line.
5634 * Skip over "\:", replacing it with ":".
5635 */
5636 for (e = s; *e != ':' && *e != NUL; ++e)
5637 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005638 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 if (*e == NUL)
5640 end = TRUE;
5641
5642 /*
5643 * If there is a "set" command, require a terminating ':' and
5644 * ignore the stuff after the ':'.
5645 * "vi:set opt opt opt: foo" -- foo not interpreted
5646 * "vi:opt opt opt: foo" -- foo interpreted
5647 * Accept "se" for compatibility with Elvis.
5648 */
5649 if (STRNCMP(s, "set ", (size_t)4) == 0
5650 || STRNCMP(s, "se ", (size_t)3) == 0)
5651 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005652 if (*e != ':') // no terminating ':'?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 break;
5654 end = TRUE;
5655 s = vim_strchr(s, ' ') + 1;
5656 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005657 *e = NUL; // truncate the set command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658
Bram Moolenaarc667da52019-11-30 20:52:27 +01005659 if (*s != NUL) // skip over an empty "::"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 {
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005661 int secure_save = secure;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005662
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005663 save_current_sctx = current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005664 current_sctx.sc_version = 1;
5665#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005666 current_sctx.sc_sid = SID_MODELINE;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005667 current_sctx.sc_seq = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005668 current_sctx.sc_lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005670
Bram Moolenaar5958f952018-11-20 04:25:21 +01005671 // Make sure no risky things are executed as a side effect.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01005672 secure = 1;
Bram Moolenaar5958f952018-11-20 04:25:21 +01005673
Bram Moolenaara3227e22006-03-08 21:32:40 +00005674 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar5958f952018-11-20 04:25:21 +01005675
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005676 secure = secure_save;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005677 current_sctx = save_current_sctx;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005678 if (retval == FAIL) // stop if error found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 break;
5680 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005681 s = e + 1; // advance to next part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 }
5683
Bram Moolenaare31ee862020-01-07 20:59:34 +01005684 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005685 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 vim_free(linecopy);
5687 }
5688 return retval;
5689}
5690
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005691/*
Bram Moolenaar91335e52018-08-01 17:53:12 +02005692 * Return TRUE if "buf" is a normal buffer, 'buftype' is empty.
5693 */
5694 int
5695bt_normal(buf_T *buf)
5696{
5697 return buf != NULL && buf->b_p_bt[0] == NUL;
5698}
5699
5700/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005701 * Return TRUE if "buf" is the quickfix buffer.
5702 */
5703 int
Bram Moolenaar340dafd2022-08-25 16:16:45 +01005704bt_quickfix(buf_T *buf UNUSED)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005705{
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005706#ifdef FEAT_QUICKFIX
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005707 return buf != NULL && buf->b_p_bt[0] == 'q';
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005708#else
5709 return FALSE;
Bram Moolenaar113e1072019-01-20 15:30:40 +01005710#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005711}
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005712
5713/*
5714 * Return TRUE if "buf" is a terminal buffer.
5715 */
5716 int
Bram Moolenaar340dafd2022-08-25 16:16:45 +01005717bt_terminal(buf_T *buf UNUSED)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005718{
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005719#if defined(FEAT_TERMINAL)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005720 return buf != NULL && buf->b_p_bt[0] == 't';
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005721#else
5722 return FALSE;
Bram Moolenaar113e1072019-01-20 15:30:40 +01005723#endif
Bram Moolenaar6d4b2f52022-08-25 15:11:15 +01005724}
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005725
5726/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005727 * Return TRUE if "buf" is a help buffer.
5728 */
5729 int
5730bt_help(buf_T *buf)
5731{
5732 return buf != NULL && buf->b_help;
5733}
5734
5735/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02005736 * Return TRUE if "buf" is a prompt buffer.
5737 */
5738 int
5739bt_prompt(buf_T *buf)
5740{
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005741 return buf != NULL && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'r';
5742}
5743
Dominique Pelle748b3082022-01-08 12:41:16 +00005744#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005745/*
5746 * Return TRUE if "buf" is a buffer for a popup window.
5747 */
5748 int
5749bt_popup(buf_T *buf)
5750{
5751 return buf != NULL && buf->b_p_bt != NULL
5752 && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'o';
Bram Moolenaarf2732452018-06-03 14:47:35 +02005753}
Dominique Pelle748b3082022-01-08 12:41:16 +00005754#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005755
5756/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005757 * Return TRUE if "buf" is a "nofile", "acwrite", "terminal" or "prompt"
Bram Moolenaarc3126192022-08-26 12:58:17 +01005758 * buffer. This means the buffer name may not be a file name, at least not for
5759 * writing the buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005760 */
5761 int
Bram Moolenaar26910de2019-06-15 19:37:15 +02005762bt_nofilename(buf_T *buf)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005763{
5764 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5765 || buf->b_p_bt[0] == 'a'
Bram Moolenaarf2732452018-06-03 14:47:35 +02005766 || buf->b_p_bt[0] == 't'
5767 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005768}
5769
Bram Moolenaarc3126192022-08-26 12:58:17 +01005770/*
5771 * Return TRUE if "buf" is a "nofile", "quickfix", "terminal" or "prompt"
5772 * buffer. This means the buffer is not to be read from a file.
5773 */
5774 static int
5775bt_nofileread(buf_T *buf)
5776{
5777 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5778 || buf->b_p_bt[0] == 't'
5779 || buf->b_p_bt[0] == 'q'
5780 || buf->b_p_bt[0] == 'p');
5781}
5782
Dominique Pelle748b3082022-01-08 12:41:16 +00005783#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005784/*
Bram Moolenaar26910de2019-06-15 19:37:15 +02005785 * Return TRUE if "buf" has 'buftype' set to "nofile".
5786 */
5787 int
5788bt_nofile(buf_T *buf)
5789{
5790 return buf != NULL && buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f';
5791}
Dominique Pelle748b3082022-01-08 12:41:16 +00005792#endif
Bram Moolenaar26910de2019-06-15 19:37:15 +02005793
5794/*
Yee Cheng Chin15b314f2022-10-09 18:53:32 +01005795 * Return TRUE if "buf" is a "nowrite", "nofile", "terminal", "prompt", or
5796 * "popup" buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005797 */
5798 int
5799bt_dontwrite(buf_T *buf)
5800{
Bram Moolenaarf2732452018-06-03 14:47:35 +02005801 return buf != NULL && (buf->b_p_bt[0] == 'n'
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005802 || buf->b_p_bt[0] == 't'
5803 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005804}
5805
5806 int
5807bt_dontwrite_msg(buf_T *buf)
5808{
5809 if (bt_dontwrite(buf))
5810 {
Bram Moolenaarf1474d82021-12-31 19:59:55 +00005811 emsg(_(e_cannot_write_buftype_option_is_set));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005812 return TRUE;
5813 }
5814 return FALSE;
5815}
5816
5817/*
5818 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
5819 * and 'bufhidden'.
5820 */
5821 int
5822buf_hide(buf_T *buf)
5823{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005824 // 'bufhidden' overrules 'hidden' and ":hide", check it first
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005825 switch (buf->b_p_bh[0])
5826 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005827 case 'u': // "unload"
5828 case 'w': // "wipe"
5829 case 'd': return FALSE; // "delete"
5830 case 'h': return TRUE; // "hide"
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005831 }
Bram Moolenaare1004402020-10-24 20:49:43 +02005832 return (p_hid || (cmdmod.cmod_flags & CMOD_HIDE));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005833}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834
5835/*
5836 * Return special buffer name.
5837 * Returns NULL when the buffer has a normal file name.
5838 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005839 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005840buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841{
Bram Moolenaar4033c552017-09-16 20:54:51 +02005842#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005844 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005845 /*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005846 * Differentiate between the quickfix and location list buffers using
5847 * the buffer number stored in the global quickfix stack.
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005848 */
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005849 if (buf->b_fnum == qf_stack_get_bufnr())
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005850 return (char_u *)_(msg_qflist);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005851 else
5852 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02005855
Bram Moolenaarc667da52019-11-30 20:52:27 +01005856 // There is no _file_ when 'buftype' is "nofile", b_sfname
5857 // contains the name as specified by the user.
Bram Moolenaar26910de2019-06-15 19:37:15 +02005858 if (bt_nofilename(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 {
Bram Moolenaar21554412017-07-24 21:44:43 +02005860#ifdef FEAT_TERMINAL
5861 if (buf->b_term != NULL)
5862 return term_get_status_text(buf->b_term);
5863#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02005864 if (buf->b_fname != NULL)
5865 return buf->b_fname;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02005866#ifdef FEAT_JOB_CHANNEL
5867 if (bt_prompt(buf))
5868 return (char_u *)_("[Prompt]");
5869#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005870#ifdef FEAT_PROP_POPUP
Bram Moolenaarc6896e22019-05-30 22:32:34 +02005871 if (bt_popup(buf))
5872 return (char_u *)_("[Popup]");
5873#endif
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005874 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 }
Bram Moolenaar21554412017-07-24 21:44:43 +02005876
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 if (buf->b_fname == NULL)
Bram Moolenaar00806bc2020-11-05 19:36:38 +01005878 return buf_get_fname(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 return NULL;
5880}
5881
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882/*
Bram Moolenaar00806bc2020-11-05 19:36:38 +01005883 * Get "buf->b_fname", use "[No Name]" if it is NULL.
5884 */
5885 char_u *
5886buf_get_fname(buf_T *buf)
5887{
5888 if (buf->b_fname == NULL)
5889 return (char_u *)_("[No Name]");
5890 return buf->b_fname;
5891}
5892
5893/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5895 */
5896 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005897set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898{
5899 if (on != curbuf->b_p_bl)
5900 {
5901 curbuf->b_p_bl = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 if (on)
5903 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5904 else
5905 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 }
5907}
5908
5909/*
5910 * Read the file for "buf" again and check if the contents changed.
5911 * Return TRUE if it changed or this could not be checked.
5912 */
5913 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005914buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915{
5916 buf_T *newbuf;
5917 int differ = TRUE;
5918 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 exarg_T ea;
5921
Bram Moolenaarc667da52019-11-30 20:52:27 +01005922 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5924 if (newbuf == NULL)
5925 return TRUE;
5926
Bram Moolenaarc667da52019-11-30 20:52:27 +01005927 // Force the 'fileencoding' and 'fileformat' to be equal.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 if (prep_exarg(&ea, buf) == FAIL)
5929 {
5930 wipe_buffer(newbuf, FALSE);
5931 return TRUE;
5932 }
5933
Bram Moolenaarc667da52019-11-30 20:52:27 +01005934 // set curwin/curbuf to buf and save a few things
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936
Bram Moolenaar4770d092006-01-12 23:22:24 +00005937 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 && readfile(buf->b_ffname, buf->b_fname,
5939 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5940 &ea, READ_NEW | READ_DUMMY) == OK)
5941 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005942 // compare the two files line by line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5944 {
5945 differ = FALSE;
5946 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5947 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5948 {
5949 differ = TRUE;
5950 break;
5951 }
5952 }
5953 }
5954 vim_free(ea.cmd);
5955
Bram Moolenaarc667da52019-11-30 20:52:27 +01005956 // restore curwin/curbuf and a few other things
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958
Bram Moolenaarc667da52019-11-30 20:52:27 +01005959 if (curbuf != newbuf) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 wipe_buffer(newbuf, FALSE);
5961
5962 return differ;
5963}
5964
5965/*
5966 * Wipe out a buffer and decrement the last buffer number if it was used for
5967 * this buffer. Call this to wipe out a temp buffer that does not contain any
5968 * marks.
5969 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005971wipe_buffer(
5972 buf_T *buf,
Bram Moolenaara6e8f882019-12-14 16:18:15 +01005973 int aucmd) // When TRUE trigger autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974{
5975 if (buf->b_fnum == top_file_num - 1)
5976 --top_file_num;
5977
Bram Moolenaarc667da52019-11-30 20:52:27 +01005978 if (!aucmd) // Don't trigger BufDelete autocommands here.
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005979 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005980
Bram Moolenaara6e8f882019-12-14 16:18:15 +01005981 close_buffer(NULL, buf, DOBUF_WIPE, FALSE, TRUE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005982
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005984 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985}