blob: c5b273c4a61f1ab784e445ca49a06b7694a569fb [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
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141/*
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200142 * Ensure buffer "buf" is loaded. Does not trigger the swap-exists action.
143 */
144 void
145buffer_ensure_loaded(buf_T *buf)
146{
147 if (buf->b_ml.ml_mfp == NULL)
148 {
149 aco_save_T aco;
150
151 aucmd_prepbuf(&aco, buf);
152 swap_exists_action = SEA_NONE;
153 open_buffer(FALSE, NULL, 0);
154 aucmd_restbuf(&aco);
155 }
156}
157
158/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200159 * Open current buffer, that is: open the memfile and read the file into
160 * memory.
161 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 */
163 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100164open_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +0100165 int read_stdin, // read file from stdin
166 exarg_T *eap, // for forced 'ff' and 'fenc' or NULL
167 int flags) // extra flags for readfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168{
169 int retval = OK;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200170 bufref_T old_curbuf;
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100171#ifdef FEAT_SYN_HL
172 long old_tw = curbuf->b_p_tw;
173#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200174 int read_fifo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100176 // The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
177 // When re-entering the same buffer, it should not change, because the
178 // user may have reset the flag by hand.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 if (readonlymode && curbuf->b_ffname != NULL
180 && (curbuf->b_flags & BF_NEVERLOADED))
181 curbuf->b_p_ro = TRUE;
182
Bram Moolenaar4770d092006-01-12 23:22:24 +0000183 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100185 // There MUST be a memfile, otherwise we can't do anything
186 // If we can't create one for the current buffer, take another buffer
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100187 close_buffer(NULL, curbuf, 0, FALSE, FALSE);
Bram Moolenaar29323592016-07-24 22:04:11 +0200188 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 if (curbuf->b_ml.ml_mfp != NULL)
190 break;
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100191 // If there is no memfile at all, exit.
192 // This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 if (curbuf == NULL)
194 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +0000195 emsg(_(e_cannot_allocate_any_buffer_exiting));
Bram Moolenaar6f10c702019-08-20 22:58:37 +0200196
197 // Don't try to do any saving, with "curbuf" NULL almost nothing
198 // will work.
199 v_dying = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200 getout(2);
201 }
Bram Moolenaar6f10c702019-08-20 22:58:37 +0200202
Bram Moolenaar40bcec12021-12-05 22:19:27 +0000203 emsg(_(e_cannot_allocate_buffer_using_other_one));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204 enter_buffer(curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100205#ifdef FEAT_SYN_HL
206 if (old_tw != curbuf->b_p_tw)
207 check_colorcolumn(curwin);
208#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 return FAIL;
210 }
211
Bram Moolenaarc667da52019-11-30 20:52:27 +0100212 // The autocommands in readfile() may change the buffer, but only AFTER
213 // reading the file.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200214 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 modified_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216
Bram Moolenaarc667da52019-11-30 20:52:27 +0100217 // mark cursor position as being invalid
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100218 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219
220 if (curbuf->b_ffname != NULL
221#ifdef FEAT_NETBEANS_INTG
222 && netbeansReadFile
223#endif
224 )
225 {
Bram Moolenaar426dd022016-03-15 15:09:29 +0100226 int old_msg_silent = msg_silent;
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200227#ifdef UNIX
228 int save_bin = curbuf->b_p_bin;
229 int perm;
230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231#ifdef FEAT_NETBEANS_INTG
232 int oldFire = netbeansFireChanges;
233
234 netbeansFireChanges = 0;
235#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200236#ifdef UNIX
237 perm = mch_getperm(curbuf->b_ffname);
Bram Moolenaard569bb02018-08-11 13:57:20 +0200238 if (perm >= 0 && (S_ISFIFO(perm)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200239 || S_ISSOCK(perm)
Bram Moolenaarf04507d2016-08-20 15:05:39 +0200240# ifdef OPEN_CHR_FILES
241 || (S_ISCHR(perm) && is_dev_fd_file(curbuf->b_ffname))
242# endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200243 ))
244 read_fifo = TRUE;
245 if (read_fifo)
246 curbuf->b_p_bin = TRUE;
247#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100248 if (shortmess(SHM_FILEINFO))
249 msg_silent = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200251 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200252 flags | READ_NEW | (read_fifo ? READ_FIFO : 0));
253#ifdef UNIX
254 if (read_fifo)
255 {
256 curbuf->b_p_bin = save_bin;
257 if (retval == OK)
258 retval = read_buffer(FALSE, eap, flags);
259 }
260#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100261 msg_silent = old_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262#ifdef FEAT_NETBEANS_INTG
263 netbeansFireChanges = oldFire;
264#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100265 // Help buffer is filtered.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +0200266 if (bt_help(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 fix_help_buffer();
268 }
269 else if (read_stdin)
270 {
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200271 int save_bin = curbuf->b_p_bin;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100273 // First read the text in binary mode into the buffer.
274 // Then read from that same buffer and append at the end. This makes
275 // it possible to retry when 'fileformat' or 'fileencoding' was
276 // guessed wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 curbuf->b_p_bin = TRUE;
278 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200279 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
280 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281 curbuf->b_p_bin = save_bin;
282 if (retval == OK)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200283 retval = read_buffer(TRUE, eap, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284 }
285
Bram Moolenaarc667da52019-11-30 20:52:27 +0100286 // if first time loading this buffer, init b_chartab[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 if (curbuf->b_flags & BF_NEVERLOADED)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100288 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 (void)buf_init_chartab(curbuf, FALSE);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100290#ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100291 parse_cino(curbuf);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100292#endif
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100295 // Set/reset the Changed flag first, autocmds may change the buffer.
296 // Apply the automatic commands, before processing the modelines.
297 // So the modelines have priority over autocommands.
298 //
Bram Moolenaarc667da52019-11-30 20:52:27 +0100299 // When reading stdin, the buffer contents always needs writing, so set
300 // the changed flag. Unless in readonly mode: "ls | gview -".
301 // When interrupted and 'cpoptions' contains 'i' set changed flag.
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000302 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100303 || modified_was_set // ":set modified" used in autocmd
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100304#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000307 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100309 else if (retval == OK && !read_stdin && !read_fifo)
Bram Moolenaarc024b462019-06-08 18:07:21 +0200310 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaarc667da52019-11-30 20:52:27 +0100311 save_file_ff(curbuf); // keep this fileformat
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312
Bram Moolenaarc667da52019-11-30 20:52:27 +0100313 // Set last_changedtick to avoid triggering a TextChanged autocommand right
314 // after it was added.
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100315 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Christian Brabandtdb3b4462021-10-16 11:58:55 +0100316 curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100317 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100318
Bram Moolenaarc667da52019-11-30 20:52:27 +0100319 // require "!" to overwrite the file, because it wasn't read completely
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320#ifdef FEAT_EVAL
321 if (aborting())
322#else
323 if (got_int)
324#endif
325 curbuf->b_flags |= BF_READERR;
326
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000327#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +0100328 // Need to update automatic folding. Do this before the autocommands,
329 // they may use the fold info.
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000330 foldUpdateAll(curwin);
331#endif
332
Bram Moolenaarc667da52019-11-30 20:52:27 +0100333 // need to set w_topline, unless some autocommand already did that.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 if (!(curwin->w_valid & VALID_TOPLINE))
335 {
336 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100337#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100341#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100343#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345#endif
346
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100347 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100349 // The autocommands may have changed the current buffer. Apply the
350 // modelines to the correct buffer, if it still exists and is loaded.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200351 if (bufref_valid(&old_curbuf) && old_curbuf.br_buf->b_ml.ml_mfp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 {
353 aco_save_T aco;
354
Bram Moolenaarc667da52019-11-30 20:52:27 +0100355 // Go to the buffer that was opened.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200356 aucmd_prepbuf(&aco, old_curbuf.br_buf);
Bram Moolenaara3227e22006-03-08 21:32:40 +0000357 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
359
Bram Moolenaar1d30fde2021-10-20 21:58:42 +0100360 if ((flags & READ_NOWINENTER) == 0)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100361#ifdef FEAT_EVAL
Bram Moolenaar1d30fde2021-10-20 21:58:42 +0100362 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE,
363 curbuf, &retval);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100364#else
Bram Moolenaar1d30fde2021-10-20 21:58:42 +0100365 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367
Bram Moolenaarc667da52019-11-30 20:52:27 +0100368 // restore curwin/curbuf and a few other things
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 aucmd_restbuf(&aco);
370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 }
372
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373 return retval;
374}
375
376/*
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200377 * Store "buf" in "bufref" and set the free count.
378 */
379 void
380set_bufref(bufref_T *bufref, buf_T *buf)
381{
382 bufref->br_buf = buf;
Bram Moolenaarfadacf02017-06-19 20:35:32 +0200383 bufref->br_fnum = buf == NULL ? 0 : buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200384 bufref->br_buf_free_count = buf_free_count;
385}
386
387/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200388 * Return TRUE if "bufref->br_buf" points to the same buffer as when
389 * set_bufref() was called and it is a valid buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200390 * Only goes through the buffer list if buf_free_count changed.
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200391 * Also checks if b_fnum is still the same, a :bwipe followed by :new might get
392 * the same allocated memory, but it's a different buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200393 */
394 int
395bufref_valid(bufref_T *bufref)
396{
397 return bufref->br_buf_free_count == buf_free_count
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200398 ? TRUE : buf_valid(bufref->br_buf)
399 && bufref->br_fnum == bufref->br_buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200400}
401
402/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200404 * This can be slow if there are many buffers, prefer using bufref_valid().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405 */
406 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100407buf_valid(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408{
409 buf_T *bp;
410
Bram Moolenaarc667da52019-11-30 20:52:27 +0100411 // Assume that we more often have a recent buffer, start with the last
412 // one.
Bram Moolenaar00d253e2020-04-06 22:13:01 +0200413 FOR_ALL_BUFS_FROM_LAST(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 if (bp == buf)
415 return TRUE;
416 return FALSE;
417}
418
419/*
Bram Moolenaar480778b2016-07-14 22:09:39 +0200420 * A hash table used to quickly lookup a buffer by its number.
421 */
422static hashtab_T buf_hashtab;
423
424 static void
425buf_hashtab_add(buf_T *buf)
426{
427 sprintf((char *)buf->b_key, "%x", buf->b_fnum);
428 if (hash_add(&buf_hashtab, buf->b_key) == FAIL)
Bram Moolenaarf1474d82021-12-31 19:59:55 +0000429 emsg(_(e_buffer_cannot_be_registered));
Bram Moolenaar480778b2016-07-14 22:09:39 +0200430}
431
432 static void
433buf_hashtab_remove(buf_T *buf)
434{
435 hashitem_T *hi = hash_find(&buf_hashtab, buf->b_key);
436
437 if (!HASHITEM_EMPTY(hi))
438 hash_remove(&buf_hashtab, hi);
439}
440
Bram Moolenaar94f01952018-09-01 15:30:03 +0200441/*
442 * Return TRUE when buffer "buf" can be unloaded.
443 * Give an error message and return FALSE when the buffer is locked or the
444 * screen is being redrawn and the buffer is in a window.
445 */
446 static int
447can_unload_buffer(buf_T *buf)
448{
449 int can_unload = !buf->b_locked;
450
451 if (can_unload && updating_screen)
452 {
453 win_T *wp;
454
455 FOR_ALL_WINDOWS(wp)
456 if (wp->w_buffer == buf)
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200457 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200458 can_unload = FALSE;
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200459 break;
460 }
Bram Moolenaar94f01952018-09-01 15:30:03 +0200461 }
462 if (!can_unload)
Bram Moolenaarf1474d82021-12-31 19:59:55 +0000463 semsg(_(e_attempt_to_delete_buffer_that_is_in_use_str), buf->b_fname);
Bram Moolenaar94f01952018-09-01 15:30:03 +0200464 return can_unload;
465}
Bram Moolenaara997b452018-04-17 23:24:06 +0200466
Bram Moolenaar480778b2016-07-14 22:09:39 +0200467/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468 * Close the link to a buffer.
469 * "action" is used when there is no longer a window for the buffer.
470 * It can be:
471 * 0 buffer becomes hidden
472 * DOBUF_UNLOAD buffer is unloaded
473 * DOBUF_DELETE buffer is unloaded and removed from buffer list
474 * DOBUF_WIPE buffer is unloaded and really deleted
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200475 * DOBUF_WIPE_REUSE idem, and add to buf_reuse list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 * When doing all but the first one on the current buffer, the caller should
477 * get a new buffer very soon!
478 *
479 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100480 *
481 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
482 * cause there to be only one window with this buffer. e.g. when ":quit" is
483 * supposed to close the window but autocommands close all other windows.
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100484 *
485 * When "ignore_abort" is TRUE don't abort even when aborting() returns TRUE.
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100486 *
487 * Return TRUE when we got to the end and b_nwindows was decremented.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 */
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100489 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100490close_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +0100491 win_T *win, // if not NULL, set b_last_cursor
Bram Moolenaar7454a062016-01-30 15:14:10 +0100492 buf_T *buf,
493 int action,
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100494 int abort_if_last,
495 int ignore_abort)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100498 int nwindows;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200499 bufref_T bufref;
Bram Moolenaar3a117e12016-10-30 21:57:52 +0100500 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200501 win_T *the_curwin = curwin;
502 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 int unload_buf = (action != 0);
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200504 int wipe_buf = (action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
505 int del_buf = (action == DOBUF_DEL || wipe_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506
Bram Moolenaarcee52202020-03-11 14:19:58 +0100507 CHECK_CURBUF;
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100508
509 // Force unloading or deleting when 'bufhidden' says so.
510 // The caller must take care of NOT deleting/freeing when 'bufhidden' is
511 // "hide" (otherwise we could never free or delete a buffer).
Bram Moolenaarc667da52019-11-30 20:52:27 +0100512 if (buf->b_p_bh[0] == 'd') // 'bufhidden' == "delete"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200513 {
514 del_buf = TRUE;
515 unload_buf = TRUE;
516 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100517 else if (buf->b_p_bh[0] == 'w') // 'bufhidden' == "wipe"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200518 {
519 del_buf = TRUE;
520 unload_buf = TRUE;
521 wipe_buf = TRUE;
522 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100523 else if (buf->b_p_bh[0] == 'u') // 'bufhidden' == "unload"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200524 unload_buf = TRUE;
525
Bram Moolenaar94053a52017-08-01 21:44:33 +0200526#ifdef FEAT_TERMINAL
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200527 if (bt_terminal(buf) && (buf->b_nwindows == 1 || del_buf))
Bram Moolenaar94053a52017-08-01 21:44:33 +0200528 {
Bram Moolenaarcee52202020-03-11 14:19:58 +0100529 CHECK_CURBUF;
Bram Moolenaar94053a52017-08-01 21:44:33 +0200530 if (term_job_running(buf->b_term))
531 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200532 if (wipe_buf || unload_buf)
Bram Moolenaara997b452018-04-17 23:24:06 +0200533 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200534 if (!can_unload_buffer(buf))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100535 return FALSE;
Bram Moolenaar94f01952018-09-01 15:30:03 +0200536
Bram Moolenaarc667da52019-11-30 20:52:27 +0100537 // Wiping out or unloading a terminal buffer kills the job.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200538 free_terminal(buf);
Bram Moolenaara997b452018-04-17 23:24:06 +0200539 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200540 else
541 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100542 // The job keeps running, hide the buffer.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200543 del_buf = FALSE;
544 unload_buf = FALSE;
545 }
546 }
Bram Moolenaarc9f8b842020-11-24 19:36:16 +0100547 else if (buf->b_p_bh[0] == 'h' && !del_buf)
548 {
549 // Hide a terminal buffer.
550 unload_buf = FALSE;
551 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200552 else
553 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100554 // A terminal buffer is wiped out if the job has finished.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200555 del_buf = TRUE;
556 unload_buf = TRUE;
557 wipe_buf = TRUE;
558 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100559 CHECK_CURBUF;
Bram Moolenaar94053a52017-08-01 21:44:33 +0200560 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200561#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562
Bram Moolenaarc667da52019-11-30 20:52:27 +0100563 // Disallow deleting the buffer when it is locked (already being closed or
564 // halfway a command that relies on it). Unloading is allowed.
Bram Moolenaar94f01952018-09-01 15:30:03 +0200565 if ((del_buf || wipe_buf) && !can_unload_buffer(buf))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100566 return FALSE;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200567
Bram Moolenaarc667da52019-11-30 20:52:27 +0100568 // check no autocommands closed the window
Bram Moolenaar4033c552017-09-16 20:54:51 +0200569 if (win != NULL && win_valid_any_tab(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100571 // Set b_last_cursor when closing the last window for the buffer.
572 // Remember the last cursor position and window options of the buffer.
573 // This used to be only for the current window, but then options like
574 // 'foldmethod' may be lost with a ":only" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 if (buf->b_nwindows == 1)
576 set_last_cursor(win);
577 buflist_setfpos(buf, win,
578 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
579 win->w_cursor.col, TRUE);
580 }
581
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200582 set_bufref(&bufref, buf);
583
Bram Moolenaarc667da52019-11-30 20:52:27 +0100584 // When the buffer is no longer in a window, trigger BufWinLeave
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 if (buf->b_nwindows == 1)
586 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200587 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100588 ++buf->b_locked_split;
Bram Moolenaar82404332016-07-10 17:00:38 +0200589 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
590 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200591 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100592 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100593 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200594aucmd_abort:
Bram Moolenaarf1474d82021-12-31 19:59:55 +0000595 emsg(_(e_autocommands_caused_command_to_abort));
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100596 return FALSE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100597 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200598 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100599 --buf->b_locked_split;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200600 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100601 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200602 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603
Bram Moolenaarc667da52019-11-30 20:52:27 +0100604 // When the buffer becomes hidden, but is not unloaded, trigger
605 // BufHidden
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 if (!unload_buf)
607 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200608 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100609 ++buf->b_locked_split;
Bram Moolenaar82404332016-07-10 17:00:38 +0200610 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
611 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200612 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100613 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200614 goto aucmd_abort;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200615 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100616 --buf->b_locked_split;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200617 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100618 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200619 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100621#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100622 // autocmds may abort script processing
623 if (!ignore_abort && aborting())
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100624 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100625#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200627
Bram Moolenaarc667da52019-11-30 20:52:27 +0100628 // If the buffer was in curwin and the window has changed, go back to that
629 // window, if it still exists. This avoids that ":edit x" triggering a
630 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200631 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
632 {
633 block_autocmds();
634 goto_tabpage_win(the_curtab, the_curwin);
635 unblock_autocmds();
636 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200637
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 nwindows = buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639
Bram Moolenaarc667da52019-11-30 20:52:27 +0100640 // decrease the link count from windows (unless not in any window)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 if (buf->b_nwindows > 0)
642 --buf->b_nwindows;
643
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100644#ifdef FEAT_DIFF
645 if (diffopt_hiddenoff() && !unload_buf && buf->b_nwindows == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100646 diff_buf_delete(buf); // Clear 'diff' for hidden buffer.
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100647#endif
648
Bram Moolenaarc667da52019-11-30 20:52:27 +0100649 // Return when a window is displaying the buffer or when it's not
650 // unloaded.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100652 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653
Bram Moolenaarc667da52019-11-30 20:52:27 +0100654 // Always remove the buffer when there is no file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 if (buf->b_ffname == NULL)
656 del_buf = TRUE;
657
Bram Moolenaarc667da52019-11-30 20:52:27 +0100658 // When closing the current buffer stop Visual mode before freeing
659 // anything.
Bram Moolenaar4930a762016-09-11 14:39:53 +0200660 if (buf == curbuf && VIsual_active
Bram Moolenaar9a27c7f2016-09-09 12:57:09 +0200661#if defined(EXITFREE)
662 && !entered_free_all_mem
663#endif
664 )
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200665 end_visual_mode();
666
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100667 // Free all things allocated for this buffer.
668 // Also calls the "BufDelete" autocommands when del_buf is TRUE.
669 //
Bram Moolenaarc667da52019-11-30 20:52:27 +0100670 // Remember if we are closing the current buffer. Restore the number of
671 // windows, so that autocommands in buf_freeall() don't get confused.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 is_curbuf = (buf == curbuf);
673 buf->b_nwindows = nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100675 buf_freeall(buf, (del_buf ? BFA_DEL : 0)
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100676 + (wipe_buf ? BFA_WIPE : 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100677 + (ignore_abort ? BFA_IGNORE_ABORT : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678
Bram Moolenaarc667da52019-11-30 20:52:27 +0100679 // Autocommands may have deleted the buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200680 if (!bufref_valid(&bufref))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100681 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100682#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100683 // autocmds may abort script processing
684 if (!ignore_abort && aborting())
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100685 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100688 // It's possible that autocommands change curbuf to the one being deleted.
689 // This might cause the previous curbuf to be deleted unexpectedly. But
690 // in some cases it's OK to delete the curbuf, because a new one is
691 // obtained anyway. Therefore only return if curbuf changed to the
692 // deleted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 if (buf == curbuf && !is_curbuf)
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100694 return FALSE;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200695
Bram Moolenaar4033c552017-09-16 20:54:51 +0200696 if (win_valid_any_tab(win) && win->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100697 win->w_buffer = NULL; // make sure we don't use the buffer now
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200698
Bram Moolenaarc667da52019-11-30 20:52:27 +0100699 // Autocommands may have opened or closed windows for this buffer.
700 // Decrement the count for the close we do here.
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200701 if (buf->b_nwindows > 0)
702 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 /*
705 * Remove the buffer from the list.
706 */
707 if (wipe_buf)
708 {
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200709 if (action == DOBUF_WIPE_REUSE)
710 {
711 // we can re-use this buffer number, store it
712 if (buf_reuse.ga_itemsize == 0)
713 ga_init2(&buf_reuse, sizeof(int), 50);
714 if (ga_grow(&buf_reuse, 1) == OK)
715 ((int *)buf_reuse.ga_data)[buf_reuse.ga_len++] = buf->b_fnum;
716 }
Bram Moolenaar3d6014f2018-10-11 19:27:47 +0200717 if (buf->b_sfname != buf->b_ffname)
718 VIM_CLEAR(buf->b_sfname);
719 else
720 buf->b_sfname = NULL;
721 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 if (buf->b_prev == NULL)
723 firstbuf = buf->b_next;
724 else
725 buf->b_prev->b_next = buf->b_next;
726 if (buf->b_next == NULL)
727 lastbuf = buf->b_prev;
728 else
729 buf->b_next->b_prev = buf->b_prev;
730 free_buffer(buf);
731 }
732 else
733 {
734 if (del_buf)
735 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100736 // Free all internal variables and reset option values, to make
737 // ":bdel" compatible with Vim 5.7.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 free_buffer_stuff(buf, TRUE);
739
Bram Moolenaarc667da52019-11-30 20:52:27 +0100740 // Make it look like a new buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
742
Bram Moolenaarc667da52019-11-30 20:52:27 +0100743 // Init the options when loaded again.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 buf->b_p_initialized = FALSE;
745 }
746 buf_clear_file(buf);
747 if (del_buf)
748 buf->b_p_bl = FALSE;
749 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100750 // NOTE: at this point "curbuf" may be invalid!
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100751 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752}
753
754/*
755 * Make buffer not contain a file.
756 */
757 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100758buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759{
760 buf->b_ml.ml_line_count = 1;
Bram Moolenaarc024b462019-06-08 18:07:21 +0200761 unchanged(buf, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 buf->b_p_eol = TRUE;
764 buf->b_start_eol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000766 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 buf->b_ml.ml_mfp = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +0100768 buf->b_ml.ml_flags = ML_EMPTY; // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769#ifdef FEAT_NETBEANS_INTG
770 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771#endif
772}
773
774/*
775 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200776 * the file. Careful: get here with "curwin" NULL when exiting.
777 * flags:
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100778 * BFA_DEL buffer is going to be deleted
779 * BFA_WIPE buffer is going to be wiped out
780 * BFA_KEEP_UNDO do not free undo information
781 * BFA_IGNORE_ABORT don't abort even when aborting() returns TRUE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100784buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200787 bufref_T bufref;
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200788 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaar5a497892016-09-03 16:29:04 +0200789 win_T *the_curwin = curwin;
790 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791
Bram Moolenaarc667da52019-11-30 20:52:27 +0100792 // Make sure the buffer isn't closed by autocommands.
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200793 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100794 ++buf->b_locked_split;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200795 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200796 if (buf->b_ml.ml_mfp != NULL)
797 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200798 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
799 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200800 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100801 // autocommands deleted the buffer
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200802 return;
803 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200804 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200806 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
807 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200808 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100809 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 return;
811 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200812 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200814 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
815 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200816 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100817 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 return;
819 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200820 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100821 --buf->b_locked_split;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200822
Bram Moolenaarc667da52019-11-30 20:52:27 +0100823 // If the buffer was in curwin and the window has changed, go back to that
824 // window, if it still exists. This avoids that ":edit x" triggering a
825 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaar5a497892016-09-03 16:29:04 +0200826 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
827 {
828 block_autocmds();
829 goto_tabpage_win(the_curtab, the_curwin);
830 unblock_autocmds();
831 }
Bram Moolenaar5a497892016-09-03 16:29:04 +0200832
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100833#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100834 // autocmds may abort script processing
835 if ((flags & BFA_IGNORE_ABORT) == 0 && aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100837#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100839 // It's possible that autocommands change curbuf to the one being deleted.
840 // This might cause curbuf to be deleted unexpectedly. But in some cases
841 // it's OK to delete the curbuf, because a new one is obtained anyway.
842 // Therefore only return if curbuf changed to the deleted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 if (buf == curbuf && !is_curbuf)
844 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845#ifdef FEAT_DIFF
Bram Moolenaarc667da52019-11-30 20:52:27 +0100846 diff_buf_delete(buf); // Can't use 'diff' for unloaded buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200848#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100849 // Remove any ownsyntax, unless exiting.
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200850 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaar89c71222011-11-30 15:40:56 +0100851 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200852#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000853
854#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +0100855 // No folds in an empty buffer.
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000856 {
857 win_T *win;
858 tabpage_T *tp;
859
860 FOR_ALL_TAB_WINDOWS(tp, win)
861 if (win->w_buffer == buf)
862 clearFolding(win);
863 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000864#endif
865
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866#ifdef FEAT_TCL
867 tcl_buffer_free(buf);
868#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100869 ml_close(buf, TRUE); // close and delete the memline/memfile
870 buf->b_ml.ml_line_count = 0; // no lines in buffer
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200871 if ((flags & BFA_KEEP_UNDO) == 0)
872 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100873 u_blockfree(buf); // free the memory allocated for undo
874 u_clearall(buf); // reset all undo information
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100877 syntax_clear(&buf->b_s); // reset syntax info
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100879#ifdef FEAT_PROP_POPUP
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100880 clear_buf_prop_types(buf);
881#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100882 buf->b_flags &= ~BF_READERR; // a read error is no longer relevant
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883}
884
885/*
886 * Free a buffer structure and the things it contains related to the buffer
887 * itself (not the file, that must have been done already).
888 */
889 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100890free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200892 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200894#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100895 // b:changedtick uses an item in buf_T, remove it now
Bram Moolenaarb2c87502017-10-14 21:15:58 +0200896 dictitem_remove(buf->b_vars, (dictitem_T *)&buf->b_ct_di);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200897 unref_var_dict(buf->b_vars);
Bram Moolenaar86173482019-10-01 17:02:16 +0200898 remove_listeners(buf);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200899#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200900#ifdef FEAT_LUA
901 lua_buffer_free(buf);
902#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000903#ifdef FEAT_MZSCHEME
904 mzscheme_buffer_free(buf);
905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906#ifdef FEAT_PERL
907 perl_buf_free(buf);
908#endif
909#ifdef FEAT_PYTHON
910 python_buffer_free(buf);
911#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200912#ifdef FEAT_PYTHON3
913 python3_buffer_free(buf);
914#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915#ifdef FEAT_RUBY
916 ruby_buffer_free(buf);
917#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200918#ifdef FEAT_JOB_CHANNEL
919 channel_buffer_free(buf);
920#endif
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200921#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +0200922 free_terminal(buf);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200923#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +0200924#ifdef FEAT_JOB_CHANNEL
925 vim_free(buf->b_prompt_text);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200926 free_callback(&buf->b_prompt_callback);
Bram Moolenaar86173482019-10-01 17:02:16 +0200927 free_callback(&buf->b_prompt_interrupt);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200928#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200929
930 buf_hashtab_remove(buf);
931
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200932 aubuflocal_remove(buf);
933
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200934 if (autocmd_busy)
935 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100936 // Do not free the buffer structure while autocommands are executing,
937 // it's still needed. Free it when autocmd_busy is reset.
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200938 buf->b_next = au_pending_free_buf;
939 au_pending_free_buf = buf;
940 }
941 else
Bram Moolenaarcee52202020-03-11 14:19:58 +0100942 {
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200943 vim_free(buf);
Bram Moolenaarcee52202020-03-11 14:19:58 +0100944 if (curbuf == buf)
945 curbuf = NULL; // make clear it's not to be used
946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947}
948
949/*
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100950 * Initializes b:changedtick.
Bram Moolenaar79518e22017-02-17 16:31:35 +0100951 */
952 static void
953init_changedtick(buf_T *buf)
954{
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100955 dictitem_T *di = (dictitem_T *)&buf->b_ct_di;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100956
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100957 di->di_flags = DI_FLAGS_FIX | DI_FLAGS_RO;
958 di->di_tv.v_type = VAR_NUMBER;
959 di->di_tv.v_lock = VAR_FIXED;
960 di->di_tv.vval.v_number = 0;
961
Bram Moolenaar92769c32017-02-25 15:41:37 +0100962#ifdef FEAT_EVAL
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100963 STRCPY(buf->b_ct_di.di_key, "changedtick");
964 (void)dict_add(buf->b_vars, di);
Bram Moolenaar92769c32017-02-25 15:41:37 +0100965#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +0100966}
967
968/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
970 */
971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100972free_buffer_stuff(
973 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +0100974 int free_options) // free options as well
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975{
976 if (free_options)
977 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100978 clear_wininfo(buf); // including window-local options
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200980#ifdef FEAT_SPELL
981 ga_clear(&buf->b_s.b_langp);
982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 }
984#ifdef FEAT_EVAL
Bram Moolenaar79518e22017-02-17 16:31:35 +0100985 {
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100986 varnumber_T tick = CHANGEDTICK(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +0100987
Bram Moolenaarc667da52019-11-30 20:52:27 +0100988 vars_clear(&buf->b_vars->dv_hashtab); // free all buffer variables
Bram Moolenaar79518e22017-02-17 16:31:35 +0100989 hash_init(&buf->b_vars->dv_hashtab);
990 init_changedtick(buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100991 CHANGEDTICK(buf) = tick;
Bram Moolenaarf10997a2020-01-03 21:00:02 +0100992 remove_listeners(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +0100993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994#endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200995 uc_clear(&buf->b_ucmds); // clear local user commands
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996#ifdef FEAT_SIGNS
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200997 buf_delete_signs(buf, (char_u *)"*"); // delete any signs
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000999#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001000 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00001001#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01001002 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); // clear local mappings
1003 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); // clear local abbrevs
Bram Moolenaard23a8232018-02-10 18:45:26 +01001004 VIM_CLEAR(buf->b_start_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005}
1006
1007/*
Bram Moolenaar4882d982020-10-25 17:55:09 +01001008 * Free one wininfo_T.
1009 */
1010 void
1011free_wininfo(wininfo_T *wip)
1012{
1013 if (wip->wi_optset)
1014 {
1015 clear_winopt(&wip->wi_opt);
1016#ifdef FEAT_FOLDING
1017 deleteFoldRecurse(&wip->wi_folds);
1018#endif
1019 }
1020 vim_free(wip);
1021}
1022
1023/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 * Free the b_wininfo list for buffer "buf".
1025 */
1026 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001027clear_wininfo(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028{
1029 wininfo_T *wip;
1030
1031 while (buf->b_wininfo != NULL)
1032 {
1033 wip = buf->b_wininfo;
1034 buf->b_wininfo = wip->wi_next;
Bram Moolenaar4882d982020-10-25 17:55:09 +01001035 free_wininfo(wip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 }
1037}
1038
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039/*
1040 * Go to another buffer. Handles the result of the ATTENTION dialog.
1041 */
1042 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001043goto_buffer(
1044 exarg_T *eap,
1045 int start,
1046 int dir,
1047 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001049 bufref_T old_curbuf;
1050
1051 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052
1053 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
1055 start, dir, count, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
1057 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001058#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001059 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001060
Bram Moolenaarc667da52019-11-30 20:52:27 +01001061 // Reset the error/interrupt/exception state here so that
1062 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001063 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001064#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001065
Bram Moolenaarc667da52019-11-30 20:52:27 +01001066 // Quitting means closing the split window, nothing else.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 win_close(curwin, TRUE);
1068 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001069 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001070
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001071#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001072 // Restore the error/interrupt/exception state if not discarded by a
1073 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001074 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 }
1077 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001078 handle_swap_exists(&old_curbuf);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001079}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081/*
1082 * Handle the situation of swap_exists_action being set.
1083 * It is allowed for "old_curbuf" to be NULL or invalid.
1084 */
1085 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001086handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087{
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001088#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001089 cleanup_T cs;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001090#endif
1091#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001092 long old_tw = curbuf->b_p_tw;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001093#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001094 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001095
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 if (swap_exists_action == SEA_QUIT)
1097 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001098#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001099 // Reset the error/interrupt/exception state here so that
1100 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001101 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001102#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001103
Bram Moolenaarc667da52019-11-30 20:52:27 +01001104 // User selected Quit at ATTENTION prompt. Go back to previous
1105 // buffer. If that buffer is gone or the same as the current one,
1106 // open a new, empty buffer.
1107 swap_exists_action = SEA_NONE; // don't want it again
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001108 swap_exists_did_quit = TRUE;
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001109 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001110 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1111 || old_curbuf->br_buf == curbuf)
Bram Moolenaar1d97efc2021-07-04 13:27:11 +02001112 {
1113 // Block autocommands here because curwin->w_buffer is NULL.
1114 block_autocmds();
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001115 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar1d97efc2021-07-04 13:27:11 +02001116 unblock_autocmds();
1117 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001118 else
1119 buf = old_curbuf->br_buf;
1120 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001121 {
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001122 int old_msg_silent = msg_silent;
1123
1124 if (shortmess(SHM_FILEINFO))
1125 msg_silent = 1; // prevent fileinfo message
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001126 enter_buffer(buf);
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001127 // restore msg_silent, so that the command line will be shown
1128 msg_silent = old_msg_silent;
1129
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001130#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001131 if (old_tw != curbuf->b_p_tw)
1132 check_colorcolumn(curwin);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001133#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001134 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001135 // If "old_curbuf" is NULL we are in big trouble here...
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001136
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001137#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001138 // Restore the error/interrupt/exception state if not discarded by a
1139 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001140 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 }
1143 else if (swap_exists_action == SEA_RECOVER)
1144 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001145#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001146 // Reset the error/interrupt/exception state here so that
1147 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001148 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001149#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001150
Bram Moolenaarc667da52019-11-30 20:52:27 +01001151 // User selected Recover at ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 msg_scroll = TRUE;
Bram Moolenaar99499b12019-05-23 21:35:48 +02001153 ml_recover(FALSE);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001154 msg_puts("\n"); // don't overwrite the last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001156 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001157
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001158#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001159 // Restore the error/interrupt/exception state if not discarded by a
1160 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001161 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 }
1164 swap_exists_action = SEA_NONE;
1165}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167/*
Bram Moolenaara02471e2014-01-10 16:43:14 +01001168 * Make the current buffer empty.
1169 * Used when it is wiped out and it's the last buffer.
1170 */
1171 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001172empty_curbuf(
1173 int close_others,
1174 int forceit,
1175 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001176{
1177 int retval;
1178 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001179 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001180
1181 if (action == DOBUF_UNLOAD)
1182 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001183 emsg(_(e_cannot_unload_last_buffer));
Bram Moolenaara02471e2014-01-10 16:43:14 +01001184 return FAIL;
1185 }
1186
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001187 set_bufref(&bufref, buf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001188 if (close_others)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001189 // Close any other windows on this buffer, then make it empty.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001190 close_windows(buf, TRUE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001191
1192 setpcmark();
1193 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1194 forceit ? ECMD_FORCEIT : 0, curwin);
1195
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001196 // do_ecmd() may create a new buffer, then we have to delete
1197 // the old one. But do_ecmd() may have done that already, check
1198 // if the buffer still exists.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001199 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001200 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001201 if (!close_others)
1202 need_fileinfo = FALSE;
1203 return retval;
1204}
Bram Moolenaar94f01952018-09-01 15:30:03 +02001205
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206/*
1207 * Implementation of the commands for the buffer list.
1208 *
1209 * action == DOBUF_GOTO go to specified buffer
1210 * action == DOBUF_SPLIT split window and go to specified buffer
1211 * action == DOBUF_UNLOAD unload specified buffer(s)
1212 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1213 * action == DOBUF_WIPE delete specified buffer(s) really
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001214 * action == DOBUF_WIPE_REUSE idem, and add number to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 *
1216 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1217 * start == DOBUF_FIRST go to "count" buffer from first buffer
1218 * start == DOBUF_LAST go to "count" buffer from last buffer
1219 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1220 *
1221 * Return FAIL or OK.
1222 */
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001223 static int
1224do_buffer_ext(
Bram Moolenaar7454a062016-01-30 15:14:10 +01001225 int action,
1226 int start,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001227 int dir, // FORWARD or BACKWARD
1228 int count, // buffer number or number of buffers
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001229 int flags) // DOBUF_FORCEIT etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230{
1231 buf_T *buf;
1232 buf_T *bp;
1233 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001234 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235
1236 switch (start)
1237 {
1238 case DOBUF_FIRST: buf = firstbuf; break;
1239 case DOBUF_LAST: buf = lastbuf; break;
1240 default: buf = curbuf; break;
1241 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001242 if (start == DOBUF_MOD) // find next modified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 {
1244 while (count-- > 0)
1245 {
1246 do
1247 {
1248 buf = buf->b_next;
1249 if (buf == NULL)
1250 buf = firstbuf;
1251 }
1252 while (buf != curbuf && !bufIsChanged(buf));
1253 }
1254 if (!bufIsChanged(buf))
1255 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001256 emsg(_(e_no_modified_buffer_found));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 return FAIL;
1258 }
1259 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001260 else if (start == DOBUF_FIRST && count) // find specified buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 {
1262 while (buf != NULL && buf->b_fnum != count)
1263 buf = buf->b_next;
1264 }
1265 else
1266 {
1267 bp = NULL;
1268 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1269 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001270 // remember the buffer where we start, we come back there when all
1271 // buffers are unlisted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 if (bp == NULL)
1273 bp = buf;
1274 if (dir == FORWARD)
1275 {
1276 buf = buf->b_next;
1277 if (buf == NULL)
1278 buf = firstbuf;
1279 }
1280 else
1281 {
1282 buf = buf->b_prev;
1283 if (buf == NULL)
1284 buf = lastbuf;
1285 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001286 // don't count unlisted buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 if (unload || buf->b_p_bl)
1288 {
1289 --count;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001290 bp = NULL; // use this buffer as new starting point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 }
1292 if (bp == buf)
1293 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001294 // back where we started, didn't find anything.
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001295 emsg(_(e_there_is_no_listed_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 return FAIL;
1297 }
1298 }
1299 }
1300
Bram Moolenaarc667da52019-11-30 20:52:27 +01001301 if (buf == NULL) // could not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 {
1303 if (start == DOBUF_FIRST)
1304 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001305 // don't warn when deleting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 if (!unload)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001307 semsg(_(e_buffer_nr_does_not_exist), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 }
1309 else if (dir == FORWARD)
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001310 emsg(_(e_cannot_go_beyond_last_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 else
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001312 emsg(_(e_cannot_go_before_first_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 return FAIL;
1314 }
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001315#ifdef FEAT_PROP_POPUP
1316 if ((flags & DOBUF_NOPOPUP) && bt_popup(buf)
1317# ifdef FEAT_TERMINAL
1318 && !bt_terminal(buf)
1319#endif
1320 )
1321 return OK;
1322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323
1324#ifdef FEAT_GUI
1325 need_mouse_correct = TRUE;
1326#endif
1327
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001329 * delete buffer "buf" from memory and/or the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 */
1331 if (unload)
1332 {
1333 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001334 bufref_T bufref;
1335
Bram Moolenaar94f01952018-09-01 15:30:03 +02001336 if (!can_unload_buffer(buf))
Bram Moolenaara997b452018-04-17 23:24:06 +02001337 return FAIL;
Bram Moolenaara997b452018-04-17 23:24:06 +02001338
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001339 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340
Bram Moolenaarc667da52019-11-30 20:52:27 +01001341 // When unloading or deleting a buffer that's already unloaded and
1342 // unlisted: fail silently.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001343 if (action != DOBUF_WIPE && action != DOBUF_WIPE_REUSE
1344 && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 return FAIL;
1346
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001347 if ((flags & DOBUF_FORCEIT) == 0 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001349#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001350 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 {
1352 dialog_changed(buf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001353 if (!bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001354 // Autocommand deleted buffer, oops! It's not changed
1355 // now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 return FAIL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001357 // If it's still changed fail silently, the dialog already
1358 // mentioned why it fails.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001359 if (bufIsChanged(buf))
1360 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001362 else
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00001365 semsg(_(e_no_write_since_last_change_for_buffer_nr_add_bang_to_override),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 buf->b_fnum);
1367 return FAIL;
1368 }
1369 }
1370
Bram Moolenaarc667da52019-11-30 20:52:27 +01001371 // When closing the current buffer stop Visual mode.
Bram Moolenaar4930a762016-09-11 14:39:53 +02001372 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001373 end_visual_mode();
1374
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001375 // If deleting the last (listed) buffer, make it empty.
1376 // The last (listed) buffer cannot be unloaded.
Bram Moolenaar29323592016-07-24 22:04:11 +02001377 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 if (bp->b_p_bl && bp != buf)
1379 break;
1380 if (bp == NULL && buf == curbuf)
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001381 return empty_curbuf(TRUE, (flags & DOBUF_FORCEIT), action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001383 // If the deleted buffer is the current one, close the current window
1384 // (unless it's the only window). Repeat this so long as we end up in
1385 // a window with this buffer.
Bram Moolenaarf740b292006-02-16 22:11:02 +00001386 while (buf == curbuf
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001387 && !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
Bram Moolenaar459ca562016-11-10 18:16:33 +01001388 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001389 {
1390 if (win_close(curwin, FALSE) == FAIL)
1391 break;
1392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001394 // If the buffer to be deleted is not the current one, delete it here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 if (buf != curbuf)
1396 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001397 close_windows(buf, FALSE);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001398 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows <= 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001399 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 return OK;
1401 }
1402
1403 /*
1404 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001405 * There should be another, otherwise it would have been handled
1406 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001407 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 * Then prefer the buffer we most recently visited.
1409 * Else try to find one that is loaded, after the current buffer,
1410 * then before the current buffer.
1411 * Finally use any buffer.
1412 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001413 buf = NULL; // selected buffer
1414 bp = NULL; // used when no loaded buffer found
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001415 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1416 buf = au_new_curbuf.br_buf;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001417 else if (curwin->w_jumplistlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 {
1419 int jumpidx;
1420
1421 jumpidx = curwin->w_jumplistidx - 1;
1422 if (jumpidx < 0)
1423 jumpidx = curwin->w_jumplistlen - 1;
1424
1425 forward = jumpidx;
1426 while (jumpidx != curwin->w_jumplistidx)
1427 {
1428 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1429 if (buf != NULL)
1430 {
1431 if (buf == curbuf || !buf->b_p_bl)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001432 buf = NULL; // skip current and unlisted bufs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 else if (buf->b_ml.ml_mfp == NULL)
1434 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001435 // skip unloaded buf, but may keep it for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 if (bp == NULL)
1437 bp = buf;
1438 buf = NULL;
1439 }
1440 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001441 if (buf != NULL) // found a valid buffer: stop searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001443 // advance to older entry in jump list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1445 break;
1446 if (--jumpidx < 0)
1447 jumpidx = curwin->w_jumplistlen - 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001448 if (jumpidx == forward) // List exhausted for sure
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 break;
1450 }
1451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452
Bram Moolenaarc667da52019-11-30 20:52:27 +01001453 if (buf == NULL) // No previous buffer, Try 2'nd approach
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 {
1455 forward = TRUE;
1456 buf = curbuf->b_next;
1457 for (;;)
1458 {
1459 if (buf == NULL)
1460 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001461 if (!forward) // tried both directions
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 break;
1463 buf = curbuf->b_prev;
1464 forward = FALSE;
1465 continue;
1466 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001467 // in non-help buffer, try to skip help buffers, and vv
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1469 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001470 if (buf->b_ml.ml_mfp != NULL) // found loaded buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001472 if (bp == NULL) // remember unloaded buf for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 bp = buf;
1474 }
1475 if (forward)
1476 buf = buf->b_next;
1477 else
1478 buf = buf->b_prev;
1479 }
1480 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001481 if (buf == NULL) // No loaded buffer, use unloaded one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 buf = bp;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001483 if (buf == NULL) // No loaded buffer, find listed one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001485 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 if (buf->b_p_bl && buf != curbuf)
1487 break;
1488 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001489 if (buf == NULL) // Still no buffer, just take one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 {
1491 if (curbuf->b_next != NULL)
1492 buf = curbuf->b_next;
1493 else
1494 buf = curbuf->b_prev;
1495 }
1496 }
1497
Bram Moolenaara02471e2014-01-10 16:43:14 +01001498 if (buf == NULL)
1499 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001500 // Autocommands must have wiped out all other buffers. Only option
1501 // now is to make the current buffer empty.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001502 return empty_curbuf(FALSE, (flags & DOBUF_FORCEIT), action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001503 }
1504
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001506 * make "buf" the current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001508 if (action == DOBUF_SPLIT) // split window first
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001510 // If 'switchbuf' contains "useopen": jump to first window containing
1511 // "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00001512 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001513 return OK;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001514 // If 'switchbuf' contains "usetab": jump to first window in any tab
1515 // page containing "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00001516 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 return OK;
1518 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 return FAIL;
1520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521
Bram Moolenaarc667da52019-11-30 20:52:27 +01001522 // go to current buffer - nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 if (buf == curbuf)
1524 return OK;
1525
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001526 // Check if the current buffer may be abandoned.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001527 if (action == DOBUF_GOTO && !can_abandon(curbuf, (flags & DOBUF_FORCEIT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 {
1529#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001530 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001532 bufref_T bufref;
1533
1534 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 dialog_changed(curbuf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001536 if (!bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001537 // Autocommand deleted buffer, oops!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 }
1540 if (bufIsChanged(curbuf))
1541#endif
1542 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001543 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 return FAIL;
1545 }
1546 }
1547
Bram Moolenaarc667da52019-11-30 20:52:27 +01001548 // Go to the other buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 set_curbuf(buf, action);
1550
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 if (action == DOBUF_SPLIT)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001552 RESET_BINDING(curwin); // reset 'scrollbind' and 'cursorbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001554#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001555 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 return FAIL;
1557#endif
1558
1559 return OK;
1560}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001562 int
1563do_buffer(
1564 int action,
1565 int start,
1566 int dir, // FORWARD or BACKWARD
1567 int count, // buffer number or number of buffers
1568 int forceit) // TRUE when using !
1569{
1570 return do_buffer_ext(action, start, dir, count,
1571 forceit ? DOBUF_FORCEIT : 0);
1572}
1573
1574/*
1575 * do_bufdel() - delete or unload buffer(s)
1576 *
1577 * addr_count == 0: ":bdel" - delete current buffer
1578 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1579 * buffer "end_bnr", then any other arguments.
1580 * addr_count == 2: ":N,N bdel" - delete buffers in range
1581 *
1582 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1583 * DOBUF_DEL (":bdel")
1584 *
1585 * Returns error message or NULL
1586 */
1587 char *
1588do_bufdel(
1589 int command,
1590 char_u *arg, // pointer to extra arguments
1591 int addr_count,
1592 int start_bnr, // first buffer number in a range
1593 int end_bnr, // buffer nr or last buffer nr in a range
1594 int forceit)
1595{
1596 int do_current = 0; // delete current buffer?
1597 int deleted = 0; // number of buffers deleted
1598 char *errormsg = NULL; // return value
1599 int bnr; // buffer number
1600 char_u *p;
1601
1602 if (addr_count == 0)
1603 {
1604 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1605 }
1606 else
1607 {
1608 if (addr_count == 2)
1609 {
1610 if (*arg) // both range and argument is not allowed
1611 return ex_errmsg(e_trailing_arg, arg);
1612 bnr = start_bnr;
1613 }
1614 else // addr_count == 1
1615 bnr = end_bnr;
1616
1617 for ( ;!got_int; ui_breakcheck())
1618 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001619 // Delete the current buffer last, otherwise when the
1620 // current buffer is deleted, the next buffer becomes
1621 // the current one and will be loaded, which may then
1622 // also be deleted, etc.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001623 if (bnr == curbuf->b_fnum)
1624 do_current = bnr;
1625 else if (do_buffer_ext(command, DOBUF_FIRST, FORWARD, (int)bnr,
1626 DOBUF_NOPOPUP | (forceit ? DOBUF_FORCEIT : 0)) == OK)
1627 ++deleted;
1628
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001629 // find next buffer number to delete/unload
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001630 if (addr_count == 2)
1631 {
1632 if (++bnr > end_bnr)
1633 break;
1634 }
1635 else // addr_count == 1
1636 {
1637 arg = skipwhite(arg);
1638 if (*arg == NUL)
1639 break;
1640 if (!VIM_ISDIGIT(*arg))
1641 {
1642 p = skiptowhite_esc(arg);
1643 bnr = buflist_findpat(arg, p,
1644 command == DOBUF_WIPE || command == DOBUF_WIPE_REUSE,
1645 FALSE, FALSE);
1646 if (bnr < 0) // failed
1647 break;
1648 arg = p;
1649 }
1650 else
1651 bnr = getdigits(&arg);
1652 }
1653 }
1654 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1655 FORWARD, do_current, forceit) == OK)
1656 ++deleted;
1657
1658 if (deleted == 0)
1659 {
1660 if (command == DOBUF_UNLOAD)
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001661 STRCPY(IObuff, _(e_no_buffers_were_unloaded));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001662 else if (command == DOBUF_DEL)
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001663 STRCPY(IObuff, _(e_no_buffers_were_deleted));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001664 else
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001665 STRCPY(IObuff, _(e_no_buffers_were_wiped_out));
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001666 errormsg = (char *)IObuff;
1667 }
1668 else if (deleted >= p_report)
1669 {
1670 if (command == DOBUF_UNLOAD)
1671 smsg(NGETTEXT("%d buffer unloaded",
1672 "%d buffers unloaded", deleted), deleted);
1673 else if (command == DOBUF_DEL)
1674 smsg(NGETTEXT("%d buffer deleted",
1675 "%d buffers deleted", deleted), deleted);
1676 else
1677 smsg(NGETTEXT("%d buffer wiped out",
1678 "%d buffers wiped out", deleted), deleted);
1679 }
1680 }
1681
1682
1683 return errormsg;
1684}
1685
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686/*
1687 * Set current buffer to "buf". Executes autocommands and closes current
1688 * buffer. "action" tells how to close the current buffer:
1689 * DOBUF_GOTO free or hide it
1690 * DOBUF_SPLIT nothing
1691 * DOBUF_UNLOAD unload it
1692 * DOBUF_DEL delete it
1693 * DOBUF_WIPE wipe it out
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001694 * DOBUF_WIPE_REUSE wipe it out and add to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 */
1696 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001697set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698{
1699 buf_T *prevbuf;
1700 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001701 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001702#ifdef FEAT_SYN_HL
1703 long old_tw = curbuf->b_p_tw;
1704#endif
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001705 bufref_T newbufref;
1706 bufref_T prevbufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707
1708 setpcmark();
Bram Moolenaare1004402020-10-24 20:49:43 +02001709 if ((cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001710 curwin->w_alt_fnum = curbuf->b_fnum; // remember alternate file
1711 buflist_altfpos(curwin); // remember curpos
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712
Bram Moolenaarc667da52019-11-30 20:52:27 +01001713 // Don't restart Select mode after switching to another buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715
Bram Moolenaarc667da52019-11-30 20:52:27 +01001716 // close_windows() or apply_autocmds() may change curbuf and wipe out "buf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 prevbuf = curbuf;
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001718 set_bufref(&prevbufref, prevbuf);
1719 set_bufref(&newbufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720
Bram Moolenaar983d83f2021-02-07 12:12:43 +01001721 // Autocommands may delete the current buffer and/or the buffer we want to
1722 // go to. In those cases don't close the buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001723 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001724 || (bufref_valid(&prevbufref)
1725 && bufref_valid(&newbufref)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001726#ifdef FEAT_EVAL
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001727 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001729 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001731#ifdef FEAT_SYN_HL
1732 if (prevbuf == curwin->w_buffer)
1733 reset_synblock(curwin);
1734#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001736 close_windows(prevbuf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001737#if defined(FEAT_EVAL)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001738 if (bufref_valid(&prevbufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739#else
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001740 if (bufref_valid(&prevbufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001742 {
Bram Moolenaare25865a2012-07-06 16:22:02 +02001743 win_T *previouswin = curwin;
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001744
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001745 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001746 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1748 unload ? action : (action == DOBUF_GOTO
Bram Moolenaareb44a682017-08-03 22:44:55 +02001749 && !buf_hide(prevbuf)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001750 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0,
1751 FALSE, FALSE);
Bram Moolenaare25865a2012-07-06 16:22:02 +02001752 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001753 // autocommands changed curwin, Grr!
Bram Moolenaare25865a2012-07-06 16:22:02 +02001754 curwin = previouswin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001757 // An autocommand may have deleted "buf", already entered it (e.g., when
1758 // it did ":bunload") or aborted the script processing.
1759 // If curwin->w_buffer is null, enter_buffer() will make it valid again
Bram Moolenaar9e931222012-06-20 11:55:01 +02001760 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001761#ifdef FEAT_EVAL
Bram Moolenaar4033c552017-09-16 20:54:51 +02001762 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001764 ) || curwin->w_buffer == NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001765 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001767#ifdef FEAT_SYN_HL
1768 if (old_tw != curbuf->b_p_tw)
1769 check_colorcolumn(curwin);
1770#endif
1771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772}
1773
1774/*
1775 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001776 * Old curbuf must have been abandoned already! This also means "curbuf" may
1777 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001779 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001780enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781{
Bram Moolenaar010ee962019-09-25 20:37:36 +02001782 // Get the buffer in the current window.
1783 curwin->w_buffer = buf;
1784 curbuf = buf;
1785 ++curbuf->b_nwindows;
1786
1787 // Copy buffer and window local option values. Not for a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1789 if (!buf->b_help)
1790 get_winopts(buf);
1791#ifdef FEAT_FOLDING
1792 else
Bram Moolenaar010ee962019-09-25 20:37:36 +02001793 // Remove all folds in the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 clearFolding(curwin);
Bram Moolenaar010ee962019-09-25 20:37:36 +02001795 foldUpdateAll(curwin); // update folds (later).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796#endif
1797
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001799 if (curwin->w_p_diff)
1800 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801#endif
1802
Bram Moolenaara971b822011-09-14 14:43:25 +02001803#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001804 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001805#endif
1806
Bram Moolenaarc667da52019-11-30 20:52:27 +01001807 // Cursor on first line by default.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 curwin->w_cursor.lnum = 1;
1809 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001812 curwin->w_topline_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813
Bram Moolenaarc667da52019-11-30 20:52:27 +01001814 // mark cursor position as being invalid
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001815 curwin->w_valid = 0;
1816
Bram Moolenaar9cea87c2018-09-21 16:59:45 +02001817 buflist_setfpos(curbuf, curwin, curbuf->b_last_cursor.lnum,
1818 curbuf->b_last_cursor.col, TRUE);
1819
Bram Moolenaarc667da52019-11-30 20:52:27 +01001820 // Make sure the buffer is loaded.
1821 if (curbuf->b_ml.ml_mfp == NULL) // need to load the file
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001822 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001823 // If there is no filetype, allow for detecting one. Esp. useful for
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001824 // ":ball" used in an autocommand. If there already is a filetype we
Bram Moolenaarc667da52019-11-30 20:52:27 +01001825 // might prefer to keep it.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001826 if (*curbuf->b_p_ft == NUL)
1827 did_filetype = FALSE;
1828
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001829 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 else
1832 {
Bram Moolenaareda65222019-05-16 20:29:44 +02001833 if (!msg_silent && !shortmess(SHM_FILEINFO))
1834 need_fileinfo = TRUE; // display file info after redraw
1835
1836 // check if file changed
1837 (void)buf_check_timestamp(curbuf, FALSE);
1838
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001840#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001842#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1844 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 }
1846
Bram Moolenaarc667da52019-11-30 20:52:27 +01001847 // If autocommands did not change the cursor position, restore cursor lnum
1848 // and possibly cursor col.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 if (curwin->w_cursor.lnum == 1 && inindent(0))
1850 buflist_getfpos();
1851
Bram Moolenaarc667da52019-11-30 20:52:27 +01001852 check_arg_idx(curwin); // check for valid arg_idx
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 maketitle();
Bram Moolenaarc667da52019-11-30 20:52:27 +01001854 // when autocmds didn't change it
Bram Moolenaard4153d42008-11-15 15:06:17 +00001855 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001856 scroll_cursor_halfway(FALSE); // redisplay at correct position
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857
Bram Moolenaar009b2592004-10-24 19:18:58 +00001858#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarc667da52019-11-30 20:52:27 +01001859 // Send fileOpened event because we've changed buffers.
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001860 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001861#endif
1862
Bram Moolenaarc667da52019-11-30 20:52:27 +01001863 // Change directories when the 'acd' option is set.
Bram Moolenaar6f470022018-04-10 18:47:20 +02001864 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865
1866#ifdef FEAT_KEYMAP
1867 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001868 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001870#ifdef FEAT_SPELL
Bram Moolenaarc667da52019-11-30 20:52:27 +01001871 // May need to set the spell language. Can only do this after the buffer
1872 // has been properly setup.
Bram Moolenaar860cae12010-06-05 23:22:07 +02001873 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1874 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001875#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001876#ifdef FEAT_VIMINFO
1877 curbuf->b_last_used = vim_time();
1878#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001879
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 redraw_later(NOT_VALID);
1881}
1882
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001883#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1884/*
1885 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001886 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001887 */
1888 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001889do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001890{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001891 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001892 && curbuf->b_ffname != NULL
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001893 && vim_chdirfile(curbuf->b_ffname, "auto") == OK)
Bram Moolenaar05268152021-11-18 18:53:45 +00001894 {
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001895 shorten_fnames(TRUE);
Bram Moolenaar05268152021-11-18 18:53:45 +00001896 last_chdir_reason = "autochdir";
1897 }
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001898}
1899#endif
1900
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001901 void
1902no_write_message(void)
1903{
1904#ifdef FEAT_TERMINAL
1905 if (term_job_running(curbuf->b_term))
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001906 emsg(_(e_job_still_running_add_bang_to_end_the_job));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001907 else
1908#endif
Bram Moolenaare29a27f2021-07-20 21:07:36 +02001909 emsg(_(e_no_write_since_last_change_add_bang_to_override));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001910}
1911
1912 void
Bram Moolenaar7a760922018-02-19 23:10:02 +01001913no_write_message_nobang(buf_T *buf UNUSED)
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001914{
1915#ifdef FEAT_TERMINAL
Bram Moolenaar7a760922018-02-19 23:10:02 +01001916 if (term_job_running(buf->b_term))
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001917 emsg(_(e_job_still_running));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001918 else
1919#endif
Bram Moolenaare29a27f2021-07-20 21:07:36 +02001920 emsg(_(e_no_write_since_last_change));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001921}
1922
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923/*
1924 * functions for dealing with the buffer list
1925 */
1926
1927/*
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001928 * Return TRUE if the current buffer is empty, unnamed, unmodified and used in
1929 * only one window. That means it can be re-used.
1930 */
1931 int
1932curbuf_reusable(void)
1933{
1934 return (curbuf != NULL
1935 && curbuf->b_ffname == NULL
1936 && curbuf->b_nwindows <= 1
1937 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY())
Bram Moolenaard85c3962019-04-07 14:19:14 +02001938#if defined(FEAT_QUICKFIX)
Bram Moolenaar39803d82019-04-07 12:04:51 +02001939 && !bt_quickfix(curbuf)
Bram Moolenaard85c3962019-04-07 14:19:14 +02001940#endif
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001941 && !curbufIsChanged());
1942}
1943
1944/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 * Add a file name to the buffer list. Return a pointer to the buffer.
1946 * If the same file name already exists return a pointer to that buffer.
1947 * If it does not exist, or if fname == NULL, a new entry is created.
1948 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1949 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1950 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001951 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001952 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
1953 * if the buffer already exists.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001954 * If (flags & BLN_REUSE) is TRUE, may use buffer number from "buf_reuse".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 * This is the ONLY way to create a new buffer.
1956 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001958buflist_new(
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001959 char_u *ffname_arg, // full path of fname or relative
1960 char_u *sfname_arg, // short fname or NULL
1961 linenr_T lnum, // preferred cursor line
1962 int flags) // BLN_ defines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001964 char_u *ffname = ffname_arg;
1965 char_u *sfname = sfname_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 buf_T *buf;
1967#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001968 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969#endif
1970
Bram Moolenaar480778b2016-07-14 22:09:39 +02001971 if (top_file_num == 1)
1972 hash_init(&buf_hashtab);
1973
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001974 fname_expand(curbuf, &ffname, &sfname); // will allocate ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975
1976 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001977 * If the file name already exists in the list, update the entry.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 */
1979#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01001980 // On Unix we can use inode numbers when the file exists. Works better
1981 // for hard links.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1983 st.st_dev = (dev_T)-1;
1984#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001985 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986#ifdef UNIX
1987 buflist_findname_stat(ffname, &st)
1988#else
1989 buflist_findname(ffname)
1990#endif
1991 ) != NULL)
1992 {
1993 vim_free(ffname);
1994 if (lnum != 0)
Bram Moolenaar89b693e2020-10-25 17:09:50 +01001995 buflist_setfpos(buf, (flags & BLN_NOCURWIN) ? NULL : curwin,
1996 lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02001997
1998 if ((flags & BLN_NOOPT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001999 // copy the options now, if 'cpo' doesn't have 's' and not done
2000 // already
Bram Moolenaar82404332016-07-10 17:00:38 +02002001 buf_copy_options(buf, 0);
2002
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 if ((flags & BLN_LISTED) && !buf->b_p_bl)
2004 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002005 bufref_T bufref;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002006
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 buf->b_p_bl = TRUE;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002008 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002010 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002011 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002012 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002013 return NULL;
2014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 }
2016 return buf;
2017 }
2018
2019 /*
2020 * If the current buffer has no name and no contents, use the current
2021 * buffer. Otherwise: Need to allocate a new buffer structure.
2022 *
2023 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00002024 * (A spell file buffer is allocated in spell.c, but that's not a normal
2025 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 */
2027 buf = NULL;
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002028 if ((flags & BLN_CURBUF) && curbuf_reusable())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 {
2030 buf = curbuf;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002031 // It's like this buffer is deleted. Watch out for autocommands that
2032 // change curbuf! If that happens, allocate a new buffer anyway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 if (curbuf->b_p_bl)
2034 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
2035 if (buf == curbuf)
2036 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002037#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002038 if (aborting()) // autocmds may abort script processing
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002039 {
2040 vim_free(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 return NULL;
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 if (buf == curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002046 // Make sure 'bufhidden' and 'buftype' are empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 clear_string_option(&buf->b_p_bh);
2048 clear_string_option(&buf->b_p_bt);
2049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 }
2051 if (buf != curbuf || curbuf == NULL)
2052 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002053 buf = ALLOC_CLEAR_ONE(buf_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 if (buf == NULL)
2055 {
2056 vim_free(ffname);
2057 return NULL;
2058 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02002059#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002060 // init b: variables
Bram Moolenaar429fa852013-04-15 12:27:36 +02002061 buf->b_vars = dict_alloc();
2062 if (buf->b_vars == NULL)
2063 {
2064 vim_free(ffname);
2065 vim_free(buf);
2066 return NULL;
2067 }
2068 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
2069#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01002070 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 }
2072
2073 if (ffname != NULL)
2074 {
2075 buf->b_ffname = ffname;
2076 buf->b_sfname = vim_strsave(sfname);
2077 }
2078
2079 clear_wininfo(buf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002080 buf->b_wininfo = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081
2082 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
2083 || buf->b_wininfo == NULL)
2084 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002085 if (buf->b_sfname != buf->b_ffname)
2086 VIM_CLEAR(buf->b_sfname);
2087 else
2088 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01002089 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 if (buf != curbuf)
2091 free_buffer(buf);
2092 return NULL;
2093 }
2094
2095 if (buf == curbuf)
2096 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002097 // free all things allocated for this buffer
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002098 buf_freeall(buf, 0);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002099 if (buf != curbuf) // autocommands deleted the buffer!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 return NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002101#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002102 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 return NULL;
2104#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01002105 free_buffer_stuff(buf, FALSE); // delete local variables et al.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002106
Bram Moolenaarc667da52019-11-30 20:52:27 +01002107 // Init the options.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002108 buf->b_p_initialized = FALSE;
2109 buf_copy_options(buf, BCO_ENTER);
2110
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111#ifdef FEAT_KEYMAP
Bram Moolenaarc667da52019-11-30 20:52:27 +01002112 // need to reload lmaps and set b:keymap_name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 curbuf->b_kmap_state |= KEYMAP_INIT;
2114#endif
2115 }
2116 else
2117 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002118 // put the new buffer at the end of the buffer list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 buf->b_next = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002120 if (firstbuf == NULL) // buffer list is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 {
2122 buf->b_prev = NULL;
2123 firstbuf = buf;
2124 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002125 else // append new buffer at end of list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 {
2127 lastbuf->b_next = buf;
2128 buf->b_prev = lastbuf;
2129 }
2130 lastbuf = buf;
2131
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002132 if ((flags & BLN_REUSE) && buf_reuse.ga_len > 0)
2133 {
2134 // Recycle a previously used buffer number. Used for buffers which
2135 // are normally hidden, e.g. in a popup window. Avoids that the
2136 // buffer number grows rapidly.
2137 --buf_reuse.ga_len;
2138 buf->b_fnum = ((int *)buf_reuse.ga_data)[buf_reuse.ga_len];
Bram Moolenaar99ebf222019-12-10 23:44:48 +01002139
2140 // Move buffer to the right place in the buffer list.
2141 while (buf->b_prev != NULL && buf->b_fnum < buf->b_prev->b_fnum)
2142 {
2143 buf_T *prev = buf->b_prev;
2144
2145 prev->b_next = buf->b_next;
2146 if (prev->b_next != NULL)
2147 prev->b_next->b_prev = prev;
2148 buf->b_next = prev;
2149 buf->b_prev = prev->b_prev;
2150 if (buf->b_prev != NULL)
2151 buf->b_prev->b_next = buf;
2152 prev->b_prev = buf;
2153 if (lastbuf == buf)
2154 lastbuf = prev;
2155 if (firstbuf == prev)
2156 firstbuf = buf;
2157 }
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002158 }
2159 else
2160 buf->b_fnum = top_file_num++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002161 if (top_file_num < 0) // wrap around (may cause duplicates)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002163 emsg(_("W14: Warning: List of file names overflow"));
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002164 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 {
2166 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002167 ui_delay(3001L, TRUE); // make sure it is noticed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 }
2169 top_file_num = 1;
2170 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002171 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002173 // Always copy the options from the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 buf_copy_options(buf, BCO_ALWAYS);
2175 }
2176
2177 buf->b_wininfo->wi_fpos.lnum = lnum;
2178 buf->b_wininfo->wi_win = curwin;
2179
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002180#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002181 hash_init(&buf->b_s.b_keywtab);
2182 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183#endif
2184
2185 buf->b_fname = buf->b_sfname;
2186#ifdef UNIX
2187 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002188 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 else
2190 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002191 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 buf->b_dev = st.st_dev;
2193 buf->b_ino = st.st_ino;
2194 }
2195#endif
2196 buf->b_u_synced = TRUE;
2197 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002198 if (flags & BLN_DUMMY)
2199 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 buf_clear_file(buf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002201 clrallmarks(buf); // clear marks
2202 fmarks_check_names(buf); // check file marks for this file
2203 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; // init 'buflisted'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 if (!(flags & BLN_DUMMY))
2205 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002206 bufref_T bufref;
2207
Bram Moolenaarc667da52019-11-30 20:52:27 +01002208 // Tricky: these autocommands may change the buffer list. They could
2209 // also split the window with re-using the one empty buffer. This may
2210 // result in unexpectedly losing the empty buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002211 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002212 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002213 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002214 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002216 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002217 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002218 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002219 return NULL;
2220 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002221#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002222 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226
2227 return buf;
2228}
2229
2230/*
2231 * Free the memory for the options of a buffer.
2232 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2233 * 'fileencoding'.
2234 */
2235 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002236free_buf_options(
2237 buf_T *buf,
2238 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239{
2240 if (free_p_ff)
2241 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 clear_string_option(&buf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 clear_string_option(&buf->b_p_bh);
2245 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 }
2247#ifdef FEAT_FIND_ID
2248 clear_string_option(&buf->b_p_def);
2249 clear_string_option(&buf->b_p_inc);
2250# ifdef FEAT_EVAL
2251 clear_string_option(&buf->b_p_inex);
2252# endif
2253#endif
2254#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
2255 clear_string_option(&buf->b_p_inde);
2256 clear_string_option(&buf->b_p_indk);
2257#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002258#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2259 clear_string_option(&buf->b_p_bexpr);
2260#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002261#if defined(FEAT_CRYPT)
2262 clear_string_option(&buf->b_p_cm);
2263#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002264 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002265#if defined(FEAT_EVAL)
2266 clear_string_option(&buf->b_p_fex);
2267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268#ifdef FEAT_CRYPT
Bram Moolenaar131530a2021-07-29 20:37:49 +02002269# ifdef FEAT_SODIUM
2270 if (buf->b_p_key != NULL && (crypt_get_method_nr(buf) == CRYPT_M_SOD))
2271 sodium_munlock(buf->b_p_key, STRLEN(buf->b_p_key));
2272# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 clear_string_option(&buf->b_p_key);
2274#endif
2275 clear_string_option(&buf->b_p_kp);
2276 clear_string_option(&buf->b_p_mps);
2277 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002278 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 clear_string_option(&buf->b_p_isk);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002280#ifdef FEAT_VARTABS
2281 clear_string_option(&buf->b_p_vsts);
Bram Moolenaarbdace832019-03-02 10:13:42 +01002282 vim_free(buf->b_p_vsts_nopaste);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002283 buf->b_p_vsts_nopaste = NULL;
Bram Moolenaarbdace832019-03-02 10:13:42 +01002284 vim_free(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002285 buf->b_p_vsts_array = NULL;
2286 clear_string_option(&buf->b_p_vts);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002287 VIM_CLEAR(buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289#ifdef FEAT_KEYMAP
2290 clear_string_option(&buf->b_p_keymap);
Bram Moolenaar50138322018-01-28 17:05:16 +01002291 keymap_clear(&buf->b_kmap_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 ga_clear(&buf->b_kmap_ga);
2293#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 clear_string_option(&buf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295#ifdef FEAT_FOLDING
2296 clear_string_option(&buf->b_p_cms);
2297#endif
2298 clear_string_option(&buf->b_p_nf);
2299#ifdef FEAT_SYN_HL
2300 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002301 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002302#endif
2303#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002304 clear_string_option(&buf->b_s.b_p_spc);
2305 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002306 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002307 buf->b_s.b_cap_prog = NULL;
2308 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02002309 clear_string_option(&buf->b_s.b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310#endif
2311#ifdef FEAT_SEARCHPATH
2312 clear_string_option(&buf->b_p_sua);
2313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 clear_string_option(&buf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315#ifdef FEAT_CINDENT
2316 clear_string_option(&buf->b_p_cink);
2317 clear_string_option(&buf->b_p_cino);
2318#endif
2319#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
2320 clear_string_option(&buf->b_p_cinw);
2321#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 clear_string_option(&buf->b_p_cpt);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002323#ifdef FEAT_COMPL_FUNC
2324 clear_string_option(&buf->b_p_cfu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002325 free_callback(&buf->b_cfu_cb);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002326 clear_string_option(&buf->b_p_ofu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002327 free_callback(&buf->b_ofu_cb);
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +01002328 clear_string_option(&buf->b_p_tsrfu);
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00002329 free_callback(&buf->b_tsrfu_cb);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002330#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331#ifdef FEAT_QUICKFIX
2332 clear_string_option(&buf->b_p_gp);
2333 clear_string_option(&buf->b_p_mp);
2334 clear_string_option(&buf->b_p_efm);
2335#endif
2336 clear_string_option(&buf->b_p_ep);
2337 clear_string_option(&buf->b_p_path);
2338 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002339 clear_string_option(&buf->b_p_tc);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002340#ifdef FEAT_EVAL
2341 clear_string_option(&buf->b_p_tfu);
Yegappan Lakshmanan19916a82021-11-24 16:32:55 +00002342 free_callback(&buf->b_tfu_cb);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 clear_string_option(&buf->b_p_dict);
2345 clear_string_option(&buf->b_p_tsr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002346#ifdef FEAT_TEXTOBJ
2347 clear_string_option(&buf->b_p_qe);
2348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002350 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002351#ifdef FEAT_LISP
2352 clear_string_option(&buf->b_p_lw);
2353#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002354 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002355 clear_string_option(&buf->b_p_menc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356}
2357
2358/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002359 * Get alternate file "n".
2360 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2361 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 * if (options & GETF_SETMARK) call setpcmark()
2363 * if (options & GETF_ALT) we are jumping to an alternate file.
2364 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2365 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002366 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 */
2368 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002369buflist_getfile(
2370 int n,
2371 linenr_T lnum,
2372 int options,
2373 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374{
2375 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 pos_T *fpos;
2378 colnr_T col;
2379
2380 buf = buflist_findnr(n);
2381 if (buf == NULL)
2382 {
2383 if ((options & GETF_ALT) && n == 0)
Bram Moolenaar108010a2021-06-27 22:03:33 +02002384 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 else
Bram Moolenaare1242042021-12-16 20:56:57 +00002386 semsg(_(e_buffer_nr_not_found), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 return FAIL;
2388 }
2389
Bram Moolenaarc667da52019-11-30 20:52:27 +01002390 // if alternate file is the current buffer, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 if (buf == curbuf)
2392 return OK;
2393
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002394 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002395 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002396 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002398 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002399 if (curbuf_locked())
2400 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401
Bram Moolenaarc667da52019-11-30 20:52:27 +01002402 // altfpos may be changed by getfile(), get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 if (lnum == 0)
2404 {
2405 fpos = buflist_findfpos(buf);
2406 lnum = fpos->lnum;
2407 col = fpos->col;
2408 }
2409 else
2410 col = 0;
2411
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 if (options & GETF_SWITCH)
2413 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002414 // If 'switchbuf' contains "useopen": jump to first window containing
2415 // "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00002416 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002418
Bram Moolenaarc667da52019-11-30 20:52:27 +01002419 // If 'switchbuf' contains "usetab": jump to first window in any tab
2420 // page containing "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00002421 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002422 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002423
Bram Moolenaarc667da52019-11-30 20:52:27 +01002424 // If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2425 // current buffer isn't empty: open new tab or window
Bram Moolenaara594d772015-06-19 14:41:49 +02002426 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002427 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002429 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002430 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002431 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2432 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002434 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 }
2436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437
2438 ++RedrawingDisabled;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002439 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2440 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 {
2442 --RedrawingDisabled;
2443
Bram Moolenaarc667da52019-11-30 20:52:27 +01002444 // cursor is at to BOL and w_cursor.lnum is checked due to getfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 if (!p_sol && col != 0)
2446 {
2447 curwin->w_cursor.col = col;
2448 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 curwin->w_set_curswant = TRUE;
2451 }
2452 return OK;
2453 }
2454 --RedrawingDisabled;
2455 return FAIL;
2456}
2457
2458/*
2459 * go to the last know line number for the current buffer
2460 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002461 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002462buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463{
2464 pos_T *fpos;
2465
2466 fpos = buflist_findfpos(curbuf);
2467
2468 curwin->w_cursor.lnum = fpos->lnum;
2469 check_cursor_lnum();
2470
2471 if (p_sol)
2472 curwin->w_cursor.col = 0;
2473 else
2474 {
2475 curwin->w_cursor.col = fpos->col;
2476 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 curwin->w_set_curswant = TRUE;
2479 }
2480}
2481
Bram Moolenaar81695252004-12-29 20:58:21 +00002482#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2483/*
2484 * Find file in buffer list by name (it has to be for the current window).
2485 * Returns NULL if not found.
2486 */
2487 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002488buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002489{
2490 char_u *ffname;
2491 buf_T *buf = NULL;
2492
Bram Moolenaarc667da52019-11-30 20:52:27 +01002493 // First make the name into a full path name
Bram Moolenaar81695252004-12-29 20:58:21 +00002494 ffname = FullName_save(fname,
2495#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01002496 TRUE // force expansion, get rid of symbolic links
Bram Moolenaar81695252004-12-29 20:58:21 +00002497#else
2498 FALSE
2499#endif
2500 );
2501 if (ffname != NULL)
2502 {
2503 buf = buflist_findname(ffname);
2504 vim_free(ffname);
2505 }
2506 return buf;
2507}
2508#endif
2509
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510/*
2511 * Find file in buffer list by name (it has to be for the current window).
2512 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002513 * Skips dummy buffers.
2514 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 */
2516 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002517buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518{
2519#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002520 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521
2522 if (mch_stat((char *)ffname, &st) < 0)
2523 st.st_dev = (dev_T)-1;
2524 return buflist_findname_stat(ffname, &st);
2525}
2526
2527/*
2528 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2529 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002530 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 */
2532 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002533buflist_findname_stat(
2534 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002535 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536{
2537#endif
2538 buf_T *buf;
2539
Bram Moolenaarc667da52019-11-30 20:52:27 +01002540 // Start at the last buffer, expect to find a match sooner.
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002541 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaar81695252004-12-29 20:58:21 +00002542 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543#ifdef UNIX
2544 , stp
2545#endif
2546 ))
2547 return buf;
2548 return NULL;
2549}
2550
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551/*
2552 * Find file in buffer list by a regexp pattern.
2553 * Return fnum of the found buffer.
2554 * Return < 0 for error.
2555 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002557buflist_findpat(
2558 char_u *pattern,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002559 char_u *pattern_end, // pointer to first char after pattern
2560 int unlisted, // find unlisted buffers
2561 int diffmode UNUSED, // find diff-mode buffers only
2562 int curtab_only) // find buffers in current tab only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563{
2564 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 int match = -1;
2566 int find_listed;
2567 char_u *pat;
2568 char_u *patend;
2569 int attempt;
2570 char_u *p;
2571 int toggledollar;
2572
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002573 // "%" is current file, "%%" or "#" is alternate file
2574 if ((pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2575 || (in_vim9script() && pattern_end == pattern + 2
2576 && pattern[0] == '%' && pattern[1] == '%'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 {
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002578 if (*pattern == '#' || pattern_end == pattern + 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 match = curwin->w_alt_fnum;
Bram Moolenaardfbc5fd2021-01-23 15:15:01 +01002580 else
2581 match = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582#ifdef FEAT_DIFF
2583 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2584 match = -1;
2585#endif
2586 }
2587
2588 /*
2589 * Try four ways of matching a listed buffer:
2590 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002591 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 * attempt == 2: with '$' at end (only match at end)
2593 * attempt == 3: with '^' at start and '$' at end (only full match)
2594 * Repeat this for finding an unlisted buffer if there was no matching
2595 * listed buffer.
2596 */
2597 else
2598 {
2599 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2600 if (pat == NULL)
2601 return -1;
2602 patend = pat + STRLEN(pat) - 1;
2603 toggledollar = (patend > pat && *patend == '$');
2604
Bram Moolenaarc667da52019-11-30 20:52:27 +01002605 // First try finding a listed buffer. If not found and "unlisted"
2606 // is TRUE, try finding an unlisted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 find_listed = TRUE;
2608 for (;;)
2609 {
2610 for (attempt = 0; attempt <= 3; ++attempt)
2611 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002612 regmatch_T regmatch;
2613
Bram Moolenaarc667da52019-11-30 20:52:27 +01002614 // may add '^' and '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 if (toggledollar)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002616 *patend = (attempt < 2) ? NUL : '$'; // add/remove '$'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 p = pat;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002618 if (*p == '^' && !(attempt & 1)) // add/remove '^'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 ++p;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01002620 regmatch.regprog = vim_regcomp(p, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002621 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 {
2623 vim_free(pat);
2624 return -1;
2625 }
2626
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002627 FOR_ALL_BUFS_FROM_LAST(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 if (buf->b_p_bl == find_listed
2629#ifdef FEAT_DIFF
2630 && (!diffmode || diff_mode_buf(buf))
2631#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002632 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002634 if (curtab_only)
2635 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002636 // Ignore the match if the buffer is not open in
2637 // the current tab.
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002638 win_T *wp;
2639
Bram Moolenaar29323592016-07-24 22:04:11 +02002640 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002641 if (wp->w_buffer == buf)
2642 break;
2643 if (wp == NULL)
2644 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002645 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002646 if (match >= 0) // already found a match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 {
2648 match = -2;
2649 break;
2650 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002651 match = buf->b_fnum; // remember first match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 }
2653
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002654 vim_regfree(regmatch.regprog);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002655 if (match >= 0) // found one match
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 break;
2657 }
2658
Bram Moolenaarc667da52019-11-30 20:52:27 +01002659 // Only search for unlisted buffers if there was no match with
2660 // a listed buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 if (!unlisted || !find_listed || match != -1)
2662 break;
2663 find_listed = FALSE;
2664 }
2665
2666 vim_free(pat);
2667 }
2668
2669 if (match == -2)
Bram Moolenaare1242042021-12-16 20:56:57 +00002670 semsg(_(e_more_than_one_match_for_str), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 else if (match < 0)
Bram Moolenaare1242042021-12-16 20:56:57 +00002672 semsg(_(e_no_matching_buffer_for_str), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 return match;
2674}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675
Bram Moolenaar52410572019-10-27 05:12:45 +01002676#ifdef FEAT_VIMINFO
2677typedef struct {
2678 buf_T *buf;
2679 char_u *match;
2680} bufmatch_T;
2681#endif
2682
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683/*
2684 * Find all buffer names that match.
2685 * For command line expansion of ":buf" and ":sbuf".
2686 * Return OK if matches found, FAIL otherwise.
2687 */
2688 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002689ExpandBufnames(
2690 char_u *pat,
2691 int *num_file,
2692 char_u ***file,
2693 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694{
2695 int count = 0;
2696 buf_T *buf;
2697 int round;
2698 char_u *p;
2699 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002700 char_u *patc;
Bram Moolenaar52410572019-10-27 05:12:45 +01002701#ifdef FEAT_VIMINFO
2702 bufmatch_T *matches = NULL;
2703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704
Bram Moolenaarc667da52019-11-30 20:52:27 +01002705 *num_file = 0; // return values in case of FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 *file = NULL;
2707
Bram Moolenaarefcc3292019-12-30 21:59:03 +01002708#ifdef FEAT_DIFF
2709 if ((options & BUF_DIFF_FILTER) && !curwin->w_p_diff)
2710 return FAIL;
2711#endif
2712
Bram Moolenaarc667da52019-11-30 20:52:27 +01002713 // Make a copy of "pat" and change "^" to "\(^\|[\/]\)".
Bram Moolenaar05159a02005-02-26 23:04:13 +00002714 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002716 patc = alloc(STRLEN(pat) + 11);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002717 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002719 STRCPY(patc, "\\(^\\|[\\/]\\)");
2720 STRCPY(patc + 11, pat + 1);
2721 }
2722 else
2723 patc = pat;
2724
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002725 // attempt == 0: try match with '\<', match at start of word
2726 // attempt == 1: try match without '\<', match anywhere
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002727 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002728 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002729 regmatch_T regmatch;
2730
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002731 if (attempt > 0 && patc == pat)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002732 break; // there was no anchor, no need to try again
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002733 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2734 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002735 {
2736 if (patc != pat)
2737 vim_free(patc);
2738 return FAIL;
2739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002741 // round == 1: Count the matches.
2742 // round == 2: Build the array to keep the matches.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 for (round = 1; round <= 2; ++round)
2744 {
2745 count = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002746 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002748 if (!buf->b_p_bl) // skip unlisted buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 continue;
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002750#ifdef FEAT_DIFF
2751 if (options & BUF_DIFF_FILTER)
2752 // Skip buffers not suitable for
2753 // :diffget or :diffput completion.
Bram Moolenaarefcc3292019-12-30 21:59:03 +01002754 if (buf == curbuf || !diff_mode_buf(buf))
Bram Moolenaarae7dba82019-12-29 13:56:33 +01002755 continue;
2756#endif
2757
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002758 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 if (p != NULL)
2760 {
2761 if (round == 1)
2762 ++count;
2763 else
2764 {
2765 if (options & WILD_HOME_REPLACE)
2766 p = home_replace_save(buf, p);
2767 else
2768 p = vim_strsave(p);
Bram Moolenaar52410572019-10-27 05:12:45 +01002769#ifdef FEAT_VIMINFO
2770 if (matches != NULL)
2771 {
2772 matches[count].buf = buf;
2773 matches[count].match = p;
2774 count++;
2775 }
2776 else
2777#endif
2778 (*file)[count++] = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 }
2780 }
2781 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002782 if (count == 0) // no match found, break here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 break;
2784 if (round == 1)
2785 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002786 *file = ALLOC_MULT(char_u *, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 if (*file == NULL)
2788 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002789 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002790 if (patc != pat)
2791 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 return FAIL;
2793 }
Bram Moolenaar52410572019-10-27 05:12:45 +01002794#ifdef FEAT_VIMINFO
2795 if (options & WILD_BUFLASTUSED)
2796 matches = ALLOC_MULT(bufmatch_T, count);
2797#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 }
2799 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002800 vim_regfree(regmatch.regprog);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002801 if (count) // match(es) found, break here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 break;
2803 }
2804
Bram Moolenaar05159a02005-02-26 23:04:13 +00002805 if (patc != pat)
2806 vim_free(patc);
2807
Bram Moolenaar52410572019-10-27 05:12:45 +01002808#ifdef FEAT_VIMINFO
2809 if (matches != NULL)
2810 {
2811 int i;
2812 if (count > 1)
2813 qsort(matches, count, sizeof(bufmatch_T), buf_compare);
2814 // if the current buffer is first in the list, place it at the end
2815 if (matches[0].buf == curbuf)
2816 {
2817 for (i = 1; i < count; i++)
2818 (*file)[i-1] = matches[i].match;
2819 (*file)[count-1] = matches[0].match;
2820 }
2821 else
2822 {
2823 for (i = 0; i < count; i++)
2824 (*file)[i] = matches[i].match;
2825 }
2826 vim_free(matches);
2827 }
2828#endif
2829
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 *num_file = count;
2831 return (count == 0 ? FAIL : OK);
2832}
2833
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834/*
2835 * Check for a match on the file name for buffer "buf" with regprog "prog".
2836 */
2837 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002838buflist_match(
2839 regmatch_T *rmp,
2840 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002841 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842{
2843 char_u *match;
2844
Bram Moolenaarc667da52019-11-30 20:52:27 +01002845 // First try the short file name, then the long file name.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002846 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002848 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849
2850 return match;
2851}
2852
2853/*
2854 * Try matching the regexp in "prog" with file name "name".
2855 * Return "name" when there is a match, NULL when not.
2856 */
2857 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002858fname_match(
2859 regmatch_T *rmp,
2860 char_u *name,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002861 int ignore_case) // when TRUE ignore case, when FALSE use 'fic'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862{
2863 char_u *match = NULL;
2864 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865
2866 if (name != NULL)
2867 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002868 // Ignore case when 'fileignorecase' or the argument is set.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002869 rmp->rm_ic = p_fic || ignore_case;
2870 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 match = name;
2872 else
2873 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002874 // Replace $(HOME) with '~' and try matching again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002876 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 match = name;
2878 vim_free(p);
2879 }
2880 }
2881
2882 return match;
2883}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884
2885/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02002886 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 */
2888 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002889buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890{
Bram Moolenaar480778b2016-07-14 22:09:39 +02002891 char_u key[VIM_SIZEOF_INT * 2 + 1];
2892 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893
2894 if (nr == 0)
2895 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02002896 sprintf((char *)key, "%x", nr);
2897 hi = hash_find(&buf_hashtab, key);
2898
2899 if (!HASHITEM_EMPTY(hi))
2900 return (buf_T *)(hi->hi_key
2901 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 return NULL;
2903}
2904
2905/*
2906 * Get name of file 'n' in the buffer list.
2907 * When the file has no name an empty string is returned.
2908 * home_replace() is used to shorten the file name (used for marks).
2909 * Returns a pointer to allocated memory, of NULL when failed.
2910 */
2911 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002912buflist_nr2name(
2913 int n,
2914 int fullname,
Bram Moolenaarc667da52019-11-30 20:52:27 +01002915 int helptail) // for help buffers return tail only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916{
2917 buf_T *buf;
2918
2919 buf = buflist_findnr(n);
2920 if (buf == NULL)
2921 return NULL;
2922 return home_replace_save(helptail ? buf : NULL,
2923 fullname ? buf->b_ffname : buf->b_fname);
2924}
2925
2926/*
2927 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2928 * When "copy_options" is TRUE save the local window option values.
2929 * When "lnum" is 0 only do the options.
2930 */
Bram Moolenaardefa0672019-07-21 19:25:37 +02002931 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002932buflist_setfpos(
2933 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01002934 win_T *win, // may be NULL when using :badd
Bram Moolenaar7454a062016-01-30 15:14:10 +01002935 linenr_T lnum,
2936 colnr_T col,
2937 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938{
2939 wininfo_T *wip;
2940
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002941 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 if (wip->wi_win == win)
2943 break;
2944 if (wip == NULL)
2945 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002946 // allocate a new entry
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002947 wip = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 if (wip == NULL)
2949 return;
2950 wip->wi_win = win;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002951 if (lnum == 0) // set lnum even when it's 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 lnum = 1;
2953 }
2954 else
2955 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002956 // remove the entry from the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 if (wip->wi_prev)
2958 wip->wi_prev->wi_next = wip->wi_next;
2959 else
2960 buf->b_wininfo = wip->wi_next;
2961 if (wip->wi_next)
2962 wip->wi_next->wi_prev = wip->wi_prev;
2963 if (copy_options && wip->wi_optset)
2964 {
2965 clear_winopt(&wip->wi_opt);
2966#ifdef FEAT_FOLDING
2967 deleteFoldRecurse(&wip->wi_folds);
2968#endif
2969 }
2970 }
2971 if (lnum != 0)
2972 {
2973 wip->wi_fpos.lnum = lnum;
2974 wip->wi_fpos.col = col;
2975 }
Bram Moolenaar89b693e2020-10-25 17:09:50 +01002976 if (copy_options && win != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002978 // Save the window-specific option values.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2980#ifdef FEAT_FOLDING
2981 wip->wi_fold_manual = win->w_fold_manual;
2982 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2983#endif
2984 wip->wi_optset = TRUE;
2985 }
2986
Bram Moolenaarc667da52019-11-30 20:52:27 +01002987 // insert the entry in front of the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 wip->wi_next = buf->b_wininfo;
2989 buf->b_wininfo = wip;
2990 wip->wi_prev = NULL;
2991 if (wip->wi_next)
2992 wip->wi_next->wi_prev = wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993}
2994
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002995#ifdef FEAT_DIFF
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002996/*
2997 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2998 * page. That's because a diff is local to a tab page.
2999 */
3000 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003001wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003002{
3003 win_T *wp;
3004
3005 if (wip->wi_opt.wo_diff)
3006 {
Bram Moolenaar29323592016-07-24 22:04:11 +02003007 FOR_ALL_WINDOWS(wp)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003008 // return FALSE when it's a window in the current tab page, thus
3009 // the buffer was in diff mode here
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003010 if (wip->wi_win == wp)
3011 return FALSE;
3012 return TRUE;
3013 }
3014 return FALSE;
3015}
3016#endif
3017
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018/*
3019 * Find info for the current window in buffer "buf".
3020 * If not found, return the info for the most recently used window.
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003021 * When "need_options" is TRUE skip entries where wi_optset is FALSE.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003022 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
3023 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 * Returns NULL when there isn't any info.
3025 */
3026 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003027find_wininfo(
3028 buf_T *buf,
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003029 int need_options,
Bram Moolenaar7454a062016-01-30 15:14:10 +01003030 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031{
3032 wininfo_T *wip;
3033
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003034 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003035 if (wip->wi_win == curwin
3036#ifdef FEAT_DIFF
3037 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
3038#endif
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003039
3040 && (!need_options || wip->wi_optset))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003042
Bram Moolenaarc667da52019-11-30 20:52:27 +01003043 // If no wininfo for curwin, use the first in the list (that doesn't have
3044 // 'diff' set and is in another tab page).
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003045 // If "need_options" is TRUE skip entries that don't have options set,
3046 // unless the window is editing "buf", so we can copy from the window
3047 // itself.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003048 if (wip == NULL)
3049 {
3050#ifdef FEAT_DIFF
3051 if (skip_diff_buffer)
3052 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003053 FOR_ALL_BUF_WININFO(buf, wip)
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003054 if (!wininfo_other_tab_diff(wip)
3055 && (!need_options || wip->wi_optset
3056 || (wip->wi_win != NULL
3057 && wip->wi_win->w_buffer == buf)))
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003058 break;
3059 }
3060 else
3061#endif
3062 wip = buf->b_wininfo;
3063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 return wip;
3065}
3066
3067/*
3068 * Reset the local window options to the values last used in this window.
3069 * If the buffer wasn't used in this window before, use the values from
3070 * the most recently used window. If the values were never set, use the
3071 * global values for the window.
3072 */
3073 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003074get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075{
3076 wininfo_T *wip;
3077
3078 clear_winopt(&curwin->w_onebuf_opt);
3079#ifdef FEAT_FOLDING
3080 clearFolding(curwin);
3081#endif
3082
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003083 wip = find_wininfo(buf, TRUE, TRUE);
Bram Moolenaar25782a72018-05-13 18:05:33 +02003084 if (wip != NULL && wip->wi_win != NULL
3085 && wip->wi_win != curwin && wip->wi_win->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003087 // The buffer is currently displayed in the window: use the actual
3088 // option values instead of the saved (possibly outdated) values.
Bram Moolenaar25782a72018-05-13 18:05:33 +02003089 win_T *wp = wip->wi_win;
3090
3091 copy_winopt(&wp->w_onebuf_opt, &curwin->w_onebuf_opt);
3092#ifdef FEAT_FOLDING
3093 curwin->w_fold_manual = wp->w_fold_manual;
3094 curwin->w_foldinvalid = TRUE;
3095 cloneFoldGrowArray(&wp->w_folds, &curwin->w_folds);
3096#endif
3097 }
3098 else if (wip != NULL && wip->wi_optset)
3099 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003100 // the buffer was displayed in the current window earlier
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
3102#ifdef FEAT_FOLDING
3103 curwin->w_fold_manual = wip->wi_fold_manual;
3104 curwin->w_foldinvalid = TRUE;
3105 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
3106#endif
3107 }
3108 else
3109 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
3110
3111#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +01003112 // Set 'foldlevel' to 'foldlevelstart' if it's not negative.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 if (p_fdls >= 0)
3114 curwin->w_p_fdl = p_fdls;
3115#endif
Bram Moolenaar010ee962019-09-25 20:37:36 +02003116 after_copy_winopt(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117}
3118
3119/*
3120 * Find the position (lnum and col) for the buffer 'buf' for the current
3121 * window.
3122 * Returns a pointer to no_position if no position is found.
3123 */
3124 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003125buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126{
3127 wininfo_T *wip;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003128 static pos_T no_position = {1, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129
Bram Moolenaar89b693e2020-10-25 17:09:50 +01003130 wip = find_wininfo(buf, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 if (wip != NULL)
3132 return &(wip->wi_fpos);
3133 else
3134 return &no_position;
3135}
3136
3137/*
3138 * Find the lnum for the buffer 'buf' for the current window.
3139 */
3140 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01003141buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142{
3143 return buflist_findfpos(buf)->lnum;
3144}
3145
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02003147 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003150buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151{
Bram Moolenaar52410572019-10-27 05:12:45 +01003152 buf_T *buf = firstbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 int len;
3154 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003155 int ro_char;
3156 int changed_char;
Bram Moolenaar0751f512018-03-29 16:37:16 +02003157#ifdef FEAT_TERMINAL
3158 int job_running;
3159 int job_none_open;
3160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161
Bram Moolenaar52410572019-10-27 05:12:45 +01003162#ifdef FEAT_VIMINFO
3163 garray_T buflist;
3164 buf_T **buflist_data = NULL, **p;
3165
3166 if (vim_strchr(eap->arg, 't'))
3167 {
3168 ga_init2(&buflist, sizeof(buf_T *), 50);
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003169 FOR_ALL_BUFFERS(buf)
Bram Moolenaar52410572019-10-27 05:12:45 +01003170 {
3171 if (ga_grow(&buflist, 1) == OK)
3172 ((buf_T **)buflist.ga_data)[buflist.ga_len++] = buf;
3173 }
3174
3175 qsort(buflist.ga_data, (size_t)buflist.ga_len,
3176 sizeof(buf_T *), buf_compare);
3177
Bram Moolenaar3b991522019-11-06 23:26:20 +01003178 buflist_data = (buf_T **)buflist.ga_data;
3179 buf = *buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003180 }
Bram Moolenaar3b991522019-11-06 23:26:20 +01003181 p = buflist_data;
Bram Moolenaar52410572019-10-27 05:12:45 +01003182
Bram Moolenaar3b991522019-11-06 23:26:20 +01003183 for (; buf != NULL && !got_int; buf = buflist_data != NULL
Bram Moolenaar52410572019-10-27 05:12:45 +01003184 ? (++p < buflist_data + buflist.ga_len ? *p : NULL)
3185 : buf->b_next)
3186#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
Bram Moolenaar52410572019-10-27 05:12:45 +01003188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 {
Bram Moolenaar0751f512018-03-29 16:37:16 +02003190#ifdef FEAT_TERMINAL
3191 job_running = term_job_running(buf->b_term);
3192 job_none_open = job_running && term_none_open(buf->b_term);
3193#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003194 // skip unlisted buffers, unless ! was used
Bram Moolenaard51cb702015-07-21 15:03:06 +02003195 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
3196 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
3197 || (vim_strchr(eap->arg, '+')
3198 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
3199 || (vim_strchr(eap->arg, 'a')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003200 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
Bram Moolenaard51cb702015-07-21 15:03:06 +02003201 || (vim_strchr(eap->arg, 'h')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003202 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3203#ifdef FEAT_TERMINAL
3204 || (vim_strchr(eap->arg, 'R')
3205 && (!job_running || (job_running && job_none_open)))
3206 || (vim_strchr(eap->arg, '?')
3207 && (!job_running || (job_running && !job_none_open)))
3208 || (vim_strchr(eap->arg, 'F')
3209 && (job_running || buf->b_term == NULL))
3210#endif
Bram Moolenaard51cb702015-07-21 15:03:06 +02003211 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3212 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3213 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3214 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3215 || (vim_strchr(eap->arg, '#')
3216 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003219 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 else
3221 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003222 if (message_filtered(NameBuff))
3223 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003225 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3226 : (bufIsChanged(buf) ? '+' : ' ');
3227#ifdef FEAT_TERMINAL
3228 if (term_job_running(buf->b_term))
3229 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02003230 if (term_none_open(buf->b_term))
3231 ro_char = '?';
3232 else
3233 ro_char = 'R';
Bram Moolenaarc667da52019-11-30 20:52:27 +01003234 changed_char = ' '; // bufIsChanged() returns TRUE to avoid
3235 // closing, but it's not actually changed.
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003236 }
3237 else if (buf->b_term != NULL)
3238 ro_char = 'F';
3239 else
3240#endif
3241 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3242
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003243 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00003244 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 buf->b_fnum,
3246 buf->b_p_bl ? ' ' : 'u',
3247 buf == curbuf ? '%' :
3248 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3249 buf->b_ml.ml_mfp == NULL ? ' ' :
3250 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003251 ro_char,
3252 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003253 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003254 if (len > IOSIZE - 20)
3255 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256
Bram Moolenaarc667da52019-11-30 20:52:27 +01003257 // put "line 999" in column 40 or after the file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 i = 40 - vim_strsize(IObuff);
3259 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 IObuff[len++] = ' ';
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003261 while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar52410572019-10-27 05:12:45 +01003262#ifdef FEAT_VIMINFO
3263 if (vim_strchr(eap->arg, 't') && buf->b_last_used)
3264 add_time(IObuff + len, (size_t)(IOSIZE - len), buf->b_last_used);
3265 else
3266#endif
3267 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3268 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003269 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 msg_outtrans(IObuff);
Bram Moolenaarc667da52019-11-30 20:52:27 +01003271 out_flush(); // output one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 ui_breakcheck();
3273 }
Bram Moolenaar52410572019-10-27 05:12:45 +01003274
3275#ifdef FEAT_VIMINFO
3276 if (buflist_data)
3277 ga_clear(&buflist);
3278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280
3281/*
3282 * Get file name and line number for file 'fnum'.
3283 * Used by DoOneCmd() for translating '%' and '#'.
3284 * Used by insert_reg() and cmdline_paste() for '#' register.
3285 * Return FAIL if not found, OK for success.
3286 */
3287 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003288buflist_name_nr(
3289 int fnum,
3290 char_u **fname,
3291 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292{
3293 buf_T *buf;
3294
3295 buf = buflist_findnr(fnum);
3296 if (buf == NULL || buf->b_fname == NULL)
3297 return FAIL;
3298
3299 *fname = buf->b_fname;
3300 *lnum = buflist_findlnum(buf);
3301
3302 return OK;
3303}
3304
3305/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003306 * Set the file name for "buf"' to "ffname_arg", short file name to
3307 * "sfname_arg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 * The file name with the full path is also remembered, for when :cd is used.
3309 * Returns FAIL for failure (file name already in use by other buffer)
3310 * OK otherwise.
3311 */
3312 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003313setfname(
3314 buf_T *buf,
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003315 char_u *ffname_arg,
3316 char_u *sfname_arg,
Bram Moolenaarc667da52019-11-30 20:52:27 +01003317 int message) // give message when buffer already exists
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003319 char_u *ffname = ffname_arg;
3320 char_u *sfname = sfname_arg;
Bram Moolenaar81695252004-12-29 20:58:21 +00003321 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003323 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324#endif
3325
3326 if (ffname == NULL || *ffname == NUL)
3327 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003328 // Removing the name.
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003329 if (buf->b_sfname != buf->b_ffname)
3330 VIM_CLEAR(buf->b_sfname);
3331 else
3332 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003333 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334#ifdef UNIX
3335 st.st_dev = (dev_T)-1;
3336#endif
3337 }
3338 else
3339 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003340 fname_expand(buf, &ffname, &sfname); // will allocate ffname
3341 if (ffname == NULL) // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 return FAIL;
3343
3344 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01003345 * If the file name is already used in another buffer:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 * - if the buffer is loaded, fail
3347 * - if the buffer is not loaded, delete it from the list
3348 */
3349#ifdef UNIX
3350 if (mch_stat((char *)ffname, &st) < 0)
3351 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003352#endif
3353 if (!(buf->b_flags & BF_DUMMY))
3354#ifdef UNIX
3355 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003357 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358#endif
3359 if (obuf != NULL && obuf != buf)
3360 {
Bram Moolenaard3710cf2021-10-04 23:13:13 +01003361 win_T *win;
3362 tabpage_T *tab;
3363 int in_use = FALSE;
3364
3365 // during startup a window may use a buffer that is not loaded yet
3366 FOR_ALL_TAB_WINDOWS(tab, win)
3367 if (win->w_buffer == obuf)
3368 in_use = TRUE;
3369
3370 // it's loaded or used in a window, fail
3371 if (obuf->b_ml.ml_mfp != NULL || in_use)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 {
3373 if (message)
Bram Moolenaare1242042021-12-16 20:56:57 +00003374 emsg(_(e_buffer_with_this_name_already_exists));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 vim_free(ffname);
3376 return FAIL;
3377 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003378 // delete from the list
Bram Moolenaara6e8f882019-12-14 16:18:15 +01003379 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 }
3381 sfname = vim_strsave(sfname);
3382 if (ffname == NULL || sfname == NULL)
3383 {
3384 vim_free(sfname);
3385 vim_free(ffname);
3386 return FAIL;
3387 }
3388#ifdef USE_FNAME_CASE
Bram Moolenaarc667da52019-11-30 20:52:27 +01003389 fname_case(sfname, 0); // set correct case for short file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390#endif
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003391 if (buf->b_sfname != buf->b_ffname)
3392 vim_free(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 vim_free(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 buf->b_ffname = ffname;
3395 buf->b_sfname = sfname;
3396 }
3397 buf->b_fname = buf->b_sfname;
3398#ifdef UNIX
3399 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003400 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 else
3402 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003403 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 buf->b_dev = st.st_dev;
3405 buf->b_ino = st.st_ino;
3406 }
3407#endif
3408
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410
3411 buf_name_changed(buf);
3412 return OK;
3413}
3414
3415/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003416 * Crude way of changing the name of a buffer. Use with care!
3417 * The name should be relative to the current directory.
3418 */
3419 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003420buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003421{
3422 buf_T *buf;
3423
3424 buf = buflist_findnr(fnum);
3425 if (buf != NULL)
3426 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003427 if (buf->b_sfname != buf->b_ffname)
3428 vim_free(buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003429 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003430 buf->b_ffname = vim_strsave(name);
3431 buf->b_sfname = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01003432 // Allocate ffname and expand into full path. Also resolves .lnk
3433 // files on Win32.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003434 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003435 buf->b_fname = buf->b_sfname;
3436 }
3437}
3438
3439/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 * Take care of what needs to be done when the name of buffer "buf" has
3441 * changed.
3442 */
3443 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003444buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445{
3446 /*
3447 * If the file name changed, also change the name of the swapfile
3448 */
3449 if (buf->b_ml.ml_mfp != NULL)
3450 ml_setname(buf);
3451
Bram Moolenaar3ad69532021-11-19 17:01:08 +00003452#ifdef FEAT_TERMINAL
3453 if (buf->b_term != NULL)
3454 term_clear_status_text(buf->b_term);
3455#endif
3456
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 if (curwin->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003458 check_arg_idx(curwin); // check file name for arg list
Bram Moolenaarc667da52019-11-30 20:52:27 +01003459 maketitle(); // set window title
Bram Moolenaarc667da52019-11-30 20:52:27 +01003460 status_redraw_all(); // status lines need to be redrawn
3461 fmarks_check_names(buf); // check named file marks
3462 ml_timestamp(buf); // reset timestamp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463}
3464
3465/*
3466 * set alternate file name for current window
3467 *
3468 * Used by do_one_cmd(), do_write() and do_ecmd().
3469 * Return the buffer.
3470 */
3471 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003472setaltfname(
3473 char_u *ffname,
3474 char_u *sfname,
3475 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476{
3477 buf_T *buf;
3478
Bram Moolenaarc667da52019-11-30 20:52:27 +01003479 // Create a buffer. 'buflisted' is not set if it's a new buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaare1004402020-10-24 20:49:43 +02003481 if (buf != NULL && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 curwin->w_alt_fnum = buf->b_fnum;
3483 return buf;
3484}
3485
3486/*
3487 * Get alternate file name for current window.
3488 * Return NULL if there isn't any, and give error message if requested.
3489 */
3490 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003491getaltfname(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003492 int errmsg) // give error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493{
3494 char_u *fname;
3495 linenr_T dummy;
3496
3497 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3498 {
3499 if (errmsg)
Bram Moolenaar108010a2021-06-27 22:03:33 +02003500 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 return NULL;
3502 }
3503 return fname;
3504}
3505
3506/*
3507 * Add a file name to the buflist and return its number.
3508 * Uses same flags as buflist_new(), except BLN_DUMMY.
3509 *
3510 * used by qf_init(), main() and doarglist()
3511 */
3512 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003513buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514{
3515 buf_T *buf;
3516
3517 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3518 if (buf != NULL)
3519 return buf->b_fnum;
3520 return 0;
3521}
3522
3523#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3524/*
3525 * Adjust slashes in file names. Called after 'shellslash' was set.
3526 */
3527 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003528buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529{
3530 buf_T *bp;
3531
Bram Moolenaar29323592016-07-24 22:04:11 +02003532 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 {
3534 if (bp->b_ffname != NULL)
3535 slash_adjust(bp->b_ffname);
3536 if (bp->b_sfname != NULL)
3537 slash_adjust(bp->b_sfname);
3538 }
3539}
3540#endif
3541
3542/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003543 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 * Also save the local window option values.
3545 */
3546 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003547buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003549 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550}
3551
3552/*
3553 * Return TRUE if 'ffname' is not the same file as current file.
3554 * Fname must have a full path (expanded by mch_FullName()).
3555 */
3556 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003557otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558{
3559 return otherfile_buf(curbuf, ffname
3560#ifdef UNIX
3561 , NULL
3562#endif
3563 );
3564}
3565
3566 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003567otherfile_buf(
3568 buf_T *buf,
3569 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003571 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003573 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574{
Bram Moolenaarc667da52019-11-30 20:52:27 +01003575 // no name is different
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3577 return TRUE;
3578 if (fnamecmp(ffname, buf->b_ffname) == 0)
3579 return FALSE;
3580#ifdef UNIX
3581 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003582 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583
Bram Moolenaarc667da52019-11-30 20:52:27 +01003584 // If no stat_T given, get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 if (stp == NULL)
3586 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003587 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 st.st_dev = (dev_T)-1;
3589 stp = &st;
3590 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003591 // Use dev/ino to check if the files are the same, even when the names
3592 // are different (possible with links). Still need to compare the
3593 // name above, for when the file doesn't exist yet.
3594 // Problem: The dev/ino changes when a file is deleted (and created
3595 // again) and remains the same when renamed/moved. We don't want to
3596 // mch_stat() each buffer each time, that would be too slow. Get the
3597 // dev/ino again when they appear to match, but not when they appear
3598 // to be different: Could skip a buffer when it's actually the same
3599 // file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 if (buf_same_ino(buf, stp))
3601 {
3602 buf_setino(buf);
3603 if (buf_same_ino(buf, stp))
3604 return FALSE;
3605 }
3606 }
3607#endif
3608 return TRUE;
3609}
3610
3611#if defined(UNIX) || defined(PROTO)
3612/*
3613 * Set inode and device number for a buffer.
3614 * Must always be called when b_fname is changed!.
3615 */
3616 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003617buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003619 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620
3621 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3622 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003623 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 buf->b_dev = st.st_dev;
3625 buf->b_ino = st.st_ino;
3626 }
3627 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003628 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629}
3630
3631/*
3632 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3633 */
3634 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003635buf_same_ino(
3636 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003637 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003639 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 && stp->st_dev == buf->b_dev
3641 && stp->st_ino == buf->b_ino);
3642}
3643#endif
3644
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003645/*
3646 * Print info about the current buffer.
3647 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003649fileinfo(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003650 int fullname, // when non-zero print full path
Bram Moolenaar7454a062016-01-30 15:14:10 +01003651 int shorthelp,
3652 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653{
3654 char_u *name;
3655 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003656 char *p;
3657 char *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003658 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003660 buffer = alloc(IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 if (buffer == NULL)
3662 return;
3663
Bram Moolenaarc667da52019-11-30 20:52:27 +01003664 if (fullname > 1) // 2 CTRL-G: include buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003666 vim_snprintf(buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 p = buffer + STRLEN(buffer);
3668 }
3669 else
3670 p = buffer;
3671
3672 *p++ = '"';
3673 if (buf_spname(curbuf) != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003674 vim_strncpy((char_u *)p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 else
3676 {
3677 if (!fullname && curbuf->b_fname != NULL)
3678 name = curbuf->b_fname;
3679 else
3680 name = curbuf->b_ffname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003681 home_replace(shorthelp ? curbuf : NULL, name, (char_u *)p,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 (int)(IOSIZE - (p - buffer)), TRUE);
3683 }
3684
Bram Moolenaar32526b32019-01-19 17:43:09 +01003685 vim_snprintf_add(buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 curbufIsChanged() ? (shortmess(SHM_MOD)
3687 ? " [+]" : _(" [Modified]")) : " ",
3688 (curbuf->b_flags & BF_NOTEDITED)
3689#ifdef FEAT_QUICKFIX
3690 && !bt_dontwrite(curbuf)
3691#endif
3692 ? _("[Not edited]") : "",
3693 (curbuf->b_flags & BF_NEW)
3694#ifdef FEAT_QUICKFIX
3695 && !bt_dontwrite(curbuf)
3696#endif
Bram Moolenaar722e5052020-06-12 22:31:00 +02003697 ? new_file_message() : "",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003699 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 : _("[readonly]")) : "",
3701 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3702 || curbuf->b_p_ro) ?
3703 " " : "");
Bram Moolenaarc667da52019-11-30 20:52:27 +01003704 // With 32 bit longs and more than 21,474,836 lines multiplying by 100
3705 // causes an overflow, thus for large numbers divide instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 if (curwin->w_cursor.lnum > 1000000L)
3707 n = (int)(((long)curwin->w_cursor.lnum) /
3708 ((long)curbuf->b_ml.ml_line_count / 100L));
3709 else
3710 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3711 (long)curbuf->b_ml.ml_line_count);
3712 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003713 vim_snprintf_add(buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714#ifdef FEAT_CMDL_INFO
3715 else if (p_ru)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003716 // Current line and column are already on the screen -- webb
Bram Moolenaar32526b32019-01-19 17:43:09 +01003717 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003718 NGETTEXT("%ld line --%d%%--", "%ld lines --%d%%--",
3719 curbuf->b_ml.ml_line_count),
3720 (long)curbuf->b_ml.ml_line_count, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721#endif
3722 else
3723 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003724 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 _("line %ld of %ld --%d%%-- col "),
3726 (long)curwin->w_cursor.lnum,
3727 (long)curbuf->b_ml.ml_line_count,
3728 n);
3729 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003730 len = STRLEN(buffer);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003731 col_print((char_u *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3733 }
3734
Bram Moolenaar32526b32019-01-19 17:43:09 +01003735 (void)append_arg_number(curwin, (char_u *)buffer, IOSIZE,
3736 !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737
3738 if (dont_truncate)
3739 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003740 // Temporarily set msg_scroll to avoid the message being truncated.
3741 // First call msg_start() to get the message in the right place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 msg_start();
3743 n = msg_scroll;
3744 msg_scroll = TRUE;
3745 msg(buffer);
3746 msg_scroll = n;
3747 }
3748 else
3749 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003750 p = (char *)msg_trunc_attr(buffer, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaarc667da52019-11-30 20:52:27 +01003752 // Need to repeat the message after redrawing when:
3753 // - When restart_edit is set (otherwise there will be a delay
3754 // before redrawing).
3755 // - When the screen was scrolled but there is no wait-return
3756 // prompt.
Bram Moolenaar32526b32019-01-19 17:43:09 +01003757 set_keep_msg((char_u *)p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 }
3759
3760 vim_free(buffer);
3761}
3762
3763 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003764col_print(
3765 char_u *buf,
3766 size_t buflen,
3767 int col,
3768 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769{
3770 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003771 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003773 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774}
3775
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776static char_u *lasttitle = NULL;
3777static char_u *lasticon = NULL;
3778
Bram Moolenaar84a93082018-06-16 22:58:15 +02003779/*
3780 * Put the file name in the title bar and icon of the window.
3781 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003783maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784{
3785 char_u *p;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003786 char_u *title_str = NULL;
3787 char_u *icon_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 int maxlen = 0;
3789 int len;
3790 int mustset;
3791 char_u buf[IOSIZE];
3792 int off;
3793
3794 if (!redrawing())
3795 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003796 // Postpone updating the title when 'lazyredraw' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 need_maketitle = TRUE;
3798 return;
3799 }
3800
3801 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003802 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003803 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804
3805 if (p_title)
3806 {
3807 if (p_titlelen > 0)
3808 {
3809 maxlen = p_titlelen * Columns / 100;
3810 if (maxlen < 10)
3811 maxlen = 10;
3812 }
3813
Bram Moolenaar84a93082018-06-16 22:58:15 +02003814 title_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 if (*p_titlestring != NUL)
3816 {
3817#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003818 if (stl_syntax & STL_IN_TITLE)
3819 {
3820 int use_sandbox = FALSE;
Bram Moolenaar53989552019-12-23 22:59:18 +01003821 int called_emsg_before = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003822
3823# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003824 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003825# endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003826 build_stl_str_hl(curwin, title_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003827 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003828 0, maxlen, NULL, NULL);
Bram Moolenaar53989552019-12-23 22:59:18 +01003829 if (called_emsg > called_emsg_before)
Bram Moolenaard3667a22006-03-16 21:35:52 +00003830 set_string_option_direct((char_u *)"titlestring", -1,
3831 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 else
3834#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003835 title_str = p_titlestring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 }
3837 else
3838 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003839 // format: "fname + (path) (1 of 2) - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840
Bram Moolenaar2c666692012-09-05 13:30:40 +02003841#define SPACE_FOR_FNAME (IOSIZE - 100)
3842#define SPACE_FOR_DIR (IOSIZE - 20)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003843#define SPACE_FOR_ARGNR (IOSIZE - 10) // at least room for " - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003845 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar21554412017-07-24 21:44:43 +02003846#ifdef FEAT_TERMINAL
3847 else if (curbuf->b_term != NULL)
3848 {
3849 vim_strncpy(buf, term_get_status_text(curbuf->b_term),
3850 SPACE_FOR_FNAME);
3851 }
3852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 else
3854 {
3855 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003856 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 }
3859
Bram Moolenaar21554412017-07-24 21:44:43 +02003860#ifdef FEAT_TERMINAL
3861 if (curbuf->b_term == NULL)
3862#endif
3863 switch (bufIsChanged(curbuf)
3864 + (curbuf->b_p_ro * 2)
3865 + (!curbuf->b_p_ma * 4))
3866 {
3867 case 1: STRCAT(buf, " +"); break;
3868 case 2: STRCAT(buf, " ="); break;
3869 case 3: STRCAT(buf, " =+"); break;
3870 case 4:
3871 case 6: STRCAT(buf, " -"); break;
3872 case 5:
3873 case 7: STRCAT(buf, " -+"); break;
3874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875
Bram Moolenaar21554412017-07-24 21:44:43 +02003876 if (curbuf->b_fname != NULL
3877#ifdef FEAT_TERMINAL
3878 && curbuf->b_term == NULL
3879#endif
3880 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003882 // Get path of file, replace home dir with ~
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 off = (int)STRLEN(buf);
3884 buf[off++] = ' ';
3885 buf[off++] = '(';
3886 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003887 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc667da52019-11-30 20:52:27 +01003889 // avoid "c:/name" to be reduced to "c"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 if (isalpha(buf[off]) && buf[off + 1] == ':')
3891 off += 2;
3892#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003893 // remove the file name
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003894 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 if (p == buf + off)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003896 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003897 // must be a help buffer
Bram Moolenaar21554412017-07-24 21:44:43 +02003898 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003899 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003903
Bram Moolenaarc667da52019-11-30 20:52:27 +01003904 // Translate unprintable chars and concatenate. Keep some
3905 // room for the server name. When there is no room (very long
3906 // file name) use (...).
Bram Moolenaar2c666692012-09-05 13:30:40 +02003907 if (off < SPACE_FOR_DIR)
3908 {
3909 p = transstr(buf + off);
3910 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3911 vim_free(p);
3912 }
3913 else
3914 {
3915 vim_strncpy(buf + off, (char_u *)"...",
3916 (size_t)(SPACE_FOR_ARGNR - off));
3917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 STRCAT(buf, ")");
3919 }
3920
Bram Moolenaar2c666692012-09-05 13:30:40 +02003921 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922
3923#if defined(FEAT_CLIENTSERVER)
3924 if (serverName != NULL)
3925 {
3926 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003927 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 }
3929 else
3930#endif
3931 STRCAT(buf, " - VIM");
3932
3933 if (maxlen > 0)
3934 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003935 // make it shorter by removing a bit in the middle
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003936 if (vim_strsize(buf) > maxlen)
3937 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 }
3939 }
3940 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02003941 mustset = value_changed(title_str, &lasttitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942
3943 if (p_icon)
3944 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02003945 icon_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 if (*p_iconstring != NUL)
3947 {
3948#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003949 if (stl_syntax & STL_IN_ICON)
3950 {
3951 int use_sandbox = FALSE;
Bram Moolenaar53989552019-12-23 22:59:18 +01003952 int called_emsg_before = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003953
3954# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003955 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003956# endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003957 build_stl_str_hl(curwin, icon_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003958 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003959 0, 0, NULL, NULL);
Bram Moolenaar53989552019-12-23 22:59:18 +01003960 if (called_emsg > called_emsg_before)
Bram Moolenaard3667a22006-03-16 21:35:52 +00003961 set_string_option_direct((char_u *)"iconstring", -1,
3962 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 else
3965#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003966 icon_str = p_iconstring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 }
3968 else
3969 {
3970 if (buf_spname(curbuf) != NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003971 p = buf_spname(curbuf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01003972 else // use file name only in icon
Bram Moolenaar84a93082018-06-16 22:58:15 +02003973 p = gettail(curbuf->b_ffname);
3974 *icon_str = NUL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01003975 // Truncate name at 100 bytes.
Bram Moolenaar84a93082018-06-16 22:58:15 +02003976 len = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 if (len > 100)
3978 {
3979 len -= 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 if (has_mbyte)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003981 len += (*mb_tail_off)(p, p + len) + 1;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003982 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02003984 STRCPY(icon_str, p);
3985 trans_characters(icon_str, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 }
3987 }
3988
Bram Moolenaar84a93082018-06-16 22:58:15 +02003989 mustset |= value_changed(icon_str, &lasticon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990
3991 if (mustset)
3992 resettitle();
3993}
3994
3995/*
3996 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3997 * from "str" if it does.
Bram Moolenaar84a93082018-06-16 22:58:15 +02003998 * Return TRUE if resettitle() is to be called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 */
4000 static int
Bram Moolenaar84a93082018-06-16 22:58:15 +02004001value_changed(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002{
4003 if ((str == NULL) != (*last == NULL)
4004 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
4005 {
4006 vim_free(*last);
4007 if (str == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004008 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 *last = NULL;
Bram Moolenaar40385db2018-08-07 22:31:44 +02004010 mch_restore_title(
4011 last == &lasttitle ? SAVE_RESTORE_TITLE : SAVE_RESTORE_ICON);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 else
Bram Moolenaar84a93082018-06-16 22:58:15 +02004014 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 *last = vim_strsave(str);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004016 return TRUE;
4017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 }
4019 return FALSE;
4020}
4021
4022/*
4023 * Put current window title back (used after calling a shell)
4024 */
4025 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004026resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027{
4028 mch_settitle(lasttitle, lasticon);
4029}
Bram Moolenaarea408852005-06-25 22:49:46 +00004030
4031# if defined(EXITFREE) || defined(PROTO)
4032 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004033free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00004034{
4035 vim_free(lasttitle);
4036 vim_free(lasticon);
4037}
4038# endif
4039
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004041#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004042
4043/*
4044 * Used for building in the status line.
4045 */
4046typedef struct
4047{
4048 char_u *stl_start;
4049 int stl_minwid;
4050 int stl_maxwid;
4051 enum {
4052 Normal,
4053 Empty,
4054 Group,
4055 Middle,
4056 Highlight,
4057 TabPage,
4058 Trunc
4059 } stl_type;
4060} stl_item_T;
4061
4062static size_t stl_items_len = 20; // Initial value, grows as needed.
4063static stl_item_T *stl_items = NULL;
4064static int *stl_groupitem = NULL;
4065static stl_hlrec_T *stl_hltab = NULL;
4066static stl_hlrec_T *stl_tabtab = NULL;
4067
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004069 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 * Return length of string in screen cells.
4071 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004072 * Normally works for window "wp", except when working for 'tabline' then it
4073 * is "curwin".
4074 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 * Items are drawn interspersed with the text that surrounds it
4076 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
4077 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
4078 *
4079 * If maxwidth is not zero, the string will be filled at any middle marker
4080 * or truncated if too long, fillchar is used for all whitespace.
4081 */
4082 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004083build_stl_str_hl(
4084 win_T *wp,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004085 char_u *out, // buffer to write into != NameBuff
4086 size_t outlen, // length of out[]
Bram Moolenaar7454a062016-01-30 15:14:10 +01004087 char_u *fmt,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004088 int use_sandbox UNUSED, // "fmt" was set insecurely, use sandbox
Bram Moolenaar7454a062016-01-30 15:14:10 +01004089 int fillchar,
4090 int maxwidth,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004091 stl_hlrec_T **hltab, // return: HL attributes (can be NULL)
4092 stl_hlrec_T **tabtab) // return: tab page nrs (can be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093{
Bram Moolenaar10772302019-01-20 18:25:54 +01004094 linenr_T lnum;
4095 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 char_u *p;
4097 char_u *s;
4098 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004099 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100#ifdef FEAT_EVAL
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004101 win_T *save_curwin;
4102 buf_T *save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004103 int save_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104#endif
4105 int empty_line;
4106 colnr_T virtcol;
4107 long l;
4108 long n;
4109 int prevchar_isflag;
4110 int prevchar_isitem;
4111 int itemisflag;
4112 int fillable;
4113 char_u *str;
4114 long num;
4115 int width;
4116 int itemcnt;
4117 int curitem;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004118 int group_end_userhl;
4119 int group_start_userhl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 int groupdepth;
shadmansaleh30e3de22021-05-15 17:23:28 +02004121#ifdef FEAT_EVAL
4122 int evaldepth;
4123#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 int minwid;
4125 int maxwid;
4126 int zeropad;
4127 char_u base;
4128 char_u opt;
4129#define TMPLEN 70
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004130 char_u buf_tmp[TMPLEN];
4131 char_u win_tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004132 char_u *usefmt = fmt;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004133 stl_hlrec_T *sp;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004134 int save_must_redraw = must_redraw;
4135 int save_redr_type = curwin->w_redr_type;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004136
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004137 if (stl_items == NULL)
4138 {
4139 stl_items = ALLOC_MULT(stl_item_T, stl_items_len);
4140 stl_groupitem = ALLOC_MULT(int, stl_items_len);
4141 stl_hltab = ALLOC_MULT(stl_hlrec_T, stl_items_len);
4142 stl_tabtab = ALLOC_MULT(stl_hlrec_T, stl_items_len);
4143 }
4144
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004145#ifdef FEAT_EVAL
4146 /*
4147 * When the format starts with "%!" then evaluate it as an expression and
4148 * use the result as the actual format string.
4149 */
4150 if (fmt[0] == '%' && fmt[1] == '!')
4151 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004152 typval_T tv;
4153
4154 tv.v_type = VAR_NUMBER;
4155 tv.vval.v_number = wp->w_id;
4156 set_var((char_u *)"g:statusline_winid", &tv, FALSE);
4157
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004158 usefmt = eval_to_string_safe(fmt + 2, use_sandbox);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004159 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00004160 usefmt = fmt;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004161
4162 do_unlet((char_u *)"g:statusline_winid", TRUE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004163 }
4164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165
4166 if (fillchar == 0)
4167 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004168
Bram Moolenaar10772302019-01-20 18:25:54 +01004169 // The cursor in windows other than the current one isn't always
4170 // up-to-date, esp. because of autocommands and timers.
4171 lnum = wp->w_cursor.lnum;
4172 if (lnum > wp->w_buffer->b_ml.ml_line_count)
4173 {
4174 lnum = wp->w_buffer->b_ml.ml_line_count;
4175 wp->w_cursor.lnum = lnum;
4176 }
4177
4178 // Get line & check if empty (cursorpos will show "0-1"). Note that
4179 // p will become invalid when getting another buffer line.
4180 p = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar567199b2013-04-24 16:52:36 +02004181 empty_line = (*p == NUL);
4182
Bram Moolenaar10772302019-01-20 18:25:54 +01004183 // Get the byte value now, in case we need it below. This is more efficient
4184 // than making a copy of the line.
4185 len = STRLEN(p);
4186 if (wp->w_cursor.col > (colnr_T)len)
4187 {
4188 // Line may have changed since checking the cursor column, or the lnum
4189 // was adjusted above.
4190 wp->w_cursor.col = (colnr_T)len;
Bram Moolenaar10772302019-01-20 18:25:54 +01004191 wp->w_cursor.coladd = 0;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004192 byteval = 0;
Bram Moolenaar10772302019-01-20 18:25:54 +01004193 }
Bram Moolenaar567199b2013-04-24 16:52:36 +02004194 else
Bram Moolenaar567199b2013-04-24 16:52:36 +02004195 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196
4197 groupdepth = 0;
shadmansaleh30e3de22021-05-15 17:23:28 +02004198#ifdef FEAT_EVAL
4199 evaldepth = 0;
4200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 p = out;
4202 curitem = 0;
4203 prevchar_isflag = TRUE;
4204 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004205 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004207 if (curitem == (int)stl_items_len)
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004208 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004209 size_t new_len = stl_items_len * 3 / 2;
4210 stl_item_T *new_items;
4211 int *new_groupitem;
4212 stl_hlrec_T *new_hlrec;
4213
4214 new_items = vim_realloc(stl_items, sizeof(stl_item_T) * new_len);
4215 if (new_items == NULL)
4216 break;
4217 stl_items = new_items;
4218 new_groupitem = vim_realloc(stl_groupitem, sizeof(int) * new_len);
4219 if (new_groupitem == NULL)
4220 break;
4221 stl_groupitem = new_groupitem;
4222 new_hlrec = vim_realloc(stl_hltab, sizeof(stl_hlrec_T) * new_len);
4223 if (new_hlrec == NULL)
4224 break;
4225 stl_hltab = new_hlrec;
4226 new_hlrec = vim_realloc(stl_tabtab, sizeof(stl_hlrec_T) * new_len);
4227 if (new_hlrec == NULL)
4228 break;
4229 stl_tabtab = new_hlrec;
4230 stl_items_len = new_len;
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004231 }
4232
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 if (*s != NUL && *s != '%')
4234 prevchar_isflag = prevchar_isitem = FALSE;
4235
4236 /*
4237 * Handle up to the next '%' or the end.
4238 */
4239 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
4240 *p++ = *s++;
4241 if (*s == NUL || p + 1 >= out + outlen)
4242 break;
4243
4244 /*
4245 * Handle one '%' item.
4246 */
4247 s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004248 if (*s == NUL) // ignore trailing %
Bram Moolenaar1d87f512011-02-01 21:55:01 +01004249 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 if (*s == '%')
4251 {
4252 if (p + 1 >= out + outlen)
4253 break;
4254 *p++ = *s++;
4255 prevchar_isflag = prevchar_isitem = FALSE;
4256 continue;
4257 }
4258 if (*s == STL_MIDDLEMARK)
4259 {
4260 s++;
4261 if (groupdepth > 0)
4262 continue;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004263 stl_items[curitem].stl_type = Middle;
4264 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 continue;
4266 }
4267 if (*s == STL_TRUNCMARK)
4268 {
4269 s++;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004270 stl_items[curitem].stl_type = Trunc;
4271 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 continue;
4273 }
4274 if (*s == ')')
4275 {
4276 s++;
4277 if (groupdepth < 1)
4278 continue;
4279 groupdepth--;
4280
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004281 t = stl_items[stl_groupitem[groupdepth]].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 *p = NUL;
4283 l = vim_strsize(t);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004284 if (curitem > stl_groupitem[groupdepth] + 1
4285 && stl_items[stl_groupitem[groupdepth]].stl_minwid == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004287 // remove group if all items are empty and highlight group
4288 // doesn't change
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004289 group_start_userhl = group_end_userhl = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004290 for (n = stl_groupitem[groupdepth] - 1; n >= 0; n--)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004291 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004292 if (stl_items[n].stl_type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004293 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004294 group_start_userhl = group_end_userhl =
4295 stl_items[n].stl_minwid;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004296 break;
4297 }
4298 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004299 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004300 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004301 if (stl_items[n].stl_type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 break;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004303 if (stl_items[n].stl_type == Highlight)
4304 group_end_userhl = stl_items[n].stl_minwid;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004305 }
4306 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 {
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004308 // empty group
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 p = t;
4310 l = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004311 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004312 {
4313 // do not use the highlighting from the removed group
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004314 if (stl_items[n].stl_type == Highlight)
4315 stl_items[n].stl_type = Empty;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004316 // adjust the start position of TabPage to the next
4317 // item position
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004318 if (stl_items[n].stl_type == TabPage)
4319 stl_items[n].stl_start = p;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 }
4322 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004323 if (l > stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004325 // truncate, remove n bytes of text at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 if (has_mbyte)
4327 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004328 // Find the first character that should be included.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 n = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004330 while (l >= stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 {
4332 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004333 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 }
4335 }
4336 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004337 n = (long)(p - t) - stl_items[stl_groupitem[groupdepth]]
4338 .stl_maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339
4340 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004341 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 p = p - n + 1;
Bram Moolenaar13505972019-01-24 15:04:48 +01004343
4344 // Fill up space left over by half a double-wide char.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004345 while (++l < stl_items[stl_groupitem[groupdepth]].stl_minwid)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004346 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347
Bram Moolenaarc667da52019-11-30 20:52:27 +01004348 // correct the start of the items for the truncation
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004349 for (l = stl_groupitem[groupdepth] + 1; l < curitem; l++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004351 stl_items[l].stl_start -= n;
4352 if (stl_items[l].stl_start < t)
4353 stl_items[l].stl_start = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 }
4355 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004356 else if (abs(stl_items[stl_groupitem[groupdepth]].stl_minwid) > l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004358 // fill
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004359 n = stl_items[stl_groupitem[groupdepth]].stl_minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 if (n < 0)
4361 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004362 // fill by appending characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 n = 0 - n;
4364 while (l++ < n && p + 1 < out + outlen)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004365 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 }
4367 else
4368 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004369 // fill by inserting characters
Bram Moolenaar008bff92021-03-04 21:55:58 +01004370 l = (n - l) * MB_CHAR2LEN(fillchar);
4371 mch_memmove(t + l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004373 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 p += l;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004375 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
4376 stl_items[n].stl_start += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 for ( ; l > 0; l--)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004378 MB_CHAR2BYTES(fillchar, t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 }
4380 }
4381 continue;
4382 }
4383 minwid = 0;
4384 maxwid = 9999;
4385 zeropad = FALSE;
4386 l = 1;
4387 if (*s == '0')
4388 {
4389 s++;
4390 zeropad = TRUE;
4391 }
4392 if (*s == '-')
4393 {
4394 s++;
4395 l = -1;
4396 }
4397 if (VIM_ISDIGIT(*s))
4398 {
4399 minwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004400 if (minwid < 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 minwid = 0;
4402 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004403 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004405 stl_items[curitem].stl_type = Highlight;
4406 stl_items[curitem].stl_start = p;
4407 stl_items[curitem].stl_minwid = minwid > 9 ? 1 : minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 s++;
4409 curitem++;
4410 continue;
4411 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004412 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4413 {
4414 if (*s == STL_TABCLOSENR)
4415 {
4416 if (minwid == 0)
4417 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004418 // %X ends the close label, go back to the previously
4419 // define tab label nr.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004420 for (n = curitem - 1; n >= 0; --n)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004421 if (stl_items[n].stl_type == TabPage
4422 && stl_items[n].stl_minwid >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004423 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004424 minwid = stl_items[n].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004425 break;
4426 }
4427 }
4428 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01004429 // close nrs are stored as negative values
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004430 minwid = - minwid;
4431 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004432 stl_items[curitem].stl_type = TabPage;
4433 stl_items[curitem].stl_start = p;
4434 stl_items[curitem].stl_minwid = minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004435 s++;
4436 curitem++;
4437 continue;
4438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 if (*s == '.')
4440 {
4441 s++;
4442 if (VIM_ISDIGIT(*s))
4443 {
4444 maxwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004445 if (maxwid <= 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 maxwid = 50;
4447 }
4448 }
4449 minwid = (minwid > 50 ? 50 : minwid) * l;
4450 if (*s == '(')
4451 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004452 stl_groupitem[groupdepth++] = curitem;
4453 stl_items[curitem].stl_type = Group;
4454 stl_items[curitem].stl_start = p;
4455 stl_items[curitem].stl_minwid = minwid;
4456 stl_items[curitem].stl_maxwid = maxwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 s++;
4458 curitem++;
4459 continue;
4460 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004461#ifdef FEAT_EVAL
4462 // Denotes end of expanded %{} block
4463 if (*s == '}' && evaldepth > 0)
4464 {
4465 s++;
4466 evaldepth--;
4467 continue;
4468 }
4469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 if (vim_strchr(STL_ALL, *s) == NULL)
4471 {
4472 s++;
4473 continue;
4474 }
4475 opt = *s++;
4476
Bram Moolenaarc667da52019-11-30 20:52:27 +01004477 // OK - now for the real work
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 base = 'D';
4479 itemisflag = FALSE;
4480 fillable = TRUE;
4481 num = -1;
4482 str = NULL;
4483 switch (opt)
4484 {
4485 case STL_FILEPATH:
4486 case STL_FULLPATH:
4487 case STL_FILENAME:
Bram Moolenaarc667da52019-11-30 20:52:27 +01004488 fillable = FALSE; // don't change ' ' to fillchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004490 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 else
4492 {
4493 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004494 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4496 }
4497 trans_characters(NameBuff, MAXPATHL);
4498 if (opt != STL_FILENAME)
4499 str = NameBuff;
4500 else
4501 str = gettail(NameBuff);
4502 break;
4503
Bram Moolenaarc667da52019-11-30 20:52:27 +01004504 case STL_VIM_EXPR: // '{'
shadmansaleh30e3de22021-05-15 17:23:28 +02004505 {
4506#ifdef FEAT_EVAL
4507 char_u *block_start = s - 1;
4508#endif
4509 int reevaluate = (*s == '%');
4510
4511 if (reevaluate)
4512 s++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 itemisflag = TRUE;
4514 t = p;
shadmansaleh30e3de22021-05-15 17:23:28 +02004515 while ((*s != '}' || (reevaluate && s[-1] != '%'))
4516 && *s != NUL && p + 1 < out + outlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 *p++ = *s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004518 if (*s != '}') // missing '}' or out of space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 break;
4520 s++;
shadmansaleh30e3de22021-05-15 17:23:28 +02004521 if (reevaluate)
4522 p[-1] = 0; // remove the % at the end of %{% expr %}
4523 else
4524 *p = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 p = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526#ifdef FEAT_EVAL
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004527 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp),
4528 "%d", curbuf->b_fnum);
4529 set_internal_string_var((char_u *)"g:actual_curbuf", buf_tmp);
4530 vim_snprintf((char *)win_tmp, sizeof(win_tmp), "%d", curwin->w_id);
4531 set_internal_string_var((char_u *)"g:actual_curwin", win_tmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004533 save_curbuf = curbuf;
4534 save_curwin = curwin;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004535 save_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 curwin = wp;
4537 curbuf = wp->w_buffer;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004538 // Visual mode is only valid in the current window.
4539 if (curwin != save_curwin)
4540 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004542 str = eval_to_string_safe(p, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004544 curwin = save_curwin;
4545 curbuf = save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004546 VIsual_active = save_VIsual_active;
Bram Moolenaar01824652005-01-31 18:58:23 +00004547 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004548 do_unlet((char_u *)"g:actual_curwin", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549
4550 if (str != NULL && *str != 0)
4551 {
4552 if (*skipdigits(str) == NUL)
4553 {
4554 num = atoi((char *)str);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004555 VIM_CLEAR(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 itemisflag = FALSE;
4557 }
4558 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004559
4560 // If the output of the expression needs to be evaluated
4561 // replace the %{} block with the result of evaluation
4562 if (reevaluate && str != NULL && *str != 0
4563 && strchr((const char *)str, '%') != NULL
4564 && evaldepth < MAX_STL_EVAL_DEPTH)
4565 {
4566 size_t parsed_usefmt = (size_t)(block_start - usefmt);
4567 size_t str_length = strlen((const char *)str);
4568 size_t fmt_length = strlen((const char *)s);
4569 size_t new_fmt_len = parsed_usefmt
4570 + str_length + fmt_length + 3;
4571 char_u *new_fmt = (char_u *)alloc(new_fmt_len * sizeof(char_u));
4572 char_u *new_fmt_p = new_fmt;
4573
4574 new_fmt_p = (char_u *)memcpy(new_fmt_p, usefmt, parsed_usefmt)
4575 + parsed_usefmt;
4576 new_fmt_p = (char_u *)memcpy(new_fmt_p , str, str_length)
4577 + str_length;
4578 new_fmt_p = (char_u *)memcpy(new_fmt_p, "%}", 2) + 2;
4579 new_fmt_p = (char_u *)memcpy(new_fmt_p , s, fmt_length)
4580 + fmt_length;
4581 *new_fmt_p = 0;
4582 new_fmt_p = NULL;
4583
4584 if (usefmt != fmt)
4585 vim_free(usefmt);
4586 VIM_CLEAR(str);
4587 usefmt = new_fmt;
4588 s = usefmt + parsed_usefmt;
4589 evaldepth++;
4590 continue;
4591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592#endif
4593 break;
shadmansaleh30e3de22021-05-15 17:23:28 +02004594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 case STL_LINE:
4596 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4597 ? 0L : (long)(wp->w_cursor.lnum);
4598 break;
4599
4600 case STL_NUMLINES:
4601 num = wp->w_buffer->b_ml.ml_line_count;
4602 break;
4603
4604 case STL_COLUMN:
4605 num = !(State & INSERT) && empty_line
4606 ? 0 : (int)wp->w_cursor.col + 1;
4607 break;
4608
4609 case STL_VIRTCOL:
4610 case STL_VIRTCOL_ALT:
Bram Moolenaarc667da52019-11-30 20:52:27 +01004611 // In list mode virtcol needs to be recomputed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 virtcol = wp->w_virtcol;
Bram Moolenaareed9d462021-02-15 20:38:25 +01004613 if (wp->w_p_list && wp->w_lcs_chars.tab1 == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 {
4615 wp->w_p_list = FALSE;
4616 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
4617 wp->w_p_list = TRUE;
4618 }
4619 ++virtcol;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004620 // Don't display %V if it's the same as %c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 if (opt == STL_VIRTCOL_ALT
4622 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
4623 ? 0 : (int)wp->w_cursor.col + 1)))
4624 break;
4625 num = (long)virtcol;
4626 break;
4627
4628 case STL_PERCENTAGE:
4629 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4630 (long)wp->w_buffer->b_ml.ml_line_count);
4631 break;
4632
4633 case STL_ALTPERCENT:
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004634 str = buf_tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004635 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 break;
4637
4638 case STL_ARGLISTSTAT:
4639 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004640 buf_tmp[0] = 0;
4641 if (append_arg_number(wp, buf_tmp, (int)sizeof(buf_tmp), FALSE))
4642 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 break;
4644
4645 case STL_KEYMAP:
4646 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004647 if (get_keymap_str(wp, (char_u *)"<%s>", buf_tmp, TMPLEN))
4648 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 break;
4650 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004651#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004652 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653#else
4654 num = 0;
4655#endif
4656 break;
4657
4658 case STL_BUFNO:
4659 num = wp->w_buffer->b_fnum;
4660 break;
4661
4662 case STL_OFFSET_X:
4663 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004664 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 case STL_OFFSET:
4666#ifdef FEAT_BYTEOFF
4667 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4668 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4669 0L : l + 1 + (!(State & INSERT) && empty_line ?
4670 0 : (int)wp->w_cursor.col);
4671#endif
4672 break;
4673
4674 case STL_BYTEVAL_X:
4675 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004676 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004678 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 if (num == NL)
4680 num = 0;
4681 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4682 num = NL;
4683 break;
4684
4685 case STL_ROFLAG:
4686 case STL_ROFLAG_ALT:
4687 itemisflag = TRUE;
4688 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004689 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 break;
4691
4692 case STL_HELPFLAG:
4693 case STL_HELPFLAG_ALT:
4694 itemisflag = TRUE;
4695 if (wp->w_buffer->b_help)
4696 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004697 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 break;
4699
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 case STL_FILETYPE:
4701 if (*wp->w_buffer->b_p_ft != NUL
4702 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4703 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004704 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), "[%s]",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004705 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004706 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 }
4708 break;
4709
4710 case STL_FILETYPE_ALT:
4711 itemisflag = TRUE;
4712 if (*wp->w_buffer->b_p_ft != NUL
4713 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4714 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004715 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), ",%s",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004716 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004717 for (t = buf_tmp; *t != 0; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 *t = TOUPPER_LOC(*t);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004719 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 }
4721 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722
Bram Moolenaar4033c552017-09-16 20:54:51 +02004723#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 case STL_PREVIEWFLAG:
4725 case STL_PREVIEWFLAG_ALT:
4726 itemisflag = TRUE;
4727 if (wp->w_p_pvw)
4728 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4729 : _("[Preview]"));
4730 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004731
4732 case STL_QUICKFIX:
4733 if (bt_quickfix(wp->w_buffer))
4734 str = (char_u *)(wp->w_llist_ref
4735 ? _(msg_loclist)
4736 : _(msg_qflist));
4737 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738#endif
4739
4740 case STL_MODIFIED:
4741 case STL_MODIFIED_ALT:
4742 itemisflag = TRUE;
4743 switch ((opt == STL_MODIFIED_ALT)
4744 + bufIsChanged(wp->w_buffer) * 2
4745 + (!wp->w_buffer->b_p_ma) * 4)
4746 {
4747 case 2: str = (char_u *)"[+]"; break;
4748 case 3: str = (char_u *)",+"; break;
4749 case 4: str = (char_u *)"[-]"; break;
4750 case 5: str = (char_u *)",-"; break;
4751 case 6: str = (char_u *)"[+-]"; break;
4752 case 7: str = (char_u *)",+-"; break;
4753 }
4754 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004755
4756 case STL_HIGHLIGHT:
4757 t = s;
4758 while (*s != '#' && *s != NUL)
4759 ++s;
4760 if (*s == '#')
4761 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004762 stl_items[curitem].stl_type = Highlight;
4763 stl_items[curitem].stl_start = p;
4764 stl_items[curitem].stl_minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004765 curitem++;
4766 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004767 if (*s != NUL)
4768 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004769 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 }
4771
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004772 stl_items[curitem].stl_start = p;
4773 stl_items[curitem].stl_type = Normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 if (str != NULL && *str)
4775 {
4776 t = str;
4777 if (itemisflag)
4778 {
4779 if ((t[0] && t[1])
4780 && ((!prevchar_isitem && *t == ',')
4781 || (prevchar_isflag && *t == ' ')))
4782 t++;
4783 prevchar_isflag = TRUE;
4784 }
4785 l = vim_strsize(t);
4786 if (l > 0)
4787 prevchar_isitem = TRUE;
4788 if (l > maxwid)
4789 {
4790 while (l >= maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 if (has_mbyte)
4792 {
4793 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004794 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 }
4796 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 l -= byte2cells(*t++);
4798 if (p + 1 >= out + outlen)
4799 break;
4800 *p++ = '<';
4801 }
4802 if (minwid > 0)
4803 {
4804 for (; l < minwid && p + 1 < out + outlen; l++)
4805 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004806 // Don't put a "-" in front of a digit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4808 *p++ = ' ';
4809 else
Bram Moolenaar008bff92021-03-04 21:55:58 +01004810 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 }
4812 minwid = 0;
4813 }
4814 else
4815 minwid *= -1;
Bram Moolenaar008bff92021-03-04 21:55:58 +01004816 for (; *t && p + 1 < out + outlen; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004818 // Change a space by fillchar, unless fillchar is '-' and a
4819 // digit follows.
Bram Moolenaar008bff92021-03-04 21:55:58 +01004820 if (fillable && *t == ' '
4821 && (!VIM_ISDIGIT(*(t + 1)) || fillchar != '-'))
4822 MB_CHAR2BYTES(fillchar, p);
4823 else
4824 *p++ = *t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 }
4826 for (; l < minwid && p + 1 < out + outlen; l++)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004827 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 }
4829 else if (num >= 0)
4830 {
4831 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4832 char_u nstr[20];
4833
4834 if (p + 20 >= out + outlen)
Bram Moolenaarc667da52019-11-30 20:52:27 +01004835 break; // not sufficient space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 prevchar_isitem = TRUE;
4837 t = nstr;
4838 if (opt == STL_VIRTCOL_ALT)
4839 {
4840 *t++ = '-';
4841 minwid--;
4842 }
4843 *t++ = '%';
4844 if (zeropad)
4845 *t++ = '0';
4846 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004847 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 *t = 0;
4849
4850 for (n = num, l = 1; n >= nbase; n /= nbase)
4851 l++;
4852 if (opt == STL_VIRTCOL_ALT)
4853 l++;
4854 if (l > maxwid)
4855 {
4856 l += 2;
4857 n = l - maxwid;
4858 while (l-- > maxwid)
4859 num /= nbase;
4860 *t++ = '>';
4861 *t++ = '%';
4862 *t = t[-3];
4863 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004864 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4865 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 }
4867 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004868 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4869 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 p += STRLEN(p);
4871 }
4872 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004873 stl_items[curitem].stl_type = Empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874
4875 if (opt == STL_VIM_EXPR)
4876 vim_free(str);
4877
4878 if (num >= 0 || (!itemisflag && str && *str))
Bram Moolenaarc667da52019-11-30 20:52:27 +01004879 prevchar_isflag = FALSE; // Item not NULL, but not a flag
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 curitem++;
4881 }
4882 *p = NUL;
4883 itemcnt = curitem;
4884
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004885#ifdef FEAT_EVAL
4886 if (usefmt != fmt)
4887 vim_free(usefmt);
4888#endif
4889
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 width = vim_strsize(out);
4891 if (maxwidth > 0 && width > maxwidth)
4892 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004893 // Result is too long, must truncate somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 l = 0;
4895 if (itemcnt == 0)
4896 s = out;
4897 else
4898 {
4899 for ( ; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004900 if (stl_items[l].stl_type == Trunc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004902 // Truncate at %< item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004903 s = stl_items[l].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 break;
4905 }
4906 if (l == itemcnt)
4907 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004908 // No %< item, truncate first item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004909 s = stl_items[0].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 l = 0;
4911 }
4912 }
4913
4914 if (width - vim_strsize(s) >= maxwidth)
4915 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004916 // Truncation mark is beyond max length
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 if (has_mbyte)
4918 {
4919 s = out;
4920 width = 0;
4921 for (;;)
4922 {
4923 width += ptr2cells(s);
4924 if (width >= maxwidth)
4925 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004926 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01004928 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 while (++width < maxwidth)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004930 MB_CHAR2BYTES(fillchar, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 }
4932 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 s = out + maxwidth - 1;
4934 for (l = 0; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004935 if (stl_items[l].stl_start > s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 break;
4937 itemcnt = l;
4938 *s++ = '>';
4939 *s = 0;
4940 }
4941 else
4942 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 if (has_mbyte)
4944 {
4945 n = 0;
4946 while (width >= maxwidth)
4947 {
4948 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004949 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 }
4951 }
4952 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 n = width - maxwidth + 1;
4954 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004955 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 *s = '<';
4957
Bram Moolenaarc667da52019-11-30 20:52:27 +01004958 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 while (++width < maxwidth)
4960 {
4961 s = s + STRLEN(s);
Bram Moolenaar008bff92021-03-04 21:55:58 +01004962 MB_CHAR2BYTES(fillchar, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 *s = NUL;
4964 }
4965
Bram Moolenaarc667da52019-11-30 20:52:27 +01004966 --n; // count the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 for (; l < itemcnt; l++)
4968 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004969 if (stl_items[l].stl_start - n >= s)
4970 stl_items[l].stl_start -= n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004972 stl_items[l].stl_start = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 }
4974 }
4975 width = maxwidth;
4976 }
4977 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4978 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004979 // Apply STL_MIDDLE if any
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 for (l = 0; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004981 if (stl_items[l].stl_type == Middle)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 break;
4983 if (l < itemcnt)
4984 {
Bram Moolenaar008bff92021-03-04 21:55:58 +01004985 int middlelength = (maxwidth - width) * MB_CHAR2LEN(fillchar);
4986 p = stl_items[l].stl_start + middlelength;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004987 STRMOVE(p, stl_items[l].stl_start);
Bram Moolenaar008bff92021-03-04 21:55:58 +01004988 for (s = stl_items[l].stl_start; s < p;)
4989 MB_CHAR2BYTES(fillchar, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 for (l++; l < itemcnt; l++)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004991 stl_items[l].stl_start += middlelength;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 width = maxwidth;
4993 }
4994 }
4995
Bram Moolenaarc667da52019-11-30 20:52:27 +01004996 // Store the info about highlighting.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004997 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004999 *hltab = stl_hltab;
5000 sp = stl_hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 for (l = 0; l < itemcnt; l++)
5002 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005003 if (stl_items[l].stl_type == Highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005005 sp->start = stl_items[l].stl_start;
5006 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005007 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 }
5009 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005010 sp->start = NULL;
5011 sp->userhl = 0;
5012 }
5013
Bram Moolenaarc667da52019-11-30 20:52:27 +01005014 // Store the info about tab pages labels.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005015 if (tabtab != NULL)
5016 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005017 *tabtab = stl_tabtab;
5018 sp = stl_tabtab;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005019 for (l = 0; l < itemcnt; l++)
5020 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005021 if (stl_items[l].stl_type == TabPage)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005022 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005023 sp->start = stl_items[l].stl_start;
5024 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005025 sp++;
5026 }
5027 }
5028 sp->start = NULL;
5029 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 }
5031
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00005032 // When inside update_screen we do not want redrawing a statusline, ruler,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005033 // title, etc. to trigger another redraw, it may cause an endless loop.
Bram Moolenaar65ed1362017-09-30 16:00:14 +02005034 if (updating_screen)
5035 {
5036 must_redraw = save_must_redraw;
5037 curwin->w_redr_type = save_redr_type;
5038 }
Bram Moolenaarba2929b2017-09-08 13:59:21 +02005039
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 return width;
5041}
Bram Moolenaarc667da52019-11-30 20:52:27 +01005042#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005044#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
5045 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005047 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
5048 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 */
5050 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005051get_rel_pos(
5052 win_T *wp,
5053 char_u *buf,
5054 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005056 long above; // number of lines above window
5057 long below; // number of lines below window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058
Bram Moolenaarc667da52019-11-30 20:52:27 +01005059 if (buflen < 3) // need at least 3 chars for writing
Bram Moolenaar0027c212015-01-07 13:31:52 +01005060 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 above = wp->w_topline - 1;
5062#ifdef FEAT_DIFF
5063 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02005064 if (wp->w_topline == 1 && wp->w_topfill >= 1)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005065 above = 0; // All buffer lines are displayed and there is an
5066 // indication of filler lines, that can be considered
5067 // seeing all lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068#endif
5069 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
5070 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005071 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
5072 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005074 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005076 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 ? (int)(above / ((above + below) / 100L))
5078 : (int)(above * 100L / (above + below)));
5079}
5080#endif
5081
5082/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005083 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 * Return TRUE if it was appended.
5085 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005086 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005087append_arg_number(
5088 win_T *wp,
5089 char_u *buf,
5090 int buflen,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005091 int add_file) // Add "file" before the arg number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092{
5093 char_u *p;
5094
Bram Moolenaarc667da52019-11-30 20:52:27 +01005095 if (ARGCOUNT <= 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 return FALSE;
5097
Bram Moolenaarc667da52019-11-30 20:52:27 +01005098 p = buf + STRLEN(buf); // go to the end of the buffer
5099 if (p - buf + 35 >= buflen) // getting too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 return FALSE;
5101 *p++ = ' ';
5102 *p++ = '(';
5103 if (add_file)
5104 {
5105 STRCPY(p, "file ");
5106 p += 5;
5107 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005108 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
5109 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
5111 return TRUE;
5112}
5113
5114/*
5115 * If fname is not a full path, make it a full path.
5116 * Returns pointer to allocated memory (NULL for failure).
5117 */
5118 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005119fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120{
5121 /*
5122 * Force expanding the path always for Unix, because symbolic links may
5123 * mess up the full path name, even though it starts with a '/'.
5124 * Also expand when there is ".." in the file name, try to remove it,
5125 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00005126 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 * For MS-Windows also expand names like "longna~1" to "longname".
5128 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00005129#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 return FullName_save(fname, TRUE);
5131#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00005132 if (!vim_isAbsName(fname)
5133 || strstr((char *)fname, "..") != NULL
5134 || strstr((char *)fname, "//") != NULL
5135# ifdef BACKSLASH_IN_FILENAME
5136 || strstr((char *)fname, "\\\\") != NULL
5137# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005138# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00005140# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 )
5142 return FullName_save(fname, FALSE);
5143
5144 fname = vim_strsave(fname);
5145
Bram Moolenaar9b942202007-10-03 12:31:33 +00005146# ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01005147 if (fname != NULL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005148 fname_case(fname, 0); // set correct case for file name
Bram Moolenaar9b942202007-10-03 12:31:33 +00005149# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150
5151 return fname;
5152#endif
5153}
5154
5155/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005156 * Make "*ffname" a full file name, set "*sfname" to "*ffname" if not NULL.
5157 * "*ffname" becomes a pointer to allocated memory (or NULL).
5158 * When resolving a link both "*sfname" and "*ffname" will point to the same
5159 * allocated memory.
5160 * The "*ffname" and "*sfname" pointer values on call will not be freed.
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005161 * Note that the resulting "*ffname" pointer should be considered not allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005164fname_expand(
5165 buf_T *buf UNUSED,
5166 char_u **ffname,
5167 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005169 if (*ffname == NULL) // no file name given, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 return;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005171 if (*sfname == NULL) // no short file name given, use ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 *sfname = *ffname;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005173 *ffname = fix_fname(*ffname); // expand to full path
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174
5175#ifdef FEAT_SHORTCUT
5176 if (!buf->b_p_bin)
5177 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005178 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005180 // If the file name is a shortcut file, use the file it links to.
Bram Moolenaardce1e892019-02-10 23:18:53 +01005181 rfname = mch_resolve_path(*ffname, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005182 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 {
5184 vim_free(*ffname);
5185 *ffname = rfname;
5186 *sfname = rfname;
5187 }
5188 }
5189#endif
5190}
5191
5192/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 * Open a window for a number of buffers.
5194 */
5195 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005196ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197{
5198 buf_T *buf;
5199 win_T *wp, *wpnext;
5200 int split_ret = OK;
5201 int p_ea_save;
5202 int open_wins = 0;
5203 int r;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005204 int count; // Maximum number of windows to open.
5205 int all; // When TRUE also load inactive buffers.
Bram Moolenaare1004402020-10-24 20:49:43 +02005206 int had_tab = cmdmod.cmod_tab;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005207 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208
Bram Moolenaarc667da52019-11-30 20:52:27 +01005209 if (eap->addr_count == 0) // make as many windows as possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 count = 9999;
5211 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01005212 count = eap->line2; // make as many windows as specified
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5214 all = FALSE;
5215 else
5216 all = TRUE;
5217
5218 setpcmark();
5219
5220#ifdef FEAT_GUI
5221 need_mouse_correct = TRUE;
5222#endif
5223
5224 /*
5225 * Close superfluous windows (two windows for the same buffer).
5226 * Also close windows that are not full-width.
5227 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005228 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005229 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005230 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005232 tpnext = curtab->tp_next;
5233 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005235 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005236 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1004402020-10-24 20:49:43 +02005237 || ((cmdmod.cmod_split & WSP_VERT)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005238 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005239 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005240 : wp->w_width != Columns)
Bram Moolenaar4033c552017-09-16 20:54:51 +02005241 || (had_tab > 0 && wp != firstwin)) && !ONE_WINDOW
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005242 && !(wp->w_closing || wp->w_buffer->b_locked > 0))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005243 {
5244 win_close(wp, FALSE);
Bram Moolenaarc667da52019-11-30 20:52:27 +01005245 wpnext = firstwin; // just in case an autocommand does
5246 // something strange with windows
5247 tpnext = first_tabpage; // start all over...
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005248 open_wins = 0;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005249 }
5250 else
5251 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005253
Bram Moolenaarc667da52019-11-30 20:52:27 +01005254 // Without the ":tab" modifier only do the current tab page.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005255 if (had_tab == 0 || tpnext == NULL)
5256 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005257 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 }
5259
5260 /*
5261 * Go through the buffer list. When a buffer doesn't have a window yet,
5262 * open one. Otherwise move the window to the right position.
5263 * Watch out for autocommands that delete buffers or windows!
5264 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01005265 // Don't execute Win/Buf Enter/Leave autocommands here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 ++autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 win_enter(lastwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5270 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005271 // Check if this buffer needs a window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5273 continue;
5274
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005275 if (had_tab != 0)
5276 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005277 // With the ":tab" modifier don't move the window.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005278 if (buf->b_nwindows > 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005279 wp = lastwin; // buffer has a window, skip it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005280 else
5281 wp = NULL;
5282 }
5283 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005284 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005285 // Check if this buffer already has a window
Bram Moolenaar29323592016-07-24 22:04:11 +02005286 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005287 if (wp->w_buffer == buf)
5288 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005289 // If the buffer already has a window, move it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005290 if (wp != NULL)
5291 win_move_after(wp, curwin);
5292 }
5293
5294 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005296 bufref_T bufref;
5297
5298 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005299
Bram Moolenaarc667da52019-11-30 20:52:27 +01005300 // Split the window and put the buffer in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 p_ea_save = p_ea;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005302 p_ea = TRUE; // use space from all windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5304 ++open_wins;
5305 p_ea = p_ea_save;
5306 if (split_ret == FAIL)
5307 continue;
5308
Bram Moolenaarc667da52019-11-30 20:52:27 +01005309 // Open the buffer in this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005312 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005314 // autocommands deleted the buffer!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 swap_exists_action = SEA_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 break;
5317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 if (swap_exists_action == SEA_QUIT)
5319 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005320#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005321 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005322
Bram Moolenaarc667da52019-11-30 20:52:27 +01005323 // Reset the error/interrupt/exception state here so that
5324 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005325 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005326#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005327
Bram Moolenaarc667da52019-11-30 20:52:27 +01005328 // User selected Quit at ATTENTION prompt; close this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 win_close(curwin, TRUE);
5330 --open_wins;
5331 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005332 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005333
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005334#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005335 // Restore the error/interrupt/exception state if not
5336 // discarded by a new aborting error, interrupt, or uncaught
5337 // exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005338 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 }
5341 else
5342 handle_swap_exists(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 }
5344
5345 ui_breakcheck();
5346 if (got_int)
5347 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005348 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 break;
5350 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005351#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01005352 // Autocommands deleted the buffer or aborted script processing!!!
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005353 if (aborting())
5354 break;
5355#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01005356 // When ":tab" was used open a new tab for a new window repeatedly.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005357 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
Bram Moolenaare1004402020-10-24 20:49:43 +02005358 cmdmod.cmod_tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 --autocmd_no_enter;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005361 win_enter(firstwin, FALSE); // back to first window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363
5364 /*
5365 * Close superfluous windows.
5366 */
5367 for (wp = lastwin; open_wins > count; )
5368 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005369 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 || autowrite(wp->w_buffer, FALSE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 if (!win_valid(wp))
5372 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005373 // BufWrite Autocommands made the window invalid, start over
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 wp = lastwin;
5375 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005376 else if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005378 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 --open_wins;
5380 wp = lastwin;
5381 }
5382 else
5383 {
5384 wp = wp->w_prev;
5385 if (wp == NULL)
5386 break;
5387 }
5388 }
5389}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005392static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005393
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394/*
5395 * do_modelines() - process mode lines for the current file
5396 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005397 * "flags" can be:
5398 * OPT_WINONLY only set options local to window
5399 * OPT_NOWIN don't set options local to window
5400 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 * Returns immediately if the "ml" option isn't set.
5402 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005404do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005406 linenr_T lnum;
5407 int nmlines;
5408 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409
5410 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5411 return;
5412
Bram Moolenaarc667da52019-11-30 20:52:27 +01005413 // Disallow recursive entry here. Can happen when executing a modeline
5414 // triggers an autocommand, which reloads modelines with a ":do".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 if (entered)
5416 return;
5417
5418 ++entered;
Hu Jialun9dcd3492021-08-28 20:42:50 +02005419 for (lnum = 1; curbuf->b_p_ml && lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005421 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 nmlines = 0;
5423
Hu Jialun9dcd3492021-08-28 20:42:50 +02005424 for (lnum = curbuf->b_ml.ml_line_count; curbuf->b_p_ml && lnum > 0 && lnum > nmlines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005426 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 nmlines = 0;
5428 --entered;
5429}
5430
Bram Moolenaarc667da52019-11-30 20:52:27 +01005431#include "version.h" // for version number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432
5433/*
5434 * chk_modeline() - check a single line for a mode string
5435 * Return FAIL if an error encountered.
5436 */
5437 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005438chk_modeline(
5439 linenr_T lnum,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005440 int flags) // Same as for do_modelines().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441{
5442 char_u *s;
5443 char_u *e;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005444 char_u *linecopy; // local copy of any modeline found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 int prev;
5446 int vers;
5447 int end;
5448 int retval = OK;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005449 sctx_T save_current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005450
Bram Moolenaare31ee862020-01-07 20:59:34 +01005451 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452
5453 prev = -1;
5454 for (s = ml_get(lnum); *s != NUL; ++s)
5455 {
5456 if (prev == -1 || vim_isspace(prev))
5457 {
5458 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5459 || STRNCMP(s, "vi:", (size_t)3) == 0)
5460 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005461 // Accept both "vim" and "Vim".
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005462 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 {
5464 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5465 e = s + 4;
5466 else
5467 e = s + 3;
5468 vers = getdigits(&e);
5469 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005470 && (s[0] != 'V'
5471 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 && (s[3] == ':'
5473 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5474 || (VIM_VERSION_100 < vers && s[3] == '<')
5475 || (VIM_VERSION_100 > vers && s[3] == '>')
5476 || (VIM_VERSION_100 == vers && s[3] == '=')))
5477 break;
5478 }
5479 }
5480 prev = *s;
5481 }
5482
5483 if (*s)
5484 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005485 do // skip over "ex:", "vi:" or "vim:"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 ++s;
5487 while (s[-1] != ':');
5488
Bram Moolenaarc667da52019-11-30 20:52:27 +01005489 s = linecopy = vim_strsave(s); // copy the line, it will change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 if (linecopy == NULL)
5491 return FAIL;
5492
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005493 // prepare for emsg()
5494 estack_push(ETYPE_MODELINE, (char_u *)"modelines", lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01005495 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496
5497 end = FALSE;
5498 while (end == FALSE)
5499 {
5500 s = skipwhite(s);
5501 if (*s == NUL)
5502 break;
5503
5504 /*
5505 * Find end of set command: ':' or end of line.
5506 * Skip over "\:", replacing it with ":".
5507 */
5508 for (e = s; *e != ':' && *e != NUL; ++e)
5509 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005510 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 if (*e == NUL)
5512 end = TRUE;
5513
5514 /*
5515 * If there is a "set" command, require a terminating ':' and
5516 * ignore the stuff after the ':'.
5517 * "vi:set opt opt opt: foo" -- foo not interpreted
5518 * "vi:opt opt opt: foo" -- foo interpreted
5519 * Accept "se" for compatibility with Elvis.
5520 */
5521 if (STRNCMP(s, "set ", (size_t)4) == 0
5522 || STRNCMP(s, "se ", (size_t)3) == 0)
5523 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005524 if (*e != ':') // no terminating ':'?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 break;
5526 end = TRUE;
5527 s = vim_strchr(s, ' ') + 1;
5528 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005529 *e = NUL; // truncate the set command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530
Bram Moolenaarc667da52019-11-30 20:52:27 +01005531 if (*s != NUL) // skip over an empty "::"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 {
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005533 int secure_save = secure;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005534
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005535 save_current_sctx = current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005536 current_sctx.sc_version = 1;
5537#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005538 current_sctx.sc_sid = SID_MODELINE;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005539 current_sctx.sc_seq = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005540 current_sctx.sc_lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005542
Bram Moolenaar5958f952018-11-20 04:25:21 +01005543 // Make sure no risky things are executed as a side effect.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01005544 secure = 1;
Bram Moolenaar5958f952018-11-20 04:25:21 +01005545
Bram Moolenaara3227e22006-03-08 21:32:40 +00005546 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar5958f952018-11-20 04:25:21 +01005547
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005548 secure = secure_save;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005549 current_sctx = save_current_sctx;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005550 if (retval == FAIL) // stop if error found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 break;
5552 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005553 s = e + 1; // advance to next part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 }
5555
Bram Moolenaare31ee862020-01-07 20:59:34 +01005556 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005557 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 vim_free(linecopy);
5559 }
5560 return retval;
5561}
5562
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005563/*
Bram Moolenaar91335e52018-08-01 17:53:12 +02005564 * Return TRUE if "buf" is a normal buffer, 'buftype' is empty.
5565 */
5566 int
5567bt_normal(buf_T *buf)
5568{
5569 return buf != NULL && buf->b_p_bt[0] == NUL;
5570}
5571
Bram Moolenaar113e1072019-01-20 15:30:40 +01005572#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar91335e52018-08-01 17:53:12 +02005573/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005574 * Return TRUE if "buf" is the quickfix buffer.
5575 */
5576 int
5577bt_quickfix(buf_T *buf)
5578{
5579 return buf != NULL && buf->b_p_bt[0] == 'q';
5580}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005581#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005582
Bram Moolenaar113e1072019-01-20 15:30:40 +01005583#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005584/*
5585 * Return TRUE if "buf" is a terminal buffer.
5586 */
5587 int
5588bt_terminal(buf_T *buf)
5589{
5590 return buf != NULL && buf->b_p_bt[0] == 't';
5591}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005592#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005593
5594/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005595 * Return TRUE if "buf" is a help buffer.
5596 */
5597 int
5598bt_help(buf_T *buf)
5599{
5600 return buf != NULL && buf->b_help;
5601}
5602
5603/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02005604 * Return TRUE if "buf" is a prompt buffer.
5605 */
5606 int
5607bt_prompt(buf_T *buf)
5608{
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005609 return buf != NULL && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'r';
5610}
5611
5612/*
5613 * Return TRUE if "buf" is a buffer for a popup window.
5614 */
5615 int
5616bt_popup(buf_T *buf)
5617{
5618 return buf != NULL && buf->b_p_bt != NULL
5619 && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'o';
Bram Moolenaarf2732452018-06-03 14:47:35 +02005620}
5621
5622/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005623 * Return TRUE if "buf" is a "nofile", "acwrite", "terminal" or "prompt"
5624 * buffer. This means the buffer name is not a file name.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005625 */
5626 int
Bram Moolenaar26910de2019-06-15 19:37:15 +02005627bt_nofilename(buf_T *buf)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005628{
5629 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5630 || buf->b_p_bt[0] == 'a'
Bram Moolenaarf2732452018-06-03 14:47:35 +02005631 || buf->b_p_bt[0] == 't'
5632 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005633}
5634
5635/*
Bram Moolenaar26910de2019-06-15 19:37:15 +02005636 * Return TRUE if "buf" has 'buftype' set to "nofile".
5637 */
5638 int
5639bt_nofile(buf_T *buf)
5640{
5641 return buf != NULL && buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f';
5642}
5643
5644/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005645 * Return TRUE if "buf" is a "nowrite", "nofile", "terminal" or "prompt"
5646 * buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005647 */
5648 int
5649bt_dontwrite(buf_T *buf)
5650{
Bram Moolenaarf2732452018-06-03 14:47:35 +02005651 return buf != NULL && (buf->b_p_bt[0] == 'n'
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005652 || buf->b_p_bt[0] == 't'
5653 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005654}
5655
Bram Moolenaar113e1072019-01-20 15:30:40 +01005656#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005657 int
5658bt_dontwrite_msg(buf_T *buf)
5659{
5660 if (bt_dontwrite(buf))
5661 {
Bram Moolenaarf1474d82021-12-31 19:59:55 +00005662 emsg(_(e_cannot_write_buftype_option_is_set));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005663 return TRUE;
5664 }
5665 return FALSE;
5666}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005667#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005668
5669/*
5670 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
5671 * and 'bufhidden'.
5672 */
5673 int
5674buf_hide(buf_T *buf)
5675{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005676 // 'bufhidden' overrules 'hidden' and ":hide", check it first
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005677 switch (buf->b_p_bh[0])
5678 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005679 case 'u': // "unload"
5680 case 'w': // "wipe"
5681 case 'd': return FALSE; // "delete"
5682 case 'h': return TRUE; // "hide"
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005683 }
Bram Moolenaare1004402020-10-24 20:49:43 +02005684 return (p_hid || (cmdmod.cmod_flags & CMOD_HIDE));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005685}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686
5687/*
5688 * Return special buffer name.
5689 * Returns NULL when the buffer has a normal file name.
5690 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005691 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005692buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693{
Bram Moolenaar4033c552017-09-16 20:54:51 +02005694#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005696 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005697 /*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005698 * Differentiate between the quickfix and location list buffers using
5699 * the buffer number stored in the global quickfix stack.
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005700 */
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005701 if (buf->b_fnum == qf_stack_get_bufnr())
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005702 return (char_u *)_(msg_qflist);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005703 else
5704 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02005707
Bram Moolenaarc667da52019-11-30 20:52:27 +01005708 // There is no _file_ when 'buftype' is "nofile", b_sfname
5709 // contains the name as specified by the user.
Bram Moolenaar26910de2019-06-15 19:37:15 +02005710 if (bt_nofilename(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 {
Bram Moolenaar21554412017-07-24 21:44:43 +02005712#ifdef FEAT_TERMINAL
5713 if (buf->b_term != NULL)
5714 return term_get_status_text(buf->b_term);
5715#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02005716 if (buf->b_fname != NULL)
5717 return buf->b_fname;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02005718#ifdef FEAT_JOB_CHANNEL
5719 if (bt_prompt(buf))
5720 return (char_u *)_("[Prompt]");
5721#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005722#ifdef FEAT_PROP_POPUP
Bram Moolenaarc6896e22019-05-30 22:32:34 +02005723 if (bt_popup(buf))
5724 return (char_u *)_("[Popup]");
5725#endif
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005726 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 }
Bram Moolenaar21554412017-07-24 21:44:43 +02005728
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 if (buf->b_fname == NULL)
Bram Moolenaar00806bc2020-11-05 19:36:38 +01005730 return buf_get_fname(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 return NULL;
5732}
5733
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734/*
Bram Moolenaar00806bc2020-11-05 19:36:38 +01005735 * Get "buf->b_fname", use "[No Name]" if it is NULL.
5736 */
5737 char_u *
5738buf_get_fname(buf_T *buf)
5739{
5740 if (buf->b_fname == NULL)
5741 return (char_u *)_("[No Name]");
5742 return buf->b_fname;
5743}
5744
5745/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5747 */
5748 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005749set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750{
5751 if (on != curbuf->b_p_bl)
5752 {
5753 curbuf->b_p_bl = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 if (on)
5755 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5756 else
5757 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 }
5759}
5760
5761/*
5762 * Read the file for "buf" again and check if the contents changed.
5763 * Return TRUE if it changed or this could not be checked.
5764 */
5765 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005766buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767{
5768 buf_T *newbuf;
5769 int differ = TRUE;
5770 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 exarg_T ea;
5773
Bram Moolenaarc667da52019-11-30 20:52:27 +01005774 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5776 if (newbuf == NULL)
5777 return TRUE;
5778
Bram Moolenaarc667da52019-11-30 20:52:27 +01005779 // Force the 'fileencoding' and 'fileformat' to be equal.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 if (prep_exarg(&ea, buf) == FAIL)
5781 {
5782 wipe_buffer(newbuf, FALSE);
5783 return TRUE;
5784 }
5785
Bram Moolenaarc667da52019-11-30 20:52:27 +01005786 // set curwin/curbuf to buf and save a few things
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788
Bram Moolenaar4770d092006-01-12 23:22:24 +00005789 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 && readfile(buf->b_ffname, buf->b_fname,
5791 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5792 &ea, READ_NEW | READ_DUMMY) == OK)
5793 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005794 // compare the two files line by line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5796 {
5797 differ = FALSE;
5798 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5799 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5800 {
5801 differ = TRUE;
5802 break;
5803 }
5804 }
5805 }
5806 vim_free(ea.cmd);
5807
Bram Moolenaarc667da52019-11-30 20:52:27 +01005808 // restore curwin/curbuf and a few other things
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810
Bram Moolenaarc667da52019-11-30 20:52:27 +01005811 if (curbuf != newbuf) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 wipe_buffer(newbuf, FALSE);
5813
5814 return differ;
5815}
5816
5817/*
5818 * Wipe out a buffer and decrement the last buffer number if it was used for
5819 * this buffer. Call this to wipe out a temp buffer that does not contain any
5820 * marks.
5821 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005823wipe_buffer(
5824 buf_T *buf,
Bram Moolenaara6e8f882019-12-14 16:18:15 +01005825 int aucmd) // When TRUE trigger autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826{
5827 if (buf->b_fnum == top_file_num - 1)
5828 --top_file_num;
5829
Bram Moolenaarc667da52019-11-30 20:52:27 +01005830 if (!aucmd) // Don't trigger BufDelete autocommands here.
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005831 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005832
Bram Moolenaara6e8f882019-12-14 16:18:15 +01005833 close_buffer(NULL, buf, DOBUF_WIPE, FALSE, TRUE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005834
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005836 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837}