blob: 808249908c1f2a9ac1a73be5437d5f367f0db397 [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
47#ifdef FEAT_TITLE
Bram Moolenaar84a93082018-06-16 22:58:15 +020048static int value_changed(char_u *str, char_u **last);
Bram Moolenaar071d4272004-06-13 20:20:40 +000049#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010050static int append_arg_number(win_T *wp, char_u *buf, int buflen, int add_file);
51static void free_buffer(buf_T *);
52static void free_buffer_stuff(buf_T *buf, int free_options);
53static void clear_wininfo(buf_T *buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000054
55#ifdef UNIX
56# define dev_T dev_t
57#else
58# define dev_T unsigned
59#endif
60
Bram Moolenaar00d253e2020-04-06 22:13:01 +020061#define FOR_ALL_BUFS_FROM_LAST(buf) \
62 for ((buf) = lastbuf; (buf) != NULL; (buf) = (buf)->b_prev)
63
Bram Moolenaar4033c552017-09-16 20:54:51 +020064#if defined(FEAT_QUICKFIX)
Bram Moolenaar7fd73202010-07-25 16:58:46 +020065static char *msg_loclist = N_("[Location List]");
66static char *msg_qflist = N_("[Quickfix List]");
67#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +010068static char *e_auabort = N_("E855: Autocommands caused command to abort");
Bram Moolenaar7fd73202010-07-25 16:58:46 +020069
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +020070// Number of times free_buffer() was called.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +020071static int buf_free_count = 0;
72
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +020073static int top_file_num = 1; // highest file number
74static garray_T buf_reuse = GA_EMPTY; // file numbers to recycle
75
76/*
77 * Return the highest possible buffer number.
78 */
79 int
80get_highest_fnum(void)
81{
82 return top_file_num - 1;
83}
84
Bram Moolenaarc024b462019-06-08 18:07:21 +020085/*
86 * Read data from buffer for retrying.
87 */
Bram Moolenaarf71d7b92016-08-09 22:14:05 +020088 static int
89read_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +010090 int read_stdin, // read file from stdin, otherwise fifo
91 exarg_T *eap, // for forced 'ff' and 'fenc' or NULL
92 int flags) // extra flags for readfile()
Bram Moolenaarf71d7b92016-08-09 22:14:05 +020093{
94 int retval = OK;
95 linenr_T line_count;
96
Bram Moolenaarc5935a82021-10-19 20:48:52 +010097 // Read from the buffer which the text is already filled in and append at
98 // the end. This makes it possible to retry when 'fileformat' or
99 // 'fileencoding' was guessed wrong.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200100 line_count = curbuf->b_ml.ml_line_count;
101 retval = readfile(
102 read_stdin ? NULL : curbuf->b_ffname,
103 read_stdin ? NULL : curbuf->b_fname,
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +0100104 line_count, (linenr_T)0, (linenr_T)MAXLNUM, eap,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200105 flags | READ_BUFFER);
106 if (retval == OK)
107 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100108 // Delete the binary lines.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200109 while (--line_count >= 0)
Bram Moolenaarca70c072020-05-30 20:30:46 +0200110 ml_delete((linenr_T)1);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200111 }
112 else
113 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100114 // Delete the converted lines.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200115 while (curbuf->b_ml.ml_line_count > line_count)
Bram Moolenaarca70c072020-05-30 20:30:46 +0200116 ml_delete(line_count);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200117 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100118 // Put the cursor on the first line.
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200119 curwin->w_cursor.lnum = 1;
120 curwin->w_cursor.col = 0;
121
122 if (read_stdin)
123 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100124 // Set or reset 'modified' before executing autocommands, so that
125 // it can be changed there.
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100126 if (!readonlymode && !BUFEMPTY())
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200127 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100128 else if (retval == OK)
Bram Moolenaarc024b462019-06-08 18:07:21 +0200129 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200130
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100131 if (retval == OK)
132 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100133#ifdef FEAT_EVAL
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100134 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100135 curbuf, &retval);
136#else
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100137 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200138#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100139 }
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200140 }
141 return retval;
142}
143
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144/*
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200145 * Ensure buffer "buf" is loaded. Does not trigger the swap-exists action.
146 */
147 void
148buffer_ensure_loaded(buf_T *buf)
149{
150 if (buf->b_ml.ml_mfp == NULL)
151 {
152 aco_save_T aco;
153
154 aucmd_prepbuf(&aco, buf);
155 swap_exists_action = SEA_NONE;
156 open_buffer(FALSE, NULL, 0);
157 aucmd_restbuf(&aco);
158 }
159}
160
161/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200162 * Open current buffer, that is: open the memfile and read the file into
163 * memory.
164 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165 */
166 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100167open_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +0100168 int read_stdin, // read file from stdin
169 exarg_T *eap, // for forced 'ff' and 'fenc' or NULL
170 int flags) // extra flags for readfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171{
172 int retval = OK;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200173 bufref_T old_curbuf;
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100174#ifdef FEAT_SYN_HL
175 long old_tw = curbuf->b_p_tw;
176#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200177 int read_fifo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100179 // The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
180 // When re-entering the same buffer, it should not change, because the
181 // user may have reset the flag by hand.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 if (readonlymode && curbuf->b_ffname != NULL
183 && (curbuf->b_flags & BF_NEVERLOADED))
184 curbuf->b_p_ro = TRUE;
185
Bram Moolenaar4770d092006-01-12 23:22:24 +0000186 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100188 // There MUST be a memfile, otherwise we can't do anything
189 // If we can't create one for the current buffer, take another buffer
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100190 close_buffer(NULL, curbuf, 0, FALSE, FALSE);
Bram Moolenaar29323592016-07-24 22:04:11 +0200191 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 if (curbuf->b_ml.ml_mfp != NULL)
193 break;
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100194 // If there is no memfile at all, exit.
195 // This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 if (curbuf == NULL)
197 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100198 emsg(_("E82: Cannot allocate any buffer, exiting..."));
Bram Moolenaar6f10c702019-08-20 22:58:37 +0200199
200 // Don't try to do any saving, with "curbuf" NULL almost nothing
201 // will work.
202 v_dying = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 getout(2);
204 }
Bram Moolenaar6f10c702019-08-20 22:58:37 +0200205
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100206 emsg(_("E83: Cannot allocate buffer, using other one..."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 enter_buffer(curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100208#ifdef FEAT_SYN_HL
209 if (old_tw != curbuf->b_p_tw)
210 check_colorcolumn(curwin);
211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212 return FAIL;
213 }
214
Bram Moolenaarc667da52019-11-30 20:52:27 +0100215 // The autocommands in readfile() may change the buffer, but only AFTER
216 // reading the file.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200217 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218 modified_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219
Bram Moolenaarc667da52019-11-30 20:52:27 +0100220 // mark cursor position as being invalid
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100221 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222
223 if (curbuf->b_ffname != NULL
224#ifdef FEAT_NETBEANS_INTG
225 && netbeansReadFile
226#endif
227 )
228 {
Bram Moolenaar426dd022016-03-15 15:09:29 +0100229 int old_msg_silent = msg_silent;
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200230#ifdef UNIX
231 int save_bin = curbuf->b_p_bin;
232 int perm;
233#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234#ifdef FEAT_NETBEANS_INTG
235 int oldFire = netbeansFireChanges;
236
237 netbeansFireChanges = 0;
238#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200239#ifdef UNIX
240 perm = mch_getperm(curbuf->b_ffname);
Bram Moolenaard569bb02018-08-11 13:57:20 +0200241 if (perm >= 0 && (S_ISFIFO(perm)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200242 || S_ISSOCK(perm)
Bram Moolenaarf04507d2016-08-20 15:05:39 +0200243# ifdef OPEN_CHR_FILES
244 || (S_ISCHR(perm) && is_dev_fd_file(curbuf->b_ffname))
245# endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200246 ))
247 read_fifo = TRUE;
248 if (read_fifo)
249 curbuf->b_p_bin = TRUE;
250#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100251 if (shortmess(SHM_FILEINFO))
252 msg_silent = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200254 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200255 flags | READ_NEW | (read_fifo ? READ_FIFO : 0));
256#ifdef UNIX
257 if (read_fifo)
258 {
259 curbuf->b_p_bin = save_bin;
260 if (retval == OK)
261 retval = read_buffer(FALSE, eap, flags);
262 }
263#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100264 msg_silent = old_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265#ifdef FEAT_NETBEANS_INTG
266 netbeansFireChanges = oldFire;
267#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100268 // Help buffer is filtered.
Bram Moolenaard28cc3f2017-07-27 22:03:50 +0200269 if (bt_help(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 fix_help_buffer();
271 }
272 else if (read_stdin)
273 {
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200274 int save_bin = curbuf->b_p_bin;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100276 // First read the text in binary mode into the buffer.
277 // Then read from that same buffer and append at the end. This makes
278 // it possible to retry when 'fileformat' or 'fileencoding' was
279 // guessed wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280 curbuf->b_p_bin = TRUE;
281 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200282 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
283 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284 curbuf->b_p_bin = save_bin;
285 if (retval == OK)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200286 retval = read_buffer(TRUE, eap, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 }
288
Bram Moolenaarc667da52019-11-30 20:52:27 +0100289 // if first time loading this buffer, init b_chartab[]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290 if (curbuf->b_flags & BF_NEVERLOADED)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100291 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292 (void)buf_init_chartab(curbuf, FALSE);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100293#ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100294 parse_cino(curbuf);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100295#endif
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100298 // Set/reset the Changed flag first, autocmds may change the buffer.
299 // Apply the automatic commands, before processing the modelines.
300 // So the modelines have priority over autocommands.
301 //
Bram Moolenaarc667da52019-11-30 20:52:27 +0100302 // When reading stdin, the buffer contents always needs writing, so set
303 // the changed flag. Unless in readonly mode: "ls | gview -".
304 // When interrupted and 'cpoptions' contains 'i' set changed flag.
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000305 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100306 || modified_was_set // ":set modified" used in autocmd
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100307#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000310 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100312 else if (retval == OK && !read_stdin && !read_fifo)
Bram Moolenaarc024b462019-06-08 18:07:21 +0200313 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaarc667da52019-11-30 20:52:27 +0100314 save_file_ff(curbuf); // keep this fileformat
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315
Bram Moolenaarc667da52019-11-30 20:52:27 +0100316 // Set last_changedtick to avoid triggering a TextChanged autocommand right
317 // after it was added.
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100318 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Christian Brabandtdb3b4462021-10-16 11:58:55 +0100319 curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100320 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100321
Bram Moolenaarc667da52019-11-30 20:52:27 +0100322 // require "!" to overwrite the file, because it wasn't read completely
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323#ifdef FEAT_EVAL
324 if (aborting())
325#else
326 if (got_int)
327#endif
328 curbuf->b_flags |= BF_READERR;
329
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000330#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +0100331 // Need to update automatic folding. Do this before the autocommands,
332 // they may use the fold info.
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000333 foldUpdateAll(curwin);
334#endif
335
Bram Moolenaarc667da52019-11-30 20:52:27 +0100336 // need to set w_topline, unless some autocommand already did that.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337 if (!(curwin->w_valid & VALID_TOPLINE))
338 {
339 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100340#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100344#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100346#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348#endif
349
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100350 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100352 // The autocommands may have changed the current buffer. Apply the
353 // modelines to the correct buffer, if it still exists and is loaded.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200354 if (bufref_valid(&old_curbuf) && old_curbuf.br_buf->b_ml.ml_mfp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355 {
356 aco_save_T aco;
357
Bram Moolenaarc667da52019-11-30 20:52:27 +0100358 // Go to the buffer that was opened.
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200359 aucmd_prepbuf(&aco, old_curbuf.br_buf);
Bram Moolenaara3227e22006-03-08 21:32:40 +0000360 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
362
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100363#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100365 &retval);
366#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100368#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369
Bram Moolenaarc667da52019-11-30 20:52:27 +0100370 // restore curwin/curbuf and a few other things
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 aucmd_restbuf(&aco);
372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373 }
374
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 return retval;
376}
377
378/*
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200379 * Store "buf" in "bufref" and set the free count.
380 */
381 void
382set_bufref(bufref_T *bufref, buf_T *buf)
383{
384 bufref->br_buf = buf;
Bram Moolenaarfadacf02017-06-19 20:35:32 +0200385 bufref->br_fnum = buf == NULL ? 0 : buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200386 bufref->br_buf_free_count = buf_free_count;
387}
388
389/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200390 * Return TRUE if "bufref->br_buf" points to the same buffer as when
391 * set_bufref() was called and it is a valid buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200392 * Only goes through the buffer list if buf_free_count changed.
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200393 * Also checks if b_fnum is still the same, a :bwipe followed by :new might get
394 * the same allocated memory, but it's a different buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200395 */
396 int
397bufref_valid(bufref_T *bufref)
398{
399 return bufref->br_buf_free_count == buf_free_count
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200400 ? TRUE : buf_valid(bufref->br_buf)
401 && bufref->br_fnum == bufref->br_buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200402}
403
404/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200406 * This can be slow if there are many buffers, prefer using bufref_valid().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 */
408 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100409buf_valid(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410{
411 buf_T *bp;
412
Bram Moolenaarc667da52019-11-30 20:52:27 +0100413 // Assume that we more often have a recent buffer, start with the last
414 // one.
Bram Moolenaar00d253e2020-04-06 22:13:01 +0200415 FOR_ALL_BUFS_FROM_LAST(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 if (bp == buf)
417 return TRUE;
418 return FALSE;
419}
420
421/*
Bram Moolenaar480778b2016-07-14 22:09:39 +0200422 * A hash table used to quickly lookup a buffer by its number.
423 */
424static hashtab_T buf_hashtab;
425
426 static void
427buf_hashtab_add(buf_T *buf)
428{
429 sprintf((char *)buf->b_key, "%x", buf->b_fnum);
430 if (hash_add(&buf_hashtab, buf->b_key) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100431 emsg(_("E931: Buffer cannot be registered"));
Bram Moolenaar480778b2016-07-14 22:09:39 +0200432}
433
434 static void
435buf_hashtab_remove(buf_T *buf)
436{
437 hashitem_T *hi = hash_find(&buf_hashtab, buf->b_key);
438
439 if (!HASHITEM_EMPTY(hi))
440 hash_remove(&buf_hashtab, hi);
441}
442
Bram Moolenaar94f01952018-09-01 15:30:03 +0200443/*
444 * Return TRUE when buffer "buf" can be unloaded.
445 * Give an error message and return FALSE when the buffer is locked or the
446 * screen is being redrawn and the buffer is in a window.
447 */
448 static int
449can_unload_buffer(buf_T *buf)
450{
451 int can_unload = !buf->b_locked;
452
453 if (can_unload && updating_screen)
454 {
455 win_T *wp;
456
457 FOR_ALL_WINDOWS(wp)
458 if (wp->w_buffer == buf)
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200459 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200460 can_unload = FALSE;
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200461 break;
462 }
Bram Moolenaar94f01952018-09-01 15:30:03 +0200463 }
464 if (!can_unload)
Bram Moolenaardefa0672019-07-21 19:25:37 +0200465 semsg(_("E937: Attempt to delete a buffer that is in use: %s"),
466 buf->b_fname);
Bram Moolenaar94f01952018-09-01 15:30:03 +0200467 return can_unload;
468}
Bram Moolenaara997b452018-04-17 23:24:06 +0200469
Bram Moolenaar480778b2016-07-14 22:09:39 +0200470/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471 * Close the link to a buffer.
472 * "action" is used when there is no longer a window for the buffer.
473 * It can be:
474 * 0 buffer becomes hidden
475 * DOBUF_UNLOAD buffer is unloaded
476 * DOBUF_DELETE buffer is unloaded and removed from buffer list
477 * DOBUF_WIPE buffer is unloaded and really deleted
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200478 * DOBUF_WIPE_REUSE idem, and add to buf_reuse list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479 * When doing all but the first one on the current buffer, the caller should
480 * get a new buffer very soon!
481 *
482 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100483 *
484 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
485 * cause there to be only one window with this buffer. e.g. when ":quit" is
486 * supposed to close the window but autocommands close all other windows.
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100487 *
488 * When "ignore_abort" is TRUE don't abort even when aborting() returns TRUE.
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100489 *
490 * Return TRUE when we got to the end and b_nwindows was decremented.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 */
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100492 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100493close_buffer(
Bram Moolenaarc667da52019-11-30 20:52:27 +0100494 win_T *win, // if not NULL, set b_last_cursor
Bram Moolenaar7454a062016-01-30 15:14:10 +0100495 buf_T *buf,
496 int action,
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100497 int abort_if_last,
498 int ignore_abort)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100501 int nwindows;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200502 bufref_T bufref;
Bram Moolenaar3a117e12016-10-30 21:57:52 +0100503 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200504 win_T *the_curwin = curwin;
505 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 int unload_buf = (action != 0);
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200507 int wipe_buf = (action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
508 int del_buf = (action == DOBUF_DEL || wipe_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509
Bram Moolenaarcee52202020-03-11 14:19:58 +0100510 CHECK_CURBUF;
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100511
512 // Force unloading or deleting when 'bufhidden' says so.
513 // The caller must take care of NOT deleting/freeing when 'bufhidden' is
514 // "hide" (otherwise we could never free or delete a buffer).
Bram Moolenaarc667da52019-11-30 20:52:27 +0100515 if (buf->b_p_bh[0] == 'd') // 'bufhidden' == "delete"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200516 {
517 del_buf = TRUE;
518 unload_buf = TRUE;
519 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100520 else if (buf->b_p_bh[0] == 'w') // 'bufhidden' == "wipe"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200521 {
522 del_buf = TRUE;
523 unload_buf = TRUE;
524 wipe_buf = TRUE;
525 }
Bram Moolenaarc667da52019-11-30 20:52:27 +0100526 else if (buf->b_p_bh[0] == 'u') // 'bufhidden' == "unload"
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200527 unload_buf = TRUE;
528
Bram Moolenaar94053a52017-08-01 21:44:33 +0200529#ifdef FEAT_TERMINAL
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200530 if (bt_terminal(buf) && (buf->b_nwindows == 1 || del_buf))
Bram Moolenaar94053a52017-08-01 21:44:33 +0200531 {
Bram Moolenaarcee52202020-03-11 14:19:58 +0100532 CHECK_CURBUF;
Bram Moolenaar94053a52017-08-01 21:44:33 +0200533 if (term_job_running(buf->b_term))
534 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200535 if (wipe_buf || unload_buf)
Bram Moolenaara997b452018-04-17 23:24:06 +0200536 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200537 if (!can_unload_buffer(buf))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100538 return FALSE;
Bram Moolenaar94f01952018-09-01 15:30:03 +0200539
Bram Moolenaarc667da52019-11-30 20:52:27 +0100540 // Wiping out or unloading a terminal buffer kills the job.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200541 free_terminal(buf);
Bram Moolenaara997b452018-04-17 23:24:06 +0200542 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200543 else
544 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100545 // The job keeps running, hide the buffer.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200546 del_buf = FALSE;
547 unload_buf = FALSE;
548 }
549 }
Bram Moolenaarc9f8b842020-11-24 19:36:16 +0100550 else if (buf->b_p_bh[0] == 'h' && !del_buf)
551 {
552 // Hide a terminal buffer.
553 unload_buf = FALSE;
554 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200555 else
556 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100557 // A terminal buffer is wiped out if the job has finished.
Bram Moolenaar94053a52017-08-01 21:44:33 +0200558 del_buf = TRUE;
559 unload_buf = TRUE;
560 wipe_buf = TRUE;
561 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100562 CHECK_CURBUF;
Bram Moolenaar94053a52017-08-01 21:44:33 +0200563 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200564#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565
Bram Moolenaarc667da52019-11-30 20:52:27 +0100566 // Disallow deleting the buffer when it is locked (already being closed or
567 // halfway a command that relies on it). Unloading is allowed.
Bram Moolenaar94f01952018-09-01 15:30:03 +0200568 if ((del_buf || wipe_buf) && !can_unload_buffer(buf))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100569 return FALSE;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200570
Bram Moolenaarc667da52019-11-30 20:52:27 +0100571 // check no autocommands closed the window
Bram Moolenaar4033c552017-09-16 20:54:51 +0200572 if (win != NULL && win_valid_any_tab(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100574 // Set b_last_cursor when closing the last window for the buffer.
575 // Remember the last cursor position and window options of the buffer.
576 // This used to be only for the current window, but then options like
577 // 'foldmethod' may be lost with a ":only" command.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 if (buf->b_nwindows == 1)
579 set_last_cursor(win);
580 buflist_setfpos(buf, win,
581 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
582 win->w_cursor.col, TRUE);
583 }
584
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200585 set_bufref(&bufref, buf);
586
Bram Moolenaarc667da52019-11-30 20:52:27 +0100587 // When the buffer is no longer in a window, trigger BufWinLeave
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 if (buf->b_nwindows == 1)
589 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200590 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100591 ++buf->b_locked_split;
Bram Moolenaar82404332016-07-10 17:00:38 +0200592 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
593 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200594 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100595 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100596 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200597aucmd_abort:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100598 emsg(_(e_auabort));
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100599 return FALSE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100600 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200601 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100602 --buf->b_locked_split;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200603 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100604 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200605 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606
Bram Moolenaarc667da52019-11-30 20:52:27 +0100607 // When the buffer becomes hidden, but is not unloaded, trigger
608 // BufHidden
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 if (!unload_buf)
610 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200611 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100612 ++buf->b_locked_split;
Bram Moolenaar82404332016-07-10 17:00:38 +0200613 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
614 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200615 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100616 // Autocommands deleted the buffer.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200617 goto aucmd_abort;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200618 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100619 --buf->b_locked_split;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200620 if (abort_if_last && one_window())
Bram Moolenaarc667da52019-11-30 20:52:27 +0100621 // Autocommands made this the only window.
Bram Moolenaar362ce482012-06-06 19:02:45 +0200622 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100624#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100625 // autocmds may abort script processing
626 if (!ignore_abort && aborting())
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100627 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100628#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200630
Bram Moolenaarc667da52019-11-30 20:52:27 +0100631 // If the buffer was in curwin and the window has changed, go back to that
632 // window, if it still exists. This avoids that ":edit x" triggering a
633 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200634 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
635 {
636 block_autocmds();
637 goto_tabpage_win(the_curtab, the_curwin);
638 unblock_autocmds();
639 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200640
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 nwindows = buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642
Bram Moolenaarc667da52019-11-30 20:52:27 +0100643 // decrease the link count from windows (unless not in any window)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 if (buf->b_nwindows > 0)
645 --buf->b_nwindows;
646
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100647#ifdef FEAT_DIFF
648 if (diffopt_hiddenoff() && !unload_buf && buf->b_nwindows == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100649 diff_buf_delete(buf); // Clear 'diff' for hidden buffer.
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100650#endif
651
Bram Moolenaarc667da52019-11-30 20:52:27 +0100652 // Return when a window is displaying the buffer or when it's not
653 // unloaded.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100655 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656
Bram Moolenaarc667da52019-11-30 20:52:27 +0100657 // Always remove the buffer when there is no file name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 if (buf->b_ffname == NULL)
659 del_buf = TRUE;
660
Bram Moolenaarc667da52019-11-30 20:52:27 +0100661 // When closing the current buffer stop Visual mode before freeing
662 // anything.
Bram Moolenaar4930a762016-09-11 14:39:53 +0200663 if (buf == curbuf && VIsual_active
Bram Moolenaar9a27c7f2016-09-09 12:57:09 +0200664#if defined(EXITFREE)
665 && !entered_free_all_mem
666#endif
667 )
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200668 end_visual_mode();
669
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100670 // Free all things allocated for this buffer.
671 // Also calls the "BufDelete" autocommands when del_buf is TRUE.
672 //
Bram Moolenaarc667da52019-11-30 20:52:27 +0100673 // Remember if we are closing the current buffer. Restore the number of
674 // windows, so that autocommands in buf_freeall() don't get confused.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 is_curbuf = (buf == curbuf);
676 buf->b_nwindows = nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100678 buf_freeall(buf, (del_buf ? BFA_DEL : 0)
Bram Moolenaar8133cc62020-10-26 21:05:27 +0100679 + (wipe_buf ? BFA_WIPE : 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100680 + (ignore_abort ? BFA_IGNORE_ABORT : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681
Bram Moolenaarc667da52019-11-30 20:52:27 +0100682 // Autocommands may have deleted the buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200683 if (!bufref_valid(&bufref))
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100684 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100685#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100686 // autocmds may abort script processing
687 if (!ignore_abort && aborting())
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100688 return FALSE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100691 // It's possible that autocommands change curbuf to the one being deleted.
692 // This might cause the previous curbuf to be deleted unexpectedly. But
693 // in some cases it's OK to delete the curbuf, because a new one is
694 // obtained anyway. Therefore only return if curbuf changed to the
695 // deleted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 if (buf == curbuf && !is_curbuf)
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100697 return FALSE;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200698
Bram Moolenaar4033c552017-09-16 20:54:51 +0200699 if (win_valid_any_tab(win) && win->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +0100700 win->w_buffer = NULL; // make sure we don't use the buffer now
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200701
Bram Moolenaarc667da52019-11-30 20:52:27 +0100702 // Autocommands may have opened or closed windows for this buffer.
703 // Decrement the count for the close we do here.
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200704 if (buf->b_nwindows > 0)
705 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 /*
708 * Remove the buffer from the list.
709 */
710 if (wipe_buf)
711 {
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200712 if (action == DOBUF_WIPE_REUSE)
713 {
714 // we can re-use this buffer number, store it
715 if (buf_reuse.ga_itemsize == 0)
716 ga_init2(&buf_reuse, sizeof(int), 50);
717 if (ga_grow(&buf_reuse, 1) == OK)
718 ((int *)buf_reuse.ga_data)[buf_reuse.ga_len++] = buf->b_fnum;
719 }
Bram Moolenaar3d6014f2018-10-11 19:27:47 +0200720 if (buf->b_sfname != buf->b_ffname)
721 VIM_CLEAR(buf->b_sfname);
722 else
723 buf->b_sfname = NULL;
724 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 if (buf->b_prev == NULL)
726 firstbuf = buf->b_next;
727 else
728 buf->b_prev->b_next = buf->b_next;
729 if (buf->b_next == NULL)
730 lastbuf = buf->b_prev;
731 else
732 buf->b_next->b_prev = buf->b_prev;
733 free_buffer(buf);
734 }
735 else
736 {
737 if (del_buf)
738 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100739 // Free all internal variables and reset option values, to make
740 // ":bdel" compatible with Vim 5.7.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 free_buffer_stuff(buf, TRUE);
742
Bram Moolenaarc667da52019-11-30 20:52:27 +0100743 // Make it look like a new buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
745
Bram Moolenaarc667da52019-11-30 20:52:27 +0100746 // Init the options when loaded again.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 buf->b_p_initialized = FALSE;
748 }
749 buf_clear_file(buf);
750 if (del_buf)
751 buf->b_p_bl = FALSE;
752 }
Bram Moolenaarcee52202020-03-11 14:19:58 +0100753 // NOTE: at this point "curbuf" may be invalid!
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100754 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755}
756
757/*
758 * Make buffer not contain a file.
759 */
760 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100761buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762{
763 buf->b_ml.ml_line_count = 1;
Bram Moolenaarc024b462019-06-08 18:07:21 +0200764 unchanged(buf, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 buf->b_p_eol = TRUE;
767 buf->b_start_eol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000769 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 buf->b_ml.ml_mfp = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +0100771 buf->b_ml.ml_flags = ML_EMPTY; // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772#ifdef FEAT_NETBEANS_INTG
773 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774#endif
775}
776
777/*
778 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200779 * the file. Careful: get here with "curwin" NULL when exiting.
780 * flags:
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100781 * BFA_DEL buffer is going to be deleted
782 * BFA_WIPE buffer is going to be wiped out
783 * BFA_KEEP_UNDO do not free undo information
784 * BFA_IGNORE_ABORT don't abort even when aborting() returns TRUE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100787buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200790 bufref_T bufref;
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200791 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaar5a497892016-09-03 16:29:04 +0200792 win_T *the_curwin = curwin;
793 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794
Bram Moolenaarc667da52019-11-30 20:52:27 +0100795 // Make sure the buffer isn't closed by autocommands.
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200796 ++buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100797 ++buf->b_locked_split;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200798 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200799 if (buf->b_ml.ml_mfp != NULL)
800 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200801 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
802 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200803 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100804 // autocommands deleted the buffer
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200805 return;
806 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200807 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200809 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
810 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200811 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100812 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 return;
814 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200815 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200817 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
818 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200819 && !bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +0100820 // autocommands deleted the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 return;
822 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200823 --buf->b_locked;
Bram Moolenaar983d83f2021-02-07 12:12:43 +0100824 --buf->b_locked_split;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200825
Bram Moolenaarc667da52019-11-30 20:52:27 +0100826 // If the buffer was in curwin and the window has changed, go back to that
827 // window, if it still exists. This avoids that ":edit x" triggering a
828 // "tabnext" BufUnload autocmd leaves a window behind without a buffer.
Bram Moolenaar5a497892016-09-03 16:29:04 +0200829 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
830 {
831 block_autocmds();
832 goto_tabpage_win(the_curtab, the_curwin);
833 unblock_autocmds();
834 }
Bram Moolenaar5a497892016-09-03 16:29:04 +0200835
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100836#ifdef FEAT_EVAL
Bram Moolenaara6e8f882019-12-14 16:18:15 +0100837 // autocmds may abort script processing
838 if ((flags & BFA_IGNORE_ABORT) == 0 && aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841
Bram Moolenaarc5935a82021-10-19 20:48:52 +0100842 // It's possible that autocommands change curbuf to the one being deleted.
843 // This might cause curbuf to be deleted unexpectedly. But in some cases
844 // it's OK to delete the curbuf, because a new one is obtained anyway.
845 // Therefore only return if curbuf changed to the deleted buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 if (buf == curbuf && !is_curbuf)
847 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848#ifdef FEAT_DIFF
Bram Moolenaarc667da52019-11-30 20:52:27 +0100849 diff_buf_delete(buf); // Can't use 'diff' for unloaded buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200851#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100852 // Remove any ownsyntax, unless exiting.
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200853 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaar89c71222011-11-30 15:40:56 +0100854 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200855#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000856
857#ifdef FEAT_FOLDING
Bram Moolenaarc667da52019-11-30 20:52:27 +0100858 // No folds in an empty buffer.
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000859 {
860 win_T *win;
861 tabpage_T *tp;
862
863 FOR_ALL_TAB_WINDOWS(tp, win)
864 if (win->w_buffer == buf)
865 clearFolding(win);
866 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000867#endif
868
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869#ifdef FEAT_TCL
870 tcl_buffer_free(buf);
871#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100872 ml_close(buf, TRUE); // close and delete the memline/memfile
873 buf->b_ml.ml_line_count = 0; // no lines in buffer
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200874 if ((flags & BFA_KEEP_UNDO) == 0)
875 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100876 u_blockfree(buf); // free the memory allocated for undo
877 u_clearall(buf); // reset all undo information
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879#ifdef FEAT_SYN_HL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100880 syntax_clear(&buf->b_s); // reset syntax info
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100882#ifdef FEAT_PROP_POPUP
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100883 clear_buf_prop_types(buf);
884#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +0100885 buf->b_flags &= ~BF_READERR; // a read error is no longer relevant
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886}
887
888/*
889 * Free a buffer structure and the things it contains related to the buffer
890 * itself (not the file, that must have been done already).
891 */
892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100893free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200895 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200897#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +0100898 // b:changedtick uses an item in buf_T, remove it now
Bram Moolenaarb2c87502017-10-14 21:15:58 +0200899 dictitem_remove(buf->b_vars, (dictitem_T *)&buf->b_ct_di);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200900 unref_var_dict(buf->b_vars);
Bram Moolenaar86173482019-10-01 17:02:16 +0200901 remove_listeners(buf);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200902#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200903#ifdef FEAT_LUA
904 lua_buffer_free(buf);
905#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000906#ifdef FEAT_MZSCHEME
907 mzscheme_buffer_free(buf);
908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909#ifdef FEAT_PERL
910 perl_buf_free(buf);
911#endif
912#ifdef FEAT_PYTHON
913 python_buffer_free(buf);
914#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200915#ifdef FEAT_PYTHON3
916 python3_buffer_free(buf);
917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918#ifdef FEAT_RUBY
919 ruby_buffer_free(buf);
920#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200921#ifdef FEAT_JOB_CHANNEL
922 channel_buffer_free(buf);
923#endif
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200924#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +0200925 free_terminal(buf);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200926#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +0200927#ifdef FEAT_JOB_CHANNEL
928 vim_free(buf->b_prompt_text);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200929 free_callback(&buf->b_prompt_callback);
Bram Moolenaar86173482019-10-01 17:02:16 +0200930 free_callback(&buf->b_prompt_interrupt);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200931#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200932
933 buf_hashtab_remove(buf);
934
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200935 aubuflocal_remove(buf);
936
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200937 if (autocmd_busy)
938 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100939 // Do not free the buffer structure while autocommands are executing,
940 // it's still needed. Free it when autocmd_busy is reset.
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200941 buf->b_next = au_pending_free_buf;
942 au_pending_free_buf = buf;
943 }
944 else
Bram Moolenaarcee52202020-03-11 14:19:58 +0100945 {
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200946 vim_free(buf);
Bram Moolenaarcee52202020-03-11 14:19:58 +0100947 if (curbuf == buf)
948 curbuf = NULL; // make clear it's not to be used
949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950}
951
952/*
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100953 * Initializes b:changedtick.
Bram Moolenaar79518e22017-02-17 16:31:35 +0100954 */
955 static void
956init_changedtick(buf_T *buf)
957{
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100958 dictitem_T *di = (dictitem_T *)&buf->b_ct_di;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100959
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100960 di->di_flags = DI_FLAGS_FIX | DI_FLAGS_RO;
961 di->di_tv.v_type = VAR_NUMBER;
962 di->di_tv.v_lock = VAR_FIXED;
963 di->di_tv.vval.v_number = 0;
964
Bram Moolenaar92769c32017-02-25 15:41:37 +0100965#ifdef FEAT_EVAL
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100966 STRCPY(buf->b_ct_di.di_key, "changedtick");
967 (void)dict_add(buf->b_vars, di);
Bram Moolenaar92769c32017-02-25 15:41:37 +0100968#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +0100969}
970
971/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
973 */
974 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100975free_buffer_stuff(
976 buf_T *buf,
Bram Moolenaarc667da52019-11-30 20:52:27 +0100977 int free_options) // free options as well
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978{
979 if (free_options)
980 {
Bram Moolenaarc667da52019-11-30 20:52:27 +0100981 clear_wininfo(buf); // including window-local options
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200983#ifdef FEAT_SPELL
984 ga_clear(&buf->b_s.b_langp);
985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 }
987#ifdef FEAT_EVAL
Bram Moolenaar79518e22017-02-17 16:31:35 +0100988 {
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100989 varnumber_T tick = CHANGEDTICK(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +0100990
Bram Moolenaarc667da52019-11-30 20:52:27 +0100991 vars_clear(&buf->b_vars->dv_hashtab); // free all buffer variables
Bram Moolenaar79518e22017-02-17 16:31:35 +0100992 hash_init(&buf->b_vars->dv_hashtab);
993 init_changedtick(buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100994 CHANGEDTICK(buf) = tick;
Bram Moolenaarf10997a2020-01-03 21:00:02 +0100995 remove_listeners(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +0100996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997#endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200998 uc_clear(&buf->b_ucmds); // clear local user commands
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999#ifdef FEAT_SIGNS
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001000 buf_delete_signs(buf, (char_u *)"*"); // delete any signs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00001002#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001003 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +00001004#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01001005 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); // clear local mappings
1006 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); // clear local abbrevs
Bram Moolenaard23a8232018-02-10 18:45:26 +01001007 VIM_CLEAR(buf->b_start_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008}
1009
1010/*
Bram Moolenaar4882d982020-10-25 17:55:09 +01001011 * Free one wininfo_T.
1012 */
1013 void
1014free_wininfo(wininfo_T *wip)
1015{
1016 if (wip->wi_optset)
1017 {
1018 clear_winopt(&wip->wi_opt);
1019#ifdef FEAT_FOLDING
1020 deleteFoldRecurse(&wip->wi_folds);
1021#endif
1022 }
1023 vim_free(wip);
1024}
1025
1026/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 * Free the b_wininfo list for buffer "buf".
1028 */
1029 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001030clear_wininfo(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031{
1032 wininfo_T *wip;
1033
1034 while (buf->b_wininfo != NULL)
1035 {
1036 wip = buf->b_wininfo;
1037 buf->b_wininfo = wip->wi_next;
Bram Moolenaar4882d982020-10-25 17:55:09 +01001038 free_wininfo(wip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 }
1040}
1041
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042/*
1043 * Go to another buffer. Handles the result of the ATTENTION dialog.
1044 */
1045 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001046goto_buffer(
1047 exarg_T *eap,
1048 int start,
1049 int dir,
1050 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001052 bufref_T old_curbuf;
1053
1054 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055
1056 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
1058 start, dir, count, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
1060 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001061#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001062 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001063
Bram Moolenaarc667da52019-11-30 20:52:27 +01001064 // Reset the error/interrupt/exception state here so that
1065 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001066 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001067#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001068
Bram Moolenaarc667da52019-11-30 20:52:27 +01001069 // Quitting means closing the split window, nothing else.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 win_close(curwin, TRUE);
1071 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001072 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001073
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001074#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001075 // Restore the error/interrupt/exception state if not discarded by a
1076 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001077 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001078#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 }
1080 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001081 handle_swap_exists(&old_curbuf);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001082}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084/*
1085 * Handle the situation of swap_exists_action being set.
1086 * It is allowed for "old_curbuf" to be NULL or invalid.
1087 */
1088 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001089handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090{
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001091#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001092 cleanup_T cs;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001093#endif
1094#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001095 long old_tw = curbuf->b_p_tw;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001096#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001097 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001098
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 if (swap_exists_action == SEA_QUIT)
1100 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001101#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001102 // Reset the error/interrupt/exception state here so that
1103 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001104 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001105#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001106
Bram Moolenaarc667da52019-11-30 20:52:27 +01001107 // User selected Quit at ATTENTION prompt. Go back to previous
1108 // buffer. If that buffer is gone or the same as the current one,
1109 // open a new, empty buffer.
1110 swap_exists_action = SEA_NONE; // don't want it again
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001111 swap_exists_did_quit = TRUE;
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001112 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001113 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1114 || old_curbuf->br_buf == curbuf)
Bram Moolenaar1d97efc2021-07-04 13:27:11 +02001115 {
1116 // Block autocommands here because curwin->w_buffer is NULL.
1117 block_autocmds();
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001118 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar1d97efc2021-07-04 13:27:11 +02001119 unblock_autocmds();
1120 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001121 else
1122 buf = old_curbuf->br_buf;
1123 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001124 {
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001125 int old_msg_silent = msg_silent;
1126
1127 if (shortmess(SHM_FILEINFO))
1128 msg_silent = 1; // prevent fileinfo message
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001129 enter_buffer(buf);
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001130 // restore msg_silent, so that the command line will be shown
1131 msg_silent = old_msg_silent;
1132
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001133#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001134 if (old_tw != curbuf->b_p_tw)
1135 check_colorcolumn(curwin);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001136#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001137 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001138 // If "old_curbuf" is NULL we are in big trouble here...
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001139
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001140#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001141 // Restore the error/interrupt/exception state if not discarded by a
1142 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001143 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001144#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 }
1146 else if (swap_exists_action == SEA_RECOVER)
1147 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001148#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001149 // Reset the error/interrupt/exception state here so that
1150 // aborting() returns FALSE when closing a buffer.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001151 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001152#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001153
Bram Moolenaarc667da52019-11-30 20:52:27 +01001154 // User selected Recover at ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 msg_scroll = TRUE;
Bram Moolenaar99499b12019-05-23 21:35:48 +02001156 ml_recover(FALSE);
Bram Moolenaarc667da52019-11-30 20:52:27 +01001157 msg_puts("\n"); // don't overwrite the last message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001159 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001160
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001161#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001162 // Restore the error/interrupt/exception state if not discarded by a
1163 // new aborting error, interrupt, or uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001164 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 }
1167 swap_exists_action = SEA_NONE;
1168}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170/*
Bram Moolenaara02471e2014-01-10 16:43:14 +01001171 * Make the current buffer empty.
1172 * Used when it is wiped out and it's the last buffer.
1173 */
1174 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001175empty_curbuf(
1176 int close_others,
1177 int forceit,
1178 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001179{
1180 int retval;
1181 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001182 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001183
1184 if (action == DOBUF_UNLOAD)
1185 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001186 emsg(_("E90: Cannot unload last buffer"));
Bram Moolenaara02471e2014-01-10 16:43:14 +01001187 return FAIL;
1188 }
1189
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001190 set_bufref(&bufref, buf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001191 if (close_others)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001192 // Close any other windows on this buffer, then make it empty.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001193 close_windows(buf, TRUE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001194
1195 setpcmark();
1196 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1197 forceit ? ECMD_FORCEIT : 0, curwin);
1198
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001199 // do_ecmd() may create a new buffer, then we have to delete
1200 // the old one. But do_ecmd() may have done that already, check
1201 // if the buffer still exists.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001202 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001203 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001204 if (!close_others)
1205 need_fileinfo = FALSE;
1206 return retval;
1207}
Bram Moolenaar94f01952018-09-01 15:30:03 +02001208
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209/*
1210 * Implementation of the commands for the buffer list.
1211 *
1212 * action == DOBUF_GOTO go to specified buffer
1213 * action == DOBUF_SPLIT split window and go to specified buffer
1214 * action == DOBUF_UNLOAD unload specified buffer(s)
1215 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1216 * action == DOBUF_WIPE delete specified buffer(s) really
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001217 * action == DOBUF_WIPE_REUSE idem, and add number to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 *
1219 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1220 * start == DOBUF_FIRST go to "count" buffer from first buffer
1221 * start == DOBUF_LAST go to "count" buffer from last buffer
1222 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1223 *
1224 * Return FAIL or OK.
1225 */
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001226 static int
1227do_buffer_ext(
Bram Moolenaar7454a062016-01-30 15:14:10 +01001228 int action,
1229 int start,
Bram Moolenaarc667da52019-11-30 20:52:27 +01001230 int dir, // FORWARD or BACKWARD
1231 int count, // buffer number or number of buffers
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001232 int flags) // DOBUF_FORCEIT etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233{
1234 buf_T *buf;
1235 buf_T *bp;
1236 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001237 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238
1239 switch (start)
1240 {
1241 case DOBUF_FIRST: buf = firstbuf; break;
1242 case DOBUF_LAST: buf = lastbuf; break;
1243 default: buf = curbuf; break;
1244 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001245 if (start == DOBUF_MOD) // find next modified buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 {
1247 while (count-- > 0)
1248 {
1249 do
1250 {
1251 buf = buf->b_next;
1252 if (buf == NULL)
1253 buf = firstbuf;
1254 }
1255 while (buf != curbuf && !bufIsChanged(buf));
1256 }
1257 if (!bufIsChanged(buf))
1258 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001259 emsg(_("E84: No modified buffer found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 return FAIL;
1261 }
1262 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001263 else if (start == DOBUF_FIRST && count) // find specified buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 {
1265 while (buf != NULL && buf->b_fnum != count)
1266 buf = buf->b_next;
1267 }
1268 else
1269 {
1270 bp = NULL;
1271 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1272 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001273 // remember the buffer where we start, we come back there when all
1274 // buffers are unlisted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 if (bp == NULL)
1276 bp = buf;
1277 if (dir == FORWARD)
1278 {
1279 buf = buf->b_next;
1280 if (buf == NULL)
1281 buf = firstbuf;
1282 }
1283 else
1284 {
1285 buf = buf->b_prev;
1286 if (buf == NULL)
1287 buf = lastbuf;
1288 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001289 // don't count unlisted buffers
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 if (unload || buf->b_p_bl)
1291 {
1292 --count;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001293 bp = NULL; // use this buffer as new starting point
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 }
1295 if (bp == buf)
1296 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001297 // back where we started, didn't find anything.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001298 emsg(_("E85: There is no listed buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 return FAIL;
1300 }
1301 }
1302 }
1303
Bram Moolenaarc667da52019-11-30 20:52:27 +01001304 if (buf == NULL) // could not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 {
1306 if (start == DOBUF_FIRST)
1307 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001308 // don't warn when deleting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 if (!unload)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001310 semsg(_(e_nobufnr), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 }
1312 else if (dir == FORWARD)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001313 emsg(_("E87: Cannot go beyond last buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001315 emsg(_("E88: Cannot go before first buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 return FAIL;
1317 }
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001318#ifdef FEAT_PROP_POPUP
1319 if ((flags & DOBUF_NOPOPUP) && bt_popup(buf)
1320# ifdef FEAT_TERMINAL
1321 && !bt_terminal(buf)
1322#endif
1323 )
1324 return OK;
1325#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326
1327#ifdef FEAT_GUI
1328 need_mouse_correct = TRUE;
1329#endif
1330
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001332 * delete buffer "buf" from memory and/or the list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 */
1334 if (unload)
1335 {
1336 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001337 bufref_T bufref;
1338
Bram Moolenaar94f01952018-09-01 15:30:03 +02001339 if (!can_unload_buffer(buf))
Bram Moolenaara997b452018-04-17 23:24:06 +02001340 return FAIL;
Bram Moolenaara997b452018-04-17 23:24:06 +02001341
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001342 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343
Bram Moolenaarc667da52019-11-30 20:52:27 +01001344 // When unloading or deleting a buffer that's already unloaded and
1345 // unlisted: fail silently.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001346 if (action != DOBUF_WIPE && action != DOBUF_WIPE_REUSE
1347 && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 return FAIL;
1349
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001350 if ((flags & DOBUF_FORCEIT) == 0 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001352#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001353 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 {
1355 dialog_changed(buf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001356 if (!bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001357 // Autocommand deleted buffer, oops! It's not changed
1358 // now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 return FAIL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001360 // If it's still changed fail silently, the dialog already
1361 // mentioned why it fails.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001362 if (bufIsChanged(buf))
1363 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001365 else
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001368 semsg(_("E89: No write since last change for buffer %d (add ! to override)"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 buf->b_fnum);
1370 return FAIL;
1371 }
1372 }
1373
Bram Moolenaarc667da52019-11-30 20:52:27 +01001374 // When closing the current buffer stop Visual mode.
Bram Moolenaar4930a762016-09-11 14:39:53 +02001375 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001376 end_visual_mode();
1377
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001378 // If deleting the last (listed) buffer, make it empty.
1379 // The last (listed) buffer cannot be unloaded.
Bram Moolenaar29323592016-07-24 22:04:11 +02001380 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 if (bp->b_p_bl && bp != buf)
1382 break;
1383 if (bp == NULL && buf == curbuf)
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001384 return empty_curbuf(TRUE, (flags & DOBUF_FORCEIT), action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001386 // If the deleted buffer is the current one, close the current window
1387 // (unless it's the only window). Repeat this so long as we end up in
1388 // a window with this buffer.
Bram Moolenaarf740b292006-02-16 22:11:02 +00001389 while (buf == curbuf
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001390 && !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
Bram Moolenaar459ca562016-11-10 18:16:33 +01001391 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001392 {
1393 if (win_close(curwin, FALSE) == FAIL)
1394 break;
1395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001397 // If the buffer to be deleted is not the current one, delete it here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 if (buf != curbuf)
1399 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001400 close_windows(buf, FALSE);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001401 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows <= 0)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001402 close_buffer(NULL, buf, action, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 return OK;
1404 }
1405
1406 /*
1407 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001408 * There should be another, otherwise it would have been handled
1409 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001410 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 * Then prefer the buffer we most recently visited.
1412 * Else try to find one that is loaded, after the current buffer,
1413 * then before the current buffer.
1414 * Finally use any buffer.
1415 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001416 buf = NULL; // selected buffer
1417 bp = NULL; // used when no loaded buffer found
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001418 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1419 buf = au_new_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420#ifdef FEAT_JUMPLIST
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001421 else if (curwin->w_jumplistlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 {
1423 int jumpidx;
1424
1425 jumpidx = curwin->w_jumplistidx - 1;
1426 if (jumpidx < 0)
1427 jumpidx = curwin->w_jumplistlen - 1;
1428
1429 forward = jumpidx;
1430 while (jumpidx != curwin->w_jumplistidx)
1431 {
1432 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1433 if (buf != NULL)
1434 {
1435 if (buf == curbuf || !buf->b_p_bl)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001436 buf = NULL; // skip current and unlisted bufs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 else if (buf->b_ml.ml_mfp == NULL)
1438 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001439 // skip unloaded buf, but may keep it for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 if (bp == NULL)
1441 bp = buf;
1442 buf = NULL;
1443 }
1444 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001445 if (buf != NULL) // found a valid buffer: stop searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001447 // advance to older entry in jump list
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1449 break;
1450 if (--jumpidx < 0)
1451 jumpidx = curwin->w_jumplistlen - 1;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001452 if (jumpidx == forward) // List exhausted for sure
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 break;
1454 }
1455 }
1456#endif
1457
Bram Moolenaarc667da52019-11-30 20:52:27 +01001458 if (buf == NULL) // No previous buffer, Try 2'nd approach
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 {
1460 forward = TRUE;
1461 buf = curbuf->b_next;
1462 for (;;)
1463 {
1464 if (buf == NULL)
1465 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001466 if (!forward) // tried both directions
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 break;
1468 buf = curbuf->b_prev;
1469 forward = FALSE;
1470 continue;
1471 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001472 // in non-help buffer, try to skip help buffers, and vv
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1474 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001475 if (buf->b_ml.ml_mfp != NULL) // found loaded buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001477 if (bp == NULL) // remember unloaded buf for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 bp = buf;
1479 }
1480 if (forward)
1481 buf = buf->b_next;
1482 else
1483 buf = buf->b_prev;
1484 }
1485 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001486 if (buf == NULL) // No loaded buffer, use unloaded one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 buf = bp;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001488 if (buf == NULL) // No loaded buffer, find listed one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001490 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 if (buf->b_p_bl && buf != curbuf)
1492 break;
1493 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001494 if (buf == NULL) // Still no buffer, just take one
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 {
1496 if (curbuf->b_next != NULL)
1497 buf = curbuf->b_next;
1498 else
1499 buf = curbuf->b_prev;
1500 }
1501 }
1502
Bram Moolenaara02471e2014-01-10 16:43:14 +01001503 if (buf == NULL)
1504 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001505 // Autocommands must have wiped out all other buffers. Only option
1506 // now is to make the current buffer empty.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001507 return empty_curbuf(FALSE, (flags & DOBUF_FORCEIT), action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001508 }
1509
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001511 * make "buf" the current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01001513 if (action == DOBUF_SPLIT) // split window first
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001515 // If 'switchbuf' contains "useopen": jump to first window containing
1516 // "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00001517 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001518 return OK;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001519 // If 'switchbuf' contains "usetab": jump to first window in any tab
1520 // page containing "buf" if one exists
Bram Moolenaarf2330482008-06-24 20:19:36 +00001521 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 return OK;
1523 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 return FAIL;
1525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526
Bram Moolenaarc667da52019-11-30 20:52:27 +01001527 // go to current buffer - nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 if (buf == curbuf)
1529 return OK;
1530
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001531 // Check if the current buffer may be abandoned.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001532 if (action == DOBUF_GOTO && !can_abandon(curbuf, (flags & DOBUF_FORCEIT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 {
1534#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02001535 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001537 bufref_T bufref;
1538
1539 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 dialog_changed(curbuf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001541 if (!bufref_valid(&bufref))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001542 // Autocommand deleted buffer, oops!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 }
1545 if (bufIsChanged(curbuf))
1546#endif
1547 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001548 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 return FAIL;
1550 }
1551 }
1552
Bram Moolenaarc667da52019-11-30 20:52:27 +01001553 // Go to the other buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 set_curbuf(buf, action);
1555
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 if (action == DOBUF_SPLIT)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001557 RESET_BINDING(curwin); // reset 'scrollbind' and 'cursorbind'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001559#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001560 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 return FAIL;
1562#endif
1563
1564 return OK;
1565}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001567 int
1568do_buffer(
1569 int action,
1570 int start,
1571 int dir, // FORWARD or BACKWARD
1572 int count, // buffer number or number of buffers
1573 int forceit) // TRUE when using !
1574{
1575 return do_buffer_ext(action, start, dir, count,
1576 forceit ? DOBUF_FORCEIT : 0);
1577}
1578
1579/*
1580 * do_bufdel() - delete or unload buffer(s)
1581 *
1582 * addr_count == 0: ":bdel" - delete current buffer
1583 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1584 * buffer "end_bnr", then any other arguments.
1585 * addr_count == 2: ":N,N bdel" - delete buffers in range
1586 *
1587 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1588 * DOBUF_DEL (":bdel")
1589 *
1590 * Returns error message or NULL
1591 */
1592 char *
1593do_bufdel(
1594 int command,
1595 char_u *arg, // pointer to extra arguments
1596 int addr_count,
1597 int start_bnr, // first buffer number in a range
1598 int end_bnr, // buffer nr or last buffer nr in a range
1599 int forceit)
1600{
1601 int do_current = 0; // delete current buffer?
1602 int deleted = 0; // number of buffers deleted
1603 char *errormsg = NULL; // return value
1604 int bnr; // buffer number
1605 char_u *p;
1606
1607 if (addr_count == 0)
1608 {
1609 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1610 }
1611 else
1612 {
1613 if (addr_count == 2)
1614 {
1615 if (*arg) // both range and argument is not allowed
1616 return ex_errmsg(e_trailing_arg, arg);
1617 bnr = start_bnr;
1618 }
1619 else // addr_count == 1
1620 bnr = end_bnr;
1621
1622 for ( ;!got_int; ui_breakcheck())
1623 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001624 // Delete the current buffer last, otherwise when the
1625 // current buffer is deleted, the next buffer becomes
1626 // the current one and will be loaded, which may then
1627 // also be deleted, etc.
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001628 if (bnr == curbuf->b_fnum)
1629 do_current = bnr;
1630 else if (do_buffer_ext(command, DOBUF_FIRST, FORWARD, (int)bnr,
1631 DOBUF_NOPOPUP | (forceit ? DOBUF_FORCEIT : 0)) == OK)
1632 ++deleted;
1633
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001634 // find next buffer number to delete/unload
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02001635 if (addr_count == 2)
1636 {
1637 if (++bnr > end_bnr)
1638 break;
1639 }
1640 else // addr_count == 1
1641 {
1642 arg = skipwhite(arg);
1643 if (*arg == NUL)
1644 break;
1645 if (!VIM_ISDIGIT(*arg))
1646 {
1647 p = skiptowhite_esc(arg);
1648 bnr = buflist_findpat(arg, p,
1649 command == DOBUF_WIPE || command == DOBUF_WIPE_REUSE,
1650 FALSE, FALSE);
1651 if (bnr < 0) // failed
1652 break;
1653 arg = p;
1654 }
1655 else
1656 bnr = getdigits(&arg);
1657 }
1658 }
1659 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1660 FORWARD, do_current, forceit) == OK)
1661 ++deleted;
1662
1663 if (deleted == 0)
1664 {
1665 if (command == DOBUF_UNLOAD)
1666 STRCPY(IObuff, _("E515: No buffers were unloaded"));
1667 else if (command == DOBUF_DEL)
1668 STRCPY(IObuff, _("E516: No buffers were deleted"));
1669 else
1670 STRCPY(IObuff, _("E517: No buffers were wiped out"));
1671 errormsg = (char *)IObuff;
1672 }
1673 else if (deleted >= p_report)
1674 {
1675 if (command == DOBUF_UNLOAD)
1676 smsg(NGETTEXT("%d buffer unloaded",
1677 "%d buffers unloaded", deleted), deleted);
1678 else if (command == DOBUF_DEL)
1679 smsg(NGETTEXT("%d buffer deleted",
1680 "%d buffers deleted", deleted), deleted);
1681 else
1682 smsg(NGETTEXT("%d buffer wiped out",
1683 "%d buffers wiped out", deleted), deleted);
1684 }
1685 }
1686
1687
1688 return errormsg;
1689}
1690
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691/*
1692 * Set current buffer to "buf". Executes autocommands and closes current
1693 * buffer. "action" tells how to close the current buffer:
1694 * DOBUF_GOTO free or hide it
1695 * DOBUF_SPLIT nothing
1696 * DOBUF_UNLOAD unload it
1697 * DOBUF_DEL delete it
1698 * DOBUF_WIPE wipe it out
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001699 * DOBUF_WIPE_REUSE wipe it out and add to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 */
1701 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001702set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703{
1704 buf_T *prevbuf;
1705 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001706 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001707#ifdef FEAT_SYN_HL
1708 long old_tw = curbuf->b_p_tw;
1709#endif
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001710 bufref_T newbufref;
1711 bufref_T prevbufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712
1713 setpcmark();
Bram Moolenaare1004402020-10-24 20:49:43 +02001714 if ((cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001715 curwin->w_alt_fnum = curbuf->b_fnum; // remember alternate file
1716 buflist_altfpos(curwin); // remember curpos
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717
Bram Moolenaarc667da52019-11-30 20:52:27 +01001718 // Don't restart Select mode after switching to another buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720
Bram Moolenaarc667da52019-11-30 20:52:27 +01001721 // close_windows() or apply_autocmds() may change curbuf and wipe out "buf"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 prevbuf = curbuf;
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001723 set_bufref(&prevbufref, prevbuf);
1724 set_bufref(&newbufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725
Bram Moolenaar983d83f2021-02-07 12:12:43 +01001726 // Autocommands may delete the current buffer and/or the buffer we want to
1727 // go to. In those cases don't close the buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001728 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001729 || (bufref_valid(&prevbufref)
1730 && bufref_valid(&newbufref)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001731#ifdef FEAT_EVAL
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001732 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001734 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001736#ifdef FEAT_SYN_HL
1737 if (prevbuf == curwin->w_buffer)
1738 reset_synblock(curwin);
1739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001741 close_windows(prevbuf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001742#if defined(FEAT_EVAL)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001743 if (bufref_valid(&prevbufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744#else
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001745 if (bufref_valid(&prevbufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001747 {
Bram Moolenaare25865a2012-07-06 16:22:02 +02001748 win_T *previouswin = curwin;
Bram Moolenaar4b96df52020-01-26 22:00:26 +01001749
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001750 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001751 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1753 unload ? action : (action == DOBUF_GOTO
Bram Moolenaareb44a682017-08-03 22:44:55 +02001754 && !buf_hide(prevbuf)
Bram Moolenaara6e8f882019-12-14 16:18:15 +01001755 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0,
1756 FALSE, FALSE);
Bram Moolenaare25865a2012-07-06 16:22:02 +02001757 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaarc667da52019-11-30 20:52:27 +01001758 // autocommands changed curwin, Grr!
Bram Moolenaare25865a2012-07-06 16:22:02 +02001759 curwin = previouswin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01001762 // An autocommand may have deleted "buf", already entered it (e.g., when
1763 // it did ":bunload") or aborted the script processing.
1764 // If curwin->w_buffer is null, enter_buffer() will make it valid again
Bram Moolenaar9e931222012-06-20 11:55:01 +02001765 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001766#ifdef FEAT_EVAL
Bram Moolenaar4033c552017-09-16 20:54:51 +02001767 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001769 ) || curwin->w_buffer == NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001770 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001772#ifdef FEAT_SYN_HL
1773 if (old_tw != curbuf->b_p_tw)
1774 check_colorcolumn(curwin);
1775#endif
1776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777}
1778
1779/*
1780 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001781 * Old curbuf must have been abandoned already! This also means "curbuf" may
1782 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001784 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001785enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786{
Bram Moolenaar010ee962019-09-25 20:37:36 +02001787 // Get the buffer in the current window.
1788 curwin->w_buffer = buf;
1789 curbuf = buf;
1790 ++curbuf->b_nwindows;
1791
1792 // Copy buffer and window local option values. Not for a help buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1794 if (!buf->b_help)
1795 get_winopts(buf);
1796#ifdef FEAT_FOLDING
1797 else
Bram Moolenaar010ee962019-09-25 20:37:36 +02001798 // Remove all folds in the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 clearFolding(curwin);
Bram Moolenaar010ee962019-09-25 20:37:36 +02001800 foldUpdateAll(curwin); // update folds (later).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801#endif
1802
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001804 if (curwin->w_p_diff)
1805 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806#endif
1807
Bram Moolenaara971b822011-09-14 14:43:25 +02001808#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001809 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001810#endif
1811
Bram Moolenaarc667da52019-11-30 20:52:27 +01001812 // Cursor on first line by default.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 curwin->w_cursor.lnum = 1;
1814 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001817 curwin->w_topline_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818
Bram Moolenaarc667da52019-11-30 20:52:27 +01001819 // mark cursor position as being invalid
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001820 curwin->w_valid = 0;
1821
Bram Moolenaar9cea87c2018-09-21 16:59:45 +02001822 buflist_setfpos(curbuf, curwin, curbuf->b_last_cursor.lnum,
1823 curbuf->b_last_cursor.col, TRUE);
1824
Bram Moolenaarc667da52019-11-30 20:52:27 +01001825 // Make sure the buffer is loaded.
1826 if (curbuf->b_ml.ml_mfp == NULL) // need to load the file
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001827 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001828 // If there is no filetype, allow for detecting one. Esp. useful for
1829 // ":ball" used in a autocommand. If there already is a filetype we
1830 // might prefer to keep it.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001831 if (*curbuf->b_p_ft == NUL)
1832 did_filetype = FALSE;
1833
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001834 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 else
1837 {
Bram Moolenaareda65222019-05-16 20:29:44 +02001838 if (!msg_silent && !shortmess(SHM_FILEINFO))
1839 need_fileinfo = TRUE; // display file info after redraw
1840
1841 // check if file changed
1842 (void)buf_check_timestamp(curbuf, FALSE);
1843
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001845#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1849 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 }
1851
Bram Moolenaarc667da52019-11-30 20:52:27 +01001852 // If autocommands did not change the cursor position, restore cursor lnum
1853 // and possibly cursor col.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 if (curwin->w_cursor.lnum == 1 && inindent(0))
1855 buflist_getfpos();
1856
Bram Moolenaarc667da52019-11-30 20:52:27 +01001857 check_arg_idx(curwin); // check for valid arg_idx
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858#ifdef FEAT_TITLE
1859 maketitle();
1860#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01001861 // when autocmds didn't change it
Bram Moolenaard4153d42008-11-15 15:06:17 +00001862 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaarc667da52019-11-30 20:52:27 +01001863 scroll_cursor_halfway(FALSE); // redisplay at correct position
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864
Bram Moolenaar009b2592004-10-24 19:18:58 +00001865#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarc667da52019-11-30 20:52:27 +01001866 // Send fileOpened event because we've changed buffers.
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001867 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001868#endif
1869
Bram Moolenaarc667da52019-11-30 20:52:27 +01001870 // Change directories when the 'acd' option is set.
Bram Moolenaar6f470022018-04-10 18:47:20 +02001871 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872
1873#ifdef FEAT_KEYMAP
1874 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001875 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001877#ifdef FEAT_SPELL
Bram Moolenaarc667da52019-11-30 20:52:27 +01001878 // May need to set the spell language. Can only do this after the buffer
1879 // has been properly setup.
Bram Moolenaar860cae12010-06-05 23:22:07 +02001880 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1881 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001882#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001883#ifdef FEAT_VIMINFO
1884 curbuf->b_last_used = vim_time();
1885#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001886
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 redraw_later(NOT_VALID);
1888}
1889
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001890#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1891/*
1892 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001893 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001894 */
1895 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001896do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001897{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001898 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001899 && curbuf->b_ffname != NULL
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001900 && vim_chdirfile(curbuf->b_ffname, "auto") == OK)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001901 shorten_fnames(TRUE);
1902}
1903#endif
1904
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001905 void
1906no_write_message(void)
1907{
1908#ifdef FEAT_TERMINAL
1909 if (term_job_running(curbuf->b_term))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001910 emsg(_("E948: Job still running (add ! to end the job)"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001911 else
1912#endif
Bram Moolenaare29a27f2021-07-20 21:07:36 +02001913 emsg(_(e_no_write_since_last_change_add_bang_to_override));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001914}
1915
1916 void
Bram Moolenaar7a760922018-02-19 23:10:02 +01001917no_write_message_nobang(buf_T *buf UNUSED)
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001918{
1919#ifdef FEAT_TERMINAL
Bram Moolenaar7a760922018-02-19 23:10:02 +01001920 if (term_job_running(buf->b_term))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001921 emsg(_("E948: Job still running"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001922 else
1923#endif
Bram Moolenaare29a27f2021-07-20 21:07:36 +02001924 emsg(_(e_no_write_since_last_change));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001925}
1926
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927/*
1928 * functions for dealing with the buffer list
1929 */
1930
1931/*
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001932 * Return TRUE if the current buffer is empty, unnamed, unmodified and used in
1933 * only one window. That means it can be re-used.
1934 */
1935 int
1936curbuf_reusable(void)
1937{
1938 return (curbuf != NULL
1939 && curbuf->b_ffname == NULL
1940 && curbuf->b_nwindows <= 1
1941 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY())
Bram Moolenaard85c3962019-04-07 14:19:14 +02001942#if defined(FEAT_QUICKFIX)
Bram Moolenaar39803d82019-04-07 12:04:51 +02001943 && !bt_quickfix(curbuf)
Bram Moolenaard85c3962019-04-07 14:19:14 +02001944#endif
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001945 && !curbufIsChanged());
1946}
1947
1948/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 * Add a file name to the buffer list. Return a pointer to the buffer.
1950 * If the same file name already exists return a pointer to that buffer.
1951 * If it does not exist, or if fname == NULL, a new entry is created.
1952 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1953 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1954 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001955 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001956 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
1957 * if the buffer already exists.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001958 * If (flags & BLN_REUSE) is TRUE, may use buffer number from "buf_reuse".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 * This is the ONLY way to create a new buffer.
1960 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001962buflist_new(
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001963 char_u *ffname_arg, // full path of fname or relative
1964 char_u *sfname_arg, // short fname or NULL
1965 linenr_T lnum, // preferred cursor line
1966 int flags) // BLN_ defines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001968 char_u *ffname = ffname_arg;
1969 char_u *sfname = sfname_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 buf_T *buf;
1971#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001972 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973#endif
1974
Bram Moolenaar480778b2016-07-14 22:09:39 +02001975 if (top_file_num == 1)
1976 hash_init(&buf_hashtab);
1977
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001978 fname_expand(curbuf, &ffname, &sfname); // will allocate ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979
1980 /*
Bram Moolenaarc5935a82021-10-19 20:48:52 +01001981 * If the file name already exists in the list, update the entry.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 */
1983#ifdef UNIX
Bram Moolenaarc667da52019-11-30 20:52:27 +01001984 // On Unix we can use inode numbers when the file exists. Works better
1985 // for hard links.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1987 st.st_dev = (dev_T)-1;
1988#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001989 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990#ifdef UNIX
1991 buflist_findname_stat(ffname, &st)
1992#else
1993 buflist_findname(ffname)
1994#endif
1995 ) != NULL)
1996 {
1997 vim_free(ffname);
1998 if (lnum != 0)
Bram Moolenaar89b693e2020-10-25 17:09:50 +01001999 buflist_setfpos(buf, (flags & BLN_NOCURWIN) ? NULL : curwin,
2000 lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02002001
2002 if ((flags & BLN_NOOPT) == 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002003 // copy the options now, if 'cpo' doesn't have 's' and not done
2004 // already
Bram Moolenaar82404332016-07-10 17:00:38 +02002005 buf_copy_options(buf, 0);
2006
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 if ((flags & BLN_LISTED) && !buf->b_p_bl)
2008 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002009 bufref_T bufref;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002010
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 buf->b_p_bl = TRUE;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002012 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002014 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002015 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002016 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002017 return NULL;
2018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 }
2020 return buf;
2021 }
2022
2023 /*
2024 * If the current buffer has no name and no contents, use the current
2025 * buffer. Otherwise: Need to allocate a new buffer structure.
2026 *
2027 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00002028 * (A spell file buffer is allocated in spell.c, but that's not a normal
2029 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 */
2031 buf = NULL;
Bram Moolenaar46a53df2018-04-24 21:58:51 +02002032 if ((flags & BLN_CURBUF) && curbuf_reusable())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 {
2034 buf = curbuf;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002035 // It's like this buffer is deleted. Watch out for autocommands that
2036 // change curbuf! If that happens, allocate a new buffer anyway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 if (curbuf->b_p_bl)
2038 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
2039 if (buf == curbuf)
2040 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002041#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002042 if (aborting()) // autocmds may abort script processing
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002043 {
2044 vim_free(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 return NULL;
Bram Moolenaar14285cb2020-03-27 20:58:37 +01002046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 if (buf == curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002050 // Make sure 'bufhidden' and 'buftype' are empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 clear_string_option(&buf->b_p_bh);
2052 clear_string_option(&buf->b_p_bt);
2053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 }
2055 if (buf != curbuf || curbuf == NULL)
2056 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002057 buf = ALLOC_CLEAR_ONE(buf_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 if (buf == NULL)
2059 {
2060 vim_free(ffname);
2061 return NULL;
2062 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02002063#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002064 // init b: variables
Bram Moolenaar429fa852013-04-15 12:27:36 +02002065 buf->b_vars = dict_alloc();
2066 if (buf->b_vars == NULL)
2067 {
2068 vim_free(ffname);
2069 vim_free(buf);
2070 return NULL;
2071 }
2072 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
2073#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01002074 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 }
2076
2077 if (ffname != NULL)
2078 {
2079 buf->b_ffname = ffname;
2080 buf->b_sfname = vim_strsave(sfname);
2081 }
2082
2083 clear_wininfo(buf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002084 buf->b_wininfo = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085
2086 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
2087 || buf->b_wininfo == NULL)
2088 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002089 if (buf->b_sfname != buf->b_ffname)
2090 VIM_CLEAR(buf->b_sfname);
2091 else
2092 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01002093 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 if (buf != curbuf)
2095 free_buffer(buf);
2096 return NULL;
2097 }
2098
2099 if (buf == curbuf)
2100 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01002101 // free all things allocated for this buffer
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002102 buf_freeall(buf, 0);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002103 if (buf != curbuf) // autocommands deleted the buffer!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 return NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002105#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01002106 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 return NULL;
2108#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01002109 free_buffer_stuff(buf, FALSE); // delete local variables et al.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002110
Bram Moolenaarc667da52019-11-30 20:52:27 +01002111 // Init the options.
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002112 buf->b_p_initialized = FALSE;
2113 buf_copy_options(buf, BCO_ENTER);
2114
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115#ifdef FEAT_KEYMAP
Bram Moolenaarc667da52019-11-30 20:52:27 +01002116 // need to reload lmaps and set b:keymap_name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 curbuf->b_kmap_state |= KEYMAP_INIT;
2118#endif
2119 }
2120 else
2121 {
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002122 // put the new buffer at the end of the buffer list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 buf->b_next = NULL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002124 if (firstbuf == NULL) // buffer list is empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 {
2126 buf->b_prev = NULL;
2127 firstbuf = buf;
2128 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01002129 else // append new buffer at end of list
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 {
2131 lastbuf->b_next = buf;
2132 buf->b_prev = lastbuf;
2133 }
2134 lastbuf = buf;
2135
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002136 if ((flags & BLN_REUSE) && buf_reuse.ga_len > 0)
2137 {
2138 // Recycle a previously used buffer number. Used for buffers which
2139 // are normally hidden, e.g. in a popup window. Avoids that the
2140 // buffer number grows rapidly.
2141 --buf_reuse.ga_len;
2142 buf->b_fnum = ((int *)buf_reuse.ga_data)[buf_reuse.ga_len];
Bram Moolenaar99ebf222019-12-10 23:44:48 +01002143
2144 // Move buffer to the right place in the buffer list.
2145 while (buf->b_prev != NULL && buf->b_fnum < buf->b_prev->b_fnum)
2146 {
2147 buf_T *prev = buf->b_prev;
2148
2149 prev->b_next = buf->b_next;
2150 if (prev->b_next != NULL)
2151 prev->b_next->b_prev = prev;
2152 buf->b_next = prev;
2153 buf->b_prev = prev->b_prev;
2154 if (buf->b_prev != NULL)
2155 buf->b_prev->b_next = buf;
2156 prev->b_prev = buf;
2157 if (lastbuf == buf)
2158 lastbuf = prev;
2159 if (firstbuf == prev)
2160 firstbuf = buf;
2161 }
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002162 }
2163 else
2164 buf->b_fnum = top_file_num++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01002165 if (top_file_num < 0) // wrap around (may cause duplicates)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002167 emsg(_("W14: Warning: List of file names overflow"));
Bram Moolenaar28ee8922020-10-28 20:20:00 +01002168 if (emsg_silent == 0 && !in_assert_fails)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 {
2170 out_flush();
Bram Moolenaareda1da02019-11-17 17:06:33 +01002171 ui_delay(3001L, TRUE); // make sure it is noticed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 }
2173 top_file_num = 1;
2174 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002175 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176
Bram Moolenaarc5935a82021-10-19 20:48:52 +01002177 // Always copy the options from the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 buf_copy_options(buf, BCO_ALWAYS);
2179 }
2180
2181 buf->b_wininfo->wi_fpos.lnum = lnum;
2182 buf->b_wininfo->wi_win = curwin;
2183
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002184#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002185 hash_init(&buf->b_s.b_keywtab);
2186 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187#endif
2188
2189 buf->b_fname = buf->b_sfname;
2190#ifdef UNIX
2191 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002192 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 else
2194 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002195 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 buf->b_dev = st.st_dev;
2197 buf->b_ino = st.st_ino;
2198 }
2199#endif
2200 buf->b_u_synced = TRUE;
2201 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002202 if (flags & BLN_DUMMY)
2203 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 buf_clear_file(buf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01002205 clrallmarks(buf); // clear marks
2206 fmarks_check_names(buf); // check file marks for this file
2207 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; // init 'buflisted'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 if (!(flags & BLN_DUMMY))
2209 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002210 bufref_T bufref;
2211
Bram Moolenaarc667da52019-11-30 20:52:27 +01002212 // Tricky: these autocommands may change the buffer list. They could
2213 // also split the window with re-using the one empty buffer. This may
2214 // result in unexpectedly losing the empty buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002215 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002216 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002217 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002218 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002220 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002221 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002222 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002223 return NULL;
2224 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002225#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01002226 if (aborting()) // autocmds may abort script processing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230
2231 return buf;
2232}
2233
2234/*
2235 * Free the memory for the options of a buffer.
2236 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2237 * 'fileencoding'.
2238 */
2239 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002240free_buf_options(
2241 buf_T *buf,
2242 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243{
2244 if (free_p_ff)
2245 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 clear_string_option(&buf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 clear_string_option(&buf->b_p_bh);
2249 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 }
2251#ifdef FEAT_FIND_ID
2252 clear_string_option(&buf->b_p_def);
2253 clear_string_option(&buf->b_p_inc);
2254# ifdef FEAT_EVAL
2255 clear_string_option(&buf->b_p_inex);
2256# endif
2257#endif
2258#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
2259 clear_string_option(&buf->b_p_inde);
2260 clear_string_option(&buf->b_p_indk);
2261#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002262#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2263 clear_string_option(&buf->b_p_bexpr);
2264#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002265#if defined(FEAT_CRYPT)
2266 clear_string_option(&buf->b_p_cm);
2267#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002268 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002269#if defined(FEAT_EVAL)
2270 clear_string_option(&buf->b_p_fex);
2271#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272#ifdef FEAT_CRYPT
Bram Moolenaar131530a2021-07-29 20:37:49 +02002273# ifdef FEAT_SODIUM
2274 if (buf->b_p_key != NULL && (crypt_get_method_nr(buf) == CRYPT_M_SOD))
2275 sodium_munlock(buf->b_p_key, STRLEN(buf->b_p_key));
2276# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 clear_string_option(&buf->b_p_key);
2278#endif
2279 clear_string_option(&buf->b_p_kp);
2280 clear_string_option(&buf->b_p_mps);
2281 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002282 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 clear_string_option(&buf->b_p_isk);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002284#ifdef FEAT_VARTABS
2285 clear_string_option(&buf->b_p_vsts);
Bram Moolenaarbdace832019-03-02 10:13:42 +01002286 vim_free(buf->b_p_vsts_nopaste);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002287 buf->b_p_vsts_nopaste = NULL;
Bram Moolenaarbdace832019-03-02 10:13:42 +01002288 vim_free(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002289 buf->b_p_vsts_array = NULL;
2290 clear_string_option(&buf->b_p_vts);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002291 VIM_CLEAR(buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002292#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293#ifdef FEAT_KEYMAP
2294 clear_string_option(&buf->b_p_keymap);
Bram Moolenaar50138322018-01-28 17:05:16 +01002295 keymap_clear(&buf->b_kmap_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 ga_clear(&buf->b_kmap_ga);
2297#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 clear_string_option(&buf->b_p_com);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299#ifdef FEAT_FOLDING
2300 clear_string_option(&buf->b_p_cms);
2301#endif
2302 clear_string_option(&buf->b_p_nf);
2303#ifdef FEAT_SYN_HL
2304 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002305 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002306#endif
2307#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002308 clear_string_option(&buf->b_s.b_p_spc);
2309 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002310 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002311 buf->b_s.b_cap_prog = NULL;
2312 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar362b44b2020-06-10 21:47:00 +02002313 clear_string_option(&buf->b_s.b_p_spo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314#endif
2315#ifdef FEAT_SEARCHPATH
2316 clear_string_option(&buf->b_p_sua);
2317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 clear_string_option(&buf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319#ifdef FEAT_CINDENT
2320 clear_string_option(&buf->b_p_cink);
2321 clear_string_option(&buf->b_p_cino);
2322#endif
2323#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
2324 clear_string_option(&buf->b_p_cinw);
2325#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 clear_string_option(&buf->b_p_cpt);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002327#ifdef FEAT_COMPL_FUNC
2328 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002329 clear_string_option(&buf->b_p_ofu);
Bram Moolenaard4c4bfa2021-10-16 21:14:11 +01002330 clear_string_option(&buf->b_p_tsrfu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002331#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332#ifdef FEAT_QUICKFIX
2333 clear_string_option(&buf->b_p_gp);
2334 clear_string_option(&buf->b_p_mp);
2335 clear_string_option(&buf->b_p_efm);
2336#endif
2337 clear_string_option(&buf->b_p_ep);
2338 clear_string_option(&buf->b_p_path);
2339 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002340 clear_string_option(&buf->b_p_tc);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002341#ifdef FEAT_EVAL
2342 clear_string_option(&buf->b_p_tfu);
2343#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 Moolenaarb5443cc2019-01-15 20:19:40 +01002386 semsg(_("E92: Buffer %d 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 Moolenaarf9e3e092019-01-13 23:38:42 +01002670 semsg(_("E93: More than one match for %s"), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 else if (match < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002672 semsg(_("E94: No matching buffer for %s"), 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 Moolenaarf9e3e092019-01-13 23:38:42 +01003374 emsg(_("E95: 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
3452 if (curwin->w_buffer == buf)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003453 check_arg_idx(curwin); // check file name for arg list
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454#ifdef FEAT_TITLE
Bram Moolenaarc667da52019-11-30 20:52:27 +01003455 maketitle(); // set window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003457 status_redraw_all(); // status lines need to be redrawn
3458 fmarks_check_names(buf); // check named file marks
3459 ml_timestamp(buf); // reset timestamp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460}
3461
3462/*
3463 * set alternate file name for current window
3464 *
3465 * Used by do_one_cmd(), do_write() and do_ecmd().
3466 * Return the buffer.
3467 */
3468 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003469setaltfname(
3470 char_u *ffname,
3471 char_u *sfname,
3472 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473{
3474 buf_T *buf;
3475
Bram Moolenaarc667da52019-11-30 20:52:27 +01003476 // Create a buffer. 'buflisted' is not set if it's a new buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaare1004402020-10-24 20:49:43 +02003478 if (buf != NULL && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 curwin->w_alt_fnum = buf->b_fnum;
3480 return buf;
3481}
3482
3483/*
3484 * Get alternate file name for current window.
3485 * Return NULL if there isn't any, and give error message if requested.
3486 */
3487 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003488getaltfname(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003489 int errmsg) // give error message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490{
3491 char_u *fname;
3492 linenr_T dummy;
3493
3494 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3495 {
3496 if (errmsg)
Bram Moolenaar108010a2021-06-27 22:03:33 +02003497 emsg(_(e_no_alternate_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 return NULL;
3499 }
3500 return fname;
3501}
3502
3503/*
3504 * Add a file name to the buflist and return its number.
3505 * Uses same flags as buflist_new(), except BLN_DUMMY.
3506 *
3507 * used by qf_init(), main() and doarglist()
3508 */
3509 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003510buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511{
3512 buf_T *buf;
3513
3514 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3515 if (buf != NULL)
3516 return buf->b_fnum;
3517 return 0;
3518}
3519
3520#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3521/*
3522 * Adjust slashes in file names. Called after 'shellslash' was set.
3523 */
3524 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003525buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526{
3527 buf_T *bp;
3528
Bram Moolenaar29323592016-07-24 22:04:11 +02003529 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 {
3531 if (bp->b_ffname != NULL)
3532 slash_adjust(bp->b_ffname);
3533 if (bp->b_sfname != NULL)
3534 slash_adjust(bp->b_sfname);
3535 }
3536}
3537#endif
3538
3539/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003540 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 * Also save the local window option values.
3542 */
3543 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003544buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003546 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547}
3548
3549/*
3550 * Return TRUE if 'ffname' is not the same file as current file.
3551 * Fname must have a full path (expanded by mch_FullName()).
3552 */
3553 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003554otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555{
3556 return otherfile_buf(curbuf, ffname
3557#ifdef UNIX
3558 , NULL
3559#endif
3560 );
3561}
3562
3563 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003564otherfile_buf(
3565 buf_T *buf,
3566 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003568 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003570 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571{
Bram Moolenaarc667da52019-11-30 20:52:27 +01003572 // no name is different
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3574 return TRUE;
3575 if (fnamecmp(ffname, buf->b_ffname) == 0)
3576 return FALSE;
3577#ifdef UNIX
3578 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003579 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580
Bram Moolenaarc667da52019-11-30 20:52:27 +01003581 // If no stat_T given, get it now
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 if (stp == NULL)
3583 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003584 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 st.st_dev = (dev_T)-1;
3586 stp = &st;
3587 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01003588 // Use dev/ino to check if the files are the same, even when the names
3589 // are different (possible with links). Still need to compare the
3590 // name above, for when the file doesn't exist yet.
3591 // Problem: The dev/ino changes when a file is deleted (and created
3592 // again) and remains the same when renamed/moved. We don't want to
3593 // mch_stat() each buffer each time, that would be too slow. Get the
3594 // dev/ino again when they appear to match, but not when they appear
3595 // to be different: Could skip a buffer when it's actually the same
3596 // file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 if (buf_same_ino(buf, stp))
3598 {
3599 buf_setino(buf);
3600 if (buf_same_ino(buf, stp))
3601 return FALSE;
3602 }
3603 }
3604#endif
3605 return TRUE;
3606}
3607
3608#if defined(UNIX) || defined(PROTO)
3609/*
3610 * Set inode and device number for a buffer.
3611 * Must always be called when b_fname is changed!.
3612 */
3613 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003614buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003616 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617
3618 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3619 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003620 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 buf->b_dev = st.st_dev;
3622 buf->b_ino = st.st_ino;
3623 }
3624 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003625 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626}
3627
3628/*
3629 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3630 */
3631 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003632buf_same_ino(
3633 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003634 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003636 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 && stp->st_dev == buf->b_dev
3638 && stp->st_ino == buf->b_ino);
3639}
3640#endif
3641
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003642/*
3643 * Print info about the current buffer.
3644 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003646fileinfo(
Bram Moolenaarc667da52019-11-30 20:52:27 +01003647 int fullname, // when non-zero print full path
Bram Moolenaar7454a062016-01-30 15:14:10 +01003648 int shorthelp,
3649 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650{
3651 char_u *name;
3652 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003653 char *p;
3654 char *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003655 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003657 buffer = alloc(IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 if (buffer == NULL)
3659 return;
3660
Bram Moolenaarc667da52019-11-30 20:52:27 +01003661 if (fullname > 1) // 2 CTRL-G: include buffer number
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003663 vim_snprintf(buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 p = buffer + STRLEN(buffer);
3665 }
3666 else
3667 p = buffer;
3668
3669 *p++ = '"';
3670 if (buf_spname(curbuf) != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003671 vim_strncpy((char_u *)p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 else
3673 {
3674 if (!fullname && curbuf->b_fname != NULL)
3675 name = curbuf->b_fname;
3676 else
3677 name = curbuf->b_ffname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003678 home_replace(shorthelp ? curbuf : NULL, name, (char_u *)p,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 (int)(IOSIZE - (p - buffer)), TRUE);
3680 }
3681
Bram Moolenaar32526b32019-01-19 17:43:09 +01003682 vim_snprintf_add(buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 curbufIsChanged() ? (shortmess(SHM_MOD)
3684 ? " [+]" : _(" [Modified]")) : " ",
3685 (curbuf->b_flags & BF_NOTEDITED)
3686#ifdef FEAT_QUICKFIX
3687 && !bt_dontwrite(curbuf)
3688#endif
3689 ? _("[Not edited]") : "",
3690 (curbuf->b_flags & BF_NEW)
3691#ifdef FEAT_QUICKFIX
3692 && !bt_dontwrite(curbuf)
3693#endif
Bram Moolenaar722e5052020-06-12 22:31:00 +02003694 ? new_file_message() : "",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003696 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 : _("[readonly]")) : "",
3698 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3699 || curbuf->b_p_ro) ?
3700 " " : "");
Bram Moolenaarc667da52019-11-30 20:52:27 +01003701 // With 32 bit longs and more than 21,474,836 lines multiplying by 100
3702 // causes an overflow, thus for large numbers divide instead.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 if (curwin->w_cursor.lnum > 1000000L)
3704 n = (int)(((long)curwin->w_cursor.lnum) /
3705 ((long)curbuf->b_ml.ml_line_count / 100L));
3706 else
3707 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3708 (long)curbuf->b_ml.ml_line_count);
3709 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003710 vim_snprintf_add(buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711#ifdef FEAT_CMDL_INFO
3712 else if (p_ru)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003713 // Current line and column are already on the screen -- webb
Bram Moolenaar32526b32019-01-19 17:43:09 +01003714 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003715 NGETTEXT("%ld line --%d%%--", "%ld lines --%d%%--",
3716 curbuf->b_ml.ml_line_count),
3717 (long)curbuf->b_ml.ml_line_count, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718#endif
3719 else
3720 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003721 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 _("line %ld of %ld --%d%%-- col "),
3723 (long)curwin->w_cursor.lnum,
3724 (long)curbuf->b_ml.ml_line_count,
3725 n);
3726 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003727 len = STRLEN(buffer);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003728 col_print((char_u *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3730 }
3731
Bram Moolenaar32526b32019-01-19 17:43:09 +01003732 (void)append_arg_number(curwin, (char_u *)buffer, IOSIZE,
3733 !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734
3735 if (dont_truncate)
3736 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003737 // Temporarily set msg_scroll to avoid the message being truncated.
3738 // First call msg_start() to get the message in the right place.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 msg_start();
3740 n = msg_scroll;
3741 msg_scroll = TRUE;
3742 msg(buffer);
3743 msg_scroll = n;
3744 }
3745 else
3746 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003747 p = (char *)msg_trunc_attr(buffer, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaarc667da52019-11-30 20:52:27 +01003749 // Need to repeat the message after redrawing when:
3750 // - When restart_edit is set (otherwise there will be a delay
3751 // before redrawing).
3752 // - When the screen was scrolled but there is no wait-return
3753 // prompt.
Bram Moolenaar32526b32019-01-19 17:43:09 +01003754 set_keep_msg((char_u *)p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 }
3756
3757 vim_free(buffer);
3758}
3759
3760 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003761col_print(
3762 char_u *buf,
3763 size_t buflen,
3764 int col,
3765 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766{
3767 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003768 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003770 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771}
3772
3773#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774static char_u *lasttitle = NULL;
3775static char_u *lasticon = NULL;
3776
Bram Moolenaar84a93082018-06-16 22:58:15 +02003777/*
3778 * Put the file name in the title bar and icon of the window.
3779 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003781maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782{
3783 char_u *p;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003784 char_u *title_str = NULL;
3785 char_u *icon_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 int maxlen = 0;
3787 int len;
3788 int mustset;
3789 char_u buf[IOSIZE];
3790 int off;
3791
3792 if (!redrawing())
3793 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003794 // Postpone updating the title when 'lazyredraw' is set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 need_maketitle = TRUE;
3796 return;
3797 }
3798
3799 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003800 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003801 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802
3803 if (p_title)
3804 {
3805 if (p_titlelen > 0)
3806 {
3807 maxlen = p_titlelen * Columns / 100;
3808 if (maxlen < 10)
3809 maxlen = 10;
3810 }
3811
Bram Moolenaar84a93082018-06-16 22:58:15 +02003812 title_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 if (*p_titlestring != NUL)
3814 {
3815#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003816 if (stl_syntax & STL_IN_TITLE)
3817 {
3818 int use_sandbox = FALSE;
Bram Moolenaar53989552019-12-23 22:59:18 +01003819 int called_emsg_before = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003820
3821# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003822 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003823# endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003824 build_stl_str_hl(curwin, title_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003825 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003826 0, maxlen, NULL, NULL);
Bram Moolenaar53989552019-12-23 22:59:18 +01003827 if (called_emsg > called_emsg_before)
Bram Moolenaard3667a22006-03-16 21:35:52 +00003828 set_string_option_direct((char_u *)"titlestring", -1,
3829 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 else
3832#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003833 title_str = p_titlestring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 }
3835 else
3836 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003837 // format: "fname + (path) (1 of 2) - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838
Bram Moolenaar2c666692012-09-05 13:30:40 +02003839#define SPACE_FOR_FNAME (IOSIZE - 100)
3840#define SPACE_FOR_DIR (IOSIZE - 20)
Bram Moolenaarc667da52019-11-30 20:52:27 +01003841#define SPACE_FOR_ARGNR (IOSIZE - 10) // at least room for " - VIM"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003843 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar21554412017-07-24 21:44:43 +02003844#ifdef FEAT_TERMINAL
3845 else if (curbuf->b_term != NULL)
3846 {
3847 vim_strncpy(buf, term_get_status_text(curbuf->b_term),
3848 SPACE_FOR_FNAME);
3849 }
3850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 else
3852 {
3853 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003854 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 }
3857
Bram Moolenaar21554412017-07-24 21:44:43 +02003858#ifdef FEAT_TERMINAL
3859 if (curbuf->b_term == NULL)
3860#endif
3861 switch (bufIsChanged(curbuf)
3862 + (curbuf->b_p_ro * 2)
3863 + (!curbuf->b_p_ma * 4))
3864 {
3865 case 1: STRCAT(buf, " +"); break;
3866 case 2: STRCAT(buf, " ="); break;
3867 case 3: STRCAT(buf, " =+"); break;
3868 case 4:
3869 case 6: STRCAT(buf, " -"); break;
3870 case 5:
3871 case 7: STRCAT(buf, " -+"); break;
3872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873
Bram Moolenaar21554412017-07-24 21:44:43 +02003874 if (curbuf->b_fname != NULL
3875#ifdef FEAT_TERMINAL
3876 && curbuf->b_term == NULL
3877#endif
3878 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003880 // Get path of file, replace home dir with ~
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 off = (int)STRLEN(buf);
3882 buf[off++] = ' ';
3883 buf[off++] = '(';
3884 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003885 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc667da52019-11-30 20:52:27 +01003887 // avoid "c:/name" to be reduced to "c"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 if (isalpha(buf[off]) && buf[off + 1] == ':')
3889 off += 2;
3890#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01003891 // remove the file name
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003892 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 if (p == buf + off)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003894 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003895 // must be a help buffer
Bram Moolenaar21554412017-07-24 21:44:43 +02003896 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003897 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003901
Bram Moolenaarc667da52019-11-30 20:52:27 +01003902 // Translate unprintable chars and concatenate. Keep some
3903 // room for the server name. When there is no room (very long
3904 // file name) use (...).
Bram Moolenaar2c666692012-09-05 13:30:40 +02003905 if (off < SPACE_FOR_DIR)
3906 {
3907 p = transstr(buf + off);
3908 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3909 vim_free(p);
3910 }
3911 else
3912 {
3913 vim_strncpy(buf + off, (char_u *)"...",
3914 (size_t)(SPACE_FOR_ARGNR - off));
3915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 STRCAT(buf, ")");
3917 }
3918
Bram Moolenaar2c666692012-09-05 13:30:40 +02003919 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920
3921#if defined(FEAT_CLIENTSERVER)
3922 if (serverName != NULL)
3923 {
3924 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003925 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 }
3927 else
3928#endif
3929 STRCAT(buf, " - VIM");
3930
3931 if (maxlen > 0)
3932 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01003933 // make it shorter by removing a bit in the middle
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003934 if (vim_strsize(buf) > maxlen)
3935 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 }
3937 }
3938 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02003939 mustset = value_changed(title_str, &lasttitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940
3941 if (p_icon)
3942 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02003943 icon_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 if (*p_iconstring != NUL)
3945 {
3946#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003947 if (stl_syntax & STL_IN_ICON)
3948 {
3949 int use_sandbox = FALSE;
Bram Moolenaar53989552019-12-23 22:59:18 +01003950 int called_emsg_before = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003951
3952# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003953 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003954# endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003955 build_stl_str_hl(curwin, icon_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003956 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003957 0, 0, NULL, NULL);
Bram Moolenaar53989552019-12-23 22:59:18 +01003958 if (called_emsg > called_emsg_before)
Bram Moolenaard3667a22006-03-16 21:35:52 +00003959 set_string_option_direct((char_u *)"iconstring", -1,
3960 (char_u *)"", OPT_FREE, SID_ERROR);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 else
3963#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003964 icon_str = p_iconstring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 }
3966 else
3967 {
3968 if (buf_spname(curbuf) != NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003969 p = buf_spname(curbuf);
Bram Moolenaarc667da52019-11-30 20:52:27 +01003970 else // use file name only in icon
Bram Moolenaar84a93082018-06-16 22:58:15 +02003971 p = gettail(curbuf->b_ffname);
3972 *icon_str = NUL;
Bram Moolenaarc667da52019-11-30 20:52:27 +01003973 // Truncate name at 100 bytes.
Bram Moolenaar84a93082018-06-16 22:58:15 +02003974 len = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 if (len > 100)
3976 {
3977 len -= 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 if (has_mbyte)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003979 len += (*mb_tail_off)(p, p + len) + 1;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003980 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02003982 STRCPY(icon_str, p);
3983 trans_characters(icon_str, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 }
3985 }
3986
Bram Moolenaar84a93082018-06-16 22:58:15 +02003987 mustset |= value_changed(icon_str, &lasticon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988
3989 if (mustset)
3990 resettitle();
3991}
3992
3993/*
3994 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3995 * from "str" if it does.
Bram Moolenaar84a93082018-06-16 22:58:15 +02003996 * Return TRUE if resettitle() is to be called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 */
3998 static int
Bram Moolenaar84a93082018-06-16 22:58:15 +02003999value_changed(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000{
4001 if ((str == NULL) != (*last == NULL)
4002 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
4003 {
4004 vim_free(*last);
4005 if (str == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02004006 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 *last = NULL;
Bram Moolenaar40385db2018-08-07 22:31:44 +02004008 mch_restore_title(
4009 last == &lasttitle ? SAVE_RESTORE_TITLE : SAVE_RESTORE_ICON);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 else
Bram Moolenaar84a93082018-06-16 22:58:15 +02004012 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 *last = vim_strsave(str);
Bram Moolenaar84a93082018-06-16 22:58:15 +02004014 return TRUE;
4015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 }
4017 return FALSE;
4018}
4019
4020/*
4021 * Put current window title back (used after calling a shell)
4022 */
4023 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004024resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025{
4026 mch_settitle(lasttitle, lasticon);
4027}
Bram Moolenaarea408852005-06-25 22:49:46 +00004028
4029# if defined(EXITFREE) || defined(PROTO)
4030 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004031free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00004032{
4033 vim_free(lasttitle);
4034 vim_free(lasticon);
4035}
4036# endif
4037
Bram Moolenaarc667da52019-11-30 20:52:27 +01004038#endif // FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004040#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004041
4042/*
4043 * Used for building in the status line.
4044 */
4045typedef struct
4046{
4047 char_u *stl_start;
4048 int stl_minwid;
4049 int stl_maxwid;
4050 enum {
4051 Normal,
4052 Empty,
4053 Group,
4054 Middle,
4055 Highlight,
4056 TabPage,
4057 Trunc
4058 } stl_type;
4059} stl_item_T;
4060
4061static size_t stl_items_len = 20; // Initial value, grows as needed.
4062static stl_item_T *stl_items = NULL;
4063static int *stl_groupitem = NULL;
4064static stl_hlrec_T *stl_hltab = NULL;
4065static stl_hlrec_T *stl_tabtab = NULL;
4066
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004068 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 * Return length of string in screen cells.
4070 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004071 * Normally works for window "wp", except when working for 'tabline' then it
4072 * is "curwin".
4073 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 * Items are drawn interspersed with the text that surrounds it
4075 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
4076 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
4077 *
4078 * If maxwidth is not zero, the string will be filled at any middle marker
4079 * or truncated if too long, fillchar is used for all whitespace.
4080 */
4081 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004082build_stl_str_hl(
4083 win_T *wp,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004084 char_u *out, // buffer to write into != NameBuff
4085 size_t outlen, // length of out[]
Bram Moolenaar7454a062016-01-30 15:14:10 +01004086 char_u *fmt,
Bram Moolenaarc667da52019-11-30 20:52:27 +01004087 int use_sandbox UNUSED, // "fmt" was set insecurely, use sandbox
Bram Moolenaar7454a062016-01-30 15:14:10 +01004088 int fillchar,
4089 int maxwidth,
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004090 stl_hlrec_T **hltab, // return: HL attributes (can be NULL)
4091 stl_hlrec_T **tabtab) // return: tab page nrs (can be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092{
Bram Moolenaar10772302019-01-20 18:25:54 +01004093 linenr_T lnum;
4094 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 char_u *p;
4096 char_u *s;
4097 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004098 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099#ifdef FEAT_EVAL
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004100 win_T *save_curwin;
4101 buf_T *save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004102 int save_VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103#endif
4104 int empty_line;
4105 colnr_T virtcol;
4106 long l;
4107 long n;
4108 int prevchar_isflag;
4109 int prevchar_isitem;
4110 int itemisflag;
4111 int fillable;
4112 char_u *str;
4113 long num;
4114 int width;
4115 int itemcnt;
4116 int curitem;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004117 int group_end_userhl;
4118 int group_start_userhl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 int groupdepth;
shadmansaleh30e3de22021-05-15 17:23:28 +02004120#ifdef FEAT_EVAL
4121 int evaldepth;
4122#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 int minwid;
4124 int maxwid;
4125 int zeropad;
4126 char_u base;
4127 char_u opt;
4128#define TMPLEN 70
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004129 char_u buf_tmp[TMPLEN];
4130 char_u win_tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004131 char_u *usefmt = fmt;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004132 stl_hlrec_T *sp;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004133 int save_must_redraw = must_redraw;
4134 int save_redr_type = curwin->w_redr_type;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004135
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004136 if (stl_items == NULL)
4137 {
4138 stl_items = ALLOC_MULT(stl_item_T, stl_items_len);
4139 stl_groupitem = ALLOC_MULT(int, stl_items_len);
4140 stl_hltab = ALLOC_MULT(stl_hlrec_T, stl_items_len);
4141 stl_tabtab = ALLOC_MULT(stl_hlrec_T, stl_items_len);
4142 }
4143
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004144#ifdef FEAT_EVAL
4145 /*
4146 * When the format starts with "%!" then evaluate it as an expression and
4147 * use the result as the actual format string.
4148 */
4149 if (fmt[0] == '%' && fmt[1] == '!')
4150 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004151 typval_T tv;
4152
4153 tv.v_type = VAR_NUMBER;
4154 tv.vval.v_number = wp->w_id;
4155 set_var((char_u *)"g:statusline_winid", &tv, FALSE);
4156
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004157 usefmt = eval_to_string_safe(fmt + 2, use_sandbox);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004158 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00004159 usefmt = fmt;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004160
4161 do_unlet((char_u *)"g:statusline_winid", TRUE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004162 }
4163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164
4165 if (fillchar == 0)
4166 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004167
Bram Moolenaar10772302019-01-20 18:25:54 +01004168 // The cursor in windows other than the current one isn't always
4169 // up-to-date, esp. because of autocommands and timers.
4170 lnum = wp->w_cursor.lnum;
4171 if (lnum > wp->w_buffer->b_ml.ml_line_count)
4172 {
4173 lnum = wp->w_buffer->b_ml.ml_line_count;
4174 wp->w_cursor.lnum = lnum;
4175 }
4176
4177 // Get line & check if empty (cursorpos will show "0-1"). Note that
4178 // p will become invalid when getting another buffer line.
4179 p = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar567199b2013-04-24 16:52:36 +02004180 empty_line = (*p == NUL);
4181
Bram Moolenaar10772302019-01-20 18:25:54 +01004182 // Get the byte value now, in case we need it below. This is more efficient
4183 // than making a copy of the line.
4184 len = STRLEN(p);
4185 if (wp->w_cursor.col > (colnr_T)len)
4186 {
4187 // Line may have changed since checking the cursor column, or the lnum
4188 // was adjusted above.
4189 wp->w_cursor.col = (colnr_T)len;
Bram Moolenaar10772302019-01-20 18:25:54 +01004190 wp->w_cursor.coladd = 0;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004191 byteval = 0;
Bram Moolenaar10772302019-01-20 18:25:54 +01004192 }
Bram Moolenaar567199b2013-04-24 16:52:36 +02004193 else
Bram Moolenaar567199b2013-04-24 16:52:36 +02004194 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195
4196 groupdepth = 0;
shadmansaleh30e3de22021-05-15 17:23:28 +02004197#ifdef FEAT_EVAL
4198 evaldepth = 0;
4199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 p = out;
4201 curitem = 0;
4202 prevchar_isflag = TRUE;
4203 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004204 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004206 if (curitem == (int)stl_items_len)
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004207 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004208 size_t new_len = stl_items_len * 3 / 2;
4209 stl_item_T *new_items;
4210 int *new_groupitem;
4211 stl_hlrec_T *new_hlrec;
4212
4213 new_items = vim_realloc(stl_items, sizeof(stl_item_T) * new_len);
4214 if (new_items == NULL)
4215 break;
4216 stl_items = new_items;
4217 new_groupitem = vim_realloc(stl_groupitem, sizeof(int) * new_len);
4218 if (new_groupitem == NULL)
4219 break;
4220 stl_groupitem = new_groupitem;
4221 new_hlrec = vim_realloc(stl_hltab, sizeof(stl_hlrec_T) * new_len);
4222 if (new_hlrec == NULL)
4223 break;
4224 stl_hltab = new_hlrec;
4225 new_hlrec = vim_realloc(stl_tabtab, sizeof(stl_hlrec_T) * new_len);
4226 if (new_hlrec == NULL)
4227 break;
4228 stl_tabtab = new_hlrec;
4229 stl_items_len = new_len;
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004230 }
4231
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 if (*s != NUL && *s != '%')
4233 prevchar_isflag = prevchar_isitem = FALSE;
4234
4235 /*
4236 * Handle up to the next '%' or the end.
4237 */
4238 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
4239 *p++ = *s++;
4240 if (*s == NUL || p + 1 >= out + outlen)
4241 break;
4242
4243 /*
4244 * Handle one '%' item.
4245 */
4246 s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004247 if (*s == NUL) // ignore trailing %
Bram Moolenaar1d87f512011-02-01 21:55:01 +01004248 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 if (*s == '%')
4250 {
4251 if (p + 1 >= out + outlen)
4252 break;
4253 *p++ = *s++;
4254 prevchar_isflag = prevchar_isitem = FALSE;
4255 continue;
4256 }
4257 if (*s == STL_MIDDLEMARK)
4258 {
4259 s++;
4260 if (groupdepth > 0)
4261 continue;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004262 stl_items[curitem].stl_type = Middle;
4263 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 continue;
4265 }
4266 if (*s == STL_TRUNCMARK)
4267 {
4268 s++;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004269 stl_items[curitem].stl_type = Trunc;
4270 stl_items[curitem++].stl_start = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 continue;
4272 }
4273 if (*s == ')')
4274 {
4275 s++;
4276 if (groupdepth < 1)
4277 continue;
4278 groupdepth--;
4279
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004280 t = stl_items[stl_groupitem[groupdepth]].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 *p = NUL;
4282 l = vim_strsize(t);
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004283 if (curitem > stl_groupitem[groupdepth] + 1
4284 && stl_items[stl_groupitem[groupdepth]].stl_minwid == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004286 // remove group if all items are empty and highlight group
4287 // doesn't change
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004288 group_start_userhl = group_end_userhl = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004289 for (n = stl_groupitem[groupdepth] - 1; n >= 0; n--)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004290 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004291 if (stl_items[n].stl_type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004292 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004293 group_start_userhl = group_end_userhl =
4294 stl_items[n].stl_minwid;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004295 break;
4296 }
4297 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004298 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004299 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004300 if (stl_items[n].stl_type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 break;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004302 if (stl_items[n].stl_type == Highlight)
4303 group_end_userhl = stl_items[n].stl_minwid;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004304 }
4305 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 {
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004307 // empty group
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 p = t;
4309 l = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004310 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004311 {
4312 // do not use the highlighting from the removed group
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004313 if (stl_items[n].stl_type == Highlight)
4314 stl_items[n].stl_type = Empty;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004315 // adjust the start position of TabPage to the next
4316 // item position
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004317 if (stl_items[n].stl_type == TabPage)
4318 stl_items[n].stl_start = p;
Bram Moolenaarf56c95f2020-07-21 19:25:18 +02004319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 }
4321 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004322 if (l > stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004324 // truncate, remove n bytes of text at the start
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 if (has_mbyte)
4326 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004327 // Find the first character that should be included.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 n = 0;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004329 while (l >= stl_items[stl_groupitem[groupdepth]].stl_maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 {
4331 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004332 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 }
4334 }
4335 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004336 n = (long)(p - t) - stl_items[stl_groupitem[groupdepth]]
4337 .stl_maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338
4339 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004340 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 p = p - n + 1;
Bram Moolenaar13505972019-01-24 15:04:48 +01004342
4343 // Fill up space left over by half a double-wide char.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004344 while (++l < stl_items[stl_groupitem[groupdepth]].stl_minwid)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004345 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346
Bram Moolenaarc667da52019-11-30 20:52:27 +01004347 // correct the start of the items for the truncation
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004348 for (l = stl_groupitem[groupdepth] + 1; l < curitem; l++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004350 stl_items[l].stl_start -= n;
4351 if (stl_items[l].stl_start < t)
4352 stl_items[l].stl_start = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 }
4354 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004355 else if (abs(stl_items[stl_groupitem[groupdepth]].stl_minwid) > l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004357 // fill
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004358 n = stl_items[stl_groupitem[groupdepth]].stl_minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 if (n < 0)
4360 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004361 // fill by appending characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 n = 0 - n;
4363 while (l++ < n && p + 1 < out + outlen)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004364 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 }
4366 else
4367 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004368 // fill by inserting characters
Bram Moolenaar008bff92021-03-04 21:55:58 +01004369 l = (n - l) * MB_CHAR2LEN(fillchar);
4370 mch_memmove(t + l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004372 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 p += l;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004374 for (n = stl_groupitem[groupdepth] + 1; n < curitem; n++)
4375 stl_items[n].stl_start += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 for ( ; l > 0; l--)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004377 MB_CHAR2BYTES(fillchar, t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 }
4379 }
4380 continue;
4381 }
4382 minwid = 0;
4383 maxwid = 9999;
4384 zeropad = FALSE;
4385 l = 1;
4386 if (*s == '0')
4387 {
4388 s++;
4389 zeropad = TRUE;
4390 }
4391 if (*s == '-')
4392 {
4393 s++;
4394 l = -1;
4395 }
4396 if (VIM_ISDIGIT(*s))
4397 {
4398 minwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004399 if (minwid < 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 minwid = 0;
4401 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004402 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004404 stl_items[curitem].stl_type = Highlight;
4405 stl_items[curitem].stl_start = p;
4406 stl_items[curitem].stl_minwid = minwid > 9 ? 1 : minwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 s++;
4408 curitem++;
4409 continue;
4410 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004411 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4412 {
4413 if (*s == STL_TABCLOSENR)
4414 {
4415 if (minwid == 0)
4416 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004417 // %X ends the close label, go back to the previously
4418 // define tab label nr.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004419 for (n = curitem - 1; n >= 0; --n)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004420 if (stl_items[n].stl_type == TabPage
4421 && stl_items[n].stl_minwid >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004422 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004423 minwid = stl_items[n].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004424 break;
4425 }
4426 }
4427 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01004428 // close nrs are stored as negative values
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004429 minwid = - minwid;
4430 }
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004431 stl_items[curitem].stl_type = TabPage;
4432 stl_items[curitem].stl_start = p;
4433 stl_items[curitem].stl_minwid = minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004434 s++;
4435 curitem++;
4436 continue;
4437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 if (*s == '.')
4439 {
4440 s++;
4441 if (VIM_ISDIGIT(*s))
4442 {
4443 maxwid = (int)getdigits(&s);
Bram Moolenaarc667da52019-11-30 20:52:27 +01004444 if (maxwid <= 0) // overflow
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 maxwid = 50;
4446 }
4447 }
4448 minwid = (minwid > 50 ? 50 : minwid) * l;
4449 if (*s == '(')
4450 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004451 stl_groupitem[groupdepth++] = curitem;
4452 stl_items[curitem].stl_type = Group;
4453 stl_items[curitem].stl_start = p;
4454 stl_items[curitem].stl_minwid = minwid;
4455 stl_items[curitem].stl_maxwid = maxwid;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 s++;
4457 curitem++;
4458 continue;
4459 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004460#ifdef FEAT_EVAL
4461 // Denotes end of expanded %{} block
4462 if (*s == '}' && evaldepth > 0)
4463 {
4464 s++;
4465 evaldepth--;
4466 continue;
4467 }
4468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 if (vim_strchr(STL_ALL, *s) == NULL)
4470 {
4471 s++;
4472 continue;
4473 }
4474 opt = *s++;
4475
Bram Moolenaarc667da52019-11-30 20:52:27 +01004476 // OK - now for the real work
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 base = 'D';
4478 itemisflag = FALSE;
4479 fillable = TRUE;
4480 num = -1;
4481 str = NULL;
4482 switch (opt)
4483 {
4484 case STL_FILEPATH:
4485 case STL_FULLPATH:
4486 case STL_FILENAME:
Bram Moolenaarc667da52019-11-30 20:52:27 +01004487 fillable = FALSE; // don't change ' ' to fillchar
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004489 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 else
4491 {
4492 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004493 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4495 }
4496 trans_characters(NameBuff, MAXPATHL);
4497 if (opt != STL_FILENAME)
4498 str = NameBuff;
4499 else
4500 str = gettail(NameBuff);
4501 break;
4502
Bram Moolenaarc667da52019-11-30 20:52:27 +01004503 case STL_VIM_EXPR: // '{'
shadmansaleh30e3de22021-05-15 17:23:28 +02004504 {
4505#ifdef FEAT_EVAL
4506 char_u *block_start = s - 1;
4507#endif
4508 int reevaluate = (*s == '%');
4509
4510 if (reevaluate)
4511 s++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 itemisflag = TRUE;
4513 t = p;
shadmansaleh30e3de22021-05-15 17:23:28 +02004514 while ((*s != '}' || (reevaluate && s[-1] != '%'))
4515 && *s != NUL && p + 1 < out + outlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 *p++ = *s++;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004517 if (*s != '}') // missing '}' or out of space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 break;
4519 s++;
shadmansaleh30e3de22021-05-15 17:23:28 +02004520 if (reevaluate)
4521 p[-1] = 0; // remove the % at the end of %{% expr %}
4522 else
4523 *p = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 p = t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525#ifdef FEAT_EVAL
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004526 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp),
4527 "%d", curbuf->b_fnum);
4528 set_internal_string_var((char_u *)"g:actual_curbuf", buf_tmp);
4529 vim_snprintf((char *)win_tmp, sizeof(win_tmp), "%d", curwin->w_id);
4530 set_internal_string_var((char_u *)"g:actual_curwin", win_tmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004532 save_curbuf = curbuf;
4533 save_curwin = curwin;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004534 save_VIsual_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 curwin = wp;
4536 curbuf = wp->w_buffer;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004537 // Visual mode is only valid in the current window.
4538 if (curwin != save_curwin)
4539 VIsual_active = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004541 str = eval_to_string_safe(p, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004543 curwin = save_curwin;
4544 curbuf = save_curbuf;
Bram Moolenaardee50a52019-11-30 15:05:22 +01004545 VIsual_active = save_VIsual_active;
Bram Moolenaar01824652005-01-31 18:58:23 +00004546 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004547 do_unlet((char_u *)"g:actual_curwin", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548
4549 if (str != NULL && *str != 0)
4550 {
4551 if (*skipdigits(str) == NUL)
4552 {
4553 num = atoi((char *)str);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004554 VIM_CLEAR(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 itemisflag = FALSE;
4556 }
4557 }
shadmansaleh30e3de22021-05-15 17:23:28 +02004558
4559 // If the output of the expression needs to be evaluated
4560 // replace the %{} block with the result of evaluation
4561 if (reevaluate && str != NULL && *str != 0
4562 && strchr((const char *)str, '%') != NULL
4563 && evaldepth < MAX_STL_EVAL_DEPTH)
4564 {
4565 size_t parsed_usefmt = (size_t)(block_start - usefmt);
4566 size_t str_length = strlen((const char *)str);
4567 size_t fmt_length = strlen((const char *)s);
4568 size_t new_fmt_len = parsed_usefmt
4569 + str_length + fmt_length + 3;
4570 char_u *new_fmt = (char_u *)alloc(new_fmt_len * sizeof(char_u));
4571 char_u *new_fmt_p = new_fmt;
4572
4573 new_fmt_p = (char_u *)memcpy(new_fmt_p, usefmt, parsed_usefmt)
4574 + parsed_usefmt;
4575 new_fmt_p = (char_u *)memcpy(new_fmt_p , str, str_length)
4576 + str_length;
4577 new_fmt_p = (char_u *)memcpy(new_fmt_p, "%}", 2) + 2;
4578 new_fmt_p = (char_u *)memcpy(new_fmt_p , s, fmt_length)
4579 + fmt_length;
4580 *new_fmt_p = 0;
4581 new_fmt_p = NULL;
4582
4583 if (usefmt != fmt)
4584 vim_free(usefmt);
4585 VIM_CLEAR(str);
4586 usefmt = new_fmt;
4587 s = usefmt + parsed_usefmt;
4588 evaldepth++;
4589 continue;
4590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591#endif
4592 break;
shadmansaleh30e3de22021-05-15 17:23:28 +02004593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 case STL_LINE:
4595 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4596 ? 0L : (long)(wp->w_cursor.lnum);
4597 break;
4598
4599 case STL_NUMLINES:
4600 num = wp->w_buffer->b_ml.ml_line_count;
4601 break;
4602
4603 case STL_COLUMN:
4604 num = !(State & INSERT) && empty_line
4605 ? 0 : (int)wp->w_cursor.col + 1;
4606 break;
4607
4608 case STL_VIRTCOL:
4609 case STL_VIRTCOL_ALT:
Bram Moolenaarc667da52019-11-30 20:52:27 +01004610 // In list mode virtcol needs to be recomputed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 virtcol = wp->w_virtcol;
Bram Moolenaareed9d462021-02-15 20:38:25 +01004612 if (wp->w_p_list && wp->w_lcs_chars.tab1 == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 {
4614 wp->w_p_list = FALSE;
4615 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
4616 wp->w_p_list = TRUE;
4617 }
4618 ++virtcol;
Bram Moolenaarc667da52019-11-30 20:52:27 +01004619 // Don't display %V if it's the same as %c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 if (opt == STL_VIRTCOL_ALT
4621 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
4622 ? 0 : (int)wp->w_cursor.col + 1)))
4623 break;
4624 num = (long)virtcol;
4625 break;
4626
4627 case STL_PERCENTAGE:
4628 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4629 (long)wp->w_buffer->b_ml.ml_line_count);
4630 break;
4631
4632 case STL_ALTPERCENT:
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004633 str = buf_tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004634 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 break;
4636
4637 case STL_ARGLISTSTAT:
4638 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004639 buf_tmp[0] = 0;
4640 if (append_arg_number(wp, buf_tmp, (int)sizeof(buf_tmp), FALSE))
4641 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 break;
4643
4644 case STL_KEYMAP:
4645 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004646 if (get_keymap_str(wp, (char_u *)"<%s>", buf_tmp, TMPLEN))
4647 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 break;
4649 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004650#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004651 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652#else
4653 num = 0;
4654#endif
4655 break;
4656
4657 case STL_BUFNO:
4658 num = wp->w_buffer->b_fnum;
4659 break;
4660
4661 case STL_OFFSET_X:
4662 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004663 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 case STL_OFFSET:
4665#ifdef FEAT_BYTEOFF
4666 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4667 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4668 0L : l + 1 + (!(State & INSERT) && empty_line ?
4669 0 : (int)wp->w_cursor.col);
4670#endif
4671 break;
4672
4673 case STL_BYTEVAL_X:
4674 base = 'X';
Bram Moolenaarc667da52019-11-30 20:52:27 +01004675 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004677 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 if (num == NL)
4679 num = 0;
4680 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4681 num = NL;
4682 break;
4683
4684 case STL_ROFLAG:
4685 case STL_ROFLAG_ALT:
4686 itemisflag = TRUE;
4687 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004688 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 break;
4690
4691 case STL_HELPFLAG:
4692 case STL_HELPFLAG_ALT:
4693 itemisflag = TRUE;
4694 if (wp->w_buffer->b_help)
4695 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004696 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 break;
4698
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 case STL_FILETYPE:
4700 if (*wp->w_buffer->b_p_ft != NUL
4701 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4702 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004703 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), "[%s]",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004704 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004705 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 }
4707 break;
4708
4709 case STL_FILETYPE_ALT:
4710 itemisflag = TRUE;
4711 if (*wp->w_buffer->b_p_ft != NUL
4712 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4713 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004714 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), ",%s",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004715 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004716 for (t = buf_tmp; *t != 0; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 *t = TOUPPER_LOC(*t);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004718 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 }
4720 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721
Bram Moolenaar4033c552017-09-16 20:54:51 +02004722#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 case STL_PREVIEWFLAG:
4724 case STL_PREVIEWFLAG_ALT:
4725 itemisflag = TRUE;
4726 if (wp->w_p_pvw)
4727 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4728 : _("[Preview]"));
4729 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004730
4731 case STL_QUICKFIX:
4732 if (bt_quickfix(wp->w_buffer))
4733 str = (char_u *)(wp->w_llist_ref
4734 ? _(msg_loclist)
4735 : _(msg_qflist));
4736 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737#endif
4738
4739 case STL_MODIFIED:
4740 case STL_MODIFIED_ALT:
4741 itemisflag = TRUE;
4742 switch ((opt == STL_MODIFIED_ALT)
4743 + bufIsChanged(wp->w_buffer) * 2
4744 + (!wp->w_buffer->b_p_ma) * 4)
4745 {
4746 case 2: str = (char_u *)"[+]"; break;
4747 case 3: str = (char_u *)",+"; break;
4748 case 4: str = (char_u *)"[-]"; break;
4749 case 5: str = (char_u *)",-"; break;
4750 case 6: str = (char_u *)"[+-]"; break;
4751 case 7: str = (char_u *)",+-"; break;
4752 }
4753 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004754
4755 case STL_HIGHLIGHT:
4756 t = s;
4757 while (*s != '#' && *s != NUL)
4758 ++s;
4759 if (*s == '#')
4760 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004761 stl_items[curitem].stl_type = Highlight;
4762 stl_items[curitem].stl_start = p;
4763 stl_items[curitem].stl_minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004764 curitem++;
4765 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004766 if (*s != NUL)
4767 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004768 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 }
4770
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004771 stl_items[curitem].stl_start = p;
4772 stl_items[curitem].stl_type = Normal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 if (str != NULL && *str)
4774 {
4775 t = str;
4776 if (itemisflag)
4777 {
4778 if ((t[0] && t[1])
4779 && ((!prevchar_isitem && *t == ',')
4780 || (prevchar_isflag && *t == ' ')))
4781 t++;
4782 prevchar_isflag = TRUE;
4783 }
4784 l = vim_strsize(t);
4785 if (l > 0)
4786 prevchar_isitem = TRUE;
4787 if (l > maxwid)
4788 {
4789 while (l >= maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 if (has_mbyte)
4791 {
4792 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004793 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 }
4795 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 l -= byte2cells(*t++);
4797 if (p + 1 >= out + outlen)
4798 break;
4799 *p++ = '<';
4800 }
4801 if (minwid > 0)
4802 {
4803 for (; l < minwid && p + 1 < out + outlen; l++)
4804 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004805 // Don't put a "-" in front of a digit.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4807 *p++ = ' ';
4808 else
Bram Moolenaar008bff92021-03-04 21:55:58 +01004809 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 }
4811 minwid = 0;
4812 }
4813 else
4814 minwid *= -1;
Bram Moolenaar008bff92021-03-04 21:55:58 +01004815 for (; *t && p + 1 < out + outlen; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004817 // Change a space by fillchar, unless fillchar is '-' and a
4818 // digit follows.
Bram Moolenaar008bff92021-03-04 21:55:58 +01004819 if (fillable && *t == ' '
4820 && (!VIM_ISDIGIT(*(t + 1)) || fillchar != '-'))
4821 MB_CHAR2BYTES(fillchar, p);
4822 else
4823 *p++ = *t;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 }
4825 for (; l < minwid && p + 1 < out + outlen; l++)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004826 MB_CHAR2BYTES(fillchar, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 }
4828 else if (num >= 0)
4829 {
4830 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4831 char_u nstr[20];
4832
4833 if (p + 20 >= out + outlen)
Bram Moolenaarc667da52019-11-30 20:52:27 +01004834 break; // not sufficient space
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 prevchar_isitem = TRUE;
4836 t = nstr;
4837 if (opt == STL_VIRTCOL_ALT)
4838 {
4839 *t++ = '-';
4840 minwid--;
4841 }
4842 *t++ = '%';
4843 if (zeropad)
4844 *t++ = '0';
4845 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004846 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 *t = 0;
4848
4849 for (n = num, l = 1; n >= nbase; n /= nbase)
4850 l++;
4851 if (opt == STL_VIRTCOL_ALT)
4852 l++;
4853 if (l > maxwid)
4854 {
4855 l += 2;
4856 n = l - maxwid;
4857 while (l-- > maxwid)
4858 num /= nbase;
4859 *t++ = '>';
4860 *t++ = '%';
4861 *t = t[-3];
4862 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004863 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4864 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 }
4866 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004867 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4868 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 p += STRLEN(p);
4870 }
4871 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004872 stl_items[curitem].stl_type = Empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873
4874 if (opt == STL_VIM_EXPR)
4875 vim_free(str);
4876
4877 if (num >= 0 || (!itemisflag && str && *str))
Bram Moolenaarc667da52019-11-30 20:52:27 +01004878 prevchar_isflag = FALSE; // Item not NULL, but not a flag
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 curitem++;
4880 }
4881 *p = NUL;
4882 itemcnt = curitem;
4883
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004884#ifdef FEAT_EVAL
4885 if (usefmt != fmt)
4886 vim_free(usefmt);
4887#endif
4888
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 width = vim_strsize(out);
4890 if (maxwidth > 0 && width > maxwidth)
4891 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004892 // Result is too long, must truncate somewhere.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 l = 0;
4894 if (itemcnt == 0)
4895 s = out;
4896 else
4897 {
4898 for ( ; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004899 if (stl_items[l].stl_type == Trunc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004901 // Truncate at %< item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004902 s = stl_items[l].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 break;
4904 }
4905 if (l == itemcnt)
4906 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004907 // No %< item, truncate first item.
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004908 s = stl_items[0].stl_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 l = 0;
4910 }
4911 }
4912
4913 if (width - vim_strsize(s) >= maxwidth)
4914 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004915 // Truncation mark is beyond max length
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 if (has_mbyte)
4917 {
4918 s = out;
4919 width = 0;
4920 for (;;)
4921 {
4922 width += ptr2cells(s);
4923 if (width >= maxwidth)
4924 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004925 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01004927 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 while (++width < maxwidth)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004929 MB_CHAR2BYTES(fillchar, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 }
4931 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 s = out + maxwidth - 1;
4933 for (l = 0; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004934 if (stl_items[l].stl_start > s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 break;
4936 itemcnt = l;
4937 *s++ = '>';
4938 *s = 0;
4939 }
4940 else
4941 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 if (has_mbyte)
4943 {
4944 n = 0;
4945 while (width >= maxwidth)
4946 {
4947 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004948 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 }
4950 }
4951 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 n = width - maxwidth + 1;
4953 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004954 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 *s = '<';
4956
Bram Moolenaarc667da52019-11-30 20:52:27 +01004957 // Fill up for half a double-wide character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 while (++width < maxwidth)
4959 {
4960 s = s + STRLEN(s);
Bram Moolenaar008bff92021-03-04 21:55:58 +01004961 MB_CHAR2BYTES(fillchar, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 *s = NUL;
4963 }
4964
Bram Moolenaarc667da52019-11-30 20:52:27 +01004965 --n; // count the '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 for (; l < itemcnt; l++)
4967 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004968 if (stl_items[l].stl_start - n >= s)
4969 stl_items[l].stl_start -= n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 else
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004971 stl_items[l].stl_start = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 }
4973 }
4974 width = maxwidth;
4975 }
4976 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4977 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01004978 // Apply STL_MIDDLE if any
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 for (l = 0; l < itemcnt; l++)
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004980 if (stl_items[l].stl_type == Middle)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 break;
4982 if (l < itemcnt)
4983 {
Bram Moolenaar008bff92021-03-04 21:55:58 +01004984 int middlelength = (maxwidth - width) * MB_CHAR2LEN(fillchar);
4985 p = stl_items[l].stl_start + middlelength;
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004986 STRMOVE(p, stl_items[l].stl_start);
Bram Moolenaar008bff92021-03-04 21:55:58 +01004987 for (s = stl_items[l].stl_start; s < p;)
4988 MB_CHAR2BYTES(fillchar, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 for (l++; l < itemcnt; l++)
Bram Moolenaar008bff92021-03-04 21:55:58 +01004990 stl_items[l].stl_start += middlelength;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 width = maxwidth;
4992 }
4993 }
4994
Bram Moolenaarc667da52019-11-30 20:52:27 +01004995 // Store the info about highlighting.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004996 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01004998 *hltab = stl_hltab;
4999 sp = stl_hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 for (l = 0; l < itemcnt; l++)
5001 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005002 if (stl_items[l].stl_type == Highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005004 sp->start = stl_items[l].stl_start;
5005 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005006 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 }
5008 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005009 sp->start = NULL;
5010 sp->userhl = 0;
5011 }
5012
Bram Moolenaarc667da52019-11-30 20:52:27 +01005013 // Store the info about tab pages labels.
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005014 if (tabtab != NULL)
5015 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005016 *tabtab = stl_tabtab;
5017 sp = stl_tabtab;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005018 for (l = 0; l < itemcnt; l++)
5019 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005020 if (stl_items[l].stl_type == TabPage)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005021 {
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005022 sp->start = stl_items[l].stl_start;
5023 sp->userhl = stl_items[l].stl_minwid;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005024 sp++;
5025 }
5026 }
5027 sp->start = NULL;
5028 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 }
5030
Bram Moolenaarc667da52019-11-30 20:52:27 +01005031 // When inside update_screen we do not want redrawing a stausline, ruler,
5032 // title, etc. to trigger another redraw, it may cause an endless loop.
Bram Moolenaar65ed1362017-09-30 16:00:14 +02005033 if (updating_screen)
5034 {
5035 must_redraw = save_must_redraw;
5036 curwin->w_redr_type = save_redr_type;
5037 }
Bram Moolenaarba2929b2017-09-08 13:59:21 +02005038
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 return width;
5040}
Bram Moolenaarc667da52019-11-30 20:52:27 +01005041#endif // FEAT_STL_OPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042
Bram Moolenaarba6c0522006-02-25 21:45:02 +00005043#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
5044 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005046 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
5047 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 */
5049 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005050get_rel_pos(
5051 win_T *wp,
5052 char_u *buf,
5053 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005055 long above; // number of lines above window
5056 long below; // number of lines below window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057
Bram Moolenaarc667da52019-11-30 20:52:27 +01005058 if (buflen < 3) // need at least 3 chars for writing
Bram Moolenaar0027c212015-01-07 13:31:52 +01005059 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 above = wp->w_topline - 1;
5061#ifdef FEAT_DIFF
5062 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02005063 if (wp->w_topline == 1 && wp->w_topfill >= 1)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005064 above = 0; // All buffer lines are displayed and there is an
5065 // indication of filler lines, that can be considered
5066 // seeing all lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067#endif
5068 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
5069 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005070 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
5071 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005073 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005075 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 ? (int)(above / ((above + below) / 100L))
5077 : (int)(above * 100L / (above + below)));
5078}
5079#endif
5080
5081/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005082 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 * Return TRUE if it was appended.
5084 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005085 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005086append_arg_number(
5087 win_T *wp,
5088 char_u *buf,
5089 int buflen,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005090 int add_file) // Add "file" before the arg number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091{
5092 char_u *p;
5093
Bram Moolenaarc667da52019-11-30 20:52:27 +01005094 if (ARGCOUNT <= 1) // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 return FALSE;
5096
Bram Moolenaarc667da52019-11-30 20:52:27 +01005097 p = buf + STRLEN(buf); // go to the end of the buffer
5098 if (p - buf + 35 >= buflen) // getting too long
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 return FALSE;
5100 *p++ = ' ';
5101 *p++ = '(';
5102 if (add_file)
5103 {
5104 STRCPY(p, "file ");
5105 p += 5;
5106 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005107 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
5108 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
5110 return TRUE;
5111}
5112
5113/*
5114 * If fname is not a full path, make it a full path.
5115 * Returns pointer to allocated memory (NULL for failure).
5116 */
5117 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005118fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119{
5120 /*
5121 * Force expanding the path always for Unix, because symbolic links may
5122 * mess up the full path name, even though it starts with a '/'.
5123 * Also expand when there is ".." in the file name, try to remove it,
5124 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00005125 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 * For MS-Windows also expand names like "longna~1" to "longname".
5127 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00005128#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 return FullName_save(fname, TRUE);
5130#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00005131 if (!vim_isAbsName(fname)
5132 || strstr((char *)fname, "..") != NULL
5133 || strstr((char *)fname, "//") != NULL
5134# ifdef BACKSLASH_IN_FILENAME
5135 || strstr((char *)fname, "\\\\") != NULL
5136# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005137# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00005139# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 )
5141 return FullName_save(fname, FALSE);
5142
5143 fname = vim_strsave(fname);
5144
Bram Moolenaar9b942202007-10-03 12:31:33 +00005145# ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01005146 if (fname != NULL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005147 fname_case(fname, 0); // set correct case for file name
Bram Moolenaar9b942202007-10-03 12:31:33 +00005148# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149
5150 return fname;
5151#endif
5152}
5153
5154/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005155 * Make "*ffname" a full file name, set "*sfname" to "*ffname" if not NULL.
5156 * "*ffname" becomes a pointer to allocated memory (or NULL).
5157 * When resolving a link both "*sfname" and "*ffname" will point to the same
5158 * allocated memory.
5159 * The "*ffname" and "*sfname" pointer values on call will not be freed.
Bram Moolenaar32aa1022019-11-02 22:54:41 +01005160 * Note that the resulting "*ffname" pointer should be considered not allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005163fname_expand(
5164 buf_T *buf UNUSED,
5165 char_u **ffname,
5166 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005168 if (*ffname == NULL) // no file name given, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 return;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005170 if (*sfname == NULL) // no short file name given, use ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 *sfname = *ffname;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005172 *ffname = fix_fname(*ffname); // expand to full path
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173
5174#ifdef FEAT_SHORTCUT
5175 if (!buf->b_p_bin)
5176 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005177 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02005179 // If the file name is a shortcut file, use the file it links to.
Bram Moolenaardce1e892019-02-10 23:18:53 +01005180 rfname = mch_resolve_path(*ffname, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00005181 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 {
5183 vim_free(*ffname);
5184 *ffname = rfname;
5185 *sfname = rfname;
5186 }
5187 }
5188#endif
5189}
5190
5191/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 * Open a window for a number of buffers.
5193 */
5194 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005195ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196{
5197 buf_T *buf;
5198 win_T *wp, *wpnext;
5199 int split_ret = OK;
5200 int p_ea_save;
5201 int open_wins = 0;
5202 int r;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005203 int count; // Maximum number of windows to open.
5204 int all; // When TRUE also load inactive buffers.
Bram Moolenaare1004402020-10-24 20:49:43 +02005205 int had_tab = cmdmod.cmod_tab;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005206 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207
Bram Moolenaarc667da52019-11-30 20:52:27 +01005208 if (eap->addr_count == 0) // make as many windows as possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 count = 9999;
5210 else
Bram Moolenaarc667da52019-11-30 20:52:27 +01005211 count = eap->line2; // make as many windows as specified
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5213 all = FALSE;
5214 else
5215 all = TRUE;
5216
5217 setpcmark();
5218
5219#ifdef FEAT_GUI
5220 need_mouse_correct = TRUE;
5221#endif
5222
5223 /*
5224 * Close superfluous windows (two windows for the same buffer).
5225 * Also close windows that are not full-width.
5226 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005227 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005228 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005229 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005231 tpnext = curtab->tp_next;
5232 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005234 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005235 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1004402020-10-24 20:49:43 +02005236 || ((cmdmod.cmod_split & WSP_VERT)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005237 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005238 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005239 : wp->w_width != Columns)
Bram Moolenaar4033c552017-09-16 20:54:51 +02005240 || (had_tab > 0 && wp != firstwin)) && !ONE_WINDOW
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005241 && !(wp->w_closing || wp->w_buffer->b_locked > 0))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005242 {
5243 win_close(wp, FALSE);
Bram Moolenaarc667da52019-11-30 20:52:27 +01005244 wpnext = firstwin; // just in case an autocommand does
5245 // something strange with windows
5246 tpnext = first_tabpage; // start all over...
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005247 open_wins = 0;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005248 }
5249 else
5250 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005252
Bram Moolenaarc667da52019-11-30 20:52:27 +01005253 // Without the ":tab" modifier only do the current tab page.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005254 if (had_tab == 0 || tpnext == NULL)
5255 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005256 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 }
5258
5259 /*
5260 * Go through the buffer list. When a buffer doesn't have a window yet,
5261 * open one. Otherwise move the window to the right position.
5262 * Watch out for autocommands that delete buffers or windows!
5263 */
Bram Moolenaarc667da52019-11-30 20:52:27 +01005264 // Don't execute Win/Buf Enter/Leave autocommands here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 ++autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 win_enter(lastwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5269 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005270 // Check if this buffer needs a window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5272 continue;
5273
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005274 if (had_tab != 0)
5275 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005276 // With the ":tab" modifier don't move the window.
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005277 if (buf->b_nwindows > 0)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005278 wp = lastwin; // buffer has a window, skip it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005279 else
5280 wp = NULL;
5281 }
5282 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005283 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005284 // Check if this buffer already has a window
Bram Moolenaar29323592016-07-24 22:04:11 +02005285 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005286 if (wp->w_buffer == buf)
5287 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005288 // If the buffer already has a window, move it
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005289 if (wp != NULL)
5290 win_move_after(wp, curwin);
5291 }
5292
5293 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005295 bufref_T bufref;
5296
5297 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005298
Bram Moolenaarc667da52019-11-30 20:52:27 +01005299 // Split the window and put the buffer in it
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 p_ea_save = p_ea;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005301 p_ea = TRUE; // use space from all windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5303 ++open_wins;
5304 p_ea = p_ea_save;
5305 if (split_ret == FAIL)
5306 continue;
5307
Bram Moolenaarc667da52019-11-30 20:52:27 +01005308 // Open the buffer in this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005311 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005313 // autocommands deleted the buffer!!!
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 swap_exists_action = SEA_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 break;
5316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 if (swap_exists_action == SEA_QUIT)
5318 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005319#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005320 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005321
Bram Moolenaarc667da52019-11-30 20:52:27 +01005322 // Reset the error/interrupt/exception state here so that
5323 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005324 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005325#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005326
Bram Moolenaarc667da52019-11-30 20:52:27 +01005327 // User selected Quit at ATTENTION prompt; close this window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 win_close(curwin, TRUE);
5329 --open_wins;
5330 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005331 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005332
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005333#if defined(FEAT_EVAL)
Bram Moolenaarc667da52019-11-30 20:52:27 +01005334 // Restore the error/interrupt/exception state if not
5335 // discarded by a new aborting error, interrupt, or uncaught
5336 // exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005337 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 }
5340 else
5341 handle_swap_exists(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 }
5343
5344 ui_breakcheck();
5345 if (got_int)
5346 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005347 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 break;
5349 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005350#ifdef FEAT_EVAL
Bram Moolenaarc667da52019-11-30 20:52:27 +01005351 // Autocommands deleted the buffer or aborted script processing!!!
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005352 if (aborting())
5353 break;
5354#endif
Bram Moolenaarc667da52019-11-30 20:52:27 +01005355 // When ":tab" was used open a new tab for a new window repeatedly.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005356 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
Bram Moolenaare1004402020-10-24 20:49:43 +02005357 cmdmod.cmod_tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 --autocmd_no_enter;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005360 win_enter(firstwin, FALSE); // back to first window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362
5363 /*
5364 * Close superfluous windows.
5365 */
5366 for (wp = lastwin; open_wins > count; )
5367 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005368 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 || autowrite(wp->w_buffer, FALSE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 if (!win_valid(wp))
5371 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005372 // BufWrite Autocommands made the window invalid, start over
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 wp = lastwin;
5374 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005375 else if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005377 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 --open_wins;
5379 wp = lastwin;
5380 }
5381 else
5382 {
5383 wp = wp->w_prev;
5384 if (wp == NULL)
5385 break;
5386 }
5387 }
5388}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005391static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005392
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393/*
5394 * do_modelines() - process mode lines for the current file
5395 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005396 * "flags" can be:
5397 * OPT_WINONLY only set options local to window
5398 * OPT_NOWIN don't set options local to window
5399 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 * Returns immediately if the "ml" option isn't set.
5401 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005403do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005405 linenr_T lnum;
5406 int nmlines;
5407 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408
5409 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5410 return;
5411
Bram Moolenaarc667da52019-11-30 20:52:27 +01005412 // Disallow recursive entry here. Can happen when executing a modeline
5413 // triggers an autocommand, which reloads modelines with a ":do".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 if (entered)
5415 return;
5416
5417 ++entered;
Hu Jialun9dcd3492021-08-28 20:42:50 +02005418 for (lnum = 1; curbuf->b_p_ml && lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005420 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 nmlines = 0;
5422
Hu Jialun9dcd3492021-08-28 20:42:50 +02005423 for (lnum = curbuf->b_ml.ml_line_count; curbuf->b_p_ml && lnum > 0 && lnum > nmlines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005425 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 nmlines = 0;
5427 --entered;
5428}
5429
Bram Moolenaarc667da52019-11-30 20:52:27 +01005430#include "version.h" // for version number
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431
5432/*
5433 * chk_modeline() - check a single line for a mode string
5434 * Return FAIL if an error encountered.
5435 */
5436 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005437chk_modeline(
5438 linenr_T lnum,
Bram Moolenaarc667da52019-11-30 20:52:27 +01005439 int flags) // Same as for do_modelines().
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440{
5441 char_u *s;
5442 char_u *e;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005443 char_u *linecopy; // local copy of any modeline found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 int prev;
5445 int vers;
5446 int end;
5447 int retval = OK;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005448 sctx_T save_current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005449
Bram Moolenaare31ee862020-01-07 20:59:34 +01005450 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451
5452 prev = -1;
5453 for (s = ml_get(lnum); *s != NUL; ++s)
5454 {
5455 if (prev == -1 || vim_isspace(prev))
5456 {
5457 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5458 || STRNCMP(s, "vi:", (size_t)3) == 0)
5459 break;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005460 // Accept both "vim" and "Vim".
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005461 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 {
5463 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5464 e = s + 4;
5465 else
5466 e = s + 3;
5467 vers = getdigits(&e);
5468 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005469 && (s[0] != 'V'
5470 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 && (s[3] == ':'
5472 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5473 || (VIM_VERSION_100 < vers && s[3] == '<')
5474 || (VIM_VERSION_100 > vers && s[3] == '>')
5475 || (VIM_VERSION_100 == vers && s[3] == '=')))
5476 break;
5477 }
5478 }
5479 prev = *s;
5480 }
5481
5482 if (*s)
5483 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005484 do // skip over "ex:", "vi:" or "vim:"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 ++s;
5486 while (s[-1] != ':');
5487
Bram Moolenaarc667da52019-11-30 20:52:27 +01005488 s = linecopy = vim_strsave(s); // copy the line, it will change
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 if (linecopy == NULL)
5490 return FAIL;
5491
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005492 // prepare for emsg()
5493 estack_push(ETYPE_MODELINE, (char_u *)"modelines", lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01005494 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495
5496 end = FALSE;
5497 while (end == FALSE)
5498 {
5499 s = skipwhite(s);
5500 if (*s == NUL)
5501 break;
5502
5503 /*
5504 * Find end of set command: ':' or end of line.
5505 * Skip over "\:", replacing it with ":".
5506 */
5507 for (e = s; *e != ':' && *e != NUL; ++e)
5508 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005509 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 if (*e == NUL)
5511 end = TRUE;
5512
5513 /*
5514 * If there is a "set" command, require a terminating ':' and
5515 * ignore the stuff after the ':'.
5516 * "vi:set opt opt opt: foo" -- foo not interpreted
5517 * "vi:opt opt opt: foo" -- foo interpreted
5518 * Accept "se" for compatibility with Elvis.
5519 */
5520 if (STRNCMP(s, "set ", (size_t)4) == 0
5521 || STRNCMP(s, "se ", (size_t)3) == 0)
5522 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005523 if (*e != ':') // no terminating ':'?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 break;
5525 end = TRUE;
5526 s = vim_strchr(s, ' ') + 1;
5527 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005528 *e = NUL; // truncate the set command
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529
Bram Moolenaarc667da52019-11-30 20:52:27 +01005530 if (*s != NUL) // skip over an empty "::"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 {
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005532 int secure_save = secure;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005533
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005534 save_current_sctx = current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005535 current_sctx.sc_version = 1;
5536#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005537 current_sctx.sc_sid = SID_MODELINE;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005538 current_sctx.sc_seq = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005539 current_sctx.sc_lnum = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01005541
Bram Moolenaar5958f952018-11-20 04:25:21 +01005542 // Make sure no risky things are executed as a side effect.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01005543 secure = 1;
Bram Moolenaar5958f952018-11-20 04:25:21 +01005544
Bram Moolenaara3227e22006-03-08 21:32:40 +00005545 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar5958f952018-11-20 04:25:21 +01005546
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005547 secure = secure_save;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005548 current_sctx = save_current_sctx;
Bram Moolenaarc667da52019-11-30 20:52:27 +01005549 if (retval == FAIL) // stop if error found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 break;
5551 }
Bram Moolenaarc667da52019-11-30 20:52:27 +01005552 s = e + 1; // advance to next part
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 }
5554
Bram Moolenaare31ee862020-01-07 20:59:34 +01005555 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005556 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 vim_free(linecopy);
5558 }
5559 return retval;
5560}
5561
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005562/*
Bram Moolenaar91335e52018-08-01 17:53:12 +02005563 * Return TRUE if "buf" is a normal buffer, 'buftype' is empty.
5564 */
5565 int
5566bt_normal(buf_T *buf)
5567{
5568 return buf != NULL && buf->b_p_bt[0] == NUL;
5569}
5570
Bram Moolenaar113e1072019-01-20 15:30:40 +01005571#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar91335e52018-08-01 17:53:12 +02005572/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005573 * Return TRUE if "buf" is the quickfix buffer.
5574 */
5575 int
5576bt_quickfix(buf_T *buf)
5577{
5578 return buf != NULL && buf->b_p_bt[0] == 'q';
5579}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005580#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005581
Bram Moolenaar113e1072019-01-20 15:30:40 +01005582#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005583/*
5584 * Return TRUE if "buf" is a terminal buffer.
5585 */
5586 int
5587bt_terminal(buf_T *buf)
5588{
5589 return buf != NULL && buf->b_p_bt[0] == 't';
5590}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005591#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005592
5593/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005594 * Return TRUE if "buf" is a help buffer.
5595 */
5596 int
5597bt_help(buf_T *buf)
5598{
5599 return buf != NULL && buf->b_help;
5600}
5601
5602/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02005603 * Return TRUE if "buf" is a prompt buffer.
5604 */
5605 int
5606bt_prompt(buf_T *buf)
5607{
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005608 return buf != NULL && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'r';
5609}
5610
5611/*
5612 * Return TRUE if "buf" is a buffer for a popup window.
5613 */
5614 int
5615bt_popup(buf_T *buf)
5616{
5617 return buf != NULL && buf->b_p_bt != NULL
5618 && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'o';
Bram Moolenaarf2732452018-06-03 14:47:35 +02005619}
5620
5621/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005622 * Return TRUE if "buf" is a "nofile", "acwrite", "terminal" or "prompt"
5623 * buffer. This means the buffer name is not a file name.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005624 */
5625 int
Bram Moolenaar26910de2019-06-15 19:37:15 +02005626bt_nofilename(buf_T *buf)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005627{
5628 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5629 || buf->b_p_bt[0] == 'a'
Bram Moolenaarf2732452018-06-03 14:47:35 +02005630 || buf->b_p_bt[0] == 't'
5631 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005632}
5633
5634/*
Bram Moolenaar26910de2019-06-15 19:37:15 +02005635 * Return TRUE if "buf" has 'buftype' set to "nofile".
5636 */
5637 int
5638bt_nofile(buf_T *buf)
5639{
5640 return buf != NULL && buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f';
5641}
5642
5643/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005644 * Return TRUE if "buf" is a "nowrite", "nofile", "terminal" or "prompt"
5645 * buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005646 */
5647 int
5648bt_dontwrite(buf_T *buf)
5649{
Bram Moolenaarf2732452018-06-03 14:47:35 +02005650 return buf != NULL && (buf->b_p_bt[0] == 'n'
Bram Moolenaar8133cc62020-10-26 21:05:27 +01005651 || buf->b_p_bt[0] == 't'
5652 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005653}
5654
Bram Moolenaar113e1072019-01-20 15:30:40 +01005655#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005656 int
5657bt_dontwrite_msg(buf_T *buf)
5658{
5659 if (bt_dontwrite(buf))
5660 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005661 emsg(_("E382: Cannot write, 'buftype' option is set"));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005662 return TRUE;
5663 }
5664 return FALSE;
5665}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005666#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005667
5668/*
5669 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
5670 * and 'bufhidden'.
5671 */
5672 int
5673buf_hide(buf_T *buf)
5674{
Bram Moolenaarc667da52019-11-30 20:52:27 +01005675 // 'bufhidden' overrules 'hidden' and ":hide", check it first
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005676 switch (buf->b_p_bh[0])
5677 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005678 case 'u': // "unload"
5679 case 'w': // "wipe"
5680 case 'd': return FALSE; // "delete"
5681 case 'h': return TRUE; // "hide"
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005682 }
Bram Moolenaare1004402020-10-24 20:49:43 +02005683 return (p_hid || (cmdmod.cmod_flags & CMOD_HIDE));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005684}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685
5686/*
5687 * Return special buffer name.
5688 * Returns NULL when the buffer has a normal file name.
5689 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005690 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005691buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692{
Bram Moolenaar4033c552017-09-16 20:54:51 +02005693#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005695 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005696 /*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005697 * Differentiate between the quickfix and location list buffers using
5698 * the buffer number stored in the global quickfix stack.
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005699 */
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005700 if (buf->b_fnum == qf_stack_get_bufnr())
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005701 return (char_u *)_(msg_qflist);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005702 else
5703 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02005706
Bram Moolenaarc667da52019-11-30 20:52:27 +01005707 // There is no _file_ when 'buftype' is "nofile", b_sfname
5708 // contains the name as specified by the user.
Bram Moolenaar26910de2019-06-15 19:37:15 +02005709 if (bt_nofilename(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 {
Bram Moolenaar21554412017-07-24 21:44:43 +02005711#ifdef FEAT_TERMINAL
5712 if (buf->b_term != NULL)
5713 return term_get_status_text(buf->b_term);
5714#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02005715 if (buf->b_fname != NULL)
5716 return buf->b_fname;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02005717#ifdef FEAT_JOB_CHANNEL
5718 if (bt_prompt(buf))
5719 return (char_u *)_("[Prompt]");
5720#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005721#ifdef FEAT_PROP_POPUP
Bram Moolenaarc6896e22019-05-30 22:32:34 +02005722 if (bt_popup(buf))
5723 return (char_u *)_("[Popup]");
5724#endif
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005725 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 }
Bram Moolenaar21554412017-07-24 21:44:43 +02005727
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 if (buf->b_fname == NULL)
Bram Moolenaar00806bc2020-11-05 19:36:38 +01005729 return buf_get_fname(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 return NULL;
5731}
5732
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733/*
Bram Moolenaar00806bc2020-11-05 19:36:38 +01005734 * Get "buf->b_fname", use "[No Name]" if it is NULL.
5735 */
5736 char_u *
5737buf_get_fname(buf_T *buf)
5738{
5739 if (buf->b_fname == NULL)
5740 return (char_u *)_("[No Name]");
5741 return buf->b_fname;
5742}
5743
5744/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5746 */
5747 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005748set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749{
5750 if (on != curbuf->b_p_bl)
5751 {
5752 curbuf->b_p_bl = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 if (on)
5754 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5755 else
5756 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 }
5758}
5759
5760/*
5761 * Read the file for "buf" again and check if the contents changed.
5762 * Return TRUE if it changed or this could not be checked.
5763 */
5764 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005765buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766{
5767 buf_T *newbuf;
5768 int differ = TRUE;
5769 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 exarg_T ea;
5772
Bram Moolenaarc667da52019-11-30 20:52:27 +01005773 // Allocate a buffer without putting it in the buffer list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5775 if (newbuf == NULL)
5776 return TRUE;
5777
Bram Moolenaarc667da52019-11-30 20:52:27 +01005778 // Force the 'fileencoding' and 'fileformat' to be equal.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 if (prep_exarg(&ea, buf) == FAIL)
5780 {
5781 wipe_buffer(newbuf, FALSE);
5782 return TRUE;
5783 }
5784
Bram Moolenaarc667da52019-11-30 20:52:27 +01005785 // set curwin/curbuf to buf and save a few things
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787
Bram Moolenaar4770d092006-01-12 23:22:24 +00005788 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 && readfile(buf->b_ffname, buf->b_fname,
5790 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5791 &ea, READ_NEW | READ_DUMMY) == OK)
5792 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01005793 // compare the two files line by line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5795 {
5796 differ = FALSE;
5797 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5798 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5799 {
5800 differ = TRUE;
5801 break;
5802 }
5803 }
5804 }
5805 vim_free(ea.cmd);
5806
Bram Moolenaarc667da52019-11-30 20:52:27 +01005807 // restore curwin/curbuf and a few other things
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809
Bram Moolenaarc667da52019-11-30 20:52:27 +01005810 if (curbuf != newbuf) // safety check
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 wipe_buffer(newbuf, FALSE);
5812
5813 return differ;
5814}
5815
5816/*
5817 * Wipe out a buffer and decrement the last buffer number if it was used for
5818 * this buffer. Call this to wipe out a temp buffer that does not contain any
5819 * marks.
5820 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005822wipe_buffer(
5823 buf_T *buf,
Bram Moolenaara6e8f882019-12-14 16:18:15 +01005824 int aucmd) // When TRUE trigger autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825{
5826 if (buf->b_fnum == top_file_num - 1)
5827 --top_file_num;
5828
Bram Moolenaarc667da52019-11-30 20:52:27 +01005829 if (!aucmd) // Don't trigger BufDelete autocommands here.
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005830 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005831
Bram Moolenaara6e8f882019-12-14 16:18:15 +01005832 close_buffer(NULL, buf, DOBUF_WIPE, FALSE, TRUE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005833
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005835 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836}