blob: bbbfdb7814ffbb69bb805ed4eb6a287653705eb3 [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);
51static void clear_wininfo(buf_T *buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000052
53#ifdef UNIX
54# define dev_T dev_t
55#else
56# define dev_T unsigned
57#endif
58
Bram Moolenaar00d253e2020-04-06 22:13:01 +020059#define FOR_ALL_BUFS_FROM_LAST(buf) \
60 for ((buf) = lastbuf; (buf) != NULL; (buf) = (buf)->b_prev)
61
Bram Moolenaar4033c552017-09-16 20:54:51 +020062#if defined(FEAT_QUICKFIX)
Bram Moolenaar7fd73202010-07-25 16:58:46 +020063static char *msg_loclist = N_("[Location List]");
64static char *msg_qflist = N_("[Quickfix List]");
65#endif
66
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +020067// Number of times free_buffer() was called.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +020068static int buf_free_count = 0;
69
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +020070static int top_file_num = 1; // highest file number
71static garray_T buf_reuse = GA_EMPTY; // file numbers to recycle
72
73/*
74 * Return the highest possible buffer number.
75 */
76 int
77get_highest_fnum(void)
78{
79 return top_file_num - 1;
80}
81
Bram Moolenaarc024b462019-06-08 18:07:21 +020082/*
83 * Read data from buffer for retrying.
84 */
Bram Moolenaarf71d7b92016-08-09 22:14:05 +020085 static int
86read_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +010087 int read_stdin, // read file from stdin, otherwise fifo
88 exarg_T *eap, // for forced 'ff' and 'fenc' or NULL
89 int flags) // extra flags for readfile()
Bram Moolenaarf71d7b92016-08-09 22:14:05 +020090{
91 int retval = OK;
92 linenr_T line_count;
93
Bram Moolenaarc5935a82021-10-19 20:48:52 +010094 // Read from the buffer which the text is already filled in and append at
95 // the end. This makes it possible to retry when 'fileformat' or
96 // 'fileencoding' was guessed wrong.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +020097 line_count = curbuf->b_ml.ml_line_count;
98 retval = readfile(
99 read_stdin ? NULL : curbuf->b_ffname,
100 read_stdin ? NULL : curbuf->b_fname,
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +0100101 line_count, (linenr_T)0, (linenr_T)MAXLNUM, eap,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200102 flags | READ_BUFFER);
103 if (retval == OK)
104 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100105 // Delete the binary lines.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200106 while (--line_count >= 0)
Bram Moolenaarca70c072020-05-30 20:30:46 +0200107 ml_delete((linenr_T)1);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200108 }
109 else
110 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100111 // Delete the converted lines.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200112 while (curbuf->b_ml.ml_line_count > line_count)
Bram Moolenaarca70c072020-05-30 20:30:46 +0200113 ml_delete(line_count);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200114 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100115 // Put the cursor on the first line.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200116 curwin->w_cursor.lnum = 1;
117 curwin->w_cursor.col = 0;
118
119 if (read_stdin)
120 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100121 // Set or reset 'modified' before executing autocommands, so that
122 // it can be changed there.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100123 if (!readonlymode && !BUFEMPTY())
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200124 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100125 else if (retval == OK)
Bram Moolenaarc024b462019-06-08 18:07:21 +0200126 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200127
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100128 if (retval == OK)
129 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100130#ifdef FEAT_EVAL
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100131 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100132 curbuf, &retval);
133#else
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100134 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200135#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100136 }
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200137 }
138 return retval;
139}
140
Dominique Pelle748b3082022-01-08 12:41:16 +0000141#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142/*
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200143 * Ensure buffer "buf" is loaded. Does not trigger the swap-exists action.
144 */
145 void
146buffer_ensure_loaded(buf_T *buf)
147{
148 if (buf->b_ml.ml_mfp == NULL)
149 {
150 aco_save_T aco;
151
152 aucmd_prepbuf(&aco, buf);
Bram Moolenaar188639d2022-04-04 16:57:21 +0100153 if (swap_exists_action != SEA_READONLY)
154 swap_exists_action = SEA_NONE;
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200155 open_buffer(FALSE, NULL, 0);
156 aucmd_restbuf(&aco);
157 }
158}
Dominique Pelle748b3082022-01-08 12:41:16 +0000159#endif
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200160
161/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200162 * Open current buffer, that is: open the memfile and read the file into
163 * memory.
164 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165 */
166 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100167open_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +0100168 int read_stdin, // read file from stdin
169 exarg_T *eap, // for forced 'ff' and 'fenc' or NULL
170 int flags) // extra flags for readfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171{
172 int retval = OK;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200173 bufref_T old_curbuf;
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100174#ifdef FEAT_SYN_HL
175 long old_tw = curbuf->b_p_tw;
176#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200177 int read_fifo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100179 // The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
180 // When re-entering the same buffer, it should not change, because the
181 // user may have reset the flag by hand.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 if (readonlymode && curbuf->b_ffname != NULL
183 && (curbuf->b_flags & BF_NEVERLOADED))
184 curbuf->b_p_ro = TRUE;
185
Bram Moolenaar4770d092006-01-12 23:22:24 +0000186 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100188 // There MUST be a memfile, otherwise we can't do anything
189 // If we can't create one for the current buffer, take another buffer
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100190 close_buffer(NULL, curbuf, 0, FALSE, FALSE);
Bram Moolenaar29323592016-07-24 22:04:11 +0200191 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 if (curbuf->b_ml.ml_mfp != NULL)
193 break;
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100194 // If there is no memfile at all, exit.
195 // This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 if (curbuf == NULL)
197 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +0000198 emsg(_(e_cannot_allocate_any_buffer_exiting));
Bram Moolenaar6f10c702019-08-20 22:58:37 +0200199
200 // Don't try to do any saving, with "curbuf" NULL almost nothing
201 // will work.
202 v_dying = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 getout(2);
204 }
Bram Moolenaar6f10c702019-08-20 22:58:37 +0200205
Bram Moolenaar40bcec12021-12-05 22:19:27 +0000206 emsg(_(e_cannot_allocate_buffer_using_other_one));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 enter_buffer(curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100208#ifdef FEAT_SYN_HL
209 if (old_tw != curbuf->b_p_tw)
210 check_colorcolumn(curwin);
211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212 return FAIL;
213 }
214
Bram Moolenaarc667da52019-11-30 20:52:27 +0100215 // The autocommands in readfile() may change the buffer, but only AFTER
216 // reading the file.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200217 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218 modified_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219
Bram Moolenaarc667da52019-11-30 20:52:27 +0100220 // mark cursor position as being invalid
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100221 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222
223 if (curbuf->b_ffname != NULL
224#ifdef FEAT_NETBEANS_INTG
225 && netbeansReadFile
226#endif
227 )
228 {
Bram Moolenaar426dd022016-03-15 15:09:29 +0100229 int old_msg_silent = msg_silent;
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200230#ifdef UNIX
231 int save_bin = curbuf->b_p_bin;
232 int perm;
233#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234#ifdef FEAT_NETBEANS_INTG
235 int oldFire = netbeansFireChanges;
236
237 netbeansFireChanges = 0;
238#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200239#ifdef UNIX
240 perm = mch_getperm(curbuf->b_ffname);
Bram Moolenaard569bb02018-08-11 13:57:20 +0200241 if (perm >= 0 && (S_ISFIFO(perm)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200242 || S_ISSOCK(perm)
Bram Moolenaarf04507d2016-08-20 15:05:39 +0200243# ifdef OPEN_CHR_FILES
244 || (S_ISCHR(perm) && is_dev_fd_file(curbuf->b_ffname))
245# endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200246 ))
247 read_fifo = TRUE;
248 if (read_fifo)
249 curbuf->b_p_bin = TRUE;
250#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100251 if (shortmess(SHM_FILEINFO))
252 msg_silent = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200254 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200255 flags | READ_NEW | (read_fifo ? READ_FIFO : 0));
256#ifdef UNIX
257 if (read_fifo)
258 {
259 curbuf->b_p_bin = save_bin;
260 if (retval == OK)
261 retval = read_buffer(FALSE, eap, flags);
262 }
263#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100264 msg_silent = old_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265#ifdef FEAT_NETBEANS_INTG
266 netbeansFireChanges = oldFire;
267#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100268 // Help buffer is filtered.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +0200269 if (bt_help(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 fix_help_buffer();
271 }
272 else if (read_stdin)
273 {
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200274 int save_bin = curbuf->b_p_bin;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100276 // First read the text in binary mode into the buffer.
277 // Then read from that same buffer and append at the end. This makes
278 // it possible to retry when 'fileformat' or 'fileencoding' was
279 // guessed wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280 curbuf->b_p_bin = TRUE;
281 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200282 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
283 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284 curbuf->b_p_bin = save_bin;
285 if (retval == OK)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200286 retval = read_buffer(TRUE, eap, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 }
288
Bram Moolenaarc667da52019-11-30 20:52:27 +0100289 // if first time loading this buffer, init b_chartab[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290 if (curbuf->b_flags & BF_NEVERLOADED)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100291 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292 (void)buf_init_chartab(curbuf, FALSE);
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100293 parse_cino(curbuf);
294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100296 // Set/reset the Changed flag first, autocmds may change the buffer.
297 // Apply the automatic commands, before processing the modelines.
298 // So the modelines have priority over autocommands.
299 //
Bram Moolenaarc667da52019-11-30 20:52:27 +0100300 // When reading stdin, the buffer contents always needs writing, so set
301 // the changed flag. Unless in readonly mode: "ls | gview -".
302 // When interrupted and 'cpoptions' contains 'i' set changed flag.
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000303 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100304 || modified_was_set // ":set modified" used in autocmd
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100305#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000308 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100310 else if (retval == OK && !read_stdin && !read_fifo)
Bram Moolenaarc024b462019-06-08 18:07:21 +0200311 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaarc667da52019-11-30 20:52:27 +0100312 save_file_ff(curbuf); // keep this fileformat
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313
Bram Moolenaarc667da52019-11-30 20:52:27 +0100314 // Set last_changedtick to avoid triggering a TextChanged autocommand right
315 // after it was added.
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100316 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Christian Brabandtdb3b4462021-10-16 11:58:55 +0100317 curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100318 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100319
Bram Moolenaarc667da52019-11-30 20:52:27 +0100320 // require "!" to overwrite the file, because it wasn't read completely
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321#ifdef FEAT_EVAL
322 if (aborting())
323#else
324 if (got_int)
325#endif
326 curbuf->b_flags |= BF_READERR;
327
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000328#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +0100329 // Need to update automatic folding. Do this before the autocommands,
330 // they may use the fold info.
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000331 foldUpdateAll(curwin);
332#endif
333
Bram Moolenaarc667da52019-11-30 20:52:27 +0100334 // need to set w_topline, unless some autocommand already did that.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 if (!(curwin->w_valid & VALID_TOPLINE))
336 {
337 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100338#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100340#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100342#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100344#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346#endif
347
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100348 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100350 // The autocommands may have changed the current buffer. Apply the
351 // modelines to the correct buffer, if it still exists and is loaded.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200352 if (bufref_valid(&old_curbuf) && old_curbuf.br_buf->b_ml.ml_mfp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 {
354 aco_save_T aco;
355
Bram Moolenaarc667da52019-11-30 20:52:27 +0100356 // Go to the buffer that was opened.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200357 aucmd_prepbuf(&aco, old_curbuf.br_buf);
Bram Moolenaara3227e22006-03-08 21:32:40 +0000358 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
360
Bram Moolenaar1d30fde2021-10-20 21:58:42 +0100361 if ((flags & READ_NOWINENTER) == 0)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100362#ifdef FEAT_EVAL
Bram Moolenaar1d30fde2021-10-20 21:58:42 +0100363 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE,
364 curbuf, &retval);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100365#else
Bram Moolenaar1d30fde2021-10-20 21:58:42 +0100366 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100367#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368
Bram Moolenaarc667da52019-11-30 20:52:27 +0100369 // restore curwin/curbuf and a few other things
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 aucmd_restbuf(&aco);
371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 }
373
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 return retval;
375}
376
377/*
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200378 * Store "buf" in "bufref" and set the free count.
379 */
380 void
381set_bufref(bufref_T *bufref, buf_T *buf)
382{
383 bufref->br_buf = buf;
Bram Moolenaarfadacf02017-06-19 20:35:32 +0200384 bufref->br_fnum = buf == NULL ? 0 : buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200385 bufref->br_buf_free_count = buf_free_count;
386}
387
388/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200389 * Return TRUE if "bufref->br_buf" points to the same buffer as when
390 * set_bufref() was called and it is a valid buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200391 * Only goes through the buffer list if buf_free_count changed.
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200392 * Also checks if b_fnum is still the same, a :bwipe followed by :new might get
393 * the same allocated memory, but it's a different buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200394 */
395 int
396bufref_valid(bufref_T *bufref)
397{
398 return bufref->br_buf_free_count == buf_free_count
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200399 ? TRUE : buf_valid(bufref->br_buf)
400 && bufref->br_fnum == bufref->br_buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200401}
402
403/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200405 * This can be slow if there are many buffers, prefer using bufref_valid().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 */
407 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100408buf_valid(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409{
410 buf_T *bp;
411
Bram Moolenaarc667da52019-11-30 20:52:27 +0100412 // Assume that we more often have a recent buffer, start with the last
413 // one.
Bram Moolenaar00d253e2020-04-06 22:13:01 +0200414 FOR_ALL_BUFS_FROM_LAST(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415 if (bp == buf)
416 return TRUE;
417 return FALSE;
418}
419
420/*
Bram Moolenaar480778b2016-07-14 22:09:39 +0200421 * A hash table used to quickly lookup a buffer by its number.
422 */
423static hashtab_T buf_hashtab;
424
425 static void
426buf_hashtab_add(buf_T *buf)
427{
428 sprintf((char *)buf->b_key, "%x", buf->b_fnum);
429 if (hash_add(&buf_hashtab, buf->b_key) == FAIL)
Bram Moolenaarf1474d82021-12-31 19:59:55 +0000430 emsg(_(e_buffer_cannot_be_registered));
Bram Moolenaar480778b2016-07-14 22:09:39 +0200431}
432
433 static void
434buf_hashtab_remove(buf_T *buf)
435{
436 hashitem_T *hi = hash_find(&buf_hashtab, buf->b_key);
437
438 if (!HASHITEM_EMPTY(hi))
439 hash_remove(&buf_hashtab, hi);
440}
441
Bram Moolenaar94f01952018-09-01 15:30:03 +0200442/*
443 * Return TRUE when buffer "buf" can be unloaded.
444 * Give an error message and return FALSE when the buffer is locked or the
445 * screen is being redrawn and the buffer is in a window.
446 */
447 static int
448can_unload_buffer(buf_T *buf)
449{
450 int can_unload = !buf->b_locked;
451
452 if (can_unload && updating_screen)
453 {
454 win_T *wp;
455
456 FOR_ALL_WINDOWS(wp)
457 if (wp->w_buffer == buf)
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200458 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200459 can_unload = FALSE;
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200460 break;
461 }
Bram Moolenaar94f01952018-09-01 15:30:03 +0200462 }
463 if (!can_unload)
Bram Moolenaarf1474d82021-12-31 19:59:55 +0000464 semsg(_(e_attempt_to_delete_buffer_that_is_in_use_str), buf->b_fname);
Bram Moolenaar94f01952018-09-01 15:30:03 +0200465 return can_unload;
466}
Bram Moolenaara997b452018-04-17 23:24:06 +0200467
Bram Moolenaar480778b2016-07-14 22:09:39 +0200468/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 * Close the link to a buffer.
470 * "action" is used when there is no longer a window for the buffer.
471 * It can be:
472 * 0 buffer becomes hidden
473 * DOBUF_UNLOAD buffer is unloaded
474 * DOBUF_DELETE buffer is unloaded and removed from buffer list
475 * DOBUF_WIPE buffer is unloaded and really deleted
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200476 * DOBUF_WIPE_REUSE idem, and add to buf_reuse list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 * When doing all but the first one on the current buffer, the caller should
478 * get a new buffer very soon!
479 *
480 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100481 *
482 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
483 * cause there to be only one window with this buffer. e.g. when ":quit" is
484 * supposed to close the window but autocommands close all other windows.
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100485 *
486 * When "ignore_abort" is TRUE don't abort even when aborting() returns TRUE.
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100487 *
488 * Return TRUE when we got to the end and b_nwindows was decremented.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 */
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100490 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100491close_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +0100492 win_T *win, // if not NULL, set b_last_cursor
Bram Moolenaar7454a062016-01-30 15:14:10 +0100493 buf_T *buf,
494 int action,
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100495 int abort_if_last,
496 int ignore_abort)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100499 int nwindows;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200500 bufref_T bufref;
Bram Moolenaar3a117e12016-10-30 21:57:52 +0100501 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200502 win_T *the_curwin = curwin;
503 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 int unload_buf = (action != 0);
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200505 int wipe_buf = (action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
506 int del_buf = (action == DOBUF_DEL || wipe_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507
Bram Moolenaarcee52202020-03-11 14:19:58 +0100508 CHECK_CURBUF;
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100509
510 // Force unloading or deleting when 'bufhidden' says so.
511 // The caller must take care of NOT deleting/freeing when 'bufhidden' is
512 // "hide" (otherwise we could never free or delete a buffer).
Bram Moolenaarc667da52019-11-30 20:52:27 +0100513 if (buf->b_p_bh[0] == 'd') // 'bufhidden' == "delete"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200514 {
515 del_buf = TRUE;
516 unload_buf = TRUE;
517 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100518 else if (buf->b_p_bh[0] == 'w') // 'bufhidden' == "wipe"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200519 {
520 del_buf = TRUE;
521 unload_buf = TRUE;
522 wipe_buf = TRUE;
523 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100524 else if (buf->b_p_bh[0] == 'u') // 'bufhidden' == "unload"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200525 unload_buf = TRUE;
526
Bram Moolenaar94053a52017-08-01 21:44:33 +0200527#ifdef FEAT_TERMINAL
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200528 if (bt_terminal(buf) && (buf->b_nwindows == 1 || del_buf))
Bram Moolenaar94053a52017-08-01 21:44:33 +0200529 {
Bram Moolenaarcee52202020-03-11 14:19:58 +0100530 CHECK_CURBUF;
Bram Moolenaar94053a52017-08-01 21:44:33 +0200531 if (term_job_running(buf->b_term))
532 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200533 if (wipe_buf || unload_buf)
Bram Moolenaara997b452018-04-17 23:24:06 +0200534 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200535 if (!can_unload_buffer(buf))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100536 return FALSE;
Bram Moolenaar94f01952018-09-01 15:30:03 +0200537
Bram Moolenaarc667da52019-11-30 20:52:27 +0100538 // Wiping out or unloading a terminal buffer kills the job.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200539 free_terminal(buf);
Bram Moolenaara997b452018-04-17 23:24:06 +0200540 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200541 else
542 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100543 // The job keeps running, hide the buffer.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200544 del_buf = FALSE;
545 unload_buf = FALSE;
546 }
547 }
Bram Moolenaarc9f8b842020-11-24 19:36:16 +0100548 else if (buf->b_p_bh[0] == 'h' && !del_buf)
549 {
550 // Hide a terminal buffer.
551 unload_buf = FALSE;
552 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200553 else
554 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100555 // A terminal buffer is wiped out if the job has finished.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200556 del_buf = TRUE;
557 unload_buf = TRUE;
558 wipe_buf = TRUE;
559 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100560 CHECK_CURBUF;
Bram Moolenaar94053a52017-08-01 21:44:33 +0200561 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200562#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563
Bram Moolenaarc667da52019-11-30 20:52:27 +0100564 // Disallow deleting the buffer when it is locked (already being closed or
565 // halfway a command that relies on it). Unloading is allowed.
Bram Moolenaar94f01952018-09-01 15:30:03 +0200566 if ((del_buf || wipe_buf) && !can_unload_buffer(buf))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100567 return FALSE;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200568
Bram Moolenaarc667da52019-11-30 20:52:27 +0100569 // check no autocommands closed the window
Bram Moolenaar4033c552017-09-16 20:54:51 +0200570 if (win != NULL && win_valid_any_tab(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100572 // Set b_last_cursor when closing the last window for the buffer.
573 // Remember the last cursor position and window options of the buffer.
574 // This used to be only for the current window, but then options like
575 // 'foldmethod' may be lost with a ":only" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 if (buf->b_nwindows == 1)
577 set_last_cursor(win);
578 buflist_setfpos(buf, win,
579 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
580 win->w_cursor.col, TRUE);
581 }
582
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200583 set_bufref(&bufref, buf);
584
Bram Moolenaarc667da52019-11-30 20:52:27 +0100585 // When the buffer is no longer in a window, trigger BufWinLeave
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 if (buf->b_nwindows == 1)
587 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200588 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100589 ++buf->b_locked_split;
Bram Moolenaar82404332016-07-10 17:00:38 +0200590 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
591 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200592 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100593 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100594 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200595aucmd_abort:
Bram Moolenaarf1474d82021-12-31 19:59:55 +0000596 emsg(_(e_autocommands_caused_command_to_abort));
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100597 return FALSE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100598 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200599 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100600 --buf->b_locked_split;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200601 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100602 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200603 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604
Bram Moolenaarc667da52019-11-30 20:52:27 +0100605 // When the buffer becomes hidden, but is not unloaded, trigger
606 // BufHidden
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 if (!unload_buf)
608 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200609 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100610 ++buf->b_locked_split;
Bram Moolenaar82404332016-07-10 17:00:38 +0200611 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
612 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200613 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100614 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200615 goto aucmd_abort;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200616 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100617 --buf->b_locked_split;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200618 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100619 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200620 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100622#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100623 // autocmds may abort script processing
624 if (!ignore_abort && aborting())
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100625 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100626#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200628
Bram Moolenaarc667da52019-11-30 20:52:27 +0100629 // If the buffer was in curwin and the window has changed, go back to that
630 // window, if it still exists. This avoids that ":edit x" triggering a
631 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200632 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
633 {
634 block_autocmds();
635 goto_tabpage_win(the_curtab, the_curwin);
636 unblock_autocmds();
637 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200638
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 nwindows = buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640
Bram Moolenaarc667da52019-11-30 20:52:27 +0100641 // decrease the link count from windows (unless not in any window)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 if (buf->b_nwindows > 0)
643 --buf->b_nwindows;
644
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100645#ifdef FEAT_DIFF
646 if (diffopt_hiddenoff() && !unload_buf && buf->b_nwindows == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100647 diff_buf_delete(buf); // Clear 'diff' for hidden buffer.
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100648#endif
649
Bram Moolenaarc667da52019-11-30 20:52:27 +0100650 // Return when a window is displaying the buffer or when it's not
651 // unloaded.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100653 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654
Bram Moolenaarc667da52019-11-30 20:52:27 +0100655 // Always remove the buffer when there is no file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 if (buf->b_ffname == NULL)
657 del_buf = TRUE;
658
Bram Moolenaarc667da52019-11-30 20:52:27 +0100659 // When closing the current buffer stop Visual mode before freeing
660 // anything.
Bram Moolenaar4930a762016-09-11 14:39:53 +0200661 if (buf == curbuf && VIsual_active
Bram Moolenaar9a27c7f2016-09-09 12:57:09 +0200662#if defined(EXITFREE)
663 && !entered_free_all_mem
664#endif
665 )
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200666 end_visual_mode();
667
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100668 // Free all things allocated for this buffer.
669 // Also calls the "BufDelete" autocommands when del_buf is TRUE.
670 //
Bram Moolenaarc667da52019-11-30 20:52:27 +0100671 // Remember if we are closing the current buffer. Restore the number of
672 // windows, so that autocommands in buf_freeall() don't get confused.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 is_curbuf = (buf == curbuf);
674 buf->b_nwindows = nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100676 buf_freeall(buf, (del_buf ? BFA_DEL : 0)
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100677 + (wipe_buf ? BFA_WIPE : 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100678 + (ignore_abort ? BFA_IGNORE_ABORT : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679
Bram Moolenaarc667da52019-11-30 20:52:27 +0100680 // Autocommands may have deleted the buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200681 if (!bufref_valid(&bufref))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100682 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100683#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100684 // autocmds may abort script processing
685 if (!ignore_abort && aborting())
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100686 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100689 // It's possible that autocommands change curbuf to the one being deleted.
690 // This might cause the previous curbuf to be deleted unexpectedly. But
691 // in some cases it's OK to delete the curbuf, because a new one is
692 // obtained anyway. Therefore only return if curbuf changed to the
693 // deleted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 if (buf == curbuf && !is_curbuf)
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100695 return FALSE;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200696
Bram Moolenaar4033c552017-09-16 20:54:51 +0200697 if (win_valid_any_tab(win) && win->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100698 win->w_buffer = NULL; // make sure we don't use the buffer now
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200699
Bram Moolenaarc667da52019-11-30 20:52:27 +0100700 // Autocommands may have opened or closed windows for this buffer.
701 // Decrement the count for the close we do here.
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200702 if (buf->b_nwindows > 0)
703 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 /*
706 * Remove the buffer from the list.
707 */
708 if (wipe_buf)
709 {
Bram Moolenaar347538f2022-03-26 16:28:06 +0000710 // Do not wipe out the buffer if it is used in a window.
711 if (buf->b_nwindows > 0)
712 return FALSE;
713
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200714 if (action == DOBUF_WIPE_REUSE)
715 {
716 // we can re-use this buffer number, store it
717 if (buf_reuse.ga_itemsize == 0)
718 ga_init2(&buf_reuse, sizeof(int), 50);
719 if (ga_grow(&buf_reuse, 1) == OK)
720 ((int *)buf_reuse.ga_data)[buf_reuse.ga_len++] = buf->b_fnum;
721 }
Bram Moolenaar3d6014f2018-10-11 19:27:47 +0200722 if (buf->b_sfname != buf->b_ffname)
723 VIM_CLEAR(buf->b_sfname);
724 else
725 buf->b_sfname = NULL;
726 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 if (buf->b_prev == NULL)
728 firstbuf = buf->b_next;
729 else
730 buf->b_prev->b_next = buf->b_next;
731 if (buf->b_next == NULL)
732 lastbuf = buf->b_prev;
733 else
734 buf->b_next->b_prev = buf->b_prev;
735 free_buffer(buf);
736 }
737 else
738 {
739 if (del_buf)
740 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100741 // Free all internal variables and reset option values, to make
742 // ":bdel" compatible with Vim 5.7.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 free_buffer_stuff(buf, TRUE);
744
Bram Moolenaarc667da52019-11-30 20:52:27 +0100745 // Make it look like a new buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
747
Bram Moolenaarc667da52019-11-30 20:52:27 +0100748 // Init the options when loaded again.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 buf->b_p_initialized = FALSE;
750 }
751 buf_clear_file(buf);
752 if (del_buf)
753 buf->b_p_bl = FALSE;
754 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100755 // NOTE: at this point "curbuf" may be invalid!
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100756 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757}
758
759/*
760 * Make buffer not contain a file.
761 */
762 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100763buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764{
765 buf->b_ml.ml_line_count = 1;
Bram Moolenaarc024b462019-06-08 18:07:21 +0200766 unchanged(buf, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 buf->b_p_eol = TRUE;
769 buf->b_start_eol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000771 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 buf->b_ml.ml_mfp = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +0100773 buf->b_ml.ml_flags = ML_EMPTY; // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774#ifdef FEAT_NETBEANS_INTG
775 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776#endif
777}
778
779/*
780 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200781 * the file. Careful: get here with "curwin" NULL when exiting.
782 * flags:
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100783 * BFA_DEL buffer is going to be deleted
784 * BFA_WIPE buffer is going to be wiped out
785 * BFA_KEEP_UNDO do not free undo information
786 * BFA_IGNORE_ABORT don't abort even when aborting() returns TRUE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100789buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200792 bufref_T bufref;
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200793 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaar5a497892016-09-03 16:29:04 +0200794 win_T *the_curwin = curwin;
795 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796
Bram Moolenaarc667da52019-11-30 20:52:27 +0100797 // Make sure the buffer isn't closed by autocommands.
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200798 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100799 ++buf->b_locked_split;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200800 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200801 if (buf->b_ml.ml_mfp != NULL)
802 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200803 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
804 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200805 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100806 // autocommands deleted the buffer
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200807 return;
808 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200809 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200811 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
812 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200813 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100814 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 return;
816 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200817 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200819 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
820 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200821 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100822 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 return;
824 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200825 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100826 --buf->b_locked_split;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200827
Bram Moolenaarc667da52019-11-30 20:52:27 +0100828 // If the buffer was in curwin and the window has changed, go back to that
829 // window, if it still exists. This avoids that ":edit x" triggering a
830 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaar5a497892016-09-03 16:29:04 +0200831 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
832 {
833 block_autocmds();
834 goto_tabpage_win(the_curtab, the_curwin);
835 unblock_autocmds();
836 }
Bram Moolenaar5a497892016-09-03 16:29:04 +0200837
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100838#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100839 // autocmds may abort script processing
840 if ((flags & BFA_IGNORE_ABORT) == 0 && aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100842#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100844 // It's possible that autocommands change curbuf to the one being deleted.
845 // This might cause curbuf to be deleted unexpectedly. But in some cases
846 // it's OK to delete the curbuf, because a new one is obtained anyway.
847 // Therefore only return if curbuf changed to the deleted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 if (buf == curbuf && !is_curbuf)
849 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850#ifdef FEAT_DIFF
Bram Moolenaarc667da52019-11-30 20:52:27 +0100851 diff_buf_delete(buf); // Can't use 'diff' for unloaded buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200853#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100854 // Remove any ownsyntax, unless exiting.
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200855 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaar89c71222011-11-30 15:40:56 +0100856 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200857#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000858
859#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +0100860 // No folds in an empty buffer.
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000861 {
862 win_T *win;
863 tabpage_T *tp;
864
865 FOR_ALL_TAB_WINDOWS(tp, win)
866 if (win->w_buffer == buf)
867 clearFolding(win);
868 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000869#endif
870
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871#ifdef FEAT_TCL
872 tcl_buffer_free(buf);
873#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100874 ml_close(buf, TRUE); // close and delete the memline/memfile
875 buf->b_ml.ml_line_count = 0; // no lines in buffer
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200876 if ((flags & BFA_KEEP_UNDO) == 0)
877 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100878 u_blockfree(buf); // free the memory allocated for undo
879 u_clearall(buf); // reset all undo information
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100882 syntax_clear(&buf->b_s); // reset syntax info
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100884#ifdef FEAT_PROP_POPUP
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100885 clear_buf_prop_types(buf);
886#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100887 buf->b_flags &= ~BF_READERR; // a read error is no longer relevant
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888}
889
890/*
891 * Free a buffer structure and the things it contains related to the buffer
892 * itself (not the file, that must have been done already).
893 */
894 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100895free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200897 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200899#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100900 // b:changedtick uses an item in buf_T, remove it now
Bram Moolenaarb2c87502017-10-14 21:15:58 +0200901 dictitem_remove(buf->b_vars, (dictitem_T *)&buf->b_ct_di);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200902 unref_var_dict(buf->b_vars);
Bram Moolenaar86173482019-10-01 17:02:16 +0200903 remove_listeners(buf);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200904#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200905#ifdef FEAT_LUA
906 lua_buffer_free(buf);
907#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000908#ifdef FEAT_MZSCHEME
909 mzscheme_buffer_free(buf);
910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911#ifdef FEAT_PERL
912 perl_buf_free(buf);
913#endif
914#ifdef FEAT_PYTHON
915 python_buffer_free(buf);
916#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200917#ifdef FEAT_PYTHON3
918 python3_buffer_free(buf);
919#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920#ifdef FEAT_RUBY
921 ruby_buffer_free(buf);
922#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200923#ifdef FEAT_JOB_CHANNEL
924 channel_buffer_free(buf);
925#endif
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200926#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +0200927 free_terminal(buf);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200928#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +0200929#ifdef FEAT_JOB_CHANNEL
930 vim_free(buf->b_prompt_text);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200931 free_callback(&buf->b_prompt_callback);
Bram Moolenaar86173482019-10-01 17:02:16 +0200932 free_callback(&buf->b_prompt_interrupt);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200933#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200934
935 buf_hashtab_remove(buf);
936
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200937 aubuflocal_remove(buf);
938
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200939 if (autocmd_busy)
940 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100941 // Do not free the buffer structure while autocommands are executing,
942 // it's still needed. Free it when autocmd_busy is reset.
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200943 buf->b_next = au_pending_free_buf;
944 au_pending_free_buf = buf;
945 }
946 else
Bram Moolenaarcee52202020-03-11 14:19:58 +0100947 {
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200948 vim_free(buf);
Bram Moolenaarcee52202020-03-11 14:19:58 +0100949 if (curbuf == buf)
950 curbuf = NULL; // make clear it's not to be used
951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952}
953
954/*
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100955 * Initializes b:changedtick.
Bram Moolenaar79518e22017-02-17 16:31:35 +0100956 */
957 static void
958init_changedtick(buf_T *buf)
959{
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100960 dictitem_T *di = (dictitem_T *)&buf->b_ct_di;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100961
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100962 di->di_flags = DI_FLAGS_FIX | DI_FLAGS_RO;
963 di->di_tv.v_type = VAR_NUMBER;
964 di->di_tv.v_lock = VAR_FIXED;
965 di->di_tv.vval.v_number = 0;
966
Bram Moolenaar92769c32017-02-25 15:41:37 +0100967#ifdef FEAT_EVAL
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100968 STRCPY(buf->b_ct_di.di_key, "changedtick");
969 (void)dict_add(buf->b_vars, di);
Bram Moolenaar92769c32017-02-25 15:41:37 +0100970#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +0100971}
972
973/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
975 */
976 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100977free_buffer_stuff(
978 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +0100979 int free_options) // free options as well
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980{
981 if (free_options)
982 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100983 clear_wininfo(buf); // including window-local options
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200985#ifdef FEAT_SPELL
986 ga_clear(&buf->b_s.b_langp);
987#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 }
989#ifdef FEAT_EVAL
Bram Moolenaar79518e22017-02-17 16:31:35 +0100990 {
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100991 varnumber_T tick = CHANGEDTICK(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +0100992
Bram Moolenaarc667da52019-11-30 20:52:27 +0100993 vars_clear(&buf->b_vars->dv_hashtab); // free all buffer variables
Bram Moolenaar79518e22017-02-17 16:31:35 +0100994 hash_init(&buf->b_vars->dv_hashtab);
995 init_changedtick(buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100996 CHANGEDTICK(buf) = tick;
Bram Moolenaarf10997a2020-01-03 21:00:02 +0100997 remove_listeners(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +0100998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999#endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001000 uc_clear(&buf->b_ucmds); // clear local user commands
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001#ifdef FEAT_SIGNS
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001002 buf_delete_signs(buf, (char_u *)"*"); // delete any signs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00001004#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001005 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00001006#endif
zeertzjqc207fd22022-06-29 10:37:40 +01001007 map_clear_mode(buf, MAP_ALL_MODES, TRUE, FALSE); // clear local mappings
1008 map_clear_mode(buf, MAP_ALL_MODES, TRUE, TRUE); // clear local abbrevs
Bram Moolenaard23a8232018-02-10 18:45:26 +01001009 VIM_CLEAR(buf->b_start_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010}
1011
1012/*
Bram Moolenaar4882d982020-10-25 17:55:09 +01001013 * Free one wininfo_T.
1014 */
1015 void
1016free_wininfo(wininfo_T *wip)
1017{
1018 if (wip->wi_optset)
1019 {
1020 clear_winopt(&wip->wi_opt);
1021#ifdef FEAT_FOLDING
1022 deleteFoldRecurse(&wip->wi_folds);
1023#endif
1024 }
1025 vim_free(wip);
1026}
1027
1028/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 * Free the b_wininfo list for buffer "buf".
1030 */
1031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001032clear_wininfo(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033{
1034 wininfo_T *wip;
1035
1036 while (buf->b_wininfo != NULL)
1037 {
1038 wip = buf->b_wininfo;
1039 buf->b_wininfo = wip->wi_next;
Bram Moolenaar4882d982020-10-25 17:55:09 +01001040 free_wininfo(wip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 }
1042}
1043
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044/*
1045 * Go to another buffer. Handles the result of the ATTENTION dialog.
1046 */
1047 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001048goto_buffer(
1049 exarg_T *eap,
1050 int start,
1051 int dir,
1052 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001054 bufref_T old_curbuf;
Bram Moolenaar188639d2022-04-04 16:57:21 +01001055 int save_sea = swap_exists_action;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001056
1057 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058
Bram Moolenaar188639d2022-04-04 16:57:21 +01001059 if (swap_exists_action == SEA_NONE)
1060 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
1062 start, dir, count, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
1064 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001065#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001066 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001067
Bram Moolenaarc667da52019-11-30 20:52:27 +01001068 // Reset the error/interrupt/exception state here so that
1069 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001070 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001071#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001072
Bram Moolenaarc667da52019-11-30 20:52:27 +01001073 // Quitting means closing the split window, nothing else.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 win_close(curwin, TRUE);
Bram Moolenaar188639d2022-04-04 16:57:21 +01001075 swap_exists_action = save_sea;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001076 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001077
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001078#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001079 // Restore the error/interrupt/exception state if not discarded by a
1080 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001081 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 }
1084 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001085 handle_swap_exists(&old_curbuf);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001086}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088/*
1089 * Handle the situation of swap_exists_action being set.
1090 * It is allowed for "old_curbuf" to be NULL or invalid.
1091 */
1092 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001093handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094{
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001095#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001096 cleanup_T cs;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001097#endif
1098#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001099 long old_tw = curbuf->b_p_tw;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001100#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001101 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001102
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 if (swap_exists_action == SEA_QUIT)
1104 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001105#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001106 // Reset the error/interrupt/exception state here so that
1107 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001108 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001109#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001110
Bram Moolenaarc667da52019-11-30 20:52:27 +01001111 // User selected Quit at ATTENTION prompt. Go back to previous
1112 // buffer. If that buffer is gone or the same as the current one,
1113 // open a new, empty buffer.
1114 swap_exists_action = SEA_NONE; // don't want it again
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001115 swap_exists_did_quit = TRUE;
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001116 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001117 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1118 || old_curbuf->br_buf == curbuf)
Bram Moolenaar1d97efc2021-07-04 13:27:11 +02001119 {
1120 // Block autocommands here because curwin->w_buffer is NULL.
1121 block_autocmds();
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001122 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar1d97efc2021-07-04 13:27:11 +02001123 unblock_autocmds();
1124 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001125 else
1126 buf = old_curbuf->br_buf;
1127 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001128 {
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001129 int old_msg_silent = msg_silent;
1130
1131 if (shortmess(SHM_FILEINFO))
1132 msg_silent = 1; // prevent fileinfo message
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001133 enter_buffer(buf);
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001134 // restore msg_silent, so that the command line will be shown
1135 msg_silent = old_msg_silent;
1136
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001137#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001138 if (old_tw != curbuf->b_p_tw)
1139 check_colorcolumn(curwin);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001140#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001141 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001142 // If "old_curbuf" is NULL we are in big trouble here...
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001143
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001144#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001145 // Restore the error/interrupt/exception state if not discarded by a
1146 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001147 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 }
1150 else if (swap_exists_action == SEA_RECOVER)
1151 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001152#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001153 // Reset the error/interrupt/exception state here so that
1154 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001155 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001156#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001157
Bram Moolenaarc667da52019-11-30 20:52:27 +01001158 // User selected Recover at ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 msg_scroll = TRUE;
Bram Moolenaar99499b12019-05-23 21:35:48 +02001160 ml_recover(FALSE);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001161 msg_puts("\n"); // don't overwrite the last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001163 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001164
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001165#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001166 // Restore the error/interrupt/exception state if not discarded by a
1167 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001168 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 }
1171 swap_exists_action = SEA_NONE;
1172}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174/*
Bram Moolenaara02471e2014-01-10 16:43:14 +01001175 * Make the current buffer empty.
1176 * Used when it is wiped out and it's the last buffer.
1177 */
1178 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001179empty_curbuf(
1180 int close_others,
1181 int forceit,
1182 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001183{
1184 int retval;
1185 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001186 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001187
1188 if (action == DOBUF_UNLOAD)
1189 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001190 emsg(_(e_cannot_unload_last_buffer));
Bram Moolenaara02471e2014-01-10 16:43:14 +01001191 return FAIL;
1192 }
1193
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001194 set_bufref(&bufref, buf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001195 if (close_others)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001196 // Close any other windows on this buffer, then make it empty.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001197 close_windows(buf, TRUE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001198
1199 setpcmark();
1200 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1201 forceit ? ECMD_FORCEIT : 0, curwin);
1202
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001203 // do_ecmd() may create a new buffer, then we have to delete
1204 // the old one. But do_ecmd() may have done that already, check
1205 // if the buffer still exists.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001206 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001207 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001208 if (!close_others)
1209 need_fileinfo = FALSE;
1210 return retval;
1211}
Bram Moolenaar94f01952018-09-01 15:30:03 +02001212
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213/*
1214 * Implementation of the commands for the buffer list.
1215 *
1216 * action == DOBUF_GOTO go to specified buffer
1217 * action == DOBUF_SPLIT split window and go to specified buffer
1218 * action == DOBUF_UNLOAD unload specified buffer(s)
1219 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1220 * action == DOBUF_WIPE delete specified buffer(s) really
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001221 * action == DOBUF_WIPE_REUSE idem, and add number to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 *
1223 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1224 * start == DOBUF_FIRST go to "count" buffer from first buffer
1225 * start == DOBUF_LAST go to "count" buffer from last buffer
1226 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1227 *
1228 * Return FAIL or OK.
1229 */
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001230 static int
1231do_buffer_ext(
Bram Moolenaar7454a062016-01-30 15:14:10 +01001232 int action,
1233 int start,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001234 int dir, // FORWARD or BACKWARD
1235 int count, // buffer number or number of buffers
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001236 int flags) // DOBUF_FORCEIT etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237{
1238 buf_T *buf;
1239 buf_T *bp;
1240 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001241 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242
1243 switch (start)
1244 {
1245 case DOBUF_FIRST: buf = firstbuf; break;
1246 case DOBUF_LAST: buf = lastbuf; break;
1247 default: buf = curbuf; break;
1248 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001249 if (start == DOBUF_MOD) // find next modified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 {
1251 while (count-- > 0)
1252 {
1253 do
1254 {
1255 buf = buf->b_next;
1256 if (buf == NULL)
1257 buf = firstbuf;
1258 }
1259 while (buf != curbuf && !bufIsChanged(buf));
1260 }
1261 if (!bufIsChanged(buf))
1262 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001263 emsg(_(e_no_modified_buffer_found));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 return FAIL;
1265 }
1266 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001267 else if (start == DOBUF_FIRST && count) // find specified buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 {
1269 while (buf != NULL && buf->b_fnum != count)
1270 buf = buf->b_next;
1271 }
1272 else
1273 {
1274 bp = NULL;
1275 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1276 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001277 // remember the buffer where we start, we come back there when all
1278 // buffers are unlisted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 if (bp == NULL)
1280 bp = buf;
1281 if (dir == FORWARD)
1282 {
1283 buf = buf->b_next;
1284 if (buf == NULL)
1285 buf = firstbuf;
1286 }
1287 else
1288 {
1289 buf = buf->b_prev;
1290 if (buf == NULL)
1291 buf = lastbuf;
1292 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001293 // don't count unlisted buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 if (unload || buf->b_p_bl)
1295 {
1296 --count;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001297 bp = NULL; // use this buffer as new starting point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 }
1299 if (bp == buf)
1300 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001301 // back where we started, didn't find anything.
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001302 emsg(_(e_there_is_no_listed_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 return FAIL;
1304 }
1305 }
1306 }
1307
Bram Moolenaarc667da52019-11-30 20:52:27 +01001308 if (buf == NULL) // could not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 {
1310 if (start == DOBUF_FIRST)
1311 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001312 // don't warn when deleting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 if (!unload)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001314 semsg(_(e_buffer_nr_does_not_exist), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 }
1316 else if (dir == FORWARD)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001317 emsg(_(e_cannot_go_beyond_last_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 else
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001319 emsg(_(e_cannot_go_before_first_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 return FAIL;
1321 }
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001322#ifdef FEAT_PROP_POPUP
1323 if ((flags & DOBUF_NOPOPUP) && bt_popup(buf)
1324# ifdef FEAT_TERMINAL
1325 && !bt_terminal(buf)
1326#endif
1327 )
1328 return OK;
1329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330
1331#ifdef FEAT_GUI
1332 need_mouse_correct = TRUE;
1333#endif
1334
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001336 * delete buffer "buf" from memory and/or the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 */
1338 if (unload)
1339 {
1340 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001341 bufref_T bufref;
1342
Bram Moolenaar94f01952018-09-01 15:30:03 +02001343 if (!can_unload_buffer(buf))
Bram Moolenaara997b452018-04-17 23:24:06 +02001344 return FAIL;
Bram Moolenaara997b452018-04-17 23:24:06 +02001345
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001346 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347
Bram Moolenaarc667da52019-11-30 20:52:27 +01001348 // When unloading or deleting a buffer that's already unloaded and
1349 // unlisted: fail silently.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001350 if (action != DOBUF_WIPE && action != DOBUF_WIPE_REUSE
1351 && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 return FAIL;
1353
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001354 if ((flags & DOBUF_FORCEIT) == 0 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001356#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001357 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 {
1359 dialog_changed(buf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001360 if (!bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001361 // Autocommand deleted buffer, oops! It's not changed
1362 // now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 return FAIL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001364 // If it's still changed fail silently, the dialog already
1365 // mentioned why it fails.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001366 if (bufIsChanged(buf))
1367 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001369 else
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001370#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001372 semsg(_(e_no_write_since_last_change_for_buffer_nr_add_bang_to_override),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 buf->b_fnum);
1374 return FAIL;
1375 }
1376 }
1377
Bram Moolenaarc667da52019-11-30 20:52:27 +01001378 // When closing the current buffer stop Visual mode.
Bram Moolenaar4930a762016-09-11 14:39:53 +02001379 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001380 end_visual_mode();
1381
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001382 // If deleting the last (listed) buffer, make it empty.
1383 // The last (listed) buffer cannot be unloaded.
Bram Moolenaar29323592016-07-24 22:04:11 +02001384 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 if (bp->b_p_bl && bp != buf)
1386 break;
1387 if (bp == NULL && buf == curbuf)
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001388 return empty_curbuf(TRUE, (flags & DOBUF_FORCEIT), action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001390 // If the deleted buffer is the current one, close the current window
1391 // (unless it's the only window). Repeat this so long as we end up in
1392 // a window with this buffer.
Bram Moolenaarf740b292006-02-16 22:11:02 +00001393 while (buf == curbuf
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001394 && !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
Bram Moolenaar459ca562016-11-10 18:16:33 +01001395 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001396 {
1397 if (win_close(curwin, FALSE) == FAIL)
1398 break;
1399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001401 // If the buffer to be deleted is not the current one, delete it here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 if (buf != curbuf)
1403 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001404 close_windows(buf, FALSE);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001405 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows <= 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001406 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 return OK;
1408 }
1409
1410 /*
1411 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001412 * There should be another, otherwise it would have been handled
1413 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001414 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 * Then prefer the buffer we most recently visited.
1416 * Else try to find one that is loaded, after the current buffer,
1417 * then before the current buffer.
1418 * Finally use any buffer.
1419 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001420 buf = NULL; // selected buffer
1421 bp = NULL; // used when no loaded buffer found
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001422 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1423 buf = au_new_curbuf.br_buf;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001424 else if (curwin->w_jumplistlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 {
1426 int jumpidx;
1427
1428 jumpidx = curwin->w_jumplistidx - 1;
1429 if (jumpidx < 0)
1430 jumpidx = curwin->w_jumplistlen - 1;
1431
1432 forward = jumpidx;
1433 while (jumpidx != curwin->w_jumplistidx)
1434 {
1435 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1436 if (buf != NULL)
1437 {
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001438 // Skip current and unlisted bufs. Also skip a quickfix
1439 // buffer, it might be deleted soon.
1440 if (buf == curbuf || !buf->b_p_bl
1441#if defined(FEAT_QUICKFIX)
1442 || bt_quickfix(buf)
1443#endif
1444 )
1445 buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 else if (buf->b_ml.ml_mfp == NULL)
1447 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001448 // skip unloaded buf, but may keep it for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 if (bp == NULL)
1450 bp = buf;
1451 buf = NULL;
1452 }
1453 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001454 if (buf != NULL) // found a valid buffer: stop searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001456 // advance to older entry in jump list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1458 break;
1459 if (--jumpidx < 0)
1460 jumpidx = curwin->w_jumplistlen - 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001461 if (jumpidx == forward) // List exhausted for sure
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 break;
1463 }
1464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465
Bram Moolenaarc667da52019-11-30 20:52:27 +01001466 if (buf == NULL) // No previous buffer, Try 2'nd approach
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 {
1468 forward = TRUE;
1469 buf = curbuf->b_next;
1470 for (;;)
1471 {
1472 if (buf == NULL)
1473 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001474 if (!forward) // tried both directions
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 break;
1476 buf = curbuf->b_prev;
1477 forward = FALSE;
1478 continue;
1479 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001480 // in non-help buffer, try to skip help buffers, and vv
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001481 if (buf->b_help == curbuf->b_help && buf->b_p_bl
1482#if defined(FEAT_QUICKFIX)
1483 && !bt_quickfix(buf)
1484#endif
1485 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001487 if (buf->b_ml.ml_mfp != NULL) // found loaded buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001489 if (bp == NULL) // remember unloaded buf for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 bp = buf;
1491 }
1492 if (forward)
1493 buf = buf->b_next;
1494 else
1495 buf = buf->b_prev;
1496 }
1497 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001498 if (buf == NULL) // No loaded buffer, use unloaded one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 buf = bp;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001500 if (buf == NULL) // No loaded buffer, find listed one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001502 FOR_ALL_BUFFERS(buf)
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001503 if (buf->b_p_bl && buf != curbuf
1504#if defined(FEAT_QUICKFIX)
1505 && !bt_quickfix(buf)
1506#endif
1507 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 break;
1509 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001510 if (buf == NULL) // Still no buffer, just take one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 {
1512 if (curbuf->b_next != NULL)
1513 buf = curbuf->b_next;
1514 else
1515 buf = curbuf->b_prev;
Bram Moolenaare3537ae2022-02-08 15:05:20 +00001516#if defined(FEAT_QUICKFIX)
1517 if (bt_quickfix(buf))
1518 buf = NULL;
1519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 }
1521 }
1522
Bram Moolenaara02471e2014-01-10 16:43:14 +01001523 if (buf == NULL)
1524 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001525 // Autocommands must have wiped out all other buffers. Only option
1526 // now is to make the current buffer empty.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001527 return empty_curbuf(FALSE, (flags & DOBUF_FORCEIT), action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001528 }
1529
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001531 * make "buf" the current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001533 if (action == DOBUF_SPLIT) // split window first
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001535 // If 'switchbuf' contains "useopen": jump to first window containing
1536 // "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00001537 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001538 return OK;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001539 // If 'switchbuf' contains "usetab": jump to first window in any tab
1540 // page containing "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00001541 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 return OK;
1543 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 return FAIL;
1545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546
Bram Moolenaarc667da52019-11-30 20:52:27 +01001547 // go to current buffer - nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 if (buf == curbuf)
1549 return OK;
1550
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001551 // Check if the current buffer may be abandoned.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001552 if (action == DOBUF_GOTO && !can_abandon(curbuf, (flags & DOBUF_FORCEIT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 {
1554#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001555 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001557 bufref_T bufref;
1558
1559 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 dialog_changed(curbuf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001561 if (!bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001562 // Autocommand deleted buffer, oops!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 }
1565 if (bufIsChanged(curbuf))
1566#endif
1567 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001568 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 return FAIL;
1570 }
1571 }
1572
Bram Moolenaarc667da52019-11-30 20:52:27 +01001573 // Go to the other buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 set_curbuf(buf, action);
1575
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 if (action == DOBUF_SPLIT)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001577 RESET_BINDING(curwin); // reset 'scrollbind' and 'cursorbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001579#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001580 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 return FAIL;
1582#endif
1583
1584 return OK;
1585}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001587 int
1588do_buffer(
1589 int action,
1590 int start,
1591 int dir, // FORWARD or BACKWARD
1592 int count, // buffer number or number of buffers
1593 int forceit) // TRUE when using !
1594{
1595 return do_buffer_ext(action, start, dir, count,
1596 forceit ? DOBUF_FORCEIT : 0);
1597}
1598
1599/*
1600 * do_bufdel() - delete or unload buffer(s)
1601 *
1602 * addr_count == 0: ":bdel" - delete current buffer
1603 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1604 * buffer "end_bnr", then any other arguments.
1605 * addr_count == 2: ":N,N bdel" - delete buffers in range
1606 *
1607 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1608 * DOBUF_DEL (":bdel")
1609 *
1610 * Returns error message or NULL
1611 */
1612 char *
1613do_bufdel(
1614 int command,
1615 char_u *arg, // pointer to extra arguments
1616 int addr_count,
1617 int start_bnr, // first buffer number in a range
1618 int end_bnr, // buffer nr or last buffer nr in a range
1619 int forceit)
1620{
1621 int do_current = 0; // delete current buffer?
1622 int deleted = 0; // number of buffers deleted
1623 char *errormsg = NULL; // return value
1624 int bnr; // buffer number
1625 char_u *p;
1626
1627 if (addr_count == 0)
1628 {
1629 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1630 }
1631 else
1632 {
1633 if (addr_count == 2)
1634 {
1635 if (*arg) // both range and argument is not allowed
Bram Moolenaar74409f62022-01-01 15:58:22 +00001636 return ex_errmsg(e_trailing_characters_str, arg);
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001637 bnr = start_bnr;
1638 }
1639 else // addr_count == 1
1640 bnr = end_bnr;
1641
1642 for ( ;!got_int; ui_breakcheck())
1643 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001644 // Delete the current buffer last, otherwise when the
1645 // current buffer is deleted, the next buffer becomes
1646 // the current one and will be loaded, which may then
1647 // also be deleted, etc.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001648 if (bnr == curbuf->b_fnum)
1649 do_current = bnr;
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00001650 else if (do_buffer_ext(command, DOBUF_FIRST, FORWARD, bnr,
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001651 DOBUF_NOPOPUP | (forceit ? DOBUF_FORCEIT : 0)) == OK)
1652 ++deleted;
1653
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001654 // find next buffer number to delete/unload
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001655 if (addr_count == 2)
1656 {
1657 if (++bnr > end_bnr)
1658 break;
1659 }
1660 else // addr_count == 1
1661 {
1662 arg = skipwhite(arg);
1663 if (*arg == NUL)
1664 break;
1665 if (!VIM_ISDIGIT(*arg))
1666 {
1667 p = skiptowhite_esc(arg);
1668 bnr = buflist_findpat(arg, p,
1669 command == DOBUF_WIPE || command == DOBUF_WIPE_REUSE,
1670 FALSE, FALSE);
1671 if (bnr < 0) // failed
1672 break;
1673 arg = p;
1674 }
1675 else
1676 bnr = getdigits(&arg);
1677 }
1678 }
1679 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1680 FORWARD, do_current, forceit) == OK)
1681 ++deleted;
1682
1683 if (deleted == 0)
1684 {
1685 if (command == DOBUF_UNLOAD)
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001686 STRCPY(IObuff, _(e_no_buffers_were_unloaded));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001687 else if (command == DOBUF_DEL)
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001688 STRCPY(IObuff, _(e_no_buffers_were_deleted));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001689 else
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001690 STRCPY(IObuff, _(e_no_buffers_were_wiped_out));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001691 errormsg = (char *)IObuff;
1692 }
1693 else if (deleted >= p_report)
1694 {
1695 if (command == DOBUF_UNLOAD)
1696 smsg(NGETTEXT("%d buffer unloaded",
1697 "%d buffers unloaded", deleted), deleted);
1698 else if (command == DOBUF_DEL)
1699 smsg(NGETTEXT("%d buffer deleted",
1700 "%d buffers deleted", deleted), deleted);
1701 else
1702 smsg(NGETTEXT("%d buffer wiped out",
1703 "%d buffers wiped out", deleted), deleted);
1704 }
1705 }
1706
1707
1708 return errormsg;
1709}
1710
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711/*
1712 * Set current buffer to "buf". Executes autocommands and closes current
1713 * buffer. "action" tells how to close the current buffer:
1714 * DOBUF_GOTO free or hide it
1715 * DOBUF_SPLIT nothing
1716 * DOBUF_UNLOAD unload it
1717 * DOBUF_DEL delete it
1718 * DOBUF_WIPE wipe it out
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001719 * DOBUF_WIPE_REUSE wipe it out and add to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 */
1721 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001722set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723{
1724 buf_T *prevbuf;
1725 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001726 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001727#ifdef FEAT_SYN_HL
1728 long old_tw = curbuf->b_p_tw;
1729#endif
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001730 bufref_T newbufref;
1731 bufref_T prevbufref;
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001732 int valid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733
1734 setpcmark();
Bram Moolenaare1004402020-10-24 20:49:43 +02001735 if ((cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001736 curwin->w_alt_fnum = curbuf->b_fnum; // remember alternate file
1737 buflist_altfpos(curwin); // remember curpos
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738
Bram Moolenaarc667da52019-11-30 20:52:27 +01001739 // Don't restart Select mode after switching to another buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741
Bram Moolenaarc667da52019-11-30 20:52:27 +01001742 // close_windows() or apply_autocmds() may change curbuf and wipe out "buf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 prevbuf = curbuf;
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001744 set_bufref(&prevbufref, prevbuf);
1745 set_bufref(&newbufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746
Bram Moolenaar983d83f2021-02-07 12:12:43 +01001747 // Autocommands may delete the current buffer and/or the buffer we want to
1748 // go to. In those cases don't close the buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001749 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001750 || (bufref_valid(&prevbufref)
1751 && bufref_valid(&newbufref)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001752#ifdef FEAT_EVAL
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001753 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001755 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001757#ifdef FEAT_SYN_HL
1758 if (prevbuf == curwin->w_buffer)
1759 reset_synblock(curwin);
1760#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001762 close_windows(prevbuf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001763#if defined(FEAT_EVAL)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001764 if (bufref_valid(&prevbufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765#else
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001766 if (bufref_valid(&prevbufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001768 {
Bram Moolenaare25865a2012-07-06 16:22:02 +02001769 win_T *previouswin = curwin;
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001770
Bram Moolenaare615db02022-01-20 21:00:54 +00001771 // Do not sync when in Insert mode and the buffer is open in
1772 // another window, might be a timer doing something in another
1773 // window.
1774 if (prevbuf == curbuf
Bram Moolenaar24959102022-05-07 20:01:16 +01001775 && ((State & MODE_INSERT) == 0 || curbuf->b_nwindows <= 1))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001776 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1778 unload ? action : (action == DOBUF_GOTO
Bram Moolenaareb44a682017-08-03 22:44:55 +02001779 && !buf_hide(prevbuf)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001780 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0,
1781 FALSE, FALSE);
Bram Moolenaare25865a2012-07-06 16:22:02 +02001782 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001783 // autocommands changed curwin, Grr!
Bram Moolenaare25865a2012-07-06 16:22:02 +02001784 curwin = previouswin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001787 // An autocommand may have deleted "buf", already entered it (e.g., when
1788 // it did ":bunload") or aborted the script processing.
1789 // If curwin->w_buffer is null, enter_buffer() will make it valid again
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001790 valid = buf_valid(buf);
1791 if ((valid && buf != curbuf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001792#ifdef FEAT_EVAL
Bram Moolenaar4033c552017-09-16 20:54:51 +02001793 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001795 ) || curwin->w_buffer == NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001796 {
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001797 // If the buffer is not valid but curwin->w_buffer is NULL we must
1798 // enter some buffer. Using the last one is hopefully OK.
1799 if (!valid)
1800 enter_buffer(lastbuf);
1801 else
1802 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001803#ifdef FEAT_SYN_HL
1804 if (old_tw != curbuf->b_p_tw)
1805 check_colorcolumn(curwin);
1806#endif
1807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808}
1809
1810/*
1811 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001812 * Old curbuf must have been abandoned already! This also means "curbuf" may
1813 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001815 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001816enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817{
Bram Moolenaar010ee962019-09-25 20:37:36 +02001818 // Get the buffer in the current window.
1819 curwin->w_buffer = buf;
1820 curbuf = buf;
1821 ++curbuf->b_nwindows;
1822
1823 // Copy buffer and window local option values. Not for a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1825 if (!buf->b_help)
1826 get_winopts(buf);
1827#ifdef FEAT_FOLDING
1828 else
Bram Moolenaar010ee962019-09-25 20:37:36 +02001829 // Remove all folds in the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 clearFolding(curwin);
Bram Moolenaar010ee962019-09-25 20:37:36 +02001831 foldUpdateAll(curwin); // update folds (later).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832#endif
1833
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001835 if (curwin->w_p_diff)
1836 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837#endif
1838
Bram Moolenaara971b822011-09-14 14:43:25 +02001839#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001840 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001841#endif
1842
Bram Moolenaarc667da52019-11-30 20:52:27 +01001843 // Cursor on first line by default.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 curwin->w_cursor.lnum = 1;
1845 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001848 curwin->w_topline_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849
Bram Moolenaarc667da52019-11-30 20:52:27 +01001850 // mark cursor position as being invalid
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001851 curwin->w_valid = 0;
1852
Bram Moolenaar9cea87c2018-09-21 16:59:45 +02001853 buflist_setfpos(curbuf, curwin, curbuf->b_last_cursor.lnum,
1854 curbuf->b_last_cursor.col, TRUE);
1855
Bram Moolenaarc667da52019-11-30 20:52:27 +01001856 // Make sure the buffer is loaded.
1857 if (curbuf->b_ml.ml_mfp == NULL) // need to load the file
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001858 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001859 // If there is no filetype, allow for detecting one. Esp. useful for
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001860 // ":ball" used in an autocommand. If there already is a filetype we
Bram Moolenaarc667da52019-11-30 20:52:27 +01001861 // might prefer to keep it.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001862 if (*curbuf->b_p_ft == NUL)
1863 did_filetype = FALSE;
1864
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001865 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 else
1868 {
Bram Moolenaareda65222019-05-16 20:29:44 +02001869 if (!msg_silent && !shortmess(SHM_FILEINFO))
1870 need_fileinfo = TRUE; // display file info after redraw
1871
1872 // check if file changed
1873 (void)buf_check_timestamp(curbuf, FALSE);
1874
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001876#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1880 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 }
1882
Bram Moolenaarc667da52019-11-30 20:52:27 +01001883 // If autocommands did not change the cursor position, restore cursor lnum
1884 // and possibly cursor col.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 if (curwin->w_cursor.lnum == 1 && inindent(0))
1886 buflist_getfpos();
1887
Bram Moolenaarc667da52019-11-30 20:52:27 +01001888 check_arg_idx(curwin); // check for valid arg_idx
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 maketitle();
Bram Moolenaarc667da52019-11-30 20:52:27 +01001890 // when autocmds didn't change it
Bram Moolenaard4153d42008-11-15 15:06:17 +00001891 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001892 scroll_cursor_halfway(FALSE); // redisplay at correct position
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893
Bram Moolenaar009b2592004-10-24 19:18:58 +00001894#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarc667da52019-11-30 20:52:27 +01001895 // Send fileOpened event because we've changed buffers.
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001896 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001897#endif
1898
Bram Moolenaarc667da52019-11-30 20:52:27 +01001899 // Change directories when the 'acd' option is set.
Bram Moolenaar6f470022018-04-10 18:47:20 +02001900 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901
1902#ifdef FEAT_KEYMAP
1903 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001904 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001906#ifdef FEAT_SPELL
Bram Moolenaarc667da52019-11-30 20:52:27 +01001907 // May need to set the spell language. Can only do this after the buffer
1908 // has been properly setup.
Bram Moolenaar860cae12010-06-05 23:22:07 +02001909 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1910 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001911#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001912#ifdef FEAT_VIMINFO
1913 curbuf->b_last_used = vim_time();
1914#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001915
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 redraw_later(NOT_VALID);
1917}
1918
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001919#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1920/*
1921 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001922 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001923 */
1924 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001925do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001926{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001927 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001928 && curbuf->b_ffname != NULL
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001929 && vim_chdirfile(curbuf->b_ffname, "auto") == OK)
Bram Moolenaar05268152021-11-18 18:53:45 +00001930 {
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001931 shorten_fnames(TRUE);
Bram Moolenaar05268152021-11-18 18:53:45 +00001932 last_chdir_reason = "autochdir";
1933 }
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001934}
1935#endif
1936
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001937 void
1938no_write_message(void)
1939{
1940#ifdef FEAT_TERMINAL
1941 if (term_job_running(curbuf->b_term))
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001942 emsg(_(e_job_still_running_add_bang_to_end_the_job));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001943 else
1944#endif
Bram Moolenaare29a27f2021-07-20 21:07:36 +02001945 emsg(_(e_no_write_since_last_change_add_bang_to_override));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001946}
1947
1948 void
Bram Moolenaar7a760922018-02-19 23:10:02 +01001949no_write_message_nobang(buf_T *buf UNUSED)
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001950{
1951#ifdef FEAT_TERMINAL
Bram Moolenaar7a760922018-02-19 23:10:02 +01001952 if (term_job_running(buf->b_term))
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001953 emsg(_(e_job_still_running));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001954 else
1955#endif
Bram Moolenaare29a27f2021-07-20 21:07:36 +02001956 emsg(_(e_no_write_since_last_change));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001957}
1958
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959/*
1960 * functions for dealing with the buffer list
1961 */
1962
1963/*
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001964 * Return TRUE if the current buffer is empty, unnamed, unmodified and used in
1965 * only one window. That means it can be re-used.
1966 */
1967 int
1968curbuf_reusable(void)
1969{
1970 return (curbuf != NULL
1971 && curbuf->b_ffname == NULL
1972 && curbuf->b_nwindows <= 1
1973 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY())
Bram Moolenaard85c3962019-04-07 14:19:14 +02001974#if defined(FEAT_QUICKFIX)
Bram Moolenaar39803d82019-04-07 12:04:51 +02001975 && !bt_quickfix(curbuf)
Bram Moolenaard85c3962019-04-07 14:19:14 +02001976#endif
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001977 && !curbufIsChanged());
1978}
1979
1980/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 * Add a file name to the buffer list. Return a pointer to the buffer.
1982 * If the same file name already exists return a pointer to that buffer.
1983 * If it does not exist, or if fname == NULL, a new entry is created.
1984 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1985 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1986 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001987 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001988 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
1989 * if the buffer already exists.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001990 * If (flags & BLN_REUSE) is TRUE, may use buffer number from "buf_reuse".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 * This is the ONLY way to create a new buffer.
1992 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001994buflist_new(
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001995 char_u *ffname_arg, // full path of fname or relative
1996 char_u *sfname_arg, // short fname or NULL
1997 linenr_T lnum, // preferred cursor line
1998 int flags) // BLN_ defines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002000 char_u *ffname = ffname_arg;
2001 char_u *sfname = sfname_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 buf_T *buf;
2003#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002004 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005#endif
2006
Bram Moolenaar480778b2016-07-14 22:09:39 +02002007 if (top_file_num == 1)
2008 hash_init(&buf_hashtab);
2009
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002010 fname_expand(curbuf, &ffname, &sfname); // will allocate ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011
2012 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002013 * If the file name already exists in the list, update the entry.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 */
2015#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002016 // On Unix we can use inode numbers when the file exists. Works better
2017 // for hard links.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
2019 st.st_dev = (dev_T)-1;
2020#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02002021 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022#ifdef UNIX
2023 buflist_findname_stat(ffname, &st)
2024#else
2025 buflist_findname(ffname)
2026#endif
2027 ) != NULL)
2028 {
2029 vim_free(ffname);
2030 if (lnum != 0)
Bram Moolenaar89b693e2020-10-25 17:09:50 +01002031 buflist_setfpos(buf, (flags & BLN_NOCURWIN) ? NULL : curwin,
2032 lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02002033
2034 if ((flags & BLN_NOOPT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002035 // copy the options now, if 'cpo' doesn't have 's' and not done
2036 // already
Bram Moolenaar82404332016-07-10 17:00:38 +02002037 buf_copy_options(buf, 0);
2038
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 if ((flags & BLN_LISTED) && !buf->b_p_bl)
2040 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002041 bufref_T bufref;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002042
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 buf->b_p_bl = TRUE;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002044 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002046 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002047 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002048 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002049 return NULL;
2050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 }
2052 return buf;
2053 }
2054
2055 /*
2056 * If the current buffer has no name and no contents, use the current
2057 * buffer. Otherwise: Need to allocate a new buffer structure.
2058 *
2059 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00002060 * (A spell file buffer is allocated in spell.c, but that's not a normal
2061 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 */
2063 buf = NULL;
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002064 if ((flags & BLN_CURBUF) && curbuf_reusable())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 {
2066 buf = curbuf;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002067 // It's like this buffer is deleted. Watch out for autocommands that
2068 // change curbuf! If that happens, allocate a new buffer anyway.
Charlie Grovesfef44852022-04-19 16:24:12 +01002069 buf_freeall(buf, BFA_WIPE | BFA_DEL);
2070 if (buf != curbuf) // autocommands deleted the buffer!
2071 return NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002072#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002073 if (aborting()) // autocmds may abort script processing
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002074 {
2075 vim_free(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 return NULL;
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 }
2080 if (buf != curbuf || curbuf == NULL)
2081 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002082 buf = ALLOC_CLEAR_ONE(buf_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 if (buf == NULL)
2084 {
2085 vim_free(ffname);
2086 return NULL;
2087 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02002088#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002089 // init b: variables
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +01002090 buf->b_vars = dict_alloc_id(aid_newbuf_bvars);
Bram Moolenaar429fa852013-04-15 12:27:36 +02002091 if (buf->b_vars == NULL)
2092 {
2093 vim_free(ffname);
2094 vim_free(buf);
2095 return NULL;
2096 }
2097 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
2098#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01002099 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 }
2101
2102 if (ffname != NULL)
2103 {
2104 buf->b_ffname = ffname;
2105 buf->b_sfname = vim_strsave(sfname);
2106 }
2107
2108 clear_wininfo(buf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002109 buf->b_wininfo = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110
2111 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
2112 || buf->b_wininfo == NULL)
2113 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002114 if (buf->b_sfname != buf->b_ffname)
2115 VIM_CLEAR(buf->b_sfname);
2116 else
2117 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01002118 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 if (buf != curbuf)
2120 free_buffer(buf);
2121 return NULL;
2122 }
2123
2124 if (buf == curbuf)
2125 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002126 free_buffer_stuff(buf, FALSE); // delete local variables et al.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002127
Bram Moolenaarc667da52019-11-30 20:52:27 +01002128 // Init the options.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002129 buf->b_p_initialized = FALSE;
2130 buf_copy_options(buf, BCO_ENTER);
2131
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132#ifdef FEAT_KEYMAP
Bram Moolenaarc667da52019-11-30 20:52:27 +01002133 // need to reload lmaps and set b:keymap_name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 curbuf->b_kmap_state |= KEYMAP_INIT;
2135#endif
2136 }
2137 else
2138 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002139 // put the new buffer at the end of the buffer list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 buf->b_next = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002141 if (firstbuf == NULL) // buffer list is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 {
2143 buf->b_prev = NULL;
2144 firstbuf = buf;
2145 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002146 else // append new buffer at end of list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 {
2148 lastbuf->b_next = buf;
2149 buf->b_prev = lastbuf;
2150 }
2151 lastbuf = buf;
2152
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002153 if ((flags & BLN_REUSE) && buf_reuse.ga_len > 0)
2154 {
2155 // Recycle a previously used buffer number. Used for buffers which
2156 // are normally hidden, e.g. in a popup window. Avoids that the
2157 // buffer number grows rapidly.
2158 --buf_reuse.ga_len;
2159 buf->b_fnum = ((int *)buf_reuse.ga_data)[buf_reuse.ga_len];
Bram Moolenaar99ebf222019-12-10 23:44:48 +01002160
2161 // Move buffer to the right place in the buffer list.
2162 while (buf->b_prev != NULL && buf->b_fnum < buf->b_prev->b_fnum)
2163 {
2164 buf_T *prev = buf->b_prev;
2165
2166 prev->b_next = buf->b_next;
2167 if (prev->b_next != NULL)
2168 prev->b_next->b_prev = prev;
2169 buf->b_next = prev;
2170 buf->b_prev = prev->b_prev;
2171 if (buf->b_prev != NULL)
2172 buf->b_prev->b_next = buf;
2173 prev->b_prev = buf;
2174 if (lastbuf == buf)
2175 lastbuf = prev;
2176 if (firstbuf == prev)
2177 firstbuf = buf;
2178 }
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002179 }
2180 else
2181 buf->b_fnum = top_file_num++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002182 if (top_file_num < 0) // wrap around (may cause duplicates)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002184 emsg(_("W14: Warning: List of file names overflow"));
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002185 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 {
2187 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002188 ui_delay(3001L, TRUE); // make sure it is noticed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 }
2190 top_file_num = 1;
2191 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002192 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002194 // Always copy the options from the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 buf_copy_options(buf, BCO_ALWAYS);
2196 }
2197
2198 buf->b_wininfo->wi_fpos.lnum = lnum;
2199 buf->b_wininfo->wi_win = curwin;
2200
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002201#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002202 hash_init(&buf->b_s.b_keywtab);
2203 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204#endif
2205
2206 buf->b_fname = buf->b_sfname;
2207#ifdef UNIX
2208 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002209 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 else
2211 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002212 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 buf->b_dev = st.st_dev;
2214 buf->b_ino = st.st_ino;
2215 }
2216#endif
2217 buf->b_u_synced = TRUE;
2218 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002219 if (flags & BLN_DUMMY)
2220 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 buf_clear_file(buf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002222 clrallmarks(buf); // clear marks
2223 fmarks_check_names(buf); // check file marks for this file
2224 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; // init 'buflisted'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 if (!(flags & BLN_DUMMY))
2226 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002227 bufref_T bufref;
2228
Bram Moolenaarc667da52019-11-30 20:52:27 +01002229 // Tricky: these autocommands may change the buffer list. They could
2230 // also split the window with re-using the one empty buffer. This may
2231 // result in unexpectedly losing the empty buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002232 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002233 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002234 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002235 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002237 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002238 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002239 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002240 return NULL;
2241 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002242#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002243 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247
2248 return buf;
2249}
2250
2251/*
2252 * Free the memory for the options of a buffer.
2253 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2254 * 'fileencoding'.
2255 */
2256 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002257free_buf_options(
2258 buf_T *buf,
2259 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260{
2261 if (free_p_ff)
2262 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 clear_string_option(&buf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 clear_string_option(&buf->b_p_bh);
2266 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 }
2268#ifdef FEAT_FIND_ID
2269 clear_string_option(&buf->b_p_def);
2270 clear_string_option(&buf->b_p_inc);
2271# ifdef FEAT_EVAL
2272 clear_string_option(&buf->b_p_inex);
2273# endif
2274#endif
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002275#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 clear_string_option(&buf->b_p_inde);
2277 clear_string_option(&buf->b_p_indk);
2278#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002279#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2280 clear_string_option(&buf->b_p_bexpr);
2281#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002282#if defined(FEAT_CRYPT)
2283 clear_string_option(&buf->b_p_cm);
2284#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002285 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002286#if defined(FEAT_EVAL)
2287 clear_string_option(&buf->b_p_fex);
2288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289#ifdef FEAT_CRYPT
Bram Moolenaar131530a2021-07-29 20:37:49 +02002290# ifdef FEAT_SODIUM
K.Takata1a8825d2022-01-19 13:32:57 +00002291 if ((buf->b_p_key != NULL) && (*buf->b_p_key != NUL) &&
2292 (crypt_get_method_nr(buf) == CRYPT_M_SOD))
2293 crypt_sodium_munlock(buf->b_p_key, STRLEN(buf->b_p_key));
Bram Moolenaar131530a2021-07-29 20:37:49 +02002294# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 clear_string_option(&buf->b_p_key);
2296#endif
2297 clear_string_option(&buf->b_p_kp);
2298 clear_string_option(&buf->b_p_mps);
2299 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002300 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 clear_string_option(&buf->b_p_isk);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002302#ifdef FEAT_VARTABS
2303 clear_string_option(&buf->b_p_vsts);
Bram Moolenaarbdace832019-03-02 10:13:42 +01002304 vim_free(buf->b_p_vsts_nopaste);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002305 buf->b_p_vsts_nopaste = NULL;
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00002306 VIM_CLEAR(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002307 clear_string_option(&buf->b_p_vts);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002308 VIM_CLEAR(buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002309#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310#ifdef FEAT_KEYMAP
2311 clear_string_option(&buf->b_p_keymap);
Bram Moolenaar50138322018-01-28 17:05:16 +01002312 keymap_clear(&buf->b_kmap_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 ga_clear(&buf->b_kmap_ga);
2314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 clear_string_option(&buf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316#ifdef FEAT_FOLDING
2317 clear_string_option(&buf->b_p_cms);
2318#endif
2319 clear_string_option(&buf->b_p_nf);
2320#ifdef FEAT_SYN_HL
2321 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002322 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002323#endif
2324#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002325 clear_string_option(&buf->b_s.b_p_spc);
2326 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002327 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002328 buf->b_s.b_cap_prog = NULL;
2329 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02002330 clear_string_option(&buf->b_s.b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331#endif
2332#ifdef FEAT_SEARCHPATH
2333 clear_string_option(&buf->b_p_sua);
2334#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 clear_string_option(&buf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 clear_string_option(&buf->b_p_cink);
2337 clear_string_option(&buf->b_p_cino);
Tom Praschan3506cf32022-04-07 12:39:08 +01002338 clear_string_option(&buf->b_p_cinsd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 clear_string_option(&buf->b_p_cinw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 clear_string_option(&buf->b_p_cpt);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002341#ifdef FEAT_COMPL_FUNC
2342 clear_string_option(&buf->b_p_cfu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002343 free_callback(&buf->b_cfu_cb);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002344 clear_string_option(&buf->b_p_ofu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002345 free_callback(&buf->b_ofu_cb);
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +01002346 clear_string_option(&buf->b_p_tsrfu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002347 free_callback(&buf->b_tsrfu_cb);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349#ifdef FEAT_QUICKFIX
2350 clear_string_option(&buf->b_p_gp);
2351 clear_string_option(&buf->b_p_mp);
2352 clear_string_option(&buf->b_p_efm);
2353#endif
2354 clear_string_option(&buf->b_p_ep);
2355 clear_string_option(&buf->b_p_path);
2356 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002357 clear_string_option(&buf->b_p_tc);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002358#ifdef FEAT_EVAL
2359 clear_string_option(&buf->b_p_tfu);
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +00002360 free_callback(&buf->b_tfu_cb);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 clear_string_option(&buf->b_p_dict);
2363 clear_string_option(&buf->b_p_tsr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002364#ifdef FEAT_TEXTOBJ
2365 clear_string_option(&buf->b_p_qe);
2366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002368 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002369 clear_string_option(&buf->b_p_lw);
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002370 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002371 clear_string_option(&buf->b_p_menc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372}
2373
2374/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002375 * Get alternate file "n".
2376 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2377 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 * if (options & GETF_SETMARK) call setpcmark()
2379 * if (options & GETF_ALT) we are jumping to an alternate file.
2380 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2381 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002382 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 */
2384 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002385buflist_getfile(
2386 int n,
2387 linenr_T lnum,
2388 int options,
2389 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390{
2391 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 pos_T *fpos;
2394 colnr_T col;
2395
2396 buf = buflist_findnr(n);
2397 if (buf == NULL)
2398 {
2399 if ((options & GETF_ALT) && n == 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02002400 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 else
Bram Moolenaare1242042021-12-16 20:56:57 +00002402 semsg(_(e_buffer_nr_not_found), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 return FAIL;
2404 }
2405
Bram Moolenaarc667da52019-11-30 20:52:27 +01002406 // if alternate file is the current buffer, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 if (buf == curbuf)
2408 return OK;
2409
Bram Moolenaar71223e22022-05-30 15:23:09 +01002410 if (text_or_buf_locked())
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002411 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412
Bram Moolenaarc667da52019-11-30 20:52:27 +01002413 // altfpos may be changed by getfile(), get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 if (lnum == 0)
2415 {
2416 fpos = buflist_findfpos(buf);
2417 lnum = fpos->lnum;
2418 col = fpos->col;
2419 }
2420 else
2421 col = 0;
2422
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 if (options & GETF_SWITCH)
2424 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002425 // If 'switchbuf' contains "useopen": jump to first window containing
2426 // "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00002427 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002429
Bram Moolenaarc667da52019-11-30 20:52:27 +01002430 // If 'switchbuf' contains "usetab": jump to first window in any tab
2431 // page containing "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00002432 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002433 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002434
Bram Moolenaarc667da52019-11-30 20:52:27 +01002435 // If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2436 // current buffer isn't empty: open new tab or window
Bram Moolenaara594d772015-06-19 14:41:49 +02002437 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002438 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002440 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002441 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002442 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2443 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002445 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 }
2447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448
2449 ++RedrawingDisabled;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002450 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2451 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 {
2453 --RedrawingDisabled;
2454
Bram Moolenaarc667da52019-11-30 20:52:27 +01002455 // cursor is at to BOL and w_cursor.lnum is checked due to getfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 if (!p_sol && col != 0)
2457 {
2458 curwin->w_cursor.col = col;
2459 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 curwin->w_set_curswant = TRUE;
2462 }
2463 return OK;
2464 }
2465 --RedrawingDisabled;
2466 return FAIL;
2467}
2468
2469/*
2470 * go to the last know line number for the current buffer
2471 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002473buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474{
2475 pos_T *fpos;
2476
2477 fpos = buflist_findfpos(curbuf);
2478
2479 curwin->w_cursor.lnum = fpos->lnum;
2480 check_cursor_lnum();
2481
2482 if (p_sol)
2483 curwin->w_cursor.col = 0;
2484 else
2485 {
2486 curwin->w_cursor.col = fpos->col;
2487 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 curwin->w_set_curswant = TRUE;
2490 }
2491}
2492
Bram Moolenaar81695252004-12-29 20:58:21 +00002493#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2494/*
2495 * Find file in buffer list by name (it has to be for the current window).
2496 * Returns NULL if not found.
2497 */
2498 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002499buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002500{
2501 char_u *ffname;
2502 buf_T *buf = NULL;
2503
Bram Moolenaarc667da52019-11-30 20:52:27 +01002504 // First make the name into a full path name
Bram Moolenaar81695252004-12-29 20:58:21 +00002505 ffname = FullName_save(fname,
2506#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002507 TRUE // force expansion, get rid of symbolic links
Bram Moolenaar81695252004-12-29 20:58:21 +00002508#else
2509 FALSE
2510#endif
2511 );
2512 if (ffname != NULL)
2513 {
2514 buf = buflist_findname(ffname);
2515 vim_free(ffname);
2516 }
2517 return buf;
2518}
2519#endif
2520
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521/*
2522 * Find file in buffer list by name (it has to be for the current window).
2523 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002524 * Skips dummy buffers.
2525 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 */
2527 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002528buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529{
2530#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002531 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532
2533 if (mch_stat((char *)ffname, &st) < 0)
2534 st.st_dev = (dev_T)-1;
2535 return buflist_findname_stat(ffname, &st);
2536}
2537
2538/*
2539 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2540 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002541 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 */
2543 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002544buflist_findname_stat(
2545 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002546 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547{
2548#endif
2549 buf_T *buf;
2550
Bram Moolenaarc667da52019-11-30 20:52:27 +01002551 // Start at the last buffer, expect to find a match sooner.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002552 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaar81695252004-12-29 20:58:21 +00002553 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554#ifdef UNIX
2555 , stp
2556#endif
2557 ))
2558 return buf;
2559 return NULL;
2560}
2561
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562/*
2563 * Find file in buffer list by a regexp pattern.
2564 * Return fnum of the found buffer.
2565 * Return < 0 for error.
2566 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002568buflist_findpat(
2569 char_u *pattern,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002570 char_u *pattern_end, // pointer to first char after pattern
2571 int unlisted, // find unlisted buffers
2572 int diffmode UNUSED, // find diff-mode buffers only
2573 int curtab_only) // find buffers in current tab only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574{
2575 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 int match = -1;
2577 int find_listed;
2578 char_u *pat;
2579 char_u *patend;
2580 int attempt;
2581 char_u *p;
2582 int toggledollar;
2583
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002584 // "%" is current file, "%%" or "#" is alternate file
2585 if ((pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2586 || (in_vim9script() && pattern_end == pattern + 2
2587 && pattern[0] == '%' && pattern[1] == '%'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 {
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002589 if (*pattern == '#' || pattern_end == pattern + 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 match = curwin->w_alt_fnum;
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002591 else
2592 match = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593#ifdef FEAT_DIFF
2594 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2595 match = -1;
2596#endif
2597 }
2598
2599 /*
2600 * Try four ways of matching a listed buffer:
2601 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002602 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 * attempt == 2: with '$' at end (only match at end)
2604 * attempt == 3: with '^' at start and '$' at end (only full match)
2605 * Repeat this for finding an unlisted buffer if there was no matching
2606 * listed buffer.
2607 */
2608 else
2609 {
2610 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2611 if (pat == NULL)
2612 return -1;
2613 patend = pat + STRLEN(pat) - 1;
2614 toggledollar = (patend > pat && *patend == '$');
2615
Bram Moolenaarc667da52019-11-30 20:52:27 +01002616 // First try finding a listed buffer. If not found and "unlisted"
2617 // is TRUE, try finding an unlisted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 find_listed = TRUE;
2619 for (;;)
2620 {
2621 for (attempt = 0; attempt <= 3; ++attempt)
2622 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002623 regmatch_T regmatch;
2624
Bram Moolenaarc667da52019-11-30 20:52:27 +01002625 // may add '^' and '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 if (toggledollar)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002627 *patend = (attempt < 2) ? NUL : '$'; // add/remove '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 p = pat;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002629 if (*p == '^' && !(attempt & 1)) // add/remove '^'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 ++p;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01002631 regmatch.regprog = vim_regcomp(p, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002633 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002634 {
2635 if (regmatch.regprog == NULL)
2636 {
2637 // invalid pattern, possibly after switching engine
2638 vim_free(pat);
2639 return -1;
2640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 if (buf->b_p_bl == find_listed
2642#ifdef FEAT_DIFF
2643 && (!diffmode || diff_mode_buf(buf))
2644#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002645 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002647 if (curtab_only)
2648 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002649 // Ignore the match if the buffer is not open in
2650 // the current tab.
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002651 win_T *wp;
2652
Bram Moolenaar29323592016-07-24 22:04:11 +02002653 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002654 if (wp->w_buffer == buf)
2655 break;
2656 if (wp == NULL)
2657 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002658 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002659 if (match >= 0) // already found a match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 {
2661 match = -2;
2662 break;
2663 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002664 match = buf->b_fnum; // remember first match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 }
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002668 vim_regfree(regmatch.regprog);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002669 if (match >= 0) // found one match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 break;
2671 }
2672
Bram Moolenaarc667da52019-11-30 20:52:27 +01002673 // Only search for unlisted buffers if there was no match with
2674 // a listed buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 if (!unlisted || !find_listed || match != -1)
2676 break;
2677 find_listed = FALSE;
2678 }
2679
2680 vim_free(pat);
2681 }
2682
2683 if (match == -2)
Bram Moolenaare1242042021-12-16 20:56:57 +00002684 semsg(_(e_more_than_one_match_for_str), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 else if (match < 0)
Bram Moolenaare1242042021-12-16 20:56:57 +00002686 semsg(_(e_no_matching_buffer_for_str), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 return match;
2688}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689
Bram Moolenaar52410572019-10-27 05:12:45 +01002690#ifdef FEAT_VIMINFO
2691typedef struct {
2692 buf_T *buf;
2693 char_u *match;
2694} bufmatch_T;
2695#endif
2696
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697/*
2698 * Find all buffer names that match.
2699 * For command line expansion of ":buf" and ":sbuf".
2700 * Return OK if matches found, FAIL otherwise.
2701 */
2702 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002703ExpandBufnames(
2704 char_u *pat,
2705 int *num_file,
2706 char_u ***file,
2707 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708{
2709 int count = 0;
2710 buf_T *buf;
2711 int round;
2712 char_u *p;
2713 int attempt;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002714 char_u *patc = NULL;
Bram Moolenaar52410572019-10-27 05:12:45 +01002715#ifdef FEAT_VIMINFO
2716 bufmatch_T *matches = NULL;
2717#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002718 int fuzzy;
2719 fuzmatch_str_T *fuzmatch = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720
Bram Moolenaarc667da52019-11-30 20:52:27 +01002721 *num_file = 0; // return values in case of FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 *file = NULL;
2723
Bram Moolenaarefcc3292019-12-30 21:59:03 +01002724#ifdef FEAT_DIFF
2725 if ((options & BUF_DIFF_FILTER) && !curwin->w_p_diff)
2726 return FAIL;
2727#endif
2728
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002729 fuzzy = cmdline_fuzzy_complete(pat);
2730
2731 // Make a copy of "pat" and change "^" to "\(^\|[\/]\)" (if doing regular
2732 // expression matching)
2733 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002735 if (*pat == '^')
2736 {
2737 patc = alloc(STRLEN(pat) + 11);
2738 if (patc == NULL)
2739 return FAIL;
2740 STRCPY(patc, "\\(^\\|[\\/]\\)");
2741 STRCPY(patc + 11, pat + 1);
2742 }
2743 else
2744 patc = pat;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002745 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00002746
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002747 // attempt == 0: try match with '\<', match at start of word
2748 // attempt == 1: try match without '\<', match anywhere
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002749 for (attempt = 0; attempt <= (fuzzy ? 0 : 1); ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002750 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002751 regmatch_T regmatch;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002752 int score = 0;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002753
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002754 if (!fuzzy)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002755 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002756 if (attempt > 0 && patc == pat)
2757 break; // there was no anchor, no need to try again
2758 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002761 // round == 1: Count the matches.
2762 // round == 2: Build the array to keep the matches.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 for (round = 1; round <= 2; ++round)
2764 {
2765 count = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002766 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002768 if (!buf->b_p_bl) // skip unlisted buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 continue;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002770#ifdef FEAT_DIFF
2771 if (options & BUF_DIFF_FILTER)
2772 // Skip buffers not suitable for
2773 // :diffget or :diffput completion.
Bram Moolenaarefcc3292019-12-30 21:59:03 +01002774 if (buf == curbuf || !diff_mode_buf(buf))
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002775 continue;
2776#endif
2777
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002778 if (!fuzzy)
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002779 {
2780 if (regmatch.regprog == NULL)
2781 {
2782 // invalid pattern, possibly after recompiling
2783 if (patc != pat)
2784 vim_free(patc);
2785 return FAIL;
2786 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002787 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002788 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002789 else
2790 {
2791 p = NULL;
2792 // first try matching with the short file name
2793 if ((score = fuzzy_match_str(buf->b_sfname, pat)) != 0)
2794 p = buf->b_sfname;
2795 if (p == NULL)
2796 {
2797 // next try matching with the full path file name
2798 if ((score = fuzzy_match_str(buf->b_ffname, pat)) != 0)
2799 p = buf->b_ffname;
2800 }
2801 }
2802
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002803 if (p == NULL)
2804 continue;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002805
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002806 if (round == 1)
2807 {
2808 ++count;
2809 continue;
2810 }
2811
2812 if (options & WILD_HOME_REPLACE)
2813 p = home_replace_save(buf, p);
2814 else
2815 p = vim_strsave(p);
2816
2817 if (!fuzzy)
2818 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002819#ifdef FEAT_VIMINFO
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002820 if (matches != NULL)
2821 {
2822 matches[count].buf = buf;
2823 matches[count].match = p;
2824 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 }
Yegappan Lakshmanan5de4c432022-02-28 13:28:38 +00002826 else
2827#endif
2828 (*file)[count++] = p;
2829 }
2830 else
2831 {
2832 fuzmatch[count].idx = count;
2833 fuzmatch[count].str = p;
2834 fuzmatch[count].score = score;
2835 count++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 }
2837 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002838 if (count == 0) // no match found, break here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 break;
2840 if (round == 1)
2841 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002842 if (!fuzzy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002844 *file = ALLOC_MULT(char_u *, count);
2845 if (*file == NULL)
2846 {
2847 vim_regfree(regmatch.regprog);
2848 if (patc != pat)
2849 vim_free(patc);
2850 return FAIL;
2851 }
Bram Moolenaar52410572019-10-27 05:12:45 +01002852#ifdef FEAT_VIMINFO
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002853 if (options & WILD_BUFLASTUSED)
2854 matches = ALLOC_MULT(bufmatch_T, count);
Bram Moolenaar52410572019-10-27 05:12:45 +01002855#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002856 }
2857 else
2858 {
2859 fuzmatch = ALLOC_MULT(fuzmatch_str_T, count);
2860 if (fuzmatch == NULL)
2861 {
2862 *num_file = 0;
2863 *file = NULL;
2864 return FAIL;
2865 }
2866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 }
2868 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002869
2870 if (!fuzzy)
2871 {
2872 vim_regfree(regmatch.regprog);
2873 if (count) // match(es) found, break here
2874 break;
2875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 }
2877
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002878 if (!fuzzy && patc != pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002879 vim_free(patc);
2880
Bram Moolenaar52410572019-10-27 05:12:45 +01002881#ifdef FEAT_VIMINFO
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002882 if (!fuzzy)
Bram Moolenaar52410572019-10-27 05:12:45 +01002883 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002884 if (matches != NULL)
Bram Moolenaar52410572019-10-27 05:12:45 +01002885 {
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002886 int i;
2887 if (count > 1)
2888 qsort(matches, count, sizeof(bufmatch_T), buf_compare);
2889 // if the current buffer is first in the list, place it at the end
2890 if (matches[0].buf == curbuf)
2891 {
2892 for (i = 1; i < count; i++)
2893 (*file)[i-1] = matches[i].match;
2894 (*file)[count-1] = matches[0].match;
2895 }
2896 else
2897 {
2898 for (i = 0; i < count; i++)
2899 (*file)[i] = matches[i].match;
2900 }
2901 vim_free(matches);
Bram Moolenaar52410572019-10-27 05:12:45 +01002902 }
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00002903 }
2904 else
2905 {
2906 if (fuzzymatches_to_strmatches(fuzmatch, file, count, FALSE) == FAIL)
2907 return FAIL;
Bram Moolenaar52410572019-10-27 05:12:45 +01002908 }
2909#endif
2910
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 *num_file = count;
2912 return (count == 0 ? FAIL : OK);
2913}
2914
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915/*
2916 * Check for a match on the file name for buffer "buf" with regprog "prog".
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002917 * Note that rmp->regprog may become NULL when switching regexp engine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 */
2919 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002920buflist_match(
2921 regmatch_T *rmp,
2922 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002923 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924{
2925 char_u *match;
2926
Bram Moolenaarc667da52019-11-30 20:52:27 +01002927 // First try the short file name, then the long file name.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002928 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaara59f2df2022-05-11 11:42:28 +01002929 if (match == NULL && rmp->regprog != NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002930 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931
2932 return match;
2933}
2934
2935/*
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002936 * Try matching the regexp in "rmp->regprog" with file name "name".
2937 * Note that rmp->regprog may become NULL when switching regexp engine.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 * Return "name" when there is a match, NULL when not.
2939 */
2940 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002941fname_match(
2942 regmatch_T *rmp,
2943 char_u *name,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002944 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945{
2946 char_u *match = NULL;
2947 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948
Bram Moolenaarb62dc5e2022-05-15 14:50:12 +01002949 // extra check for valid arguments
2950 if (name != NULL && rmp->regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002952 // Ignore case when 'fileignorecase' or the argument is set.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002953 rmp->rm_ic = p_fic || ignore_case;
2954 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 match = name;
Bram Moolenaar8e4b76d2022-05-07 11:28:06 +01002956 else if (rmp->regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002958 // Replace $(HOME) with '~' and try matching again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002960 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 match = name;
2962 vim_free(p);
2963 }
2964 }
2965
2966 return match;
2967}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968
2969/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02002970 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 */
2972 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002973buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974{
Bram Moolenaar480778b2016-07-14 22:09:39 +02002975 char_u key[VIM_SIZEOF_INT * 2 + 1];
2976 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977
2978 if (nr == 0)
2979 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02002980 sprintf((char *)key, "%x", nr);
2981 hi = hash_find(&buf_hashtab, key);
2982
2983 if (!HASHITEM_EMPTY(hi))
2984 return (buf_T *)(hi->hi_key
2985 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 return NULL;
2987}
2988
2989/*
2990 * Get name of file 'n' in the buffer list.
2991 * When the file has no name an empty string is returned.
2992 * home_replace() is used to shorten the file name (used for marks).
2993 * Returns a pointer to allocated memory, of NULL when failed.
2994 */
2995 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002996buflist_nr2name(
2997 int n,
2998 int fullname,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002999 int helptail) // for help buffers return tail only
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000{
3001 buf_T *buf;
3002
3003 buf = buflist_findnr(n);
3004 if (buf == NULL)
3005 return NULL;
3006 return home_replace_save(helptail ? buf : NULL,
3007 fullname ? buf->b_ffname : buf->b_fname);
3008}
3009
3010/*
3011 * Set the "lnum" and "col" for the buffer "buf" and the current window.
3012 * When "copy_options" is TRUE save the local window option values.
3013 * When "lnum" is 0 only do the options.
3014 */
Bram Moolenaardefa0672019-07-21 19:25:37 +02003015 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003016buflist_setfpos(
3017 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003018 win_T *win, // may be NULL when using :badd
Bram Moolenaar7454a062016-01-30 15:14:10 +01003019 linenr_T lnum,
3020 colnr_T col,
3021 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022{
3023 wininfo_T *wip;
3024
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003025 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 if (wip->wi_win == win)
3027 break;
3028 if (wip == NULL)
3029 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003030 // allocate a new entry
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003031 wip = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 if (wip == NULL)
3033 return;
3034 wip->wi_win = win;
Bram Moolenaarc667da52019-11-30 20:52:27 +01003035 if (lnum == 0) // set lnum even when it's 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 lnum = 1;
3037 }
3038 else
3039 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003040 // remove the entry from the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 if (wip->wi_prev)
3042 wip->wi_prev->wi_next = wip->wi_next;
3043 else
3044 buf->b_wininfo = wip->wi_next;
3045 if (wip->wi_next)
3046 wip->wi_next->wi_prev = wip->wi_prev;
3047 if (copy_options && wip->wi_optset)
3048 {
3049 clear_winopt(&wip->wi_opt);
3050#ifdef FEAT_FOLDING
3051 deleteFoldRecurse(&wip->wi_folds);
3052#endif
3053 }
3054 }
3055 if (lnum != 0)
3056 {
3057 wip->wi_fpos.lnum = lnum;
3058 wip->wi_fpos.col = col;
3059 }
LemonBoydb0ea7f2022-04-10 17:59:26 +01003060 if (win != NULL)
3061 wip->wi_changelistidx = win->w_changelistidx;
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003062 if (copy_options && win != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003064 // Save the window-specific option values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
3066#ifdef FEAT_FOLDING
3067 wip->wi_fold_manual = win->w_fold_manual;
3068 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
3069#endif
3070 wip->wi_optset = TRUE;
3071 }
3072
Bram Moolenaarc667da52019-11-30 20:52:27 +01003073 // insert the entry in front of the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 wip->wi_next = buf->b_wininfo;
3075 buf->b_wininfo = wip;
3076 wip->wi_prev = NULL;
3077 if (wip->wi_next)
3078 wip->wi_next->wi_prev = wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079}
3080
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003081#ifdef FEAT_DIFF
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003082/*
3083 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
3084 * page. That's because a diff is local to a tab page.
3085 */
3086 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003087wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003088{
3089 win_T *wp;
3090
3091 if (wip->wi_opt.wo_diff)
3092 {
Bram Moolenaar29323592016-07-24 22:04:11 +02003093 FOR_ALL_WINDOWS(wp)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003094 // return FALSE when it's a window in the current tab page, thus
3095 // the buffer was in diff mode here
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003096 if (wip->wi_win == wp)
3097 return FALSE;
3098 return TRUE;
3099 }
3100 return FALSE;
3101}
3102#endif
3103
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104/*
3105 * Find info for the current window in buffer "buf".
3106 * If not found, return the info for the most recently used window.
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003107 * When "need_options" is TRUE skip entries where wi_optset is FALSE.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003108 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
3109 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 * Returns NULL when there isn't any info.
3111 */
3112 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003113find_wininfo(
3114 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003115 int need_options,
Bram Moolenaar7454a062016-01-30 15:14:10 +01003116 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117{
3118 wininfo_T *wip;
3119
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003120 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003121 if (wip->wi_win == curwin
3122#ifdef FEAT_DIFF
3123 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
3124#endif
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003125
3126 && (!need_options || wip->wi_optset))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003128
Bram Moolenaarc667da52019-11-30 20:52:27 +01003129 // If no wininfo for curwin, use the first in the list (that doesn't have
3130 // 'diff' set and is in another tab page).
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003131 // If "need_options" is TRUE skip entries that don't have options set,
3132 // unless the window is editing "buf", so we can copy from the window
3133 // itself.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003134 if (wip == NULL)
3135 {
3136#ifdef FEAT_DIFF
3137 if (skip_diff_buffer)
3138 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003139 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003140 if (!wininfo_other_tab_diff(wip)
3141 && (!need_options || wip->wi_optset
3142 || (wip->wi_win != NULL
3143 && wip->wi_win->w_buffer == buf)))
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003144 break;
3145 }
3146 else
3147#endif
3148 wip = buf->b_wininfo;
3149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 return wip;
3151}
3152
3153/*
3154 * Reset the local window options to the values last used in this window.
3155 * If the buffer wasn't used in this window before, use the values from
3156 * the most recently used window. If the values were never set, use the
3157 * global values for the window.
3158 */
3159 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003160get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161{
3162 wininfo_T *wip;
3163
3164 clear_winopt(&curwin->w_onebuf_opt);
3165#ifdef FEAT_FOLDING
3166 clearFolding(curwin);
3167#endif
3168
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003169 wip = find_wininfo(buf, TRUE, TRUE);
Bram Moolenaar25782a72018-05-13 18:05:33 +02003170 if (wip != NULL && wip->wi_win != NULL
3171 && wip->wi_win != curwin && wip->wi_win->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003173 // The buffer is currently displayed in the window: use the actual
3174 // option values instead of the saved (possibly outdated) values.
Bram Moolenaar25782a72018-05-13 18:05:33 +02003175 win_T *wp = wip->wi_win;
3176
3177 copy_winopt(&wp->w_onebuf_opt, &curwin->w_onebuf_opt);
3178#ifdef FEAT_FOLDING
3179 curwin->w_fold_manual = wp->w_fold_manual;
3180 curwin->w_foldinvalid = TRUE;
3181 cloneFoldGrowArray(&wp->w_folds, &curwin->w_folds);
3182#endif
3183 }
3184 else if (wip != NULL && wip->wi_optset)
3185 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003186 // the buffer was displayed in the current window earlier
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
3188#ifdef FEAT_FOLDING
3189 curwin->w_fold_manual = wip->wi_fold_manual;
3190 curwin->w_foldinvalid = TRUE;
3191 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
3192#endif
3193 }
3194 else
3195 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
LemonBoydb0ea7f2022-04-10 17:59:26 +01003196 if (wip != NULL)
3197 curwin->w_changelistidx = wip->wi_changelistidx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198
3199#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +01003200 // Set 'foldlevel' to 'foldlevelstart' if it's not negative.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 if (p_fdls >= 0)
3202 curwin->w_p_fdl = p_fdls;
3203#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02003204 after_copy_winopt(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205}
3206
3207/*
3208 * Find the position (lnum and col) for the buffer 'buf' for the current
3209 * window.
3210 * Returns a pointer to no_position if no position is found.
3211 */
3212 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003213buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214{
3215 wininfo_T *wip;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003216 static pos_T no_position = {1, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003218 wip = find_wininfo(buf, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 if (wip != NULL)
3220 return &(wip->wi_fpos);
3221 else
3222 return &no_position;
3223}
3224
3225/*
3226 * Find the lnum for the buffer 'buf' for the current window.
3227 */
3228 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01003229buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230{
3231 return buflist_findfpos(buf)->lnum;
3232}
3233
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02003235 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003238buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239{
Bram Moolenaar52410572019-10-27 05:12:45 +01003240 buf_T *buf = firstbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 int len;
3242 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003243 int ro_char;
3244 int changed_char;
Bram Moolenaar0751f512018-03-29 16:37:16 +02003245#ifdef FEAT_TERMINAL
3246 int job_running;
3247 int job_none_open;
3248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249
Bram Moolenaar52410572019-10-27 05:12:45 +01003250#ifdef FEAT_VIMINFO
3251 garray_T buflist;
3252 buf_T **buflist_data = NULL, **p;
3253
3254 if (vim_strchr(eap->arg, 't'))
3255 {
3256 ga_init2(&buflist, sizeof(buf_T *), 50);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003257 FOR_ALL_BUFFERS(buf)
Bram Moolenaar52410572019-10-27 05:12:45 +01003258 {
3259 if (ga_grow(&buflist, 1) == OK)
3260 ((buf_T **)buflist.ga_data)[buflist.ga_len++] = buf;
3261 }
3262
3263 qsort(buflist.ga_data, (size_t)buflist.ga_len,
3264 sizeof(buf_T *), buf_compare);
3265
Bram Moolenaar3b991522019-11-06 23:26:20 +01003266 buflist_data = (buf_T **)buflist.ga_data;
3267 buf = *buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003268 }
Bram Moolenaar3b991522019-11-06 23:26:20 +01003269 p = buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003270
Bram Moolenaar3b991522019-11-06 23:26:20 +01003271 for (; buf != NULL && !got_int; buf = buflist_data != NULL
Bram Moolenaar52410572019-10-27 05:12:45 +01003272 ? (++p < buflist_data + buflist.ga_len ? *p : NULL)
3273 : buf->b_next)
3274#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
Bram Moolenaar52410572019-10-27 05:12:45 +01003276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 {
Bram Moolenaar0751f512018-03-29 16:37:16 +02003278#ifdef FEAT_TERMINAL
3279 job_running = term_job_running(buf->b_term);
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003280 job_none_open = term_none_open(buf->b_term);
Bram Moolenaar0751f512018-03-29 16:37:16 +02003281#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003282 // skip unlisted buffers, unless ! was used
Bram Moolenaard51cb702015-07-21 15:03:06 +02003283 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
3284 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
3285 || (vim_strchr(eap->arg, '+')
3286 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
3287 || (vim_strchr(eap->arg, 'a')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003288 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
Bram Moolenaard51cb702015-07-21 15:03:06 +02003289 || (vim_strchr(eap->arg, 'h')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003290 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3291#ifdef FEAT_TERMINAL
3292 || (vim_strchr(eap->arg, 'R')
3293 && (!job_running || (job_running && job_none_open)))
3294 || (vim_strchr(eap->arg, '?')
3295 && (!job_running || (job_running && !job_none_open)))
3296 || (vim_strchr(eap->arg, 'F')
3297 && (job_running || buf->b_term == NULL))
3298#endif
Bram Moolenaard51cb702015-07-21 15:03:06 +02003299 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3300 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3301 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3302 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3303 || (vim_strchr(eap->arg, '#')
3304 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003307 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 else
3309 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003310 if (message_filtered(NameBuff))
3311 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003313 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3314 : (bufIsChanged(buf) ? '+' : ' ');
3315#ifdef FEAT_TERMINAL
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003316 if (job_running)
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003317 {
Bram Moolenaar9e636b92022-05-29 22:37:05 +01003318 if (job_none_open)
Bram Moolenaar4033c552017-09-16 20:54:51 +02003319 ro_char = '?';
3320 else
3321 ro_char = 'R';
Bram Moolenaarc667da52019-11-30 20:52:27 +01003322 changed_char = ' '; // bufIsChanged() returns TRUE to avoid
3323 // closing, but it's not actually changed.
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003324 }
3325 else if (buf->b_term != NULL)
3326 ro_char = 'F';
3327 else
3328#endif
3329 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3330
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003331 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00003332 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 buf->b_fnum,
3334 buf->b_p_bl ? ' ' : 'u',
3335 buf == curbuf ? '%' :
3336 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3337 buf->b_ml.ml_mfp == NULL ? ' ' :
3338 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003339 ro_char,
3340 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003341 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003342 if (len > IOSIZE - 20)
3343 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344
Bram Moolenaarc667da52019-11-30 20:52:27 +01003345 // put "line 999" in column 40 or after the file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 i = 40 - vim_strsize(IObuff);
3347 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 IObuff[len++] = ' ';
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003349 while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar52410572019-10-27 05:12:45 +01003350#ifdef FEAT_VIMINFO
3351 if (vim_strchr(eap->arg, 't') && buf->b_last_used)
3352 add_time(IObuff + len, (size_t)(IOSIZE - len), buf->b_last_used);
3353 else
3354#endif
3355 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3356 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003357 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 msg_outtrans(IObuff);
Bram Moolenaarc667da52019-11-30 20:52:27 +01003359 out_flush(); // output one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 ui_breakcheck();
3361 }
Bram Moolenaar52410572019-10-27 05:12:45 +01003362
3363#ifdef FEAT_VIMINFO
3364 if (buflist_data)
3365 ga_clear(&buflist);
3366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368
3369/*
3370 * Get file name and line number for file 'fnum'.
3371 * Used by DoOneCmd() for translating '%' and '#'.
3372 * Used by insert_reg() and cmdline_paste() for '#' register.
3373 * Return FAIL if not found, OK for success.
3374 */
3375 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003376buflist_name_nr(
3377 int fnum,
3378 char_u **fname,
3379 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380{
3381 buf_T *buf;
3382
3383 buf = buflist_findnr(fnum);
3384 if (buf == NULL || buf->b_fname == NULL)
3385 return FAIL;
3386
3387 *fname = buf->b_fname;
3388 *lnum = buflist_findlnum(buf);
3389
3390 return OK;
3391}
3392
3393/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003394 * Set the file name for "buf"' to "ffname_arg", short file name to
3395 * "sfname_arg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 * The file name with the full path is also remembered, for when :cd is used.
3397 * Returns FAIL for failure (file name already in use by other buffer)
3398 * OK otherwise.
3399 */
3400 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003401setfname(
3402 buf_T *buf,
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003403 char_u *ffname_arg,
3404 char_u *sfname_arg,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003405 int message) // give message when buffer already exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003407 char_u *ffname = ffname_arg;
3408 char_u *sfname = sfname_arg;
Bram Moolenaar81695252004-12-29 20:58:21 +00003409 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003411 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412#endif
3413
3414 if (ffname == NULL || *ffname == NUL)
3415 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003416 // Removing the name.
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003417 if (buf->b_sfname != buf->b_ffname)
3418 VIM_CLEAR(buf->b_sfname);
3419 else
3420 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003421 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422#ifdef UNIX
3423 st.st_dev = (dev_T)-1;
3424#endif
3425 }
3426 else
3427 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003428 fname_expand(buf, &ffname, &sfname); // will allocate ffname
3429 if (ffname == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 return FAIL;
3431
3432 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01003433 * If the file name is already used in another buffer:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 * - if the buffer is loaded, fail
3435 * - if the buffer is not loaded, delete it from the list
3436 */
3437#ifdef UNIX
3438 if (mch_stat((char *)ffname, &st) < 0)
3439 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003440#endif
3441 if (!(buf->b_flags & BF_DUMMY))
3442#ifdef UNIX
3443 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003445 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446#endif
3447 if (obuf != NULL && obuf != buf)
3448 {
Bram Moolenaard3710cf2021-10-04 23:13:13 +01003449 win_T *win;
3450 tabpage_T *tab;
3451 int in_use = FALSE;
3452
3453 // during startup a window may use a buffer that is not loaded yet
3454 FOR_ALL_TAB_WINDOWS(tab, win)
3455 if (win->w_buffer == obuf)
3456 in_use = TRUE;
3457
3458 // it's loaded or used in a window, fail
3459 if (obuf->b_ml.ml_mfp != NULL || in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 {
3461 if (message)
Bram Moolenaare1242042021-12-16 20:56:57 +00003462 emsg(_(e_buffer_with_this_name_already_exists));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 vim_free(ffname);
3464 return FAIL;
3465 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003466 // delete from the list
Bram Moolenaara6e8f882019-12-14 16:18:15 +01003467 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 }
3469 sfname = vim_strsave(sfname);
3470 if (ffname == NULL || sfname == NULL)
3471 {
3472 vim_free(sfname);
3473 vim_free(ffname);
3474 return FAIL;
3475 }
3476#ifdef USE_FNAME_CASE
Bram Moolenaarc667da52019-11-30 20:52:27 +01003477 fname_case(sfname, 0); // set correct case for short file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478#endif
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003479 if (buf->b_sfname != buf->b_ffname)
3480 vim_free(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 vim_free(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 buf->b_ffname = ffname;
3483 buf->b_sfname = sfname;
3484 }
3485 buf->b_fname = buf->b_sfname;
3486#ifdef UNIX
3487 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003488 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 else
3490 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003491 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 buf->b_dev = st.st_dev;
3493 buf->b_ino = st.st_ino;
3494 }
3495#endif
3496
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498
3499 buf_name_changed(buf);
3500 return OK;
3501}
3502
3503/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003504 * Crude way of changing the name of a buffer. Use with care!
3505 * The name should be relative to the current directory.
3506 */
3507 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003508buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003509{
3510 buf_T *buf;
3511
3512 buf = buflist_findnr(fnum);
3513 if (buf != NULL)
3514 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003515 if (buf->b_sfname != buf->b_ffname)
3516 vim_free(buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003517 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003518 buf->b_ffname = vim_strsave(name);
3519 buf->b_sfname = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01003520 // Allocate ffname and expand into full path. Also resolves .lnk
3521 // files on Win32.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003522 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003523 buf->b_fname = buf->b_sfname;
3524 }
3525}
3526
3527/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 * Take care of what needs to be done when the name of buffer "buf" has
3529 * changed.
3530 */
3531 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003532buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533{
3534 /*
3535 * If the file name changed, also change the name of the swapfile
3536 */
3537 if (buf->b_ml.ml_mfp != NULL)
3538 ml_setname(buf);
3539
Bram Moolenaar3ad69532021-11-19 17:01:08 +00003540#ifdef FEAT_TERMINAL
3541 if (buf->b_term != NULL)
3542 term_clear_status_text(buf->b_term);
3543#endif
3544
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 if (curwin->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003546 check_arg_idx(curwin); // check file name for arg list
Bram Moolenaarc667da52019-11-30 20:52:27 +01003547 maketitle(); // set window title
Bram Moolenaarc667da52019-11-30 20:52:27 +01003548 status_redraw_all(); // status lines need to be redrawn
3549 fmarks_check_names(buf); // check named file marks
3550 ml_timestamp(buf); // reset timestamp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551}
3552
3553/*
3554 * set alternate file name for current window
3555 *
3556 * Used by do_one_cmd(), do_write() and do_ecmd().
3557 * Return the buffer.
3558 */
3559 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003560setaltfname(
3561 char_u *ffname,
3562 char_u *sfname,
3563 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564{
3565 buf_T *buf;
3566
Bram Moolenaarc667da52019-11-30 20:52:27 +01003567 // Create a buffer. 'buflisted' is not set if it's a new buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaare1004402020-10-24 20:49:43 +02003569 if (buf != NULL && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 curwin->w_alt_fnum = buf->b_fnum;
3571 return buf;
3572}
3573
3574/*
3575 * Get alternate file name for current window.
3576 * Return NULL if there isn't any, and give error message if requested.
3577 */
3578 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003579getaltfname(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003580 int errmsg) // give error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581{
3582 char_u *fname;
3583 linenr_T dummy;
3584
3585 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3586 {
3587 if (errmsg)
Bram Moolenaar108010a2021-06-27 22:03:33 +02003588 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 return NULL;
3590 }
3591 return fname;
3592}
3593
3594/*
3595 * Add a file name to the buflist and return its number.
3596 * Uses same flags as buflist_new(), except BLN_DUMMY.
3597 *
3598 * used by qf_init(), main() and doarglist()
3599 */
3600 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003601buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602{
3603 buf_T *buf;
3604
3605 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3606 if (buf != NULL)
3607 return buf->b_fnum;
3608 return 0;
3609}
3610
3611#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3612/*
3613 * Adjust slashes in file names. Called after 'shellslash' was set.
3614 */
3615 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003616buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617{
3618 buf_T *bp;
3619
Bram Moolenaar29323592016-07-24 22:04:11 +02003620 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 {
3622 if (bp->b_ffname != NULL)
3623 slash_adjust(bp->b_ffname);
3624 if (bp->b_sfname != NULL)
3625 slash_adjust(bp->b_sfname);
3626 }
3627}
3628#endif
3629
3630/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003631 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 * Also save the local window option values.
3633 */
3634 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003635buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003637 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638}
3639
3640/*
3641 * Return TRUE if 'ffname' is not the same file as current file.
3642 * Fname must have a full path (expanded by mch_FullName()).
3643 */
3644 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003645otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646{
3647 return otherfile_buf(curbuf, ffname
3648#ifdef UNIX
3649 , NULL
3650#endif
3651 );
3652}
3653
3654 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003655otherfile_buf(
3656 buf_T *buf,
3657 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003659 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003661 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662{
Bram Moolenaarc667da52019-11-30 20:52:27 +01003663 // no name is different
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3665 return TRUE;
3666 if (fnamecmp(ffname, buf->b_ffname) == 0)
3667 return FALSE;
3668#ifdef UNIX
3669 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003670 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671
Bram Moolenaarc667da52019-11-30 20:52:27 +01003672 // If no stat_T given, get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 if (stp == NULL)
3674 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003675 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 st.st_dev = (dev_T)-1;
3677 stp = &st;
3678 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003679 // Use dev/ino to check if the files are the same, even when the names
3680 // are different (possible with links). Still need to compare the
3681 // name above, for when the file doesn't exist yet.
3682 // Problem: The dev/ino changes when a file is deleted (and created
3683 // again) and remains the same when renamed/moved. We don't want to
3684 // mch_stat() each buffer each time, that would be too slow. Get the
3685 // dev/ino again when they appear to match, but not when they appear
3686 // to be different: Could skip a buffer when it's actually the same
3687 // file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 if (buf_same_ino(buf, stp))
3689 {
3690 buf_setino(buf);
3691 if (buf_same_ino(buf, stp))
3692 return FALSE;
3693 }
3694 }
3695#endif
3696 return TRUE;
3697}
3698
3699#if defined(UNIX) || defined(PROTO)
3700/*
3701 * Set inode and device number for a buffer.
3702 * Must always be called when b_fname is changed!.
3703 */
3704 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003705buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003707 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708
3709 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3710 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003711 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 buf->b_dev = st.st_dev;
3713 buf->b_ino = st.st_ino;
3714 }
3715 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003716 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717}
3718
3719/*
3720 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3721 */
3722 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003723buf_same_ino(
3724 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003725 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003727 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 && stp->st_dev == buf->b_dev
3729 && stp->st_ino == buf->b_ino);
3730}
3731#endif
3732
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003733/*
3734 * Print info about the current buffer.
3735 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003737fileinfo(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003738 int fullname, // when non-zero print full path
Bram Moolenaar7454a062016-01-30 15:14:10 +01003739 int shorthelp,
3740 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741{
3742 char_u *name;
3743 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003744 char *p;
3745 char *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003746 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003748 buffer = alloc(IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 if (buffer == NULL)
3750 return;
3751
Bram Moolenaarc667da52019-11-30 20:52:27 +01003752 if (fullname > 1) // 2 CTRL-G: include buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003754 vim_snprintf(buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 p = buffer + STRLEN(buffer);
3756 }
3757 else
3758 p = buffer;
3759
3760 *p++ = '"';
3761 if (buf_spname(curbuf) != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003762 vim_strncpy((char_u *)p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 else
3764 {
3765 if (!fullname && curbuf->b_fname != NULL)
3766 name = curbuf->b_fname;
3767 else
3768 name = curbuf->b_ffname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003769 home_replace(shorthelp ? curbuf : NULL, name, (char_u *)p,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 (int)(IOSIZE - (p - buffer)), TRUE);
3771 }
3772
Bram Moolenaar32526b32019-01-19 17:43:09 +01003773 vim_snprintf_add(buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 curbufIsChanged() ? (shortmess(SHM_MOD)
3775 ? " [+]" : _(" [Modified]")) : " ",
3776 (curbuf->b_flags & BF_NOTEDITED)
3777#ifdef FEAT_QUICKFIX
3778 && !bt_dontwrite(curbuf)
3779#endif
3780 ? _("[Not edited]") : "",
3781 (curbuf->b_flags & BF_NEW)
3782#ifdef FEAT_QUICKFIX
3783 && !bt_dontwrite(curbuf)
3784#endif
Bram Moolenaar722e5052020-06-12 22:31:00 +02003785 ? new_file_message() : "",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003787 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 : _("[readonly]")) : "",
3789 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3790 || curbuf->b_p_ro) ?
3791 " " : "");
Bram Moolenaarc667da52019-11-30 20:52:27 +01003792 // With 32 bit longs and more than 21,474,836 lines multiplying by 100
3793 // causes an overflow, thus for large numbers divide instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 if (curwin->w_cursor.lnum > 1000000L)
3795 n = (int)(((long)curwin->w_cursor.lnum) /
3796 ((long)curbuf->b_ml.ml_line_count / 100L));
3797 else
3798 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3799 (long)curbuf->b_ml.ml_line_count);
3800 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003801 vim_snprintf_add(buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802#ifdef FEAT_CMDL_INFO
3803 else if (p_ru)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003804 // Current line and column are already on the screen -- webb
Bram Moolenaar32526b32019-01-19 17:43:09 +01003805 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003806 NGETTEXT("%ld line --%d%%--", "%ld lines --%d%%--",
3807 curbuf->b_ml.ml_line_count),
3808 (long)curbuf->b_ml.ml_line_count, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809#endif
3810 else
3811 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003812 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 _("line %ld of %ld --%d%%-- col "),
3814 (long)curwin->w_cursor.lnum,
3815 (long)curbuf->b_ml.ml_line_count,
3816 n);
3817 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003818 len = STRLEN(buffer);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003819 col_print((char_u *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3821 }
3822
Bram Moolenaar32526b32019-01-19 17:43:09 +01003823 (void)append_arg_number(curwin, (char_u *)buffer, IOSIZE,
3824 !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825
3826 if (dont_truncate)
3827 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003828 // Temporarily set msg_scroll to avoid the message being truncated.
3829 // First call msg_start() to get the message in the right place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 msg_start();
3831 n = msg_scroll;
3832 msg_scroll = TRUE;
3833 msg(buffer);
3834 msg_scroll = n;
3835 }
3836 else
3837 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00003838 p = msg_trunc_attr(buffer, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaarc667da52019-11-30 20:52:27 +01003840 // Need to repeat the message after redrawing when:
3841 // - When restart_edit is set (otherwise there will be a delay
3842 // before redrawing).
3843 // - When the screen was scrolled but there is no wait-return
3844 // prompt.
Bram Moolenaar32526b32019-01-19 17:43:09 +01003845 set_keep_msg((char_u *)p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 }
3847
3848 vim_free(buffer);
3849}
3850
3851 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003852col_print(
3853 char_u *buf,
3854 size_t buflen,
3855 int col,
3856 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857{
3858 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003859 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003861 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862}
3863
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864static char_u *lasttitle = NULL;
3865static char_u *lasticon = NULL;
3866
Bram Moolenaar84a93082018-06-16 22:58:15 +02003867/*
3868 * Put the file name in the title bar and icon of the window.
3869 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003871maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872{
3873 char_u *p;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003874 char_u *title_str = NULL;
3875 char_u *icon_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 int maxlen = 0;
3877 int len;
3878 int mustset;
3879 char_u buf[IOSIZE];
3880 int off;
3881
3882 if (!redrawing())
3883 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003884 // Postpone updating the title when 'lazyredraw' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 need_maketitle = TRUE;
3886 return;
3887 }
3888
3889 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003890 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003891 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892
3893 if (p_title)
3894 {
3895 if (p_titlelen > 0)
3896 {
3897 maxlen = p_titlelen * Columns / 100;
3898 if (maxlen < 10)
3899 maxlen = 10;
3900 }
3901
Bram Moolenaar84a93082018-06-16 22:58:15 +02003902 title_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 if (*p_titlestring != NUL)
3904 {
3905#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003906 if (stl_syntax & STL_IN_TITLE)
3907 {
3908 int use_sandbox = FALSE;
Bram Moolenaar53989552019-12-23 22:59:18 +01003909 int called_emsg_before = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003910
3911# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003912 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003913# endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003914 build_stl_str_hl(curwin, title_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003915 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003916 0, maxlen, NULL, NULL);
Bram Moolenaar53989552019-12-23 22:59:18 +01003917 if (called_emsg > called_emsg_before)
Bram Moolenaard3667a22006-03-16 21:35:52 +00003918 set_string_option_direct((char_u *)"titlestring", -1,
3919 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 else
3922#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003923 title_str = p_titlestring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 }
3925 else
3926 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003927 // format: "fname + (path) (1 of 2) - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928
Bram Moolenaar2c666692012-09-05 13:30:40 +02003929#define SPACE_FOR_FNAME (IOSIZE - 100)
3930#define SPACE_FOR_DIR (IOSIZE - 20)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003931#define SPACE_FOR_ARGNR (IOSIZE - 10) // at least room for " - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003933 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar21554412017-07-24 21:44:43 +02003934#ifdef FEAT_TERMINAL
3935 else if (curbuf->b_term != NULL)
3936 {
3937 vim_strncpy(buf, term_get_status_text(curbuf->b_term),
3938 SPACE_FOR_FNAME);
3939 }
3940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 else
3942 {
3943 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003944 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 }
3947
Bram Moolenaar21554412017-07-24 21:44:43 +02003948#ifdef FEAT_TERMINAL
3949 if (curbuf->b_term == NULL)
3950#endif
3951 switch (bufIsChanged(curbuf)
3952 + (curbuf->b_p_ro * 2)
3953 + (!curbuf->b_p_ma * 4))
3954 {
3955 case 1: STRCAT(buf, " +"); break;
3956 case 2: STRCAT(buf, " ="); break;
3957 case 3: STRCAT(buf, " =+"); break;
3958 case 4:
3959 case 6: STRCAT(buf, " -"); break;
3960 case 5:
3961 case 7: STRCAT(buf, " -+"); break;
3962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963
Bram Moolenaar21554412017-07-24 21:44:43 +02003964 if (curbuf->b_fname != NULL
3965#ifdef FEAT_TERMINAL
3966 && curbuf->b_term == NULL
3967#endif
3968 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003970 // Get path of file, replace home dir with ~
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 off = (int)STRLEN(buf);
3972 buf[off++] = ' ';
3973 buf[off++] = '(';
3974 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003975 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc667da52019-11-30 20:52:27 +01003977 // avoid "c:/name" to be reduced to "c"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 if (isalpha(buf[off]) && buf[off + 1] == ':')
3979 off += 2;
3980#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003981 // remove the file name
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003982 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 if (p == buf + off)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003984 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003985 // must be a help buffer
Bram Moolenaar21554412017-07-24 21:44:43 +02003986 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003987 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003991
Bram Moolenaarc667da52019-11-30 20:52:27 +01003992 // Translate unprintable chars and concatenate. Keep some
3993 // room for the server name. When there is no room (very long
3994 // file name) use (...).
Bram Moolenaar2c666692012-09-05 13:30:40 +02003995 if (off < SPACE_FOR_DIR)
3996 {
3997 p = transstr(buf + off);
3998 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3999 vim_free(p);
4000 }
4001 else
4002 {
4003 vim_strncpy(buf + off, (char_u *)"...",
4004 (size_t)(SPACE_FOR_ARGNR - off));
4005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 STRCAT(buf, ")");
4007 }
4008
Bram Moolenaar2c666692012-09-05 13:30:40 +02004009 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010
4011#if defined(FEAT_CLIENTSERVER)
4012 if (serverName != NULL)
4013 {
4014 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02004015 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 }
4017 else
4018#endif
4019 STRCAT(buf, " - VIM");
4020
4021 if (maxlen > 0)
4022 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004023 // make it shorter by removing a bit in the middle
Bram Moolenaarf31b7642012-01-20 20:44:43 +01004024 if (vim_strsize(buf) > maxlen)
4025 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 }
4027 }
4028 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02004029 mustset = value_changed(title_str, &lasttitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030
4031 if (p_icon)
4032 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02004033 icon_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 if (*p_iconstring != NUL)
4035 {
4036#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00004037 if (stl_syntax & STL_IN_ICON)
4038 {
4039 int use_sandbox = FALSE;
Bram Moolenaar53989552019-12-23 22:59:18 +01004040 int called_emsg_before = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004041
4042# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00004043 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004044# endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02004045 build_stl_str_hl(curwin, icon_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004046 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004047 0, 0, NULL, NULL);
Bram Moolenaar53989552019-12-23 22:59:18 +01004048 if (called_emsg > called_emsg_before)
Bram Moolenaard3667a22006-03-16 21:35:52 +00004049 set_string_option_direct((char_u *)"iconstring", -1,
4050 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaard3667a22006-03-16 21:35:52 +00004051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 else
4053#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02004054 icon_str = p_iconstring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 }
4056 else
4057 {
4058 if (buf_spname(curbuf) != NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004059 p = buf_spname(curbuf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004060 else // use file name only in icon
Bram Moolenaar84a93082018-06-16 22:58:15 +02004061 p = gettail(curbuf->b_ffname);
4062 *icon_str = NUL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004063 // Truncate name at 100 bytes.
Bram Moolenaar84a93082018-06-16 22:58:15 +02004064 len = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 if (len > 100)
4066 {
4067 len -= 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 if (has_mbyte)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004069 len += (*mb_tail_off)(p, p + len) + 1;
Bram Moolenaar84a93082018-06-16 22:58:15 +02004070 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02004072 STRCPY(icon_str, p);
4073 trans_characters(icon_str, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 }
4075 }
4076
Bram Moolenaar84a93082018-06-16 22:58:15 +02004077 mustset |= value_changed(icon_str, &lasticon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078
4079 if (mustset)
4080 resettitle();
4081}
4082
4083/*
4084 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
4085 * from "str" if it does.
Bram Moolenaar84a93082018-06-16 22:58:15 +02004086 * Return TRUE if resettitle() is to be called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 */
4088 static int
Bram Moolenaar84a93082018-06-16 22:58:15 +02004089value_changed(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090{
4091 if ((str == NULL) != (*last == NULL)
4092 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
4093 {
4094 vim_free(*last);
4095 if (str == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004096 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 *last = NULL;
Bram Moolenaar40385db2018-08-07 22:31:44 +02004098 mch_restore_title(
4099 last == &lasttitle ? SAVE_RESTORE_TITLE : SAVE_RESTORE_ICON);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 else
Bram Moolenaar84a93082018-06-16 22:58:15 +02004102 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 *last = vim_strsave(str);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004104 return TRUE;
4105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 }
4107 return FALSE;
4108}
4109
4110/*
4111 * Put current window title back (used after calling a shell)
4112 */
4113 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004114resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115{
4116 mch_settitle(lasttitle, lasticon);
4117}
Bram Moolenaarea408852005-06-25 22:49:46 +00004118
4119# if defined(EXITFREE) || defined(PROTO)
4120 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004121free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00004122{
4123 vim_free(lasttitle);
4124 vim_free(lasticon);
4125}
4126# endif
4127
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004129#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004130
4131/*
4132 * Used for building in the status line.
4133 */
4134typedef struct
4135{
4136 char_u *stl_start;
4137 int stl_minwid;
4138 int stl_maxwid;
4139 enum {
4140 Normal,
4141 Empty,
4142 Group,
4143 Middle,
4144 Highlight,
4145 TabPage,
4146 Trunc
4147 } stl_type;
4148} stl_item_T;
4149
4150static size_t stl_items_len = 20; // Initial value, grows as needed.
4151static stl_item_T *stl_items = NULL;
4152static int *stl_groupitem = NULL;
4153static stl_hlrec_T *stl_hltab = NULL;
4154static stl_hlrec_T *stl_tabtab = NULL;
4155
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004157 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 * Return length of string in screen cells.
4159 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004160 * Normally works for window "wp", except when working for 'tabline' then it
4161 * is "curwin".
4162 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 * Items are drawn interspersed with the text that surrounds it
4164 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
4165 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
4166 *
4167 * If maxwidth is not zero, the string will be filled at any middle marker
4168 * or truncated if too long, fillchar is used for all whitespace.
4169 */
4170 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004171build_stl_str_hl(
4172 win_T *wp,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004173 char_u *out, // buffer to write into != NameBuff
4174 size_t outlen, // length of out[]
Bram Moolenaar7454a062016-01-30 15:14:10 +01004175 char_u *fmt,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004176 int use_sandbox UNUSED, // "fmt" was set insecurely, use sandbox
Bram Moolenaar7454a062016-01-30 15:14:10 +01004177 int fillchar,
4178 int maxwidth,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004179 stl_hlrec_T **hltab, // return: HL attributes (can be NULL)
4180 stl_hlrec_T **tabtab) // return: tab page nrs (can be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181{
Bram Moolenaar10772302019-01-20 18:25:54 +01004182 linenr_T lnum;
4183 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 char_u *p;
4185 char_u *s;
4186 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004187 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188#ifdef FEAT_EVAL
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004189 win_T *save_curwin;
4190 buf_T *save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004191 int save_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192#endif
4193 int empty_line;
4194 colnr_T virtcol;
4195 long l;
4196 long n;
4197 int prevchar_isflag;
4198 int prevchar_isitem;
4199 int itemisflag;
4200 int fillable;
4201 char_u *str;
4202 long num;
4203 int width;
4204 int itemcnt;
4205 int curitem;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004206 int group_end_userhl;
4207 int group_start_userhl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 int groupdepth;
shadmansaleh30e3de22021-05-15 17:23:28 +02004209#ifdef FEAT_EVAL
4210 int evaldepth;
4211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 int minwid;
4213 int maxwid;
4214 int zeropad;
4215 char_u base;
4216 char_u opt;
4217#define TMPLEN 70
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004218 char_u buf_tmp[TMPLEN];
4219 char_u win_tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004220 char_u *usefmt = fmt;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004221 stl_hlrec_T *sp;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004222 int save_must_redraw = must_redraw;
4223 int save_redr_type = curwin->w_redr_type;
Bram Moolenaar0e1f36f2022-02-15 16:17:44 +00004224 int save_KeyTyped = KeyTyped;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004225
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004226 if (stl_items == NULL)
4227 {
4228 stl_items = ALLOC_MULT(stl_item_T, stl_items_len);
4229 stl_groupitem = ALLOC_MULT(int, stl_items_len);
Brandon Richardsona493b652022-02-19 11:45:03 +00004230
4231 // Allocate one more, because the last element is used to indicate the
4232 // end of the list.
4233 stl_hltab = ALLOC_MULT(stl_hlrec_T, stl_items_len + 1);
4234 stl_tabtab = ALLOC_MULT(stl_hlrec_T, stl_items_len + 1);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004235 }
4236
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004237#ifdef FEAT_EVAL
4238 /*
4239 * When the format starts with "%!" then evaluate it as an expression and
4240 * use the result as the actual format string.
4241 */
4242 if (fmt[0] == '%' && fmt[1] == '!')
4243 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004244 typval_T tv;
4245
4246 tv.v_type = VAR_NUMBER;
4247 tv.vval.v_number = wp->w_id;
4248 set_var((char_u *)"g:statusline_winid", &tv, FALSE);
4249
Bram Moolenaar9530b582022-01-22 13:39:08 +00004250 usefmt = eval_to_string_safe(fmt + 2, use_sandbox, FALSE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004251 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00004252 usefmt = fmt;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004253
4254 do_unlet((char_u *)"g:statusline_winid", TRUE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004255 }
4256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257
4258 if (fillchar == 0)
4259 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004260
Bram Moolenaar10772302019-01-20 18:25:54 +01004261 // The cursor in windows other than the current one isn't always
4262 // up-to-date, esp. because of autocommands and timers.
4263 lnum = wp->w_cursor.lnum;
4264 if (lnum > wp->w_buffer->b_ml.ml_line_count)
4265 {
4266 lnum = wp->w_buffer->b_ml.ml_line_count;
4267 wp->w_cursor.lnum = lnum;
4268 }
4269
4270 // Get line & check if empty (cursorpos will show "0-1"). Note that
4271 // p will become invalid when getting another buffer line.
4272 p = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar567199b2013-04-24 16:52:36 +02004273 empty_line = (*p == NUL);
4274
Bram Moolenaar10772302019-01-20 18:25:54 +01004275 // Get the byte value now, in case we need it below. This is more efficient
4276 // than making a copy of the line.
4277 len = STRLEN(p);
4278 if (wp->w_cursor.col > (colnr_T)len)
4279 {
4280 // Line may have changed since checking the cursor column, or the lnum
4281 // was adjusted above.
4282 wp->w_cursor.col = (colnr_T)len;
Bram Moolenaar10772302019-01-20 18:25:54 +01004283 wp->w_cursor.coladd = 0;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004284 byteval = 0;
Bram Moolenaar10772302019-01-20 18:25:54 +01004285 }
Bram Moolenaar567199b2013-04-24 16:52:36 +02004286 else
Bram Moolenaar567199b2013-04-24 16:52:36 +02004287 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288
4289 groupdepth = 0;
shadmansaleh30e3de22021-05-15 17:23:28 +02004290#ifdef FEAT_EVAL
4291 evaldepth = 0;
4292#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 p = out;
4294 curitem = 0;
4295 prevchar_isflag = TRUE;
4296 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004297 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004299 if (curitem == (int)stl_items_len)
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004300 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004301 size_t new_len = stl_items_len * 3 / 2;
4302 stl_item_T *new_items;
4303 int *new_groupitem;
4304 stl_hlrec_T *new_hlrec;
4305
4306 new_items = vim_realloc(stl_items, sizeof(stl_item_T) * new_len);
4307 if (new_items == NULL)
4308 break;
4309 stl_items = new_items;
4310 new_groupitem = vim_realloc(stl_groupitem, sizeof(int) * new_len);
4311 if (new_groupitem == NULL)
4312 break;
4313 stl_groupitem = new_groupitem;
Brandon Richardsona493b652022-02-19 11:45:03 +00004314 new_hlrec = vim_realloc(stl_hltab,
4315 sizeof(stl_hlrec_T) * (new_len + 1));
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004316 if (new_hlrec == NULL)
4317 break;
4318 stl_hltab = new_hlrec;
Brandon Richardsona493b652022-02-19 11:45:03 +00004319 new_hlrec = vim_realloc(stl_tabtab,
4320 sizeof(stl_hlrec_T) * (new_len + 1));
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004321 if (new_hlrec == NULL)
4322 break;
4323 stl_tabtab = new_hlrec;
4324 stl_items_len = new_len;
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004325 }
4326
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 if (*s != NUL && *s != '%')
4328 prevchar_isflag = prevchar_isitem = FALSE;
4329
4330 /*
4331 * Handle up to the next '%' or the end.
4332 */
4333 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
4334 *p++ = *s++;
4335 if (*s == NUL || p + 1 >= out + outlen)
4336 break;
4337
4338 /*
4339 * Handle one '%' item.
4340 */
4341 s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004342 if (*s == NUL) // ignore trailing %
Bram Moolenaar1d87f512011-02-01 21:55:01 +01004343 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 if (*s == '%')
4345 {
4346 if (p + 1 >= out + outlen)
4347 break;
4348 *p++ = *s++;
4349 prevchar_isflag = prevchar_isitem = FALSE;
4350 continue;
4351 }
4352 if (*s == STL_MIDDLEMARK)
4353 {
4354 s++;
4355 if (groupdepth > 0)
4356 continue;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004357 stl_items[curitem].stl_type = Middle;
4358 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 continue;
4360 }
4361 if (*s == STL_TRUNCMARK)
4362 {
4363 s++;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004364 stl_items[curitem].stl_type = Trunc;
4365 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 continue;
4367 }
4368 if (*s == ')')
4369 {
4370 s++;
4371 if (groupdepth < 1)
4372 continue;
4373 groupdepth--;
4374
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004375 t = stl_items[stl_groupitem[groupdepth]].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 *p = NUL;
4377 l = vim_strsize(t);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004378 if (curitem > stl_groupitem[groupdepth] + 1
4379 && stl_items[stl_groupitem[groupdepth]].stl_minwid == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004381 // remove group if all items are empty and highlight group
4382 // doesn't change
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004383 group_start_userhl = group_end_userhl = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004384 for (n = stl_groupitem[groupdepth] - 1; n >= 0; n--)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004385 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004386 if (stl_items[n].stl_type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004387 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004388 group_start_userhl = group_end_userhl =
4389 stl_items[n].stl_minwid;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004390 break;
4391 }
4392 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004393 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004394 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004395 if (stl_items[n].stl_type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 break;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004397 if (stl_items[n].stl_type == Highlight)
4398 group_end_userhl = stl_items[n].stl_minwid;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004399 }
4400 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 {
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004402 // empty group
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 p = t;
4404 l = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004405 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004406 {
4407 // do not use the highlighting from the removed group
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004408 if (stl_items[n].stl_type == Highlight)
4409 stl_items[n].stl_type = Empty;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004410 // adjust the start position of TabPage to the next
4411 // item position
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004412 if (stl_items[n].stl_type == TabPage)
4413 stl_items[n].stl_start = p;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 }
4416 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004417 if (l > stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004419 // truncate, remove n bytes of text at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 if (has_mbyte)
4421 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004422 // Find the first character that should be included.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 n = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004424 while (l >= stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 {
4426 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004427 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 }
4429 }
4430 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004431 n = (long)(p - t) - stl_items[stl_groupitem[groupdepth]]
4432 .stl_maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433
4434 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004435 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 p = p - n + 1;
Bram Moolenaar13505972019-01-24 15:04:48 +01004437
4438 // Fill up space left over by half a double-wide char.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004439 while (++l < stl_items[stl_groupitem[groupdepth]].stl_minwid)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004440 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441
Bram Moolenaarc667da52019-11-30 20:52:27 +01004442 // correct the start of the items for the truncation
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004443 for (l = stl_groupitem[groupdepth] + 1; l < curitem; l++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 {
LemonBoy57ff5262022-05-09 21:03:47 +01004445 // Minus one for the leading '<' added above.
4446 stl_items[l].stl_start -= n - 1;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004447 if (stl_items[l].stl_start < t)
4448 stl_items[l].stl_start = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 }
4450 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004451 else if (abs(stl_items[stl_groupitem[groupdepth]].stl_minwid) > l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004453 // fill
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004454 n = stl_items[stl_groupitem[groupdepth]].stl_minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 if (n < 0)
4456 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004457 // fill by appending characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 n = 0 - n;
4459 while (l++ < n && p + 1 < out + outlen)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004460 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 }
4462 else
4463 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004464 // fill by inserting characters
Bram Moolenaar008bff92021-03-04 21:55:58 +01004465 l = (n - l) * MB_CHAR2LEN(fillchar);
4466 mch_memmove(t + l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004468 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 p += l;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004470 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
4471 stl_items[n].stl_start += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 for ( ; l > 0; l--)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004473 MB_CHAR2BYTES(fillchar, t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 }
4475 }
4476 continue;
4477 }
4478 minwid = 0;
4479 maxwid = 9999;
4480 zeropad = FALSE;
4481 l = 1;
4482 if (*s == '0')
4483 {
4484 s++;
4485 zeropad = TRUE;
4486 }
4487 if (*s == '-')
4488 {
4489 s++;
4490 l = -1;
4491 }
4492 if (VIM_ISDIGIT(*s))
4493 {
4494 minwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004495 if (minwid < 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 minwid = 0;
4497 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004498 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004500 stl_items[curitem].stl_type = Highlight;
4501 stl_items[curitem].stl_start = p;
4502 stl_items[curitem].stl_minwid = minwid > 9 ? 1 : minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 s++;
4504 curitem++;
4505 continue;
4506 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004507 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4508 {
4509 if (*s == STL_TABCLOSENR)
4510 {
4511 if (minwid == 0)
4512 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004513 // %X ends the close label, go back to the previously
4514 // define tab label nr.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004515 for (n = curitem - 1; n >= 0; --n)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004516 if (stl_items[n].stl_type == TabPage
4517 && stl_items[n].stl_minwid >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004518 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004519 minwid = stl_items[n].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004520 break;
4521 }
4522 }
4523 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01004524 // close nrs are stored as negative values
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004525 minwid = - minwid;
4526 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004527 stl_items[curitem].stl_type = TabPage;
4528 stl_items[curitem].stl_start = p;
4529 stl_items[curitem].stl_minwid = minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004530 s++;
4531 curitem++;
4532 continue;
4533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 if (*s == '.')
4535 {
4536 s++;
4537 if (VIM_ISDIGIT(*s))
4538 {
4539 maxwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004540 if (maxwid <= 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 maxwid = 50;
4542 }
4543 }
4544 minwid = (minwid > 50 ? 50 : minwid) * l;
4545 if (*s == '(')
4546 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004547 stl_groupitem[groupdepth++] = curitem;
4548 stl_items[curitem].stl_type = Group;
4549 stl_items[curitem].stl_start = p;
4550 stl_items[curitem].stl_minwid = minwid;
4551 stl_items[curitem].stl_maxwid = maxwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 s++;
4553 curitem++;
4554 continue;
4555 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004556#ifdef FEAT_EVAL
4557 // Denotes end of expanded %{} block
4558 if (*s == '}' && evaldepth > 0)
4559 {
4560 s++;
4561 evaldepth--;
4562 continue;
4563 }
4564#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 if (vim_strchr(STL_ALL, *s) == NULL)
4566 {
4567 s++;
4568 continue;
4569 }
4570 opt = *s++;
4571
Bram Moolenaarc667da52019-11-30 20:52:27 +01004572 // OK - now for the real work
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 base = 'D';
4574 itemisflag = FALSE;
4575 fillable = TRUE;
4576 num = -1;
4577 str = NULL;
4578 switch (opt)
4579 {
4580 case STL_FILEPATH:
4581 case STL_FULLPATH:
4582 case STL_FILENAME:
Bram Moolenaarc667da52019-11-30 20:52:27 +01004583 fillable = FALSE; // don't change ' ' to fillchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004585 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 else
4587 {
4588 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004589 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4591 }
4592 trans_characters(NameBuff, MAXPATHL);
4593 if (opt != STL_FILENAME)
4594 str = NameBuff;
4595 else
4596 str = gettail(NameBuff);
4597 break;
4598
Bram Moolenaarc667da52019-11-30 20:52:27 +01004599 case STL_VIM_EXPR: // '{'
shadmansaleh30e3de22021-05-15 17:23:28 +02004600 {
4601#ifdef FEAT_EVAL
4602 char_u *block_start = s - 1;
4603#endif
4604 int reevaluate = (*s == '%');
4605
4606 if (reevaluate)
4607 s++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 itemisflag = TRUE;
4609 t = p;
shadmansaleh30e3de22021-05-15 17:23:28 +02004610 while ((*s != '}' || (reevaluate && s[-1] != '%'))
4611 && *s != NUL && p + 1 < out + outlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 *p++ = *s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004613 if (*s != '}') // missing '}' or out of space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 break;
4615 s++;
shadmansaleh30e3de22021-05-15 17:23:28 +02004616 if (reevaluate)
4617 p[-1] = 0; // remove the % at the end of %{% expr %}
4618 else
4619 *p = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 p = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621#ifdef FEAT_EVAL
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004622 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp),
4623 "%d", curbuf->b_fnum);
4624 set_internal_string_var((char_u *)"g:actual_curbuf", buf_tmp);
4625 vim_snprintf((char *)win_tmp, sizeof(win_tmp), "%d", curwin->w_id);
4626 set_internal_string_var((char_u *)"g:actual_curwin", win_tmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004628 save_curbuf = curbuf;
4629 save_curwin = curwin;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004630 save_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 curwin = wp;
4632 curbuf = wp->w_buffer;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004633 // Visual mode is only valid in the current window.
4634 if (curwin != save_curwin)
4635 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636
Bram Moolenaar9530b582022-01-22 13:39:08 +00004637 str = eval_to_string_safe(p, use_sandbox, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004639 curwin = save_curwin;
4640 curbuf = save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004641 VIsual_active = save_VIsual_active;
Bram Moolenaar01824652005-01-31 18:58:23 +00004642 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004643 do_unlet((char_u *)"g:actual_curwin", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644
4645 if (str != NULL && *str != 0)
4646 {
4647 if (*skipdigits(str) == NUL)
4648 {
4649 num = atoi((char *)str);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004650 VIM_CLEAR(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 itemisflag = FALSE;
4652 }
4653 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004654
4655 // If the output of the expression needs to be evaluated
4656 // replace the %{} block with the result of evaluation
4657 if (reevaluate && str != NULL && *str != 0
4658 && strchr((const char *)str, '%') != NULL
4659 && evaldepth < MAX_STL_EVAL_DEPTH)
4660 {
4661 size_t parsed_usefmt = (size_t)(block_start - usefmt);
4662 size_t str_length = strlen((const char *)str);
4663 size_t fmt_length = strlen((const char *)s);
4664 size_t new_fmt_len = parsed_usefmt
4665 + str_length + fmt_length + 3;
4666 char_u *new_fmt = (char_u *)alloc(new_fmt_len * sizeof(char_u));
4667 char_u *new_fmt_p = new_fmt;
4668
4669 new_fmt_p = (char_u *)memcpy(new_fmt_p, usefmt, parsed_usefmt)
4670 + parsed_usefmt;
4671 new_fmt_p = (char_u *)memcpy(new_fmt_p , str, str_length)
4672 + str_length;
4673 new_fmt_p = (char_u *)memcpy(new_fmt_p, "%}", 2) + 2;
4674 new_fmt_p = (char_u *)memcpy(new_fmt_p , s, fmt_length)
4675 + fmt_length;
4676 *new_fmt_p = 0;
4677 new_fmt_p = NULL;
4678
4679 if (usefmt != fmt)
4680 vim_free(usefmt);
4681 VIM_CLEAR(str);
4682 usefmt = new_fmt;
4683 s = usefmt + parsed_usefmt;
4684 evaldepth++;
4685 continue;
4686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687#endif
4688 break;
shadmansaleh30e3de22021-05-15 17:23:28 +02004689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 case STL_LINE:
4691 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4692 ? 0L : (long)(wp->w_cursor.lnum);
4693 break;
4694
4695 case STL_NUMLINES:
4696 num = wp->w_buffer->b_ml.ml_line_count;
4697 break;
4698
4699 case STL_COLUMN:
Bram Moolenaar24959102022-05-07 20:01:16 +01004700 num = (State & MODE_INSERT) == 0 && empty_line
4701 ? 0 : (int)wp->w_cursor.col + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 break;
4703
4704 case STL_VIRTCOL:
4705 case STL_VIRTCOL_ALT:
zeertzjq0f112052022-01-14 20:11:38 +00004706 virtcol = wp->w_virtcol + 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004707 // Don't display %V if it's the same as %c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 if (opt == STL_VIRTCOL_ALT
Bram Moolenaar24959102022-05-07 20:01:16 +01004709 && (virtcol == (colnr_T)((State & MODE_INSERT) == 0
4710 && empty_line ? 0 : (int)wp->w_cursor.col + 1)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 break;
4712 num = (long)virtcol;
4713 break;
4714
4715 case STL_PERCENTAGE:
4716 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4717 (long)wp->w_buffer->b_ml.ml_line_count);
4718 break;
4719
4720 case STL_ALTPERCENT:
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004721 str = buf_tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004722 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 break;
4724
4725 case STL_ARGLISTSTAT:
4726 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004727 buf_tmp[0] = 0;
4728 if (append_arg_number(wp, buf_tmp, (int)sizeof(buf_tmp), FALSE))
4729 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 break;
4731
4732 case STL_KEYMAP:
4733 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004734 if (get_keymap_str(wp, (char_u *)"<%s>", buf_tmp, TMPLEN))
4735 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 break;
4737 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004738#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004739 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740#else
4741 num = 0;
4742#endif
4743 break;
4744
4745 case STL_BUFNO:
4746 num = wp->w_buffer->b_fnum;
4747 break;
4748
4749 case STL_OFFSET_X:
4750 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004751 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 case STL_OFFSET:
4753#ifdef FEAT_BYTEOFF
4754 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
Bram Moolenaar24959102022-05-07 20:01:16 +01004755 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0
4756 ? 0L : l + 1 + ((State & MODE_INSERT) == 0 && empty_line
4757 ? 0 : (int)wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758#endif
4759 break;
4760
4761 case STL_BYTEVAL_X:
4762 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004763 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004765 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 if (num == NL)
4767 num = 0;
4768 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4769 num = NL;
4770 break;
4771
4772 case STL_ROFLAG:
4773 case STL_ROFLAG_ALT:
4774 itemisflag = TRUE;
4775 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004776 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 break;
4778
4779 case STL_HELPFLAG:
4780 case STL_HELPFLAG_ALT:
4781 itemisflag = TRUE;
4782 if (wp->w_buffer->b_help)
4783 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004784 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 break;
4786
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 case STL_FILETYPE:
4788 if (*wp->w_buffer->b_p_ft != NUL
4789 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4790 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004791 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), "[%s]",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004792 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004793 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 }
4795 break;
4796
4797 case STL_FILETYPE_ALT:
4798 itemisflag = TRUE;
4799 if (*wp->w_buffer->b_p_ft != NUL
4800 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4801 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004802 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), ",%s",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004803 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004804 for (t = buf_tmp; *t != 0; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 *t = TOUPPER_LOC(*t);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004806 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 }
4808 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809
Bram Moolenaar4033c552017-09-16 20:54:51 +02004810#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 case STL_PREVIEWFLAG:
4812 case STL_PREVIEWFLAG_ALT:
4813 itemisflag = TRUE;
4814 if (wp->w_p_pvw)
4815 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4816 : _("[Preview]"));
4817 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004818
4819 case STL_QUICKFIX:
4820 if (bt_quickfix(wp->w_buffer))
4821 str = (char_u *)(wp->w_llist_ref
4822 ? _(msg_loclist)
4823 : _(msg_qflist));
4824 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825#endif
4826
4827 case STL_MODIFIED:
4828 case STL_MODIFIED_ALT:
4829 itemisflag = TRUE;
4830 switch ((opt == STL_MODIFIED_ALT)
4831 + bufIsChanged(wp->w_buffer) * 2
4832 + (!wp->w_buffer->b_p_ma) * 4)
4833 {
4834 case 2: str = (char_u *)"[+]"; break;
4835 case 3: str = (char_u *)",+"; break;
4836 case 4: str = (char_u *)"[-]"; break;
4837 case 5: str = (char_u *)",-"; break;
4838 case 6: str = (char_u *)"[+-]"; break;
4839 case 7: str = (char_u *)",+-"; break;
4840 }
4841 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004842
4843 case STL_HIGHLIGHT:
4844 t = s;
4845 while (*s != '#' && *s != NUL)
4846 ++s;
4847 if (*s == '#')
4848 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004849 stl_items[curitem].stl_type = Highlight;
4850 stl_items[curitem].stl_start = p;
4851 stl_items[curitem].stl_minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004852 curitem++;
4853 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004854 if (*s != NUL)
4855 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004856 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 }
4858
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004859 stl_items[curitem].stl_start = p;
4860 stl_items[curitem].stl_type = Normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 if (str != NULL && *str)
4862 {
4863 t = str;
4864 if (itemisflag)
4865 {
4866 if ((t[0] && t[1])
4867 && ((!prevchar_isitem && *t == ',')
4868 || (prevchar_isflag && *t == ' ')))
4869 t++;
4870 prevchar_isflag = TRUE;
4871 }
4872 l = vim_strsize(t);
4873 if (l > 0)
4874 prevchar_isitem = TRUE;
4875 if (l > maxwid)
4876 {
4877 while (l >= maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 if (has_mbyte)
4879 {
4880 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004881 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 }
4883 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 l -= byte2cells(*t++);
4885 if (p + 1 >= out + outlen)
4886 break;
4887 *p++ = '<';
4888 }
4889 if (minwid > 0)
4890 {
4891 for (; l < minwid && p + 1 < out + outlen; l++)
4892 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004893 // Don't put a "-" in front of a digit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4895 *p++ = ' ';
4896 else
Bram Moolenaar008bff92021-03-04 21:55:58 +01004897 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 }
4899 minwid = 0;
4900 }
4901 else
4902 minwid *= -1;
Bram Moolenaar008bff92021-03-04 21:55:58 +01004903 for (; *t && p + 1 < out + outlen; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004905 // Change a space by fillchar, unless fillchar is '-' and a
4906 // digit follows.
Bram Moolenaar008bff92021-03-04 21:55:58 +01004907 if (fillable && *t == ' '
4908 && (!VIM_ISDIGIT(*(t + 1)) || fillchar != '-'))
4909 MB_CHAR2BYTES(fillchar, p);
4910 else
4911 *p++ = *t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 }
4913 for (; l < minwid && p + 1 < out + outlen; l++)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004914 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 }
4916 else if (num >= 0)
4917 {
4918 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4919 char_u nstr[20];
4920
4921 if (p + 20 >= out + outlen)
Bram Moolenaarc667da52019-11-30 20:52:27 +01004922 break; // not sufficient space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 prevchar_isitem = TRUE;
4924 t = nstr;
4925 if (opt == STL_VIRTCOL_ALT)
4926 {
4927 *t++ = '-';
4928 minwid--;
4929 }
4930 *t++ = '%';
4931 if (zeropad)
4932 *t++ = '0';
4933 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004934 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 *t = 0;
4936
4937 for (n = num, l = 1; n >= nbase; n /= nbase)
4938 l++;
4939 if (opt == STL_VIRTCOL_ALT)
4940 l++;
4941 if (l > maxwid)
4942 {
4943 l += 2;
4944 n = l - maxwid;
4945 while (l-- > maxwid)
4946 num /= nbase;
4947 *t++ = '>';
4948 *t++ = '%';
4949 *t = t[-3];
4950 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004951 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4952 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 }
4954 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004955 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4956 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 p += STRLEN(p);
4958 }
4959 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004960 stl_items[curitem].stl_type = Empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961
4962 if (opt == STL_VIM_EXPR)
4963 vim_free(str);
4964
4965 if (num >= 0 || (!itemisflag && str && *str))
Bram Moolenaarc667da52019-11-30 20:52:27 +01004966 prevchar_isflag = FALSE; // Item not NULL, but not a flag
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 curitem++;
4968 }
4969 *p = NUL;
4970 itemcnt = curitem;
4971
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004972#ifdef FEAT_EVAL
4973 if (usefmt != fmt)
4974 vim_free(usefmt);
4975#endif
4976
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 width = vim_strsize(out);
4978 if (maxwidth > 0 && width > maxwidth)
4979 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004980 // Result is too long, must truncate somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 l = 0;
4982 if (itemcnt == 0)
4983 s = out;
4984 else
4985 {
4986 for ( ; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004987 if (stl_items[l].stl_type == Trunc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004989 // Truncate at %< item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004990 s = stl_items[l].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 break;
4992 }
4993 if (l == itemcnt)
4994 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004995 // No %< item, truncate first item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004996 s = stl_items[0].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 l = 0;
4998 }
4999 }
5000
5001 if (width - vim_strsize(s) >= maxwidth)
5002 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005003 // Truncation mark is beyond max length
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 if (has_mbyte)
5005 {
5006 s = out;
5007 width = 0;
5008 for (;;)
5009 {
5010 width += ptr2cells(s);
5011 if (width >= maxwidth)
5012 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005013 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005015 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 while (++width < maxwidth)
Bram Moolenaar008bff92021-03-04 21:55:58 +01005017 MB_CHAR2BYTES(fillchar, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 }
5019 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 s = out + maxwidth - 1;
5021 for (l = 0; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005022 if (stl_items[l].stl_start > s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 break;
5024 itemcnt = l;
5025 *s++ = '>';
5026 *s = 0;
5027 }
5028 else
5029 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 if (has_mbyte)
5031 {
5032 n = 0;
5033 while (width >= maxwidth)
5034 {
5035 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005036 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 }
5038 }
5039 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 n = width - maxwidth + 1;
5041 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00005042 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 *s = '<';
5044
Bram Moolenaarc667da52019-11-30 20:52:27 +01005045 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 while (++width < maxwidth)
5047 {
5048 s = s + STRLEN(s);
Bram Moolenaar008bff92021-03-04 21:55:58 +01005049 MB_CHAR2BYTES(fillchar, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 *s = NUL;
5051 }
5052
Bram Moolenaarc667da52019-11-30 20:52:27 +01005053 --n; // count the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 for (; l < itemcnt; l++)
5055 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005056 if (stl_items[l].stl_start - n >= s)
5057 stl_items[l].stl_start -= n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005059 stl_items[l].stl_start = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 }
5061 }
5062 width = maxwidth;
5063 }
5064 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
5065 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005066 // Apply STL_MIDDLE if any
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 for (l = 0; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005068 if (stl_items[l].stl_type == Middle)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 break;
5070 if (l < itemcnt)
5071 {
Bram Moolenaar008bff92021-03-04 21:55:58 +01005072 int middlelength = (maxwidth - width) * MB_CHAR2LEN(fillchar);
5073 p = stl_items[l].stl_start + middlelength;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005074 STRMOVE(p, stl_items[l].stl_start);
Bram Moolenaar008bff92021-03-04 21:55:58 +01005075 for (s = stl_items[l].stl_start; s < p;)
5076 MB_CHAR2BYTES(fillchar, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 for (l++; l < itemcnt; l++)
Bram Moolenaar008bff92021-03-04 21:55:58 +01005078 stl_items[l].stl_start += middlelength;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 width = maxwidth;
5080 }
5081 }
5082
Bram Moolenaarc667da52019-11-30 20:52:27 +01005083 // Store the info about highlighting.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005084 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005086 *hltab = stl_hltab;
5087 sp = stl_hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 for (l = 0; l < itemcnt; l++)
5089 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005090 if (stl_items[l].stl_type == Highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005092 sp->start = stl_items[l].stl_start;
5093 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005094 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 }
5096 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005097 sp->start = NULL;
5098 sp->userhl = 0;
5099 }
5100
Bram Moolenaarc667da52019-11-30 20:52:27 +01005101 // Store the info about tab pages labels.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005102 if (tabtab != NULL)
5103 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005104 *tabtab = stl_tabtab;
5105 sp = stl_tabtab;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005106 for (l = 0; l < itemcnt; l++)
5107 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005108 if (stl_items[l].stl_type == TabPage)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005109 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005110 sp->start = stl_items[l].stl_start;
5111 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005112 sp++;
5113 }
5114 }
5115 sp->start = NULL;
5116 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 }
5118
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00005119 // When inside update_screen we do not want redrawing a statusline, ruler,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005120 // title, etc. to trigger another redraw, it may cause an endless loop.
Bram Moolenaar65ed1362017-09-30 16:00:14 +02005121 if (updating_screen)
5122 {
5123 must_redraw = save_must_redraw;
5124 curwin->w_redr_type = save_redr_type;
5125 }
Bram Moolenaarba2929b2017-09-08 13:59:21 +02005126
Bram Moolenaar0e1f36f2022-02-15 16:17:44 +00005127 // A user function may reset KeyTyped, restore it.
5128 KeyTyped = save_KeyTyped;
5129
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 return width;
5131}
Bram Moolenaarc667da52019-11-30 20:52:27 +01005132#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005134#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
5135 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005137 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
5138 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 */
5140 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005141get_rel_pos(
5142 win_T *wp,
5143 char_u *buf,
5144 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005146 long above; // number of lines above window
5147 long below; // number of lines below window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148
Bram Moolenaarc667da52019-11-30 20:52:27 +01005149 if (buflen < 3) // need at least 3 chars for writing
Bram Moolenaar0027c212015-01-07 13:31:52 +01005150 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 above = wp->w_topline - 1;
5152#ifdef FEAT_DIFF
5153 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02005154 if (wp->w_topline == 1 && wp->w_topfill >= 1)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005155 above = 0; // All buffer lines are displayed and there is an
5156 // indication of filler lines, that can be considered
5157 // seeing all lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158#endif
5159 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
5160 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005161 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
5162 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005164 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005166 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 ? (int)(above / ((above + below) / 100L))
5168 : (int)(above * 100L / (above + below)));
5169}
5170#endif
5171
5172/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005173 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 * Return TRUE if it was appended.
5175 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005176 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005177append_arg_number(
5178 win_T *wp,
5179 char_u *buf,
5180 int buflen,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005181 int add_file) // Add "file" before the arg number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182{
5183 char_u *p;
5184
Bram Moolenaarc667da52019-11-30 20:52:27 +01005185 if (ARGCOUNT <= 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 return FALSE;
5187
Bram Moolenaarc667da52019-11-30 20:52:27 +01005188 p = buf + STRLEN(buf); // go to the end of the buffer
5189 if (p - buf + 35 >= buflen) // getting too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 return FALSE;
5191 *p++ = ' ';
5192 *p++ = '(';
5193 if (add_file)
5194 {
5195 STRCPY(p, "file ");
5196 p += 5;
5197 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005198 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
5199 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
5201 return TRUE;
5202}
5203
5204/*
5205 * If fname is not a full path, make it a full path.
5206 * Returns pointer to allocated memory (NULL for failure).
5207 */
5208 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005209fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210{
5211 /*
5212 * Force expanding the path always for Unix, because symbolic links may
5213 * mess up the full path name, even though it starts with a '/'.
5214 * Also expand when there is ".." in the file name, try to remove it,
5215 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00005216 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 * For MS-Windows also expand names like "longna~1" to "longname".
5218 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00005219#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 return FullName_save(fname, TRUE);
5221#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00005222 if (!vim_isAbsName(fname)
5223 || strstr((char *)fname, "..") != NULL
5224 || strstr((char *)fname, "//") != NULL
5225# ifdef BACKSLASH_IN_FILENAME
5226 || strstr((char *)fname, "\\\\") != NULL
5227# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005228# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00005230# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 )
5232 return FullName_save(fname, FALSE);
5233
5234 fname = vim_strsave(fname);
5235
Bram Moolenaar9b942202007-10-03 12:31:33 +00005236# ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01005237 if (fname != NULL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005238 fname_case(fname, 0); // set correct case for file name
Bram Moolenaar9b942202007-10-03 12:31:33 +00005239# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240
5241 return fname;
5242#endif
5243}
5244
5245/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005246 * Make "*ffname" a full file name, set "*sfname" to "*ffname" if not NULL.
5247 * "*ffname" becomes a pointer to allocated memory (or NULL).
5248 * When resolving a link both "*sfname" and "*ffname" will point to the same
5249 * allocated memory.
5250 * The "*ffname" and "*sfname" pointer values on call will not be freed.
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005251 * Note that the resulting "*ffname" pointer should be considered not allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005254fname_expand(
5255 buf_T *buf UNUSED,
5256 char_u **ffname,
5257 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005259 if (*ffname == NULL) // no file name given, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 return;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005261 if (*sfname == NULL) // no short file name given, use ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 *sfname = *ffname;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005263 *ffname = fix_fname(*ffname); // expand to full path
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264
5265#ifdef FEAT_SHORTCUT
5266 if (!buf->b_p_bin)
5267 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005268 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005270 // If the file name is a shortcut file, use the file it links to.
Bram Moolenaardce1e892019-02-10 23:18:53 +01005271 rfname = mch_resolve_path(*ffname, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005272 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 {
5274 vim_free(*ffname);
5275 *ffname = rfname;
5276 *sfname = rfname;
5277 }
5278 }
5279#endif
5280}
5281
5282/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 * Open a window for a number of buffers.
5284 */
5285 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005286ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287{
5288 buf_T *buf;
5289 win_T *wp, *wpnext;
5290 int split_ret = OK;
5291 int p_ea_save;
5292 int open_wins = 0;
5293 int r;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005294 int count; // Maximum number of windows to open.
5295 int all; // When TRUE also load inactive buffers.
Bram Moolenaare1004402020-10-24 20:49:43 +02005296 int had_tab = cmdmod.cmod_tab;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005297 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298
Bram Moolenaarc667da52019-11-30 20:52:27 +01005299 if (eap->addr_count == 0) // make as many windows as possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 count = 9999;
5301 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01005302 count = eap->line2; // make as many windows as specified
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5304 all = FALSE;
5305 else
5306 all = TRUE;
5307
5308 setpcmark();
5309
5310#ifdef FEAT_GUI
5311 need_mouse_correct = TRUE;
5312#endif
5313
5314 /*
5315 * Close superfluous windows (two windows for the same buffer).
5316 * Also close windows that are not full-width.
5317 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005318 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005319 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005320 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005322 tpnext = curtab->tp_next;
5323 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005325 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005326 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005327 || ((cmdmod.cmod_split & WSP_VERT)
5328 ? wp->w_height + wp->w_status_height < Rows - p_ch
5329 - tabline_height()
5330 : wp->w_width != Columns)
5331 || (had_tab > 0 && wp != firstwin))
5332 && !ONE_WINDOW
5333 && !(wp->w_closing || wp->w_buffer->b_locked > 0)
5334 && !win_unlisted(wp))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005335 {
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00005336 if (win_close(wp, FALSE) == FAIL)
5337 break;
5338 // Just in case an autocommand does something strange with
5339 // windows: start all over...
5340 wpnext = firstwin;
5341 tpnext = first_tabpage;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005342 open_wins = 0;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005343 }
5344 else
5345 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005347
Bram Moolenaarc667da52019-11-30 20:52:27 +01005348 // Without the ":tab" modifier only do the current tab page.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005349 if (had_tab == 0 || tpnext == NULL)
5350 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005351 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 }
5353
5354 /*
5355 * Go through the buffer list. When a buffer doesn't have a window yet,
5356 * open one. Otherwise move the window to the right position.
5357 * Watch out for autocommands that delete buffers or windows!
5358 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01005359 // Don't execute Win/Buf Enter/Leave autocommands here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 ++autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 win_enter(lastwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5364 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005365 // Check if this buffer needs a window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5367 continue;
5368
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005369 if (had_tab != 0)
5370 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005371 // With the ":tab" modifier don't move the window.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005372 if (buf->b_nwindows > 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005373 wp = lastwin; // buffer has a window, skip it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005374 else
5375 wp = NULL;
5376 }
5377 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005378 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005379 // Check if this buffer already has a window
Bram Moolenaar29323592016-07-24 22:04:11 +02005380 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005381 if (wp->w_buffer == buf)
5382 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005383 // If the buffer already has a window, move it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005384 if (wp != NULL)
5385 win_move_after(wp, curwin);
5386 }
5387
5388 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005390 bufref_T bufref;
5391
5392 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005393
Bram Moolenaarc667da52019-11-30 20:52:27 +01005394 // Split the window and put the buffer in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 p_ea_save = p_ea;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005396 p_ea = TRUE; // use space from all windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5398 ++open_wins;
5399 p_ea = p_ea_save;
5400 if (split_ret == FAIL)
5401 continue;
5402
Bram Moolenaarc667da52019-11-30 20:52:27 +01005403 // Open the buffer in this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005406 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005408 // autocommands deleted the buffer!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 swap_exists_action = SEA_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 break;
5411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 if (swap_exists_action == SEA_QUIT)
5413 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005414#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005415 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005416
Bram Moolenaarc667da52019-11-30 20:52:27 +01005417 // Reset the error/interrupt/exception state here so that
5418 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005419 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005420#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005421
Bram Moolenaarc667da52019-11-30 20:52:27 +01005422 // User selected Quit at ATTENTION prompt; close this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 win_close(curwin, TRUE);
5424 --open_wins;
5425 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005426 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005427
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005428#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005429 // Restore the error/interrupt/exception state if not
5430 // discarded by a new aborting error, interrupt, or uncaught
5431 // exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005432 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005433#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 }
5435 else
5436 handle_swap_exists(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 }
5438
5439 ui_breakcheck();
5440 if (got_int)
5441 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005442 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 break;
5444 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005445#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01005446 // Autocommands deleted the buffer or aborted script processing!!!
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005447 if (aborting())
5448 break;
5449#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01005450 // When ":tab" was used open a new tab for a new window repeatedly.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005451 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
Bram Moolenaare1004402020-10-24 20:49:43 +02005452 cmdmod.cmod_tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 --autocmd_no_enter;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005455 win_enter(firstwin, FALSE); // back to first window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457
5458 /*
5459 * Close superfluous windows.
5460 */
5461 for (wp = lastwin; open_wins > count; )
5462 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005463 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 || autowrite(wp->w_buffer, FALSE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 if (!win_valid(wp))
5466 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005467 // BufWrite Autocommands made the window invalid, start over
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 wp = lastwin;
5469 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005470 else if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005472 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 --open_wins;
5474 wp = lastwin;
5475 }
5476 else
5477 {
5478 wp = wp->w_prev;
5479 if (wp == NULL)
5480 break;
5481 }
5482 }
5483}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005486static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005487
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488/*
5489 * do_modelines() - process mode lines for the current file
5490 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005491 * "flags" can be:
5492 * OPT_WINONLY only set options local to window
5493 * OPT_NOWIN don't set options local to window
5494 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 * Returns immediately if the "ml" option isn't set.
5496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005498do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005500 linenr_T lnum;
5501 int nmlines;
5502 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503
5504 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5505 return;
5506
Bram Moolenaarc667da52019-11-30 20:52:27 +01005507 // Disallow recursive entry here. Can happen when executing a modeline
5508 // triggers an autocommand, which reloads modelines with a ":do".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 if (entered)
5510 return;
5511
5512 ++entered;
Hu Jialun9dcd3492021-08-28 20:42:50 +02005513 for (lnum = 1; curbuf->b_p_ml && lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005515 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 nmlines = 0;
5517
Hu Jialun9dcd3492021-08-28 20:42:50 +02005518 for (lnum = curbuf->b_ml.ml_line_count; curbuf->b_p_ml && lnum > 0 && lnum > nmlines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005520 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 nmlines = 0;
5522 --entered;
5523}
5524
Bram Moolenaarc667da52019-11-30 20:52:27 +01005525#include "version.h" // for version number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526
5527/*
5528 * chk_modeline() - check a single line for a mode string
5529 * Return FAIL if an error encountered.
5530 */
5531 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005532chk_modeline(
5533 linenr_T lnum,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005534 int flags) // Same as for do_modelines().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535{
5536 char_u *s;
5537 char_u *e;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005538 char_u *linecopy; // local copy of any modeline found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 int prev;
5540 int vers;
5541 int end;
5542 int retval = OK;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005543 sctx_T save_current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005544
Bram Moolenaare31ee862020-01-07 20:59:34 +01005545 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546
5547 prev = -1;
5548 for (s = ml_get(lnum); *s != NUL; ++s)
5549 {
5550 if (prev == -1 || vim_isspace(prev))
5551 {
5552 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5553 || STRNCMP(s, "vi:", (size_t)3) == 0)
5554 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005555 // Accept both "vim" and "Vim".
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005556 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 {
5558 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5559 e = s + 4;
5560 else
5561 e = s + 3;
5562 vers = getdigits(&e);
5563 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005564 && (s[0] != 'V'
5565 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 && (s[3] == ':'
5567 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5568 || (VIM_VERSION_100 < vers && s[3] == '<')
5569 || (VIM_VERSION_100 > vers && s[3] == '>')
5570 || (VIM_VERSION_100 == vers && s[3] == '=')))
5571 break;
5572 }
5573 }
5574 prev = *s;
5575 }
5576
5577 if (*s)
5578 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005579 do // skip over "ex:", "vi:" or "vim:"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 ++s;
5581 while (s[-1] != ':');
5582
Bram Moolenaarc667da52019-11-30 20:52:27 +01005583 s = linecopy = vim_strsave(s); // copy the line, it will change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 if (linecopy == NULL)
5585 return FAIL;
5586
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005587 // prepare for emsg()
5588 estack_push(ETYPE_MODELINE, (char_u *)"modelines", lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01005589 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590
5591 end = FALSE;
5592 while (end == FALSE)
5593 {
5594 s = skipwhite(s);
5595 if (*s == NUL)
5596 break;
5597
5598 /*
5599 * Find end of set command: ':' or end of line.
5600 * Skip over "\:", replacing it with ":".
5601 */
5602 for (e = s; *e != ':' && *e != NUL; ++e)
5603 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005604 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 if (*e == NUL)
5606 end = TRUE;
5607
5608 /*
5609 * If there is a "set" command, require a terminating ':' and
5610 * ignore the stuff after the ':'.
5611 * "vi:set opt opt opt: foo" -- foo not interpreted
5612 * "vi:opt opt opt: foo" -- foo interpreted
5613 * Accept "se" for compatibility with Elvis.
5614 */
5615 if (STRNCMP(s, "set ", (size_t)4) == 0
5616 || STRNCMP(s, "se ", (size_t)3) == 0)
5617 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005618 if (*e != ':') // no terminating ':'?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 break;
5620 end = TRUE;
5621 s = vim_strchr(s, ' ') + 1;
5622 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005623 *e = NUL; // truncate the set command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624
Bram Moolenaarc667da52019-11-30 20:52:27 +01005625 if (*s != NUL) // skip over an empty "::"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 {
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005627 int secure_save = secure;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005628
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005629 save_current_sctx = current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005630 current_sctx.sc_version = 1;
5631#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005632 current_sctx.sc_sid = SID_MODELINE;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005633 current_sctx.sc_seq = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005634 current_sctx.sc_lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005636
Bram Moolenaar5958f952018-11-20 04:25:21 +01005637 // Make sure no risky things are executed as a side effect.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01005638 secure = 1;
Bram Moolenaar5958f952018-11-20 04:25:21 +01005639
Bram Moolenaara3227e22006-03-08 21:32:40 +00005640 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar5958f952018-11-20 04:25:21 +01005641
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005642 secure = secure_save;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005643 current_sctx = save_current_sctx;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005644 if (retval == FAIL) // stop if error found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 break;
5646 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005647 s = e + 1; // advance to next part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 }
5649
Bram Moolenaare31ee862020-01-07 20:59:34 +01005650 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005651 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 vim_free(linecopy);
5653 }
5654 return retval;
5655}
5656
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005657/*
Bram Moolenaar91335e52018-08-01 17:53:12 +02005658 * Return TRUE if "buf" is a normal buffer, 'buftype' is empty.
5659 */
5660 int
5661bt_normal(buf_T *buf)
5662{
5663 return buf != NULL && buf->b_p_bt[0] == NUL;
5664}
5665
Bram Moolenaar113e1072019-01-20 15:30:40 +01005666#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar91335e52018-08-01 17:53:12 +02005667/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005668 * Return TRUE if "buf" is the quickfix buffer.
5669 */
5670 int
5671bt_quickfix(buf_T *buf)
5672{
5673 return buf != NULL && buf->b_p_bt[0] == 'q';
5674}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005675#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005676
Bram Moolenaar113e1072019-01-20 15:30:40 +01005677#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005678/*
5679 * Return TRUE if "buf" is a terminal buffer.
5680 */
5681 int
5682bt_terminal(buf_T *buf)
5683{
5684 return buf != NULL && buf->b_p_bt[0] == 't';
5685}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005686#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005687
5688/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005689 * Return TRUE if "buf" is a help buffer.
5690 */
5691 int
5692bt_help(buf_T *buf)
5693{
5694 return buf != NULL && buf->b_help;
5695}
5696
5697/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02005698 * Return TRUE if "buf" is a prompt buffer.
5699 */
5700 int
5701bt_prompt(buf_T *buf)
5702{
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005703 return buf != NULL && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'r';
5704}
5705
Dominique Pelle748b3082022-01-08 12:41:16 +00005706#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005707/*
5708 * Return TRUE if "buf" is a buffer for a popup window.
5709 */
5710 int
5711bt_popup(buf_T *buf)
5712{
5713 return buf != NULL && buf->b_p_bt != NULL
5714 && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'o';
Bram Moolenaarf2732452018-06-03 14:47:35 +02005715}
Dominique Pelle748b3082022-01-08 12:41:16 +00005716#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +02005717
5718/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005719 * Return TRUE if "buf" is a "nofile", "acwrite", "terminal" or "prompt"
5720 * buffer. This means the buffer name is not a file name.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005721 */
5722 int
Bram Moolenaar26910de2019-06-15 19:37:15 +02005723bt_nofilename(buf_T *buf)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005724{
5725 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5726 || buf->b_p_bt[0] == 'a'
Bram Moolenaarf2732452018-06-03 14:47:35 +02005727 || buf->b_p_bt[0] == 't'
5728 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005729}
5730
Dominique Pelle748b3082022-01-08 12:41:16 +00005731#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005732/*
Bram Moolenaar26910de2019-06-15 19:37:15 +02005733 * Return TRUE if "buf" has 'buftype' set to "nofile".
5734 */
5735 int
5736bt_nofile(buf_T *buf)
5737{
5738 return buf != NULL && buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f';
5739}
Dominique Pelle748b3082022-01-08 12:41:16 +00005740#endif
Bram Moolenaar26910de2019-06-15 19:37:15 +02005741
5742/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005743 * Return TRUE if "buf" is a "nowrite", "nofile", "terminal" or "prompt"
5744 * buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005745 */
5746 int
5747bt_dontwrite(buf_T *buf)
5748{
Bram Moolenaarf2732452018-06-03 14:47:35 +02005749 return buf != NULL && (buf->b_p_bt[0] == 'n'
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005750 || buf->b_p_bt[0] == 't'
5751 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005752}
5753
Bram Moolenaar113e1072019-01-20 15:30:40 +01005754#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005755 int
5756bt_dontwrite_msg(buf_T *buf)
5757{
5758 if (bt_dontwrite(buf))
5759 {
Bram Moolenaarf1474d82021-12-31 19:59:55 +00005760 emsg(_(e_cannot_write_buftype_option_is_set));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005761 return TRUE;
5762 }
5763 return FALSE;
5764}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005765#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005766
5767/*
5768 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
5769 * and 'bufhidden'.
5770 */
5771 int
5772buf_hide(buf_T *buf)
5773{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005774 // 'bufhidden' overrules 'hidden' and ":hide", check it first
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005775 switch (buf->b_p_bh[0])
5776 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005777 case 'u': // "unload"
5778 case 'w': // "wipe"
5779 case 'd': return FALSE; // "delete"
5780 case 'h': return TRUE; // "hide"
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005781 }
Bram Moolenaare1004402020-10-24 20:49:43 +02005782 return (p_hid || (cmdmod.cmod_flags & CMOD_HIDE));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005783}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784
5785/*
5786 * Return special buffer name.
5787 * Returns NULL when the buffer has a normal file name.
5788 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005789 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005790buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791{
Bram Moolenaar4033c552017-09-16 20:54:51 +02005792#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005794 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005795 /*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005796 * Differentiate between the quickfix and location list buffers using
5797 * the buffer number stored in the global quickfix stack.
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005798 */
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005799 if (buf->b_fnum == qf_stack_get_bufnr())
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005800 return (char_u *)_(msg_qflist);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005801 else
5802 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02005805
Bram Moolenaarc667da52019-11-30 20:52:27 +01005806 // There is no _file_ when 'buftype' is "nofile", b_sfname
5807 // contains the name as specified by the user.
Bram Moolenaar26910de2019-06-15 19:37:15 +02005808 if (bt_nofilename(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 {
Bram Moolenaar21554412017-07-24 21:44:43 +02005810#ifdef FEAT_TERMINAL
5811 if (buf->b_term != NULL)
5812 return term_get_status_text(buf->b_term);
5813#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02005814 if (buf->b_fname != NULL)
5815 return buf->b_fname;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02005816#ifdef FEAT_JOB_CHANNEL
5817 if (bt_prompt(buf))
5818 return (char_u *)_("[Prompt]");
5819#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005820#ifdef FEAT_PROP_POPUP
Bram Moolenaarc6896e22019-05-30 22:32:34 +02005821 if (bt_popup(buf))
5822 return (char_u *)_("[Popup]");
5823#endif
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005824 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 }
Bram Moolenaar21554412017-07-24 21:44:43 +02005826
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 if (buf->b_fname == NULL)
Bram Moolenaar00806bc2020-11-05 19:36:38 +01005828 return buf_get_fname(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 return NULL;
5830}
5831
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832/*
Bram Moolenaar00806bc2020-11-05 19:36:38 +01005833 * Get "buf->b_fname", use "[No Name]" if it is NULL.
5834 */
5835 char_u *
5836buf_get_fname(buf_T *buf)
5837{
5838 if (buf->b_fname == NULL)
5839 return (char_u *)_("[No Name]");
5840 return buf->b_fname;
5841}
5842
5843/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5845 */
5846 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005847set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848{
5849 if (on != curbuf->b_p_bl)
5850 {
5851 curbuf->b_p_bl = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 if (on)
5853 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5854 else
5855 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 }
5857}
5858
5859/*
5860 * Read the file for "buf" again and check if the contents changed.
5861 * Return TRUE if it changed or this could not be checked.
5862 */
5863 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005864buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865{
5866 buf_T *newbuf;
5867 int differ = TRUE;
5868 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 exarg_T ea;
5871
Bram Moolenaarc667da52019-11-30 20:52:27 +01005872 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5874 if (newbuf == NULL)
5875 return TRUE;
5876
Bram Moolenaarc667da52019-11-30 20:52:27 +01005877 // Force the 'fileencoding' and 'fileformat' to be equal.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 if (prep_exarg(&ea, buf) == FAIL)
5879 {
5880 wipe_buffer(newbuf, FALSE);
5881 return TRUE;
5882 }
5883
Bram Moolenaarc667da52019-11-30 20:52:27 +01005884 // set curwin/curbuf to buf and save a few things
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886
Bram Moolenaar4770d092006-01-12 23:22:24 +00005887 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 && readfile(buf->b_ffname, buf->b_fname,
5889 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5890 &ea, READ_NEW | READ_DUMMY) == OK)
5891 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005892 // compare the two files line by line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5894 {
5895 differ = FALSE;
5896 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5897 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5898 {
5899 differ = TRUE;
5900 break;
5901 }
5902 }
5903 }
5904 vim_free(ea.cmd);
5905
Bram Moolenaarc667da52019-11-30 20:52:27 +01005906 // restore curwin/curbuf and a few other things
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908
Bram Moolenaarc667da52019-11-30 20:52:27 +01005909 if (curbuf != newbuf) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 wipe_buffer(newbuf, FALSE);
5911
5912 return differ;
5913}
5914
5915/*
5916 * Wipe out a buffer and decrement the last buffer number if it was used for
5917 * this buffer. Call this to wipe out a temp buffer that does not contain any
5918 * marks.
5919 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005921wipe_buffer(
5922 buf_T *buf,
Bram Moolenaara6e8f882019-12-14 16:18:15 +01005923 int aucmd) // When TRUE trigger autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924{
5925 if (buf->b_fnum == top_file_num - 1)
5926 --top_file_num;
5927
Bram Moolenaarc667da52019-11-30 20:52:27 +01005928 if (!aucmd) // Don't trigger BufDelete autocommands here.
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005929 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005930
Bram Moolenaara6e8f882019-12-14 16:18:15 +01005931 close_buffer(NULL, buf, DOBUF_WIPE, FALSE, TRUE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005932
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005934 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935}