blob: 257ae7d888c201a41bfec395c91318de7ff23579 [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
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020030static void enter_buffer(buf_T *buf);
31static void buflist_getfpos(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010032static char_u *buflist_match(regmatch_T *rmp, buf_T *buf, int ignore_case);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010033static char_u *fname_match(regmatch_T *rmp, char_u *name, int ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +000034#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +020035static buf_T *buflist_findname_stat(char_u *ffname, stat_T *st);
36static int otherfile_buf(buf_T *buf, char_u *ffname, stat_T *stp);
37static int buf_same_ino(buf_T *buf, stat_T *stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000038#else
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010039static int otherfile_buf(buf_T *buf, char_u *ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000040#endif
41#ifdef FEAT_TITLE
Bram Moolenaar84a93082018-06-16 22:58:15 +020042static int value_changed(char_u *str, char_u **last);
Bram Moolenaar071d4272004-06-13 20:20:40 +000043#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010044static int append_arg_number(win_T *wp, char_u *buf, int buflen, int add_file);
45static void free_buffer(buf_T *);
46static void free_buffer_stuff(buf_T *buf, int free_options);
47static void clear_wininfo(buf_T *buf);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020048#if defined(FEAT_JOB_CHANNEL) \
49 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
50static int find_win_for_buf(buf_T *buf, win_T **wp, tabpage_T **tp);
51#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000052
53#ifdef UNIX
54# define dev_T dev_t
55#else
56# define dev_T unsigned
57#endif
58
Bram Moolenaar4033c552017-09-16 20:54:51 +020059#if defined(FEAT_QUICKFIX)
Bram Moolenaar7fd73202010-07-25 16:58:46 +020060static char *msg_loclist = N_("[Location List]");
61static char *msg_qflist = N_("[Quickfix List]");
62#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +010063static char *e_auabort = N_("E855: Autocommands caused command to abort");
Bram Moolenaar7fd73202010-07-25 16:58:46 +020064
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +020065// Number of times free_buffer() was called.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +020066static int buf_free_count = 0;
67
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +020068static int top_file_num = 1; // highest file number
69static garray_T buf_reuse = GA_EMPTY; // file numbers to recycle
70
71/*
72 * Return the highest possible buffer number.
73 */
74 int
75get_highest_fnum(void)
76{
77 return top_file_num - 1;
78}
79
Bram Moolenaarc024b462019-06-08 18:07:21 +020080/*
81 * Read data from buffer for retrying.
82 */
Bram Moolenaarf71d7b92016-08-09 22:14:05 +020083 static int
84read_buffer(
85 int read_stdin, /* read file from stdin, otherwise fifo */
86 exarg_T *eap, /* for forced 'ff' and 'fenc' or NULL */
87 int flags) /* extra flags for readfile() */
88{
89 int retval = OK;
90 linenr_T line_count;
91
92 /*
93 * Read from the buffer which the text is already filled in and append at
94 * the end. This makes it possible to retry when 'fileformat' or
95 * 'fileencoding' was guessed wrong.
96 */
97 line_count = curbuf->b_ml.ml_line_count;
98 retval = readfile(
99 read_stdin ? NULL : curbuf->b_ffname,
100 read_stdin ? NULL : curbuf->b_fname,
101 (linenr_T)line_count, (linenr_T)0, (linenr_T)MAXLNUM, eap,
102 flags | READ_BUFFER);
103 if (retval == OK)
104 {
105 /* Delete the binary lines. */
106 while (--line_count >= 0)
107 ml_delete((linenr_T)1, FALSE);
108 }
109 else
110 {
111 /* Delete the converted lines. */
112 while (curbuf->b_ml.ml_line_count > line_count)
113 ml_delete(line_count, FALSE);
114 }
115 /* Put the cursor on the first line. */
116 curwin->w_cursor.lnum = 1;
117 curwin->w_cursor.col = 0;
118
119 if (read_stdin)
120 {
121 /* Set or reset 'modified' before executing autocommands, so that
122 * it can be changed there. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100123 if (!readonlymode && !BUFEMPTY())
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200124 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100125 else if (retval == OK)
Bram Moolenaarc024b462019-06-08 18:07:21 +0200126 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200127
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100128 if (retval == OK)
129 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100130#ifdef FEAT_EVAL
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100131 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100132 curbuf, &retval);
133#else
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100134 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200135#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100136 }
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200137 }
138 return retval;
139}
140
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141/*
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200142 * Ensure buffer "buf" is loaded. Does not trigger the swap-exists action.
143 */
144 void
145buffer_ensure_loaded(buf_T *buf)
146{
147 if (buf->b_ml.ml_mfp == NULL)
148 {
149 aco_save_T aco;
150
151 aucmd_prepbuf(&aco, buf);
152 swap_exists_action = SEA_NONE;
153 open_buffer(FALSE, NULL, 0);
154 aucmd_restbuf(&aco);
155 }
156}
157
158/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200159 * Open current buffer, that is: open the memfile and read the file into
160 * memory.
161 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 */
163 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100164open_buffer(
165 int read_stdin, /* read file from stdin */
166 exarg_T *eap, /* for forced 'ff' and 'fenc' or NULL */
167 int flags) /* extra flags for readfile() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168{
169 int retval = OK;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200170 bufref_T old_curbuf;
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100171#ifdef FEAT_SYN_HL
172 long old_tw = curbuf->b_p_tw;
173#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200174 int read_fifo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175
176 /*
177 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
178 * When re-entering the same buffer, it should not change, because the
179 * user may have reset the flag by hand.
180 */
181 if (readonlymode && curbuf->b_ffname != NULL
182 && (curbuf->b_flags & BF_NEVERLOADED))
183 curbuf->b_p_ro = TRUE;
184
Bram Moolenaar4770d092006-01-12 23:22:24 +0000185 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186 {
187 /*
188 * 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
190 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100191 close_buffer(NULL, curbuf, 0, FALSE);
Bram Moolenaar29323592016-07-24 22:04:11 +0200192 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 if (curbuf->b_ml.ml_mfp != NULL)
194 break;
195 /*
Bram Moolenaar6f10c702019-08-20 22:58:37 +0200196 * If there is no memfile at all, exit.
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000197 * This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 */
199 if (curbuf == NULL)
200 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100201 emsg(_("E82: Cannot allocate any buffer, exiting..."));
Bram Moolenaar6f10c702019-08-20 22:58:37 +0200202
203 // Don't try to do any saving, with "curbuf" NULL almost nothing
204 // will work.
205 v_dying = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 getout(2);
207 }
Bram Moolenaar6f10c702019-08-20 22:58:37 +0200208
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100209 emsg(_("E83: Cannot allocate buffer, using other one..."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210 enter_buffer(curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100211#ifdef FEAT_SYN_HL
212 if (old_tw != curbuf->b_p_tw)
213 check_colorcolumn(curwin);
214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 return FAIL;
216 }
217
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218 /* The autocommands in readfile() may change the buffer, but only AFTER
219 * reading the file. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200220 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221 modified_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222
223 /* mark cursor position as being invalid */
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100224 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225
226 if (curbuf->b_ffname != NULL
227#ifdef FEAT_NETBEANS_INTG
228 && netbeansReadFile
229#endif
230 )
231 {
Bram Moolenaar426dd022016-03-15 15:09:29 +0100232 int old_msg_silent = msg_silent;
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200233#ifdef UNIX
234 int save_bin = curbuf->b_p_bin;
235 int perm;
236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237#ifdef FEAT_NETBEANS_INTG
238 int oldFire = netbeansFireChanges;
239
240 netbeansFireChanges = 0;
241#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200242#ifdef UNIX
243 perm = mch_getperm(curbuf->b_ffname);
Bram Moolenaard569bb02018-08-11 13:57:20 +0200244 if (perm >= 0 && (S_ISFIFO(perm)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200245 || S_ISSOCK(perm)
Bram Moolenaarf04507d2016-08-20 15:05:39 +0200246# ifdef OPEN_CHR_FILES
247 || (S_ISCHR(perm) && is_dev_fd_file(curbuf->b_ffname))
248# endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200249 ))
250 read_fifo = TRUE;
251 if (read_fifo)
252 curbuf->b_p_bin = TRUE;
253#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100254 if (shortmess(SHM_FILEINFO))
255 msg_silent = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200257 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200258 flags | READ_NEW | (read_fifo ? READ_FIFO : 0));
259#ifdef UNIX
260 if (read_fifo)
261 {
262 curbuf->b_p_bin = save_bin;
263 if (retval == OK)
264 retval = read_buffer(FALSE, eap, flags);
265 }
266#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100267 msg_silent = old_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268#ifdef FEAT_NETBEANS_INTG
269 netbeansFireChanges = oldFire;
270#endif
271 /* Help buffer is filtered. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +0200272 if (bt_help(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 fix_help_buffer();
274 }
275 else if (read_stdin)
276 {
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200277 int save_bin = curbuf->b_p_bin;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278
279 /*
280 * First read the text in binary mode into the buffer.
281 * Then read from that same buffer and append at the end. This makes
282 * it possible to retry when 'fileformat' or 'fileencoding' was
283 * guessed wrong.
284 */
285 curbuf->b_p_bin = TRUE;
286 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200287 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
288 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 curbuf->b_p_bin = save_bin;
290 if (retval == OK)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200291 retval = read_buffer(TRUE, eap, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292 }
293
294 /* if first time loading this buffer, init b_chartab[] */
295 if (curbuf->b_flags & BF_NEVERLOADED)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100296 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297 (void)buf_init_chartab(curbuf, FALSE);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100298#ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100299 parse_cino(curbuf);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100300#endif
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302
303 /*
304 * Set/reset the Changed flag first, autocmds may change the buffer.
305 * Apply the automatic commands, before processing the modelines.
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200306 * So the modelines have priority over autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 */
308 /* When reading stdin, the buffer contents always needs writing, so set
309 * the changed flag. Unless in readonly mode: "ls | gview -".
310 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000311 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 || modified_was_set /* ":set modified" used in autocmd */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100313#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000316 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100318 else if (retval == OK && !read_stdin && !read_fifo)
Bram Moolenaarc024b462019-06-08 18:07:21 +0200319 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 save_file_ff(curbuf); /* keep this fileformat */
321
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100322 /* Set last_changedtick to avoid triggering a TextChanged autocommand right
323 * after it was added. */
324 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100325 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100326
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 /* require "!" to overwrite the file, because it wasn't read completely */
328#ifdef FEAT_EVAL
329 if (aborting())
330#else
331 if (got_int)
332#endif
333 curbuf->b_flags |= BF_READERR;
334
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000335#ifdef FEAT_FOLDING
336 /* Need to update automatic folding. Do this before the autocommands,
337 * they may use the fold info. */
338 foldUpdateAll(curwin);
339#endif
340
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 /* need to set w_topline, unless some autocommand already did that. */
342 if (!(curwin->w_valid & VALID_TOPLINE))
343 {
344 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100345#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100349#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100351#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353#endif
354
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100355 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 /*
358 * The autocommands may have changed the current buffer. Apply the
359 * modelines to the correct buffer, if it still exists and is loaded.
360 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200361 if (bufref_valid(&old_curbuf) && old_curbuf.br_buf->b_ml.ml_mfp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 {
363 aco_save_T aco;
364
365 /* Go to the buffer that was opened. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200366 aucmd_prepbuf(&aco, old_curbuf.br_buf);
Bram Moolenaara3227e22006-03-08 21:32:40 +0000367 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
369
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100370#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100372 &retval);
373#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100375#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376
377 /* restore curwin/curbuf and a few other things */
378 aucmd_restbuf(&aco);
379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 }
381
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 return retval;
383}
384
385/*
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200386 * Store "buf" in "bufref" and set the free count.
387 */
388 void
389set_bufref(bufref_T *bufref, buf_T *buf)
390{
391 bufref->br_buf = buf;
Bram Moolenaarfadacf02017-06-19 20:35:32 +0200392 bufref->br_fnum = buf == NULL ? 0 : buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200393 bufref->br_buf_free_count = buf_free_count;
394}
395
396/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200397 * Return TRUE if "bufref->br_buf" points to the same buffer as when
398 * set_bufref() was called and it is a valid buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200399 * Only goes through the buffer list if buf_free_count changed.
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200400 * Also checks if b_fnum is still the same, a :bwipe followed by :new might get
401 * the same allocated memory, but it's a different buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200402 */
403 int
404bufref_valid(bufref_T *bufref)
405{
406 return bufref->br_buf_free_count == buf_free_count
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200407 ? TRUE : buf_valid(bufref->br_buf)
408 && bufref->br_fnum == bufref->br_buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200409}
410
411/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200413 * This can be slow if there are many buffers, prefer using bufref_valid().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 */
415 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100416buf_valid(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417{
418 buf_T *bp;
419
Bram Moolenaar82404332016-07-10 17:00:38 +0200420 /* Assume that we more often have a recent buffer, start with the last
421 * one. */
422 for (bp = lastbuf; bp != NULL; bp = bp->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 if (bp == buf)
424 return TRUE;
425 return FALSE;
426}
427
428/*
Bram Moolenaar480778b2016-07-14 22:09:39 +0200429 * A hash table used to quickly lookup a buffer by its number.
430 */
431static hashtab_T buf_hashtab;
432
433 static void
434buf_hashtab_add(buf_T *buf)
435{
436 sprintf((char *)buf->b_key, "%x", buf->b_fnum);
437 if (hash_add(&buf_hashtab, buf->b_key) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100438 emsg(_("E931: Buffer cannot be registered"));
Bram Moolenaar480778b2016-07-14 22:09:39 +0200439}
440
441 static void
442buf_hashtab_remove(buf_T *buf)
443{
444 hashitem_T *hi = hash_find(&buf_hashtab, buf->b_key);
445
446 if (!HASHITEM_EMPTY(hi))
447 hash_remove(&buf_hashtab, hi);
448}
449
Bram Moolenaar94f01952018-09-01 15:30:03 +0200450/*
451 * Return TRUE when buffer "buf" can be unloaded.
452 * Give an error message and return FALSE when the buffer is locked or the
453 * screen is being redrawn and the buffer is in a window.
454 */
455 static int
456can_unload_buffer(buf_T *buf)
457{
458 int can_unload = !buf->b_locked;
459
460 if (can_unload && updating_screen)
461 {
462 win_T *wp;
463
464 FOR_ALL_WINDOWS(wp)
465 if (wp->w_buffer == buf)
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200466 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200467 can_unload = FALSE;
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200468 break;
469 }
Bram Moolenaar94f01952018-09-01 15:30:03 +0200470 }
471 if (!can_unload)
Bram Moolenaardefa0672019-07-21 19:25:37 +0200472 semsg(_("E937: Attempt to delete a buffer that is in use: %s"),
473 buf->b_fname);
Bram Moolenaar94f01952018-09-01 15:30:03 +0200474 return can_unload;
475}
Bram Moolenaara997b452018-04-17 23:24:06 +0200476
Bram Moolenaar480778b2016-07-14 22:09:39 +0200477/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 * Close the link to a buffer.
479 * "action" is used when there is no longer a window for the buffer.
480 * It can be:
481 * 0 buffer becomes hidden
482 * DOBUF_UNLOAD buffer is unloaded
483 * DOBUF_DELETE buffer is unloaded and removed from buffer list
484 * DOBUF_WIPE buffer is unloaded and really deleted
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200485 * DOBUF_WIPE_REUSE idem, and add to buf_reuse list
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 * When doing all but the first one on the current buffer, the caller should
487 * get a new buffer very soon!
488 *
489 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100490 *
491 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
492 * cause there to be only one window with this buffer. e.g. when ":quit" is
493 * supposed to close the window but autocommands close all other windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 */
495 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100496close_buffer(
497 win_T *win, /* if not NULL, set b_last_cursor */
498 buf_T *buf,
499 int action,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200500 int abort_if_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100503 int nwindows;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200504 bufref_T bufref;
Bram Moolenaar3a117e12016-10-30 21:57:52 +0100505 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200506 win_T *the_curwin = curwin;
507 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 int unload_buf = (action != 0);
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200509 int wipe_buf = (action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
510 int del_buf = (action == DOBUF_DEL || wipe_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200512 /*
513 * Force unloading or deleting when 'bufhidden' says so.
514 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
515 * "hide" (otherwise we could never free or delete a buffer).
516 */
517 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
518 {
519 del_buf = TRUE;
520 unload_buf = TRUE;
521 }
522 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
523 {
524 del_buf = TRUE;
525 unload_buf = TRUE;
526 wipe_buf = TRUE;
527 }
528 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
529 unload_buf = TRUE;
530
Bram Moolenaar94053a52017-08-01 21:44:33 +0200531#ifdef FEAT_TERMINAL
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200532 if (bt_terminal(buf) && (buf->b_nwindows == 1 || del_buf))
Bram Moolenaar94053a52017-08-01 21:44:33 +0200533 {
534 if (term_job_running(buf->b_term))
535 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200536 if (wipe_buf || unload_buf)
Bram Moolenaara997b452018-04-17 23:24:06 +0200537 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200538 if (!can_unload_buffer(buf))
Bram Moolenaara997b452018-04-17 23:24:06 +0200539 return;
Bram Moolenaar94f01952018-09-01 15:30:03 +0200540
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200541 /* Wiping out or unloading a terminal buffer kills the job. */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200542 free_terminal(buf);
Bram Moolenaara997b452018-04-17 23:24:06 +0200543 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200544 else
545 {
546 /* The job keeps running, hide the buffer. */
547 del_buf = FALSE;
548 unload_buf = FALSE;
549 }
550 }
551 else
552 {
553 /* A terminal buffer is wiped out if the job has finished. */
554 del_buf = TRUE;
555 unload_buf = TRUE;
556 wipe_buf = TRUE;
557 }
558 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200561 /* Disallow deleting the buffer when it is locked (already being closed or
562 * halfway a command that relies on it). Unloading is allowed. */
Bram Moolenaar94f01952018-09-01 15:30:03 +0200563 if ((del_buf || wipe_buf) && !can_unload_buffer(buf))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200564 return;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200565
Bram Moolenaar4033c552017-09-16 20:54:51 +0200566 /* check no autocommands closed the window */
567 if (win != NULL && win_valid_any_tab(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 {
569 /* Set b_last_cursor when closing the last window for the buffer.
570 * Remember the last cursor position and window options of the buffer.
571 * This used to be only for the current window, but then options like
572 * 'foldmethod' may be lost with a ":only" command. */
573 if (buf->b_nwindows == 1)
574 set_last_cursor(win);
575 buflist_setfpos(buf, win,
576 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
577 win->w_cursor.col, TRUE);
578 }
579
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200580 set_bufref(&bufref, buf);
581
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 /* When the buffer is no longer in a window, trigger BufWinLeave */
583 if (buf->b_nwindows == 1)
584 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200585 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200586 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
587 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200588 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100589 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200590 /* Autocommands deleted the buffer. */
591aucmd_abort:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100592 emsg(_(e_auabort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 return;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100594 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200595 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200596 if (abort_if_last && one_window())
597 /* Autocommands made this the only window. */
598 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599
600 /* When the buffer becomes hidden, but is not unloaded, trigger
601 * BufHidden */
602 if (!unload_buf)
603 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200604 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200605 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
606 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200607 && !bufref_valid(&bufref))
Bram Moolenaar362ce482012-06-06 19:02:45 +0200608 /* Autocommands deleted the buffer. */
609 goto aucmd_abort;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200610 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200611 if (abort_if_last && one_window())
612 /* Autocommands made this the only window. */
613 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100615#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 if (aborting()) /* autocmds may abort script processing */
617 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100618#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200620
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200621 /* If the buffer was in curwin and the window has changed, go back to that
622 * window, if it still exists. This avoids that ":edit x" triggering a
623 * "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
624 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
625 {
626 block_autocmds();
627 goto_tabpage_win(the_curtab, the_curwin);
628 unblock_autocmds();
629 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200630
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 nwindows = buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632
633 /* decrease the link count from windows (unless not in any window) */
634 if (buf->b_nwindows > 0)
635 --buf->b_nwindows;
636
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100637#ifdef FEAT_DIFF
638 if (diffopt_hiddenoff() && !unload_buf && buf->b_nwindows == 0)
Bram Moolenaarb7407d32018-02-03 17:36:27 +0100639 diff_buf_delete(buf); /* Clear 'diff' for hidden buffer. */
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100640#endif
641
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 /* Return when a window is displaying the buffer or when it's not
643 * unloaded. */
644 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646
647 /* Always remove the buffer when there is no file name. */
648 if (buf->b_ffname == NULL)
649 del_buf = TRUE;
650
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200651 /* When closing the current buffer stop Visual mode before freeing
652 * anything. */
Bram Moolenaar4930a762016-09-11 14:39:53 +0200653 if (buf == curbuf && VIsual_active
Bram Moolenaar9a27c7f2016-09-09 12:57:09 +0200654#if defined(EXITFREE)
655 && !entered_free_all_mem
656#endif
657 )
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200658 end_visual_mode();
659
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 /*
661 * Free all things allocated for this buffer.
662 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
663 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 /* Remember if we are closing the current buffer. Restore the number of
665 * windows, so that autocommands in buf_freeall() don't get confused. */
666 is_curbuf = (buf == curbuf);
667 buf->b_nwindows = nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200669 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 /* Autocommands may have deleted the buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200672 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100674#ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000675 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100677#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 /*
680 * It's possible that autocommands change curbuf to the one being deleted.
681 * This might cause the previous curbuf to be deleted unexpectedly. But
682 * in some cases it's OK to delete the curbuf, because a new one is
683 * obtained anyway. Therefore only return if curbuf changed to the
684 * deleted buffer.
685 */
686 if (buf == curbuf && !is_curbuf)
687 return;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200688
Bram Moolenaar4033c552017-09-16 20:54:51 +0200689 if (win_valid_any_tab(win) && win->w_buffer == buf)
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200690 win->w_buffer = NULL; /* make sure we don't use the buffer now */
691
692 /* Autocommands may have opened or closed windows for this buffer.
693 * Decrement the count for the close we do here. */
694 if (buf->b_nwindows > 0)
695 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 /*
698 * Remove the buffer from the list.
699 */
700 if (wipe_buf)
701 {
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +0200702 if (action == DOBUF_WIPE_REUSE)
703 {
704 // we can re-use this buffer number, store it
705 if (buf_reuse.ga_itemsize == 0)
706 ga_init2(&buf_reuse, sizeof(int), 50);
707 if (ga_grow(&buf_reuse, 1) == OK)
708 ((int *)buf_reuse.ga_data)[buf_reuse.ga_len++] = buf->b_fnum;
709 }
Bram Moolenaar3d6014f2018-10-11 19:27:47 +0200710 if (buf->b_sfname != buf->b_ffname)
711 VIM_CLEAR(buf->b_sfname);
712 else
713 buf->b_sfname = NULL;
714 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 if (buf->b_prev == NULL)
716 firstbuf = buf->b_next;
717 else
718 buf->b_prev->b_next = buf->b_next;
719 if (buf->b_next == NULL)
720 lastbuf = buf->b_prev;
721 else
722 buf->b_next->b_prev = buf->b_prev;
723 free_buffer(buf);
724 }
725 else
726 {
727 if (del_buf)
728 {
729 /* Free all internal variables and reset option values, to make
730 * ":bdel" compatible with Vim 5.7. */
731 free_buffer_stuff(buf, TRUE);
732
733 /* Make it look like a new buffer. */
734 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
735
736 /* Init the options when loaded again. */
737 buf->b_p_initialized = FALSE;
738 }
739 buf_clear_file(buf);
740 if (del_buf)
741 buf->b_p_bl = FALSE;
742 }
743}
744
745/*
746 * Make buffer not contain a file.
747 */
748 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100749buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750{
751 buf->b_ml.ml_line_count = 1;
Bram Moolenaarc024b462019-06-08 18:07:21 +0200752 unchanged(buf, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 buf->b_p_eol = TRUE;
755 buf->b_start_eol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000757 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 buf->b_ml.ml_mfp = NULL;
759 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
760#ifdef FEAT_NETBEANS_INTG
761 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762#endif
763}
764
765/*
766 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200767 * the file. Careful: get here with "curwin" NULL when exiting.
768 * flags:
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200769 * BFA_DEL buffer is going to be deleted
770 * BFA_WIPE buffer is going to be wiped out
771 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100774buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200777 bufref_T bufref;
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200778 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaar5a497892016-09-03 16:29:04 +0200779 win_T *the_curwin = curwin;
780 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781
Bram Moolenaar5a497892016-09-03 16:29:04 +0200782 /* Make sure the buffer isn't closed by autocommands. */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200783 ++buf->b_locked;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200784 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200785 if (buf->b_ml.ml_mfp != NULL)
786 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200787 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
788 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200789 && !bufref_valid(&bufref))
790 /* autocommands deleted the buffer */
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200791 return;
792 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200793 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200795 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
796 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200797 && !bufref_valid(&bufref))
798 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 return;
800 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200801 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200803 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
804 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200805 && !bufref_valid(&bufref))
806 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 return;
808 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200809 --buf->b_locked;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200810
Bram Moolenaar5a497892016-09-03 16:29:04 +0200811 /* If the buffer was in curwin and the window has changed, go back to that
812 * window, if it still exists. This avoids that ":edit x" triggering a
813 * "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
814 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
815 {
816 block_autocmds();
817 goto_tabpage_win(the_curtab, the_curwin);
818 unblock_autocmds();
819 }
Bram Moolenaar5a497892016-09-03 16:29:04 +0200820
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100821#ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000822 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825
826 /*
827 * It's possible that autocommands change curbuf to the one being deleted.
828 * This might cause curbuf to be deleted unexpectedly. But in some cases
829 * it's OK to delete the curbuf, because a new one is obtained anyway.
830 * Therefore only return if curbuf changed to the deleted buffer.
831 */
832 if (buf == curbuf && !is_curbuf)
833 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834#ifdef FEAT_DIFF
835 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
836#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200837#ifdef FEAT_SYN_HL
Bram Moolenaar89c71222011-11-30 15:40:56 +0100838 /* Remove any ownsyntax, unless exiting. */
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200839 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaar89c71222011-11-30 15:40:56 +0100840 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200841#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000842
843#ifdef FEAT_FOLDING
844 /* No folds in an empty buffer. */
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000845 {
846 win_T *win;
847 tabpage_T *tp;
848
849 FOR_ALL_TAB_WINDOWS(tp, win)
850 if (win->w_buffer == buf)
851 clearFolding(win);
852 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000853#endif
854
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855#ifdef FEAT_TCL
856 tcl_buffer_free(buf);
857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 ml_close(buf, TRUE); /* close and delete the memline/memfile */
859 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200860 if ((flags & BFA_KEEP_UNDO) == 0)
861 {
862 u_blockfree(buf); /* free the memory allocated for undo */
863 u_clearall(buf); /* reset all undo information */
864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200866 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100868#ifdef FEAT_TEXT_PROP
869 clear_buf_prop_types(buf);
870#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000871 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872}
873
874/*
875 * Free a buffer structure and the things it contains related to the buffer
876 * itself (not the file, that must have been done already).
877 */
878 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100879free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200881 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200883#ifdef FEAT_EVAL
Bram Moolenaarb2c87502017-10-14 21:15:58 +0200884 /* b:changedtick uses an item in buf_T, remove it now */
885 dictitem_remove(buf->b_vars, (dictitem_T *)&buf->b_ct_di);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200886 unref_var_dict(buf->b_vars);
887#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200888#ifdef FEAT_LUA
889 lua_buffer_free(buf);
890#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000891#ifdef FEAT_MZSCHEME
892 mzscheme_buffer_free(buf);
893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894#ifdef FEAT_PERL
895 perl_buf_free(buf);
896#endif
897#ifdef FEAT_PYTHON
898 python_buffer_free(buf);
899#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200900#ifdef FEAT_PYTHON3
901 python3_buffer_free(buf);
902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903#ifdef FEAT_RUBY
904 ruby_buffer_free(buf);
905#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200906#ifdef FEAT_JOB_CHANNEL
907 channel_buffer_free(buf);
908#endif
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200909#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +0200910 free_terminal(buf);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200911#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +0200912#ifdef FEAT_JOB_CHANNEL
913 vim_free(buf->b_prompt_text);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200914 free_callback(&buf->b_prompt_callback);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200915#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200916
917 buf_hashtab_remove(buf);
918
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200919 aubuflocal_remove(buf);
920
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200921 if (autocmd_busy)
922 {
923 /* Do not free the buffer structure while autocommands are executing,
924 * it's still needed. Free it when autocmd_busy is reset. */
925 buf->b_next = au_pending_free_buf;
926 au_pending_free_buf = buf;
927 }
928 else
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200929 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930}
931
932/*
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100933 * Initializes b:changedtick.
Bram Moolenaar79518e22017-02-17 16:31:35 +0100934 */
935 static void
936init_changedtick(buf_T *buf)
937{
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100938 dictitem_T *di = (dictitem_T *)&buf->b_ct_di;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100939
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100940 di->di_flags = DI_FLAGS_FIX | DI_FLAGS_RO;
941 di->di_tv.v_type = VAR_NUMBER;
942 di->di_tv.v_lock = VAR_FIXED;
943 di->di_tv.vval.v_number = 0;
944
Bram Moolenaar92769c32017-02-25 15:41:37 +0100945#ifdef FEAT_EVAL
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100946 STRCPY(buf->b_ct_di.di_key, "changedtick");
947 (void)dict_add(buf->b_vars, di);
Bram Moolenaar92769c32017-02-25 15:41:37 +0100948#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +0100949}
950
951/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
953 */
954 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100955free_buffer_stuff(
956 buf_T *buf,
957 int free_options) /* free options as well */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958{
959 if (free_options)
960 {
961 clear_wininfo(buf); /* including window-local options */
962 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200963#ifdef FEAT_SPELL
964 ga_clear(&buf->b_s.b_langp);
965#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 }
967#ifdef FEAT_EVAL
Bram Moolenaar79518e22017-02-17 16:31:35 +0100968 {
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100969 varnumber_T tick = CHANGEDTICK(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +0100970
971 vars_clear(&buf->b_vars->dv_hashtab); /* free all buffer variables */
972 hash_init(&buf->b_vars->dv_hashtab);
973 init_changedtick(buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100974 CHANGEDTICK(buf) = tick;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976#endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200977 uc_clear(&buf->b_ucmds); // clear local user commands
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978#ifdef FEAT_SIGNS
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200979 buf_delete_signs(buf, (char_u *)"*"); // delete any signs
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000981#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200982 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
985 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100986 VIM_CLEAR(buf->b_start_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987}
988
989/*
990 * Free the b_wininfo list for buffer "buf".
991 */
992 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100993clear_wininfo(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994{
995 wininfo_T *wip;
996
997 while (buf->b_wininfo != NULL)
998 {
999 wip = buf->b_wininfo;
1000 buf->b_wininfo = wip->wi_next;
1001 if (wip->wi_optset)
1002 {
1003 clear_winopt(&wip->wi_opt);
1004#ifdef FEAT_FOLDING
1005 deleteFoldRecurse(&wip->wi_folds);
1006#endif
1007 }
1008 vim_free(wip);
1009 }
1010}
1011
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012/*
1013 * Go to another buffer. Handles the result of the ATTENTION dialog.
1014 */
1015 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001016goto_buffer(
1017 exarg_T *eap,
1018 int start,
1019 int dir,
1020 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001022 bufref_T old_curbuf;
1023
1024 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025
1026 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
1028 start, dir, count, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
1030 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001031#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001032 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001033
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001034 /* Reset the error/interrupt/exception state here so that
1035 * aborting() returns FALSE when closing a window. */
1036 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001037#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001038
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001039 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 win_close(curwin, TRUE);
1041 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001042 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001043
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001044#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001045 /* Restore the error/interrupt/exception state if not discarded by a
1046 * new aborting error, interrupt, or uncaught exception. */
1047 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 }
1050 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001051 handle_swap_exists(&old_curbuf);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001052}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054/*
1055 * Handle the situation of swap_exists_action being set.
1056 * It is allowed for "old_curbuf" to be NULL or invalid.
1057 */
1058 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001059handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060{
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001061#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001062 cleanup_T cs;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001063#endif
1064#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001065 long old_tw = curbuf->b_p_tw;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001066#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001067 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001068
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 if (swap_exists_action == SEA_QUIT)
1070 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001071#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001072 /* Reset the error/interrupt/exception state here so that
1073 * aborting() returns FALSE when closing a buffer. */
1074 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001075#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001076
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 /* User selected Quit at ATTENTION prompt. Go back to previous
1078 * buffer. If that buffer is gone or the same as the current one,
1079 * open a new, empty buffer. */
1080 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001081 swap_exists_did_quit = TRUE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001082 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001083 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1084 || old_curbuf->br_buf == curbuf)
1085 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
1086 else
1087 buf = old_curbuf->br_buf;
1088 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001089 {
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001090 int old_msg_silent = msg_silent;
1091
1092 if (shortmess(SHM_FILEINFO))
1093 msg_silent = 1; // prevent fileinfo message
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001094 enter_buffer(buf);
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001095 // restore msg_silent, so that the command line will be shown
1096 msg_silent = old_msg_silent;
1097
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001098#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001099 if (old_tw != curbuf->b_p_tw)
1100 check_colorcolumn(curwin);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001101#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001104
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001105#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001106 /* Restore the error/interrupt/exception state if not discarded by a
1107 * new aborting error, interrupt, or uncaught exception. */
1108 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 }
1111 else if (swap_exists_action == SEA_RECOVER)
1112 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001113#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001114 /* Reset the error/interrupt/exception state here so that
1115 * aborting() returns FALSE when closing a buffer. */
1116 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001117#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001118
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 /* User selected Recover at ATTENTION prompt. */
1120 msg_scroll = TRUE;
Bram Moolenaar99499b12019-05-23 21:35:48 +02001121 ml_recover(FALSE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001122 msg_puts("\n"); /* don't overwrite the last message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001124 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001125
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001126#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001127 /* Restore the error/interrupt/exception state if not discarded by a
1128 * new aborting error, interrupt, or uncaught exception. */
1129 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 }
1132 swap_exists_action = SEA_NONE;
1133}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135/*
1136 * do_bufdel() - delete or unload buffer(s)
1137 *
1138 * addr_count == 0: ":bdel" - delete current buffer
1139 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1140 * buffer "end_bnr", then any other arguments.
1141 * addr_count == 2: ":N,N bdel" - delete buffers in range
1142 *
1143 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1144 * DOBUF_DEL (":bdel")
1145 *
1146 * Returns error message or NULL
1147 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001148 char *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001149do_bufdel(
1150 int command,
1151 char_u *arg, /* pointer to extra arguments */
1152 int addr_count,
1153 int start_bnr, /* first buffer number in a range */
1154 int end_bnr, /* buffer nr or last buffer nr in a range */
1155 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156{
1157 int do_current = 0; /* delete current buffer? */
1158 int deleted = 0; /* number of buffers deleted */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001159 char *errormsg = NULL; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 int bnr; /* buffer number */
1161 char_u *p;
1162
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 if (addr_count == 0)
1164 {
1165 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1166 }
1167 else
1168 {
1169 if (addr_count == 2)
1170 {
1171 if (*arg) /* both range and argument is not allowed */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001172 return _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 bnr = start_bnr;
1174 }
1175 else /* addr_count == 1 */
1176 bnr = end_bnr;
1177
1178 for ( ;!got_int; ui_breakcheck())
1179 {
1180 /*
1181 * delete the current buffer last, otherwise when the
1182 * current buffer is deleted, the next buffer becomes
1183 * the current one and will be loaded, which may then
1184 * also be deleted, etc.
1185 */
1186 if (bnr == curbuf->b_fnum)
1187 do_current = bnr;
1188 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
1189 forceit) == OK)
1190 ++deleted;
1191
1192 /*
1193 * find next buffer number to delete/unload
1194 */
1195 if (addr_count == 2)
1196 {
1197 if (++bnr > end_bnr)
1198 break;
1199 }
1200 else /* addr_count == 1 */
1201 {
1202 arg = skipwhite(arg);
1203 if (*arg == NUL)
1204 break;
1205 if (!VIM_ISDIGIT(*arg))
1206 {
1207 p = skiptowhite_esc(arg);
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001208 bnr = buflist_findpat(arg, p,
1209 command == DOBUF_WIPE || command == DOBUF_WIPE_REUSE,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01001210 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 if (bnr < 0) /* failed */
1212 break;
1213 arg = p;
1214 }
1215 else
1216 bnr = getdigits(&arg);
1217 }
1218 }
1219 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1220 FORWARD, do_current, forceit) == OK)
1221 ++deleted;
1222
1223 if (deleted == 0)
1224 {
1225 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001226 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001228 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00001230 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001231 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 }
1233 else if (deleted >= p_report)
1234 {
1235 if (command == DOBUF_UNLOAD)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001236 smsg(NGETTEXT("%d buffer unloaded",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001237 "%d buffers unloaded", deleted), deleted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 else if (command == DOBUF_DEL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001239 smsg(NGETTEXT("%d buffer deleted",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001240 "%d buffers deleted", deleted), deleted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001242 smsg(NGETTEXT("%d buffer wiped out",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001243 "%d buffers wiped out", deleted), deleted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 }
1245 }
1246
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247
1248 return errormsg;
1249}
1250
Bram Moolenaara02471e2014-01-10 16:43:14 +01001251/*
1252 * Make the current buffer empty.
1253 * Used when it is wiped out and it's the last buffer.
1254 */
1255 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001256empty_curbuf(
1257 int close_others,
1258 int forceit,
1259 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001260{
1261 int retval;
1262 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001263 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001264
1265 if (action == DOBUF_UNLOAD)
1266 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001267 emsg(_("E90: Cannot unload last buffer"));
Bram Moolenaara02471e2014-01-10 16:43:14 +01001268 return FAIL;
1269 }
1270
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001271 set_bufref(&bufref, buf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001272 if (close_others)
1273 /* Close any other windows on this buffer, then make it empty. */
Bram Moolenaara02471e2014-01-10 16:43:14 +01001274 close_windows(buf, TRUE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001275
1276 setpcmark();
1277 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1278 forceit ? ECMD_FORCEIT : 0, curwin);
1279
1280 /*
1281 * do_ecmd() may create a new buffer, then we have to delete
1282 * the old one. But do_ecmd() may have done that already, check
1283 * if the buffer still exists.
1284 */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001285 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001286 close_buffer(NULL, buf, action, FALSE);
1287 if (!close_others)
1288 need_fileinfo = FALSE;
1289 return retval;
1290}
Bram Moolenaar94f01952018-09-01 15:30:03 +02001291
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292/*
1293 * Implementation of the commands for the buffer list.
1294 *
1295 * action == DOBUF_GOTO go to specified buffer
1296 * action == DOBUF_SPLIT split window and go to specified buffer
1297 * action == DOBUF_UNLOAD unload specified buffer(s)
1298 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1299 * action == DOBUF_WIPE delete specified buffer(s) really
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001300 * action == DOBUF_WIPE_REUSE idem, and add number to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 *
1302 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1303 * start == DOBUF_FIRST go to "count" buffer from first buffer
1304 * start == DOBUF_LAST go to "count" buffer from last buffer
1305 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1306 *
1307 * Return FAIL or OK.
1308 */
1309 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001310do_buffer(
1311 int action,
1312 int start,
1313 int dir, /* FORWARD or BACKWARD */
1314 int count, /* buffer number or number of buffers */
1315 int forceit) /* TRUE for :...! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316{
1317 buf_T *buf;
1318 buf_T *bp;
1319 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001320 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321
1322 switch (start)
1323 {
1324 case DOBUF_FIRST: buf = firstbuf; break;
1325 case DOBUF_LAST: buf = lastbuf; break;
1326 default: buf = curbuf; break;
1327 }
1328 if (start == DOBUF_MOD) /* find next modified buffer */
1329 {
1330 while (count-- > 0)
1331 {
1332 do
1333 {
1334 buf = buf->b_next;
1335 if (buf == NULL)
1336 buf = firstbuf;
1337 }
1338 while (buf != curbuf && !bufIsChanged(buf));
1339 }
1340 if (!bufIsChanged(buf))
1341 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001342 emsg(_("E84: No modified buffer found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 return FAIL;
1344 }
1345 }
1346 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
1347 {
1348 while (buf != NULL && buf->b_fnum != count)
1349 buf = buf->b_next;
1350 }
1351 else
1352 {
1353 bp = NULL;
1354 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1355 {
1356 /* remember the buffer where we start, we come back there when all
1357 * buffers are unlisted. */
1358 if (bp == NULL)
1359 bp = buf;
1360 if (dir == FORWARD)
1361 {
1362 buf = buf->b_next;
1363 if (buf == NULL)
1364 buf = firstbuf;
1365 }
1366 else
1367 {
1368 buf = buf->b_prev;
1369 if (buf == NULL)
1370 buf = lastbuf;
1371 }
1372 /* don't count unlisted buffers */
1373 if (unload || buf->b_p_bl)
1374 {
1375 --count;
1376 bp = NULL; /* use this buffer as new starting point */
1377 }
1378 if (bp == buf)
1379 {
1380 /* back where we started, didn't find anything. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001381 emsg(_("E85: There is no listed buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 return FAIL;
1383 }
1384 }
1385 }
1386
1387 if (buf == NULL) /* could not find it */
1388 {
1389 if (start == DOBUF_FIRST)
1390 {
1391 /* don't warn when deleting */
1392 if (!unload)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001393 semsg(_(e_nobufnr), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 }
1395 else if (dir == FORWARD)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001396 emsg(_("E87: Cannot go beyond last buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001398 emsg(_("E88: Cannot go before first buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 return FAIL;
1400 }
1401
1402#ifdef FEAT_GUI
1403 need_mouse_correct = TRUE;
1404#endif
1405
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 /*
1407 * delete buffer buf from memory and/or the list
1408 */
1409 if (unload)
1410 {
1411 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001412 bufref_T bufref;
1413
Bram Moolenaar94f01952018-09-01 15:30:03 +02001414 if (!can_unload_buffer(buf))
Bram Moolenaara997b452018-04-17 23:24:06 +02001415 return FAIL;
Bram Moolenaara997b452018-04-17 23:24:06 +02001416
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001417 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418
1419 /* When unloading or deleting a buffer that's already unloaded and
1420 * unlisted: fail silently. */
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001421 if (action != DOBUF_WIPE && action != DOBUF_WIPE_REUSE
1422 && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 return FAIL;
1424
1425 if (!forceit && bufIsChanged(buf))
1426 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001427#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 if ((p_confirm || cmdmod.confirm) && p_write)
1429 {
1430 dialog_changed(buf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001431 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 /* Autocommand deleted buffer, oops! It's not changed
1433 * now. */
1434 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001435 /* If it's still changed fail silently, the dialog already
1436 * mentioned why it fails. */
1437 if (bufIsChanged(buf))
1438 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001440 else
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001443 semsg(_("E89: No write since last change for buffer %d (add ! to override)"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 buf->b_fnum);
1445 return FAIL;
1446 }
1447 }
1448
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001449 /* When closing the current buffer stop Visual mode. */
Bram Moolenaar4930a762016-09-11 14:39:53 +02001450 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001451 end_visual_mode();
1452
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 /*
1454 * If deleting the last (listed) buffer, make it empty.
1455 * The last (listed) buffer cannot be unloaded.
1456 */
Bram Moolenaar29323592016-07-24 22:04:11 +02001457 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 if (bp->b_p_bl && bp != buf)
1459 break;
1460 if (bp == NULL && buf == curbuf)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001461 return empty_curbuf(TRUE, forceit, action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 /*
1464 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001465 * (unless it's the only window). Repeat this so long as we end up in
1466 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001468 while (buf == curbuf
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001469 && !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
Bram Moolenaar459ca562016-11-10 18:16:33 +01001470 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001471 {
1472 if (win_close(curwin, FALSE) == FAIL)
1473 break;
1474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475
1476 /*
1477 * If the buffer to be deleted is not the current one, delete it here.
1478 */
1479 if (buf != curbuf)
1480 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001481 close_windows(buf, FALSE);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001482 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows <= 0)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001483 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 return OK;
1485 }
1486
1487 /*
1488 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001489 * There should be another, otherwise it would have been handled
1490 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001491 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 * Then prefer the buffer we most recently visited.
1493 * Else try to find one that is loaded, after the current buffer,
1494 * then before the current buffer.
1495 * Finally use any buffer.
1496 */
1497 buf = NULL; /* selected buffer */
1498 bp = NULL; /* used when no loaded buffer found */
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001499 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1500 buf = au_new_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501#ifdef FEAT_JUMPLIST
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001502 else if (curwin->w_jumplistlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 {
1504 int jumpidx;
1505
1506 jumpidx = curwin->w_jumplistidx - 1;
1507 if (jumpidx < 0)
1508 jumpidx = curwin->w_jumplistlen - 1;
1509
1510 forward = jumpidx;
1511 while (jumpidx != curwin->w_jumplistidx)
1512 {
1513 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1514 if (buf != NULL)
1515 {
1516 if (buf == curbuf || !buf->b_p_bl)
1517 buf = NULL; /* skip current and unlisted bufs */
1518 else if (buf->b_ml.ml_mfp == NULL)
1519 {
1520 /* skip unloaded buf, but may keep it for later */
1521 if (bp == NULL)
1522 bp = buf;
1523 buf = NULL;
1524 }
1525 }
1526 if (buf != NULL) /* found a valid buffer: stop searching */
1527 break;
1528 /* advance to older entry in jump list */
1529 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1530 break;
1531 if (--jumpidx < 0)
1532 jumpidx = curwin->w_jumplistlen - 1;
1533 if (jumpidx == forward) /* List exhausted for sure */
1534 break;
1535 }
1536 }
1537#endif
1538
1539 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1540 {
1541 forward = TRUE;
1542 buf = curbuf->b_next;
1543 for (;;)
1544 {
1545 if (buf == NULL)
1546 {
1547 if (!forward) /* tried both directions */
1548 break;
1549 buf = curbuf->b_prev;
1550 forward = FALSE;
1551 continue;
1552 }
1553 /* in non-help buffer, try to skip help buffers, and vv */
1554 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1555 {
1556 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1557 break;
1558 if (bp == NULL) /* remember unloaded buf for later */
1559 bp = buf;
1560 }
1561 if (forward)
1562 buf = buf->b_next;
1563 else
1564 buf = buf->b_prev;
1565 }
1566 }
1567 if (buf == NULL) /* No loaded buffer, use unloaded one */
1568 buf = bp;
1569 if (buf == NULL) /* No loaded buffer, find listed one */
1570 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001571 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 if (buf->b_p_bl && buf != curbuf)
1573 break;
1574 }
1575 if (buf == NULL) /* Still no buffer, just take one */
1576 {
1577 if (curbuf->b_next != NULL)
1578 buf = curbuf->b_next;
1579 else
1580 buf = curbuf->b_prev;
1581 }
1582 }
1583
Bram Moolenaara02471e2014-01-10 16:43:14 +01001584 if (buf == NULL)
1585 {
1586 /* Autocommands must have wiped out all other buffers. Only option
1587 * now is to make the current buffer empty. */
1588 return empty_curbuf(FALSE, forceit, action);
1589 }
1590
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 /*
1592 * make buf current buffer
1593 */
1594 if (action == DOBUF_SPLIT) /* split window first */
1595 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001596 /* If 'switchbuf' contains "useopen": jump to first window containing
1597 * "buf" if one exists */
1598 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001599 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001600 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001601 * page containing "buf" if one exists */
1602 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 return OK;
1604 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 return FAIL;
1606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607
1608 /* go to current buffer - nothing to do */
1609 if (buf == curbuf)
1610 return OK;
1611
1612 /*
1613 * Check if the current buffer may be abandoned.
1614 */
1615 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1616 {
1617#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1618 if ((p_confirm || cmdmod.confirm) && p_write)
1619 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001620 bufref_T bufref;
1621
1622 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 dialog_changed(curbuf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001624 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 /* Autocommand deleted buffer, oops! */
1626 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 }
1628 if (bufIsChanged(curbuf))
1629#endif
1630 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001631 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 return FAIL;
1633 }
1634 }
1635
1636 /* Go to the other buffer. */
1637 set_curbuf(buf, action);
1638
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001640 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001642#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 if (aborting()) /* autocmds may abort script processing */
1644 return FAIL;
1645#endif
1646
1647 return OK;
1648}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649
1650/*
1651 * Set current buffer to "buf". Executes autocommands and closes current
1652 * buffer. "action" tells how to close the current buffer:
1653 * DOBUF_GOTO free or hide it
1654 * DOBUF_SPLIT nothing
1655 * DOBUF_UNLOAD unload it
1656 * DOBUF_DEL delete it
1657 * DOBUF_WIPE wipe it out
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001658 * DOBUF_WIPE_REUSE wipe it out and add to "buf_reuse"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 */
1660 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001661set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662{
1663 buf_T *prevbuf;
1664 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001665 || action == DOBUF_WIPE || action == DOBUF_WIPE_REUSE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001666#ifdef FEAT_SYN_HL
1667 long old_tw = curbuf->b_p_tw;
1668#endif
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001669 bufref_T newbufref;
1670 bufref_T prevbufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671
1672 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001673 if (!cmdmod.keepalt)
1674 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001675 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 /* Don't restart Select mode after switching to another buffer. */
1678 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001680 /* close_windows() or apply_autocmds() may change curbuf and wipe out "buf"
1681 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 prevbuf = curbuf;
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001683 set_bufref(&prevbufref, prevbuf);
1684 set_bufref(&newbufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001686 /* Autocommands may delete the curren buffer and/or the buffer we wan to go
1687 * to. In those cases don't close the buffer. */
Bram Moolenaar82404332016-07-10 17:00:38 +02001688 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001689 || (bufref_valid(&prevbufref)
1690 && bufref_valid(&newbufref)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001691#ifdef FEAT_EVAL
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001692 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001694 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001696#ifdef FEAT_SYN_HL
1697 if (prevbuf == curwin->w_buffer)
1698 reset_synblock(curwin);
1699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001701 close_windows(prevbuf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001702#if defined(FEAT_EVAL)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001703 if (bufref_valid(&prevbufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704#else
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001705 if (bufref_valid(&prevbufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001707 {
Bram Moolenaare25865a2012-07-06 16:22:02 +02001708 win_T *previouswin = curwin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001709 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001710 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1712 unload ? action : (action == DOBUF_GOTO
Bram Moolenaareb44a682017-08-03 22:44:55 +02001713 && !buf_hide(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001714 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaare25865a2012-07-06 16:22:02 +02001715 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaar9e931222012-06-20 11:55:01 +02001716 /* autocommands changed curwin, Grr! */
Bram Moolenaare25865a2012-07-06 16:22:02 +02001717 curwin = previouswin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 }
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001720 /* An autocommand may have deleted "buf", already entered it (e.g., when
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001721 * it did ":bunload") or aborted the script processing.
Bram Moolenaar9e931222012-06-20 11:55:01 +02001722 * If curwin->w_buffer is null, enter_buffer() will make it valid again */
1723 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001724#ifdef FEAT_EVAL
Bram Moolenaar4033c552017-09-16 20:54:51 +02001725 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001727 ) || curwin->w_buffer == NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001728 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001730#ifdef FEAT_SYN_HL
1731 if (old_tw != curbuf->b_p_tw)
1732 check_colorcolumn(curwin);
1733#endif
1734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735}
1736
1737/*
1738 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001739 * Old curbuf must have been abandoned already! This also means "curbuf" may
1740 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001742 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001743enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744{
1745 /* Copy buffer and window local option values. Not for a help buffer. */
1746 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1747 if (!buf->b_help)
1748 get_winopts(buf);
1749#ifdef FEAT_FOLDING
1750 else
1751 /* Remove all folds in the window. */
1752 clearFolding(curwin);
1753 foldUpdateAll(curwin); /* update folds (later). */
1754#endif
1755
1756 /* Get the buffer in the current window. */
1757 curwin->w_buffer = buf;
1758 curbuf = buf;
1759 ++curbuf->b_nwindows;
1760
1761#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001762 if (curwin->w_p_diff)
1763 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764#endif
1765
Bram Moolenaara971b822011-09-14 14:43:25 +02001766#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001767 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001768#endif
1769
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 /* Cursor on first line by default. */
1771 curwin->w_cursor.lnum = 1;
1772 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001775 curwin->w_topline_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001777 /* mark cursor position as being invalid */
1778 curwin->w_valid = 0;
1779
Bram Moolenaar9cea87c2018-09-21 16:59:45 +02001780 buflist_setfpos(curbuf, curwin, curbuf->b_last_cursor.lnum,
1781 curbuf->b_last_cursor.col, TRUE);
1782
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 /* Make sure the buffer is loaded. */
1784 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001785 {
1786 /* If there is no filetype, allow for detecting one. Esp. useful for
1787 * ":ball" used in a autocommand. If there already is a filetype we
1788 * might prefer to keep it. */
1789 if (*curbuf->b_p_ft == NUL)
1790 did_filetype = FALSE;
1791
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001792 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 else
1795 {
Bram Moolenaareda65222019-05-16 20:29:44 +02001796 if (!msg_silent && !shortmess(SHM_FILEINFO))
1797 need_fileinfo = TRUE; // display file info after redraw
1798
1799 // check if file changed
1800 (void)buf_check_timestamp(curbuf, FALSE);
1801
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001803#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1807 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 }
1809
1810 /* If autocommands did not change the cursor position, restore cursor lnum
1811 * and possibly cursor col. */
1812 if (curwin->w_cursor.lnum == 1 && inindent(0))
1813 buflist_getfpos();
1814
1815 check_arg_idx(curwin); /* check for valid arg_idx */
1816#ifdef FEAT_TITLE
1817 maketitle();
1818#endif
Bram Moolenaard4153d42008-11-15 15:06:17 +00001819 /* when autocmds didn't change it */
1820 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1822
Bram Moolenaar009b2592004-10-24 19:18:58 +00001823#ifdef FEAT_NETBEANS_INTG
1824 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001825 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001826#endif
1827
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001828 /* Change directories when the 'acd' option is set. */
Bram Moolenaar6f470022018-04-10 18:47:20 +02001829 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830
1831#ifdef FEAT_KEYMAP
1832 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001833 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001835#ifdef FEAT_SPELL
1836 /* May need to set the spell language. Can only do this after the buffer
1837 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001838 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1839 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001840#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001841#ifdef FEAT_VIMINFO
1842 curbuf->b_last_used = vim_time();
1843#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001844
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 redraw_later(NOT_VALID);
1846}
1847
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001848#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1849/*
1850 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001851 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001852 */
1853 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001854do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001855{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001856 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001857 && curbuf->b_ffname != NULL
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001858 && vim_chdirfile(curbuf->b_ffname, "auto") == OK)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001859 shorten_fnames(TRUE);
1860}
1861#endif
1862
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001863 void
1864no_write_message(void)
1865{
1866#ifdef FEAT_TERMINAL
1867 if (term_job_running(curbuf->b_term))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001868 emsg(_("E948: Job still running (add ! to end the job)"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001869 else
1870#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001871 emsg(_("E37: No write since last change (add ! to override)"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001872}
1873
1874 void
Bram Moolenaar7a760922018-02-19 23:10:02 +01001875no_write_message_nobang(buf_T *buf UNUSED)
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001876{
1877#ifdef FEAT_TERMINAL
Bram Moolenaar7a760922018-02-19 23:10:02 +01001878 if (term_job_running(buf->b_term))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001879 emsg(_("E948: Job still running"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001880 else
1881#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001882 emsg(_("E37: No write since last change"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001883}
1884
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885/*
1886 * functions for dealing with the buffer list
1887 */
1888
1889/*
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001890 * Return TRUE if the current buffer is empty, unnamed, unmodified and used in
1891 * only one window. That means it can be re-used.
1892 */
1893 int
1894curbuf_reusable(void)
1895{
1896 return (curbuf != NULL
1897 && curbuf->b_ffname == NULL
1898 && curbuf->b_nwindows <= 1
1899 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY())
Bram Moolenaard85c3962019-04-07 14:19:14 +02001900#if defined(FEAT_QUICKFIX)
Bram Moolenaar39803d82019-04-07 12:04:51 +02001901 && !bt_quickfix(curbuf)
Bram Moolenaard85c3962019-04-07 14:19:14 +02001902#endif
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001903 && !curbufIsChanged());
1904}
1905
1906/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 * Add a file name to the buffer list. Return a pointer to the buffer.
1908 * If the same file name already exists return a pointer to that buffer.
1909 * If it does not exist, or if fname == NULL, a new entry is created.
1910 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1911 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1912 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001913 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001914 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
1915 * if the buffer already exists.
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02001916 * If (flags & BLN_REUSE) is TRUE, may use buffer number from "buf_reuse".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 * This is the ONLY way to create a new buffer.
1918 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001920buflist_new(
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001921 char_u *ffname_arg, // full path of fname or relative
1922 char_u *sfname_arg, // short fname or NULL
1923 linenr_T lnum, // preferred cursor line
1924 int flags) // BLN_ defines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001926 char_u *ffname = ffname_arg;
1927 char_u *sfname = sfname_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 buf_T *buf;
1929#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001930 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931#endif
1932
Bram Moolenaar480778b2016-07-14 22:09:39 +02001933 if (top_file_num == 1)
1934 hash_init(&buf_hashtab);
1935
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001936 fname_expand(curbuf, &ffname, &sfname); // will allocate ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937
1938 /*
1939 * If file name already exists in the list, update the entry.
1940 */
1941#ifdef UNIX
1942 /* On Unix we can use inode numbers when the file exists. Works better
1943 * for hard links. */
1944 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1945 st.st_dev = (dev_T)-1;
1946#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001947 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948#ifdef UNIX
1949 buflist_findname_stat(ffname, &st)
1950#else
1951 buflist_findname(ffname)
1952#endif
1953 ) != NULL)
1954 {
1955 vim_free(ffname);
1956 if (lnum != 0)
1957 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02001958
1959 if ((flags & BLN_NOOPT) == 0)
1960 /* copy the options now, if 'cpo' doesn't have 's' and not done
1961 * already */
1962 buf_copy_options(buf, 0);
1963
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1965 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001966 bufref_T bufref;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001967
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 buf->b_p_bl = TRUE;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001969 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001971 {
Bram Moolenaar82404332016-07-10 17:00:38 +02001972 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001973 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001974 return NULL;
1975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 }
1977 return buf;
1978 }
1979
1980 /*
1981 * If the current buffer has no name and no contents, use the current
1982 * buffer. Otherwise: Need to allocate a new buffer structure.
1983 *
1984 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001985 * (A spell file buffer is allocated in spell.c, but that's not a normal
1986 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 */
1988 buf = NULL;
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001989 if ((flags & BLN_CURBUF) && curbuf_reusable())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 {
1991 buf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 /* It's like this buffer is deleted. Watch out for autocommands that
1993 * change curbuf! If that happens, allocate a new buffer anyway. */
1994 if (curbuf->b_p_bl)
1995 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1996 if (buf == curbuf)
1997 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001998#ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001999 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 if (buf == curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 {
2004 /* Make sure 'bufhidden' and 'buftype' are empty */
2005 clear_string_option(&buf->b_p_bh);
2006 clear_string_option(&buf->b_p_bt);
2007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 }
2009 if (buf != curbuf || curbuf == NULL)
2010 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002011 buf = ALLOC_CLEAR_ONE(buf_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 if (buf == NULL)
2013 {
2014 vim_free(ffname);
2015 return NULL;
2016 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02002017#ifdef FEAT_EVAL
2018 /* init b: variables */
2019 buf->b_vars = dict_alloc();
2020 if (buf->b_vars == NULL)
2021 {
2022 vim_free(ffname);
2023 vim_free(buf);
2024 return NULL;
2025 }
2026 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
2027#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01002028 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 }
2030
2031 if (ffname != NULL)
2032 {
2033 buf->b_ffname = ffname;
2034 buf->b_sfname = vim_strsave(sfname);
2035 }
2036
2037 clear_wininfo(buf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002038 buf->b_wininfo = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039
2040 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
2041 || buf->b_wininfo == NULL)
2042 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002043 if (buf->b_sfname != buf->b_ffname)
2044 VIM_CLEAR(buf->b_sfname);
2045 else
2046 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01002047 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 if (buf != curbuf)
2049 free_buffer(buf);
2050 return NULL;
2051 }
2052
2053 if (buf == curbuf)
2054 {
2055 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002056 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 if (buf != curbuf) /* autocommands deleted the buffer! */
2058 return NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002059#if defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002060 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 return NULL;
2062#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002064
2065 /* Init the options. */
2066 buf->b_p_initialized = FALSE;
2067 buf_copy_options(buf, BCO_ENTER);
2068
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069#ifdef FEAT_KEYMAP
2070 /* need to reload lmaps and set b:keymap_name */
2071 curbuf->b_kmap_state |= KEYMAP_INIT;
2072#endif
2073 }
2074 else
2075 {
2076 /*
2077 * put new buffer at the end of the buffer list
2078 */
2079 buf->b_next = NULL;
2080 if (firstbuf == NULL) /* buffer list is empty */
2081 {
2082 buf->b_prev = NULL;
2083 firstbuf = buf;
2084 }
2085 else /* append new buffer at end of list */
2086 {
2087 lastbuf->b_next = buf;
2088 buf->b_prev = lastbuf;
2089 }
2090 lastbuf = buf;
2091
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02002092 if ((flags & BLN_REUSE) && buf_reuse.ga_len > 0)
2093 {
2094 // Recycle a previously used buffer number. Used for buffers which
2095 // are normally hidden, e.g. in a popup window. Avoids that the
2096 // buffer number grows rapidly.
2097 --buf_reuse.ga_len;
2098 buf->b_fnum = ((int *)buf_reuse.ga_data)[buf_reuse.ga_len];
2099 }
2100 else
2101 buf->b_fnum = top_file_num++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 if (top_file_num < 0) /* wrap around (may cause duplicates) */
2103 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002104 emsg(_("W14: Warning: List of file names overflow"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 if (emsg_silent == 0)
2106 {
2107 out_flush();
2108 ui_delay(3000L, TRUE); /* make sure it is noticed */
2109 }
2110 top_file_num = 1;
2111 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002112 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113
2114 /*
2115 * Always copy the options from the current buffer.
2116 */
2117 buf_copy_options(buf, BCO_ALWAYS);
2118 }
2119
2120 buf->b_wininfo->wi_fpos.lnum = lnum;
2121 buf->b_wininfo->wi_win = curwin;
2122
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002123#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002124 hash_init(&buf->b_s.b_keywtab);
2125 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126#endif
2127
2128 buf->b_fname = buf->b_sfname;
2129#ifdef UNIX
2130 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002131 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 else
2133 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002134 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 buf->b_dev = st.st_dev;
2136 buf->b_ino = st.st_ino;
2137 }
2138#endif
2139 buf->b_u_synced = TRUE;
2140 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002141 if (flags & BLN_DUMMY)
2142 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 buf_clear_file(buf);
2144 clrallmarks(buf); /* clear marks */
2145 fmarks_check_names(buf); /* check file marks for this file */
2146 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 if (!(flags & BLN_DUMMY))
2148 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002149 bufref_T bufref;
2150
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01002151 /* Tricky: these autocommands may change the buffer list. They could
2152 * also split the window with re-using the one empty buffer. This may
2153 * result in unexpectedly losing the empty buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002154 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002155 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002156 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002157 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002159 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002160 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002161 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002162 return NULL;
2163 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002164#ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002165 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169
2170 return buf;
2171}
2172
2173/*
2174 * Free the memory for the options of a buffer.
2175 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2176 * 'fileencoding'.
2177 */
2178 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002179free_buf_options(
2180 buf_T *buf,
2181 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182{
2183 if (free_p_ff)
2184 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 clear_string_option(&buf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 clear_string_option(&buf->b_p_bh);
2188 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 }
2190#ifdef FEAT_FIND_ID
2191 clear_string_option(&buf->b_p_def);
2192 clear_string_option(&buf->b_p_inc);
2193# ifdef FEAT_EVAL
2194 clear_string_option(&buf->b_p_inex);
2195# endif
2196#endif
2197#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
2198 clear_string_option(&buf->b_p_inde);
2199 clear_string_option(&buf->b_p_indk);
2200#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002201#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2202 clear_string_option(&buf->b_p_bexpr);
2203#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002204#if defined(FEAT_CRYPT)
2205 clear_string_option(&buf->b_p_cm);
2206#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002207 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002208#if defined(FEAT_EVAL)
2209 clear_string_option(&buf->b_p_fex);
2210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211#ifdef FEAT_CRYPT
2212 clear_string_option(&buf->b_p_key);
2213#endif
2214 clear_string_option(&buf->b_p_kp);
2215 clear_string_option(&buf->b_p_mps);
2216 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002217 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 clear_string_option(&buf->b_p_isk);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002219#ifdef FEAT_VARTABS
2220 clear_string_option(&buf->b_p_vsts);
Bram Moolenaarbdace832019-03-02 10:13:42 +01002221 vim_free(buf->b_p_vsts_nopaste);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002222 buf->b_p_vsts_nopaste = NULL;
Bram Moolenaarbdace832019-03-02 10:13:42 +01002223 vim_free(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002224 buf->b_p_vsts_array = NULL;
2225 clear_string_option(&buf->b_p_vts);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002226 VIM_CLEAR(buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228#ifdef FEAT_KEYMAP
2229 clear_string_option(&buf->b_p_keymap);
Bram Moolenaar50138322018-01-28 17:05:16 +01002230 keymap_clear(&buf->b_kmap_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 ga_clear(&buf->b_kmap_ga);
2232#endif
2233#ifdef FEAT_COMMENTS
2234 clear_string_option(&buf->b_p_com);
2235#endif
2236#ifdef FEAT_FOLDING
2237 clear_string_option(&buf->b_p_cms);
2238#endif
2239 clear_string_option(&buf->b_p_nf);
2240#ifdef FEAT_SYN_HL
2241 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002242 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002243#endif
2244#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002245 clear_string_option(&buf->b_s.b_p_spc);
2246 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002247 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002248 buf->b_s.b_cap_prog = NULL;
2249 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250#endif
2251#ifdef FEAT_SEARCHPATH
2252 clear_string_option(&buf->b_p_sua);
2253#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 clear_string_option(&buf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255#ifdef FEAT_CINDENT
2256 clear_string_option(&buf->b_p_cink);
2257 clear_string_option(&buf->b_p_cino);
2258#endif
2259#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
2260 clear_string_option(&buf->b_p_cinw);
2261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 clear_string_option(&buf->b_p_cpt);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002263#ifdef FEAT_COMPL_FUNC
2264 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002265 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002266#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267#ifdef FEAT_QUICKFIX
2268 clear_string_option(&buf->b_p_gp);
2269 clear_string_option(&buf->b_p_mp);
2270 clear_string_option(&buf->b_p_efm);
2271#endif
2272 clear_string_option(&buf->b_p_ep);
2273 clear_string_option(&buf->b_p_path);
2274 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002275 clear_string_option(&buf->b_p_tc);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002276#ifdef FEAT_EVAL
2277 clear_string_option(&buf->b_p_tfu);
2278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 clear_string_option(&buf->b_p_dict);
2280 clear_string_option(&buf->b_p_tsr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002281#ifdef FEAT_TEXTOBJ
2282 clear_string_option(&buf->b_p_qe);
2283#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002285 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002286#ifdef FEAT_LISP
2287 clear_string_option(&buf->b_p_lw);
2288#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002289 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002290 clear_string_option(&buf->b_p_menc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291}
2292
2293/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002294 * Get alternate file "n".
2295 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2296 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 * if (options & GETF_SETMARK) call setpcmark()
2298 * if (options & GETF_ALT) we are jumping to an alternate file.
2299 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2300 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002301 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 */
2303 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002304buflist_getfile(
2305 int n,
2306 linenr_T lnum,
2307 int options,
2308 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309{
2310 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 pos_T *fpos;
2313 colnr_T col;
2314
2315 buf = buflist_findnr(n);
2316 if (buf == NULL)
2317 {
2318 if ((options & GETF_ALT) && n == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002319 emsg(_(e_noalt));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 else
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01002321 semsg(_("E92: Buffer %d not found"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 return FAIL;
2323 }
2324
2325 /* if alternate file is the current buffer, nothing to do */
2326 if (buf == curbuf)
2327 return OK;
2328
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002329 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002330 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002331 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002333 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002334 if (curbuf_locked())
2335 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336
2337 /* altfpos may be changed by getfile(), get it now */
2338 if (lnum == 0)
2339 {
2340 fpos = buflist_findfpos(buf);
2341 lnum = fpos->lnum;
2342 col = fpos->col;
2343 }
2344 else
2345 col = 0;
2346
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 if (options & GETF_SWITCH)
2348 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002349 /* If 'switchbuf' contains "useopen": jump to first window containing
2350 * "buf" if one exists */
2351 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002353
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002354 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00002355 * page containing "buf" if one exists */
2356 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002357 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002358
2359 /* If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2360 * current buffer isn't empty: open new tab or window */
2361 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002362 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002364 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002365 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002366 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2367 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002369 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 }
2371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372
2373 ++RedrawingDisabled;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002374 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2375 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 {
2377 --RedrawingDisabled;
2378
2379 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
2380 if (!p_sol && col != 0)
2381 {
2382 curwin->w_cursor.col = col;
2383 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 curwin->w_set_curswant = TRUE;
2386 }
2387 return OK;
2388 }
2389 --RedrawingDisabled;
2390 return FAIL;
2391}
2392
2393/*
2394 * go to the last know line number for the current buffer
2395 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002397buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398{
2399 pos_T *fpos;
2400
2401 fpos = buflist_findfpos(curbuf);
2402
2403 curwin->w_cursor.lnum = fpos->lnum;
2404 check_cursor_lnum();
2405
2406 if (p_sol)
2407 curwin->w_cursor.col = 0;
2408 else
2409 {
2410 curwin->w_cursor.col = fpos->col;
2411 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 curwin->w_set_curswant = TRUE;
2414 }
2415}
2416
Bram Moolenaar81695252004-12-29 20:58:21 +00002417#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2418/*
2419 * Find file in buffer list by name (it has to be for the current window).
2420 * Returns NULL if not found.
2421 */
2422 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002423buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002424{
2425 char_u *ffname;
2426 buf_T *buf = NULL;
2427
2428 /* First make the name into a full path name */
2429 ffname = FullName_save(fname,
2430#ifdef UNIX
2431 TRUE /* force expansion, get rid of symbolic links */
2432#else
2433 FALSE
2434#endif
2435 );
2436 if (ffname != NULL)
2437 {
2438 buf = buflist_findname(ffname);
2439 vim_free(ffname);
2440 }
2441 return buf;
2442}
2443#endif
2444
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445/*
2446 * Find file in buffer list by name (it has to be for the current window).
2447 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002448 * Skips dummy buffers.
2449 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 */
2451 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002452buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453{
2454#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002455 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456
2457 if (mch_stat((char *)ffname, &st) < 0)
2458 st.st_dev = (dev_T)-1;
2459 return buflist_findname_stat(ffname, &st);
2460}
2461
2462/*
2463 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2464 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002465 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 */
2467 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002468buflist_findname_stat(
2469 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002470 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471{
2472#endif
2473 buf_T *buf;
2474
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002475 /* Start at the last buffer, expect to find a match sooner. */
2476 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar81695252004-12-29 20:58:21 +00002477 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478#ifdef UNIX
2479 , stp
2480#endif
2481 ))
2482 return buf;
2483 return NULL;
2484}
2485
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486/*
2487 * Find file in buffer list by a regexp pattern.
2488 * Return fnum of the found buffer.
2489 * Return < 0 for error.
2490 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002492buflist_findpat(
2493 char_u *pattern,
2494 char_u *pattern_end, /* pointer to first char after pattern */
2495 int unlisted, /* find unlisted buffers */
2496 int diffmode UNUSED, /* find diff-mode buffers only */
2497 int curtab_only) /* find buffers in current tab only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498{
2499 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 int match = -1;
2501 int find_listed;
2502 char_u *pat;
2503 char_u *patend;
2504 int attempt;
2505 char_u *p;
2506 int toggledollar;
2507
2508 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2509 {
2510 if (*pattern == '%')
2511 match = curbuf->b_fnum;
2512 else
2513 match = curwin->w_alt_fnum;
2514#ifdef FEAT_DIFF
2515 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2516 match = -1;
2517#endif
2518 }
2519
2520 /*
2521 * Try four ways of matching a listed buffer:
2522 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002523 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 * attempt == 2: with '$' at end (only match at end)
2525 * attempt == 3: with '^' at start and '$' at end (only full match)
2526 * Repeat this for finding an unlisted buffer if there was no matching
2527 * listed buffer.
2528 */
2529 else
2530 {
2531 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2532 if (pat == NULL)
2533 return -1;
2534 patend = pat + STRLEN(pat) - 1;
2535 toggledollar = (patend > pat && *patend == '$');
2536
2537 /* First try finding a listed buffer. If not found and "unlisted"
2538 * is TRUE, try finding an unlisted buffer. */
2539 find_listed = TRUE;
2540 for (;;)
2541 {
2542 for (attempt = 0; attempt <= 3; ++attempt)
2543 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002544 regmatch_T regmatch;
2545
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 /* may add '^' and '$' */
2547 if (toggledollar)
2548 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2549 p = pat;
2550 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2551 ++p;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002552 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2553 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 {
2555 vim_free(pat);
2556 return -1;
2557 }
2558
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002559 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 if (buf->b_p_bl == find_listed
2561#ifdef FEAT_DIFF
2562 && (!diffmode || diff_mode_buf(buf))
2563#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002564 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002566 if (curtab_only)
2567 {
2568 /* Ignore the match if the buffer is not open in
2569 * the current tab. */
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002570 win_T *wp;
2571
Bram Moolenaar29323592016-07-24 22:04:11 +02002572 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002573 if (wp->w_buffer == buf)
2574 break;
2575 if (wp == NULL)
2576 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 if (match >= 0) /* already found a match */
2579 {
2580 match = -2;
2581 break;
2582 }
2583 match = buf->b_fnum; /* remember first match */
2584 }
2585
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002586 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 if (match >= 0) /* found one match */
2588 break;
2589 }
2590
2591 /* Only search for unlisted buffers if there was no match with
2592 * a listed buffer. */
2593 if (!unlisted || !find_listed || match != -1)
2594 break;
2595 find_listed = FALSE;
2596 }
2597
2598 vim_free(pat);
2599 }
2600
2601 if (match == -2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002602 semsg(_("E93: More than one match for %s"), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 else if (match < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002604 semsg(_("E94: No matching buffer for %s"), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 return match;
2606}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608/*
2609 * Find all buffer names that match.
2610 * For command line expansion of ":buf" and ":sbuf".
2611 * Return OK if matches found, FAIL otherwise.
2612 */
2613 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002614ExpandBufnames(
2615 char_u *pat,
2616 int *num_file,
2617 char_u ***file,
2618 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619{
2620 int count = 0;
2621 buf_T *buf;
2622 int round;
2623 char_u *p;
2624 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002625 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626
2627 *num_file = 0; /* return values in case of FAIL */
2628 *file = NULL;
2629
Bram Moolenaar05159a02005-02-26 23:04:13 +00002630 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2631 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002633 patc = alloc(STRLEN(pat) + 11);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002634 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002636 STRCPY(patc, "\\(^\\|[\\/]\\)");
2637 STRCPY(patc + 11, pat + 1);
2638 }
2639 else
2640 patc = pat;
2641
2642 /*
2643 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002644 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002645 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002646 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002647 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002648 regmatch_T regmatch;
2649
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002650 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002651 break; /* there was no anchor, no need to try again */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002652 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2653 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002654 {
2655 if (patc != pat)
2656 vim_free(patc);
2657 return FAIL;
2658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659
2660 /*
2661 * round == 1: Count the matches.
2662 * round == 2: Build the array to keep the matches.
2663 */
2664 for (round = 1; round <= 2; ++round)
2665 {
2666 count = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002667 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 {
2669 if (!buf->b_p_bl) /* skip unlisted buffers */
2670 continue;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002671 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 if (p != NULL)
2673 {
2674 if (round == 1)
2675 ++count;
2676 else
2677 {
2678 if (options & WILD_HOME_REPLACE)
2679 p = home_replace_save(buf, p);
2680 else
2681 p = vim_strsave(p);
2682 (*file)[count++] = p;
2683 }
2684 }
2685 }
2686 if (count == 0) /* no match found, break here */
2687 break;
2688 if (round == 1)
2689 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002690 *file = ALLOC_MULT(char_u *, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 if (*file == NULL)
2692 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002693 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002694 if (patc != pat)
2695 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 return FAIL;
2697 }
2698 }
2699 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002700 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 if (count) /* match(es) found, break here */
2702 break;
2703 }
2704
Bram Moolenaar05159a02005-02-26 23:04:13 +00002705 if (patc != pat)
2706 vim_free(patc);
2707
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 *num_file = count;
2709 return (count == 0 ? FAIL : OK);
2710}
2711
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712/*
2713 * Check for a match on the file name for buffer "buf" with regprog "prog".
2714 */
2715 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002716buflist_match(
2717 regmatch_T *rmp,
2718 buf_T *buf,
2719 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720{
2721 char_u *match;
2722
2723 /* First try the short file name, then the long file name. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002724 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002726 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727
2728 return match;
2729}
2730
2731/*
2732 * Try matching the regexp in "prog" with file name "name".
2733 * Return "name" when there is a match, NULL when not.
2734 */
2735 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002736fname_match(
2737 regmatch_T *rmp,
2738 char_u *name,
2739 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740{
2741 char_u *match = NULL;
2742 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743
2744 if (name != NULL)
2745 {
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002746 /* Ignore case when 'fileignorecase' or the argument is set. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002747 rmp->rm_ic = p_fic || ignore_case;
2748 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 match = name;
2750 else
2751 {
2752 /* Replace $(HOME) with '~' and try matching again. */
2753 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002754 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 match = name;
2756 vim_free(p);
2757 }
2758 }
2759
2760 return match;
2761}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762
2763/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02002764 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 */
2766 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002767buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768{
Bram Moolenaar480778b2016-07-14 22:09:39 +02002769 char_u key[VIM_SIZEOF_INT * 2 + 1];
2770 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771
2772 if (nr == 0)
2773 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02002774 sprintf((char *)key, "%x", nr);
2775 hi = hash_find(&buf_hashtab, key);
2776
2777 if (!HASHITEM_EMPTY(hi))
2778 return (buf_T *)(hi->hi_key
2779 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 return NULL;
2781}
2782
2783/*
2784 * Get name of file 'n' in the buffer list.
2785 * When the file has no name an empty string is returned.
2786 * home_replace() is used to shorten the file name (used for marks).
2787 * Returns a pointer to allocated memory, of NULL when failed.
2788 */
2789 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002790buflist_nr2name(
2791 int n,
2792 int fullname,
2793 int helptail) /* for help buffers return tail only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794{
2795 buf_T *buf;
2796
2797 buf = buflist_findnr(n);
2798 if (buf == NULL)
2799 return NULL;
2800 return home_replace_save(helptail ? buf : NULL,
2801 fullname ? buf->b_ffname : buf->b_fname);
2802}
2803
2804/*
2805 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2806 * When "copy_options" is TRUE save the local window option values.
2807 * When "lnum" is 0 only do the options.
2808 */
Bram Moolenaardefa0672019-07-21 19:25:37 +02002809 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002810buflist_setfpos(
2811 buf_T *buf,
2812 win_T *win,
2813 linenr_T lnum,
2814 colnr_T col,
2815 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816{
2817 wininfo_T *wip;
2818
2819 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2820 if (wip->wi_win == win)
2821 break;
2822 if (wip == NULL)
2823 {
2824 /* allocate a new entry */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002825 wip = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 if (wip == NULL)
2827 return;
2828 wip->wi_win = win;
2829 if (lnum == 0) /* set lnum even when it's 0 */
2830 lnum = 1;
2831 }
2832 else
2833 {
2834 /* remove the entry from the list */
2835 if (wip->wi_prev)
2836 wip->wi_prev->wi_next = wip->wi_next;
2837 else
2838 buf->b_wininfo = wip->wi_next;
2839 if (wip->wi_next)
2840 wip->wi_next->wi_prev = wip->wi_prev;
2841 if (copy_options && wip->wi_optset)
2842 {
2843 clear_winopt(&wip->wi_opt);
2844#ifdef FEAT_FOLDING
2845 deleteFoldRecurse(&wip->wi_folds);
2846#endif
2847 }
2848 }
2849 if (lnum != 0)
2850 {
2851 wip->wi_fpos.lnum = lnum;
2852 wip->wi_fpos.col = col;
2853 }
2854 if (copy_options)
2855 {
2856 /* Save the window-specific option values. */
2857 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2858#ifdef FEAT_FOLDING
2859 wip->wi_fold_manual = win->w_fold_manual;
2860 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2861#endif
2862 wip->wi_optset = TRUE;
2863 }
2864
2865 /* insert the entry in front of the list */
2866 wip->wi_next = buf->b_wininfo;
2867 buf->b_wininfo = wip;
2868 wip->wi_prev = NULL;
2869 if (wip->wi_next)
2870 wip->wi_next->wi_prev = wip;
2871
2872 return;
2873}
2874
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002875#ifdef FEAT_DIFF
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002876/*
2877 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2878 * page. That's because a diff is local to a tab page.
2879 */
2880 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002881wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002882{
2883 win_T *wp;
2884
2885 if (wip->wi_opt.wo_diff)
2886 {
Bram Moolenaar29323592016-07-24 22:04:11 +02002887 FOR_ALL_WINDOWS(wp)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002888 /* return FALSE when it's a window in the current tab page, thus
2889 * the buffer was in diff mode here */
2890 if (wip->wi_win == wp)
2891 return FALSE;
2892 return TRUE;
2893 }
2894 return FALSE;
2895}
2896#endif
2897
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898/*
2899 * Find info for the current window in buffer "buf".
2900 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002901 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2902 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 * Returns NULL when there isn't any info.
2904 */
2905 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002906find_wininfo(
2907 buf_T *buf,
2908 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909{
2910 wininfo_T *wip;
2911
2912 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002913 if (wip->wi_win == curwin
2914#ifdef FEAT_DIFF
2915 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2916#endif
2917 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002919
2920 /* If no wininfo for curwin, use the first in the list (that doesn't have
2921 * 'diff' set and is in another tab page). */
2922 if (wip == NULL)
2923 {
2924#ifdef FEAT_DIFF
2925 if (skip_diff_buffer)
2926 {
2927 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2928 if (!wininfo_other_tab_diff(wip))
2929 break;
2930 }
2931 else
2932#endif
2933 wip = buf->b_wininfo;
2934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 return wip;
2936}
2937
2938/*
2939 * Reset the local window options to the values last used in this window.
2940 * If the buffer wasn't used in this window before, use the values from
2941 * the most recently used window. If the values were never set, use the
2942 * global values for the window.
2943 */
2944 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002945get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946{
2947 wininfo_T *wip;
2948
2949 clear_winopt(&curwin->w_onebuf_opt);
2950#ifdef FEAT_FOLDING
2951 clearFolding(curwin);
2952#endif
2953
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002954 wip = find_wininfo(buf, TRUE);
Bram Moolenaar25782a72018-05-13 18:05:33 +02002955 if (wip != NULL && wip->wi_win != NULL
2956 && wip->wi_win != curwin && wip->wi_win->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 {
Bram Moolenaar25782a72018-05-13 18:05:33 +02002958 /* The buffer is currently displayed in the window: use the actual
2959 * option values instead of the saved (possibly outdated) values. */
2960 win_T *wp = wip->wi_win;
2961
2962 copy_winopt(&wp->w_onebuf_opt, &curwin->w_onebuf_opt);
2963#ifdef FEAT_FOLDING
2964 curwin->w_fold_manual = wp->w_fold_manual;
2965 curwin->w_foldinvalid = TRUE;
2966 cloneFoldGrowArray(&wp->w_folds, &curwin->w_folds);
2967#endif
2968 }
2969 else if (wip != NULL && wip->wi_optset)
2970 {
2971 /* the buffer was displayed in the current window earlier */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2973#ifdef FEAT_FOLDING
2974 curwin->w_fold_manual = wip->wi_fold_manual;
2975 curwin->w_foldinvalid = TRUE;
2976 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2977#endif
2978 }
2979 else
2980 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2981
2982#ifdef FEAT_FOLDING
2983 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2984 if (p_fdls >= 0)
2985 curwin->w_p_fdl = p_fdls;
2986#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002987#ifdef FEAT_SYN_HL
2988 check_colorcolumn(curwin);
2989#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990}
2991
2992/*
2993 * Find the position (lnum and col) for the buffer 'buf' for the current
2994 * window.
2995 * Returns a pointer to no_position if no position is found.
2996 */
2997 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002998buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999{
3000 wininfo_T *wip;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003001 static pos_T no_position = {1, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003003 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 if (wip != NULL)
3005 return &(wip->wi_fpos);
3006 else
3007 return &no_position;
3008}
3009
3010/*
3011 * Find the lnum for the buffer 'buf' for the current window.
3012 */
3013 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01003014buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015{
3016 return buflist_findfpos(buf)->lnum;
3017}
3018
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02003020 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003023buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024{
3025 buf_T *buf;
3026 int len;
3027 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003028 int ro_char;
3029 int changed_char;
Bram Moolenaar0751f512018-03-29 16:37:16 +02003030#ifdef FEAT_TERMINAL
3031 int job_running;
3032 int job_none_open;
3033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034
3035 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
3036 {
Bram Moolenaar0751f512018-03-29 16:37:16 +02003037#ifdef FEAT_TERMINAL
3038 job_running = term_job_running(buf->b_term);
3039 job_none_open = job_running && term_none_open(buf->b_term);
3040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 /* skip unlisted buffers, unless ! was used */
Bram Moolenaard51cb702015-07-21 15:03:06 +02003042 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
3043 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
3044 || (vim_strchr(eap->arg, '+')
3045 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
3046 || (vim_strchr(eap->arg, 'a')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003047 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
Bram Moolenaard51cb702015-07-21 15:03:06 +02003048 || (vim_strchr(eap->arg, 'h')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003049 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3050#ifdef FEAT_TERMINAL
3051 || (vim_strchr(eap->arg, 'R')
3052 && (!job_running || (job_running && job_none_open)))
3053 || (vim_strchr(eap->arg, '?')
3054 && (!job_running || (job_running && !job_none_open)))
3055 || (vim_strchr(eap->arg, 'F')
3056 && (job_running || buf->b_term == NULL))
3057#endif
Bram Moolenaard51cb702015-07-21 15:03:06 +02003058 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3059 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3060 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3061 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3062 || (vim_strchr(eap->arg, '#')
3063 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003066 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 else
3068 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003069 if (message_filtered(NameBuff))
3070 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003072 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3073 : (bufIsChanged(buf) ? '+' : ' ');
3074#ifdef FEAT_TERMINAL
3075 if (term_job_running(buf->b_term))
3076 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02003077 if (term_none_open(buf->b_term))
3078 ro_char = '?';
3079 else
3080 ro_char = 'R';
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003081 changed_char = ' '; /* bufIsChanged() returns TRUE to avoid
3082 * closing, but it's not actually changed. */
3083 }
3084 else if (buf->b_term != NULL)
3085 ro_char = 'F';
3086 else
3087#endif
3088 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3089
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003090 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00003091 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 buf->b_fnum,
3093 buf->b_p_bl ? ' ' : 'u',
3094 buf == curbuf ? '%' :
3095 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3096 buf->b_ml.ml_mfp == NULL ? ' ' :
3097 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003098 ro_char,
3099 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003100 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003101 if (len > IOSIZE - 20)
3102 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103
3104 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 i = 40 - vim_strsize(IObuff);
3106 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 IObuff[len++] = ' ';
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003108 while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003109 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3110 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003111 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 msg_outtrans(IObuff);
3113 out_flush(); /* output one line at a time */
3114 ui_breakcheck();
3115 }
3116}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117
3118/*
3119 * Get file name and line number for file 'fnum'.
3120 * Used by DoOneCmd() for translating '%' and '#'.
3121 * Used by insert_reg() and cmdline_paste() for '#' register.
3122 * Return FAIL if not found, OK for success.
3123 */
3124 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003125buflist_name_nr(
3126 int fnum,
3127 char_u **fname,
3128 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129{
3130 buf_T *buf;
3131
3132 buf = buflist_findnr(fnum);
3133 if (buf == NULL || buf->b_fname == NULL)
3134 return FAIL;
3135
3136 *fname = buf->b_fname;
3137 *lnum = buflist_findlnum(buf);
3138
3139 return OK;
3140}
3141
3142/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003143 * Set the file name for "buf"' to "ffname_arg", short file name to
3144 * "sfname_arg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 * The file name with the full path is also remembered, for when :cd is used.
3146 * Returns FAIL for failure (file name already in use by other buffer)
3147 * OK otherwise.
3148 */
3149 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003150setfname(
3151 buf_T *buf,
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003152 char_u *ffname_arg,
3153 char_u *sfname_arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +01003154 int message) /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003156 char_u *ffname = ffname_arg;
3157 char_u *sfname = sfname_arg;
Bram Moolenaar81695252004-12-29 20:58:21 +00003158 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003160 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161#endif
3162
3163 if (ffname == NULL || *ffname == NUL)
3164 {
3165 /* Removing the name. */
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003166 if (buf->b_sfname != buf->b_ffname)
3167 VIM_CLEAR(buf->b_sfname);
3168 else
3169 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003170 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171#ifdef UNIX
3172 st.st_dev = (dev_T)-1;
3173#endif
3174 }
3175 else
3176 {
3177 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
3178 if (ffname == NULL) /* out of memory */
3179 return FAIL;
3180
3181 /*
3182 * if the file name is already used in another buffer:
3183 * - if the buffer is loaded, fail
3184 * - if the buffer is not loaded, delete it from the list
3185 */
3186#ifdef UNIX
3187 if (mch_stat((char *)ffname, &st) < 0)
3188 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003189#endif
3190 if (!(buf->b_flags & BF_DUMMY))
3191#ifdef UNIX
3192 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003194 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195#endif
3196 if (obuf != NULL && obuf != buf)
3197 {
3198 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
3199 {
3200 if (message)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003201 emsg(_("E95: Buffer with this name already exists"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 vim_free(ffname);
3203 return FAIL;
3204 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01003205 /* delete from the list */
3206 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 }
3208 sfname = vim_strsave(sfname);
3209 if (ffname == NULL || sfname == NULL)
3210 {
3211 vim_free(sfname);
3212 vim_free(ffname);
3213 return FAIL;
3214 }
3215#ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003216 fname_case(sfname, 0); /* set correct case for short file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217#endif
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003218 if (buf->b_sfname != buf->b_ffname)
3219 vim_free(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 vim_free(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 buf->b_ffname = ffname;
3222 buf->b_sfname = sfname;
3223 }
3224 buf->b_fname = buf->b_sfname;
3225#ifdef UNIX
3226 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003227 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 else
3229 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003230 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 buf->b_dev = st.st_dev;
3232 buf->b_ino = st.st_ino;
3233 }
3234#endif
3235
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237
3238 buf_name_changed(buf);
3239 return OK;
3240}
3241
3242/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003243 * Crude way of changing the name of a buffer. Use with care!
3244 * The name should be relative to the current directory.
3245 */
3246 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003247buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003248{
3249 buf_T *buf;
3250
3251 buf = buflist_findnr(fnum);
3252 if (buf != NULL)
3253 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003254 if (buf->b_sfname != buf->b_ffname)
3255 vim_free(buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003256 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003257 buf->b_ffname = vim_strsave(name);
3258 buf->b_sfname = NULL;
3259 /* Allocate ffname and expand into full path. Also resolves .lnk
3260 * files on Win32. */
3261 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003262 buf->b_fname = buf->b_sfname;
3263 }
3264}
3265
3266/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 * Take care of what needs to be done when the name of buffer "buf" has
3268 * changed.
3269 */
3270 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003271buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272{
3273 /*
3274 * If the file name changed, also change the name of the swapfile
3275 */
3276 if (buf->b_ml.ml_mfp != NULL)
3277 ml_setname(buf);
3278
3279 if (curwin->w_buffer == buf)
3280 check_arg_idx(curwin); /* check file name for arg list */
3281#ifdef FEAT_TITLE
3282 maketitle(); /* set window title */
3283#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 status_redraw_all(); /* status lines need to be redrawn */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 fmarks_check_names(buf); /* check named file marks */
3286 ml_timestamp(buf); /* reset timestamp */
3287}
3288
3289/*
3290 * set alternate file name for current window
3291 *
3292 * Used by do_one_cmd(), do_write() and do_ecmd().
3293 * Return the buffer.
3294 */
3295 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003296setaltfname(
3297 char_u *ffname,
3298 char_u *sfname,
3299 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300{
3301 buf_T *buf;
3302
3303 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
3304 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003305 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 curwin->w_alt_fnum = buf->b_fnum;
3307 return buf;
3308}
3309
3310/*
3311 * Get alternate file name for current window.
3312 * Return NULL if there isn't any, and give error message if requested.
3313 */
3314 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003315getaltfname(
3316 int errmsg) /* give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317{
3318 char_u *fname;
3319 linenr_T dummy;
3320
3321 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3322 {
3323 if (errmsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003324 emsg(_(e_noalt));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 return NULL;
3326 }
3327 return fname;
3328}
3329
3330/*
3331 * Add a file name to the buflist and return its number.
3332 * Uses same flags as buflist_new(), except BLN_DUMMY.
3333 *
3334 * used by qf_init(), main() and doarglist()
3335 */
3336 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003337buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338{
3339 buf_T *buf;
3340
3341 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3342 if (buf != NULL)
3343 return buf->b_fnum;
3344 return 0;
3345}
3346
3347#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3348/*
3349 * Adjust slashes in file names. Called after 'shellslash' was set.
3350 */
3351 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003352buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353{
3354 buf_T *bp;
3355
Bram Moolenaar29323592016-07-24 22:04:11 +02003356 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 {
3358 if (bp->b_ffname != NULL)
3359 slash_adjust(bp->b_ffname);
3360 if (bp->b_sfname != NULL)
3361 slash_adjust(bp->b_sfname);
3362 }
3363}
3364#endif
3365
3366/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003367 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 * Also save the local window option values.
3369 */
3370 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003371buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003373 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374}
3375
3376/*
3377 * Return TRUE if 'ffname' is not the same file as current file.
3378 * Fname must have a full path (expanded by mch_FullName()).
3379 */
3380 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003381otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382{
3383 return otherfile_buf(curbuf, ffname
3384#ifdef UNIX
3385 , NULL
3386#endif
3387 );
3388}
3389
3390 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003391otherfile_buf(
3392 buf_T *buf,
3393 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003395 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003397 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398{
3399 /* no name is different */
3400 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3401 return TRUE;
3402 if (fnamecmp(ffname, buf->b_ffname) == 0)
3403 return FALSE;
3404#ifdef UNIX
3405 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003406 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407
Bram Moolenaar8767f522016-07-01 17:17:39 +02003408 /* If no stat_T given, get it now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 if (stp == NULL)
3410 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003411 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 st.st_dev = (dev_T)-1;
3413 stp = &st;
3414 }
3415 /* Use dev/ino to check if the files are the same, even when the names
3416 * are different (possible with links). Still need to compare the
3417 * name above, for when the file doesn't exist yet.
3418 * Problem: The dev/ino changes when a file is deleted (and created
3419 * again) and remains the same when renamed/moved. We don't want to
3420 * mch_stat() each buffer each time, that would be too slow. Get the
3421 * dev/ino again when they appear to match, but not when they appear
3422 * to be different: Could skip a buffer when it's actually the same
3423 * file. */
3424 if (buf_same_ino(buf, stp))
3425 {
3426 buf_setino(buf);
3427 if (buf_same_ino(buf, stp))
3428 return FALSE;
3429 }
3430 }
3431#endif
3432 return TRUE;
3433}
3434
3435#if defined(UNIX) || defined(PROTO)
3436/*
3437 * Set inode and device number for a buffer.
3438 * Must always be called when b_fname is changed!.
3439 */
3440 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003441buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003443 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444
3445 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3446 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003447 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 buf->b_dev = st.st_dev;
3449 buf->b_ino = st.st_ino;
3450 }
3451 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003452 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453}
3454
3455/*
3456 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3457 */
3458 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003459buf_same_ino(
3460 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003461 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003463 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 && stp->st_dev == buf->b_dev
3465 && stp->st_ino == buf->b_ino);
3466}
3467#endif
3468
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003469/*
3470 * Print info about the current buffer.
3471 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003473fileinfo(
3474 int fullname, /* when non-zero print full path */
3475 int shorthelp,
3476 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477{
3478 char_u *name;
3479 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003480 char *p;
3481 char *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003482 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003484 buffer = alloc(IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 if (buffer == NULL)
3486 return;
3487
3488 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3489 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003490 vim_snprintf(buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 p = buffer + STRLEN(buffer);
3492 }
3493 else
3494 p = buffer;
3495
3496 *p++ = '"';
3497 if (buf_spname(curbuf) != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003498 vim_strncpy((char_u *)p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 else
3500 {
3501 if (!fullname && curbuf->b_fname != NULL)
3502 name = curbuf->b_fname;
3503 else
3504 name = curbuf->b_ffname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003505 home_replace(shorthelp ? curbuf : NULL, name, (char_u *)p,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 (int)(IOSIZE - (p - buffer)), TRUE);
3507 }
3508
Bram Moolenaar32526b32019-01-19 17:43:09 +01003509 vim_snprintf_add(buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 curbufIsChanged() ? (shortmess(SHM_MOD)
3511 ? " [+]" : _(" [Modified]")) : " ",
3512 (curbuf->b_flags & BF_NOTEDITED)
3513#ifdef FEAT_QUICKFIX
3514 && !bt_dontwrite(curbuf)
3515#endif
3516 ? _("[Not edited]") : "",
3517 (curbuf->b_flags & BF_NEW)
3518#ifdef FEAT_QUICKFIX
3519 && !bt_dontwrite(curbuf)
3520#endif
3521 ? _("[New file]") : "",
3522 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003523 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 : _("[readonly]")) : "",
3525 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3526 || curbuf->b_p_ro) ?
3527 " " : "");
3528 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3529 * causes an overflow, thus for large numbers divide instead. */
3530 if (curwin->w_cursor.lnum > 1000000L)
3531 n = (int)(((long)curwin->w_cursor.lnum) /
3532 ((long)curbuf->b_ml.ml_line_count / 100L));
3533 else
3534 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3535 (long)curbuf->b_ml.ml_line_count);
3536 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003537 vim_snprintf_add(buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538#ifdef FEAT_CMDL_INFO
3539 else if (p_ru)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 /* Current line and column are already on the screen -- webb */
Bram Moolenaar32526b32019-01-19 17:43:09 +01003541 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003542 NGETTEXT("%ld line --%d%%--", "%ld lines --%d%%--",
3543 curbuf->b_ml.ml_line_count),
3544 (long)curbuf->b_ml.ml_line_count, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545#endif
3546 else
3547 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003548 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 _("line %ld of %ld --%d%%-- col "),
3550 (long)curwin->w_cursor.lnum,
3551 (long)curbuf->b_ml.ml_line_count,
3552 n);
3553 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003554 len = STRLEN(buffer);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003555 col_print((char_u *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3557 }
3558
Bram Moolenaar32526b32019-01-19 17:43:09 +01003559 (void)append_arg_number(curwin, (char_u *)buffer, IOSIZE,
3560 !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561
3562 if (dont_truncate)
3563 {
3564 /* Temporarily set msg_scroll to avoid the message being truncated.
3565 * First call msg_start() to get the message in the right place. */
3566 msg_start();
3567 n = msg_scroll;
3568 msg_scroll = TRUE;
3569 msg(buffer);
3570 msg_scroll = n;
3571 }
3572 else
3573 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003574 p = (char *)msg_trunc_attr(buffer, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 /* Need to repeat the message after redrawing when:
3577 * - When restart_edit is set (otherwise there will be a delay
3578 * before redrawing).
3579 * - When the screen was scrolled but there is no wait-return
3580 * prompt. */
Bram Moolenaar32526b32019-01-19 17:43:09 +01003581 set_keep_msg((char_u *)p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 }
3583
3584 vim_free(buffer);
3585}
3586
3587 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003588col_print(
3589 char_u *buf,
3590 size_t buflen,
3591 int col,
3592 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593{
3594 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003595 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003597 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598}
3599
3600#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601static char_u *lasttitle = NULL;
3602static char_u *lasticon = NULL;
3603
Bram Moolenaar84a93082018-06-16 22:58:15 +02003604/*
3605 * Put the file name in the title bar and icon of the window.
3606 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003608maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609{
3610 char_u *p;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003611 char_u *title_str = NULL;
3612 char_u *icon_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 int maxlen = 0;
3614 int len;
3615 int mustset;
3616 char_u buf[IOSIZE];
3617 int off;
3618
3619 if (!redrawing())
3620 {
3621 /* Postpone updating the title when 'lazyredraw' is set. */
3622 need_maketitle = TRUE;
3623 return;
3624 }
3625
3626 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003627 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003628 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629
3630 if (p_title)
3631 {
3632 if (p_titlelen > 0)
3633 {
3634 maxlen = p_titlelen * Columns / 100;
3635 if (maxlen < 10)
3636 maxlen = 10;
3637 }
3638
Bram Moolenaar84a93082018-06-16 22:58:15 +02003639 title_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 if (*p_titlestring != NUL)
3641 {
3642#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003643 if (stl_syntax & STL_IN_TITLE)
3644 {
3645 int use_sandbox = FALSE;
3646 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003647
3648# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003649 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003650# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003651 called_emsg = FALSE;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003652 build_stl_str_hl(curwin, title_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003653 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003654 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003655 if (called_emsg)
3656 set_string_option_direct((char_u *)"titlestring", -1,
3657 (char_u *)"", OPT_FREE, SID_ERROR);
3658 called_emsg |= save_called_emsg;
3659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 else
3661#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003662 title_str = p_titlestring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 }
3664 else
3665 {
3666 /* format: "fname + (path) (1 of 2) - VIM" */
3667
Bram Moolenaar2c666692012-09-05 13:30:40 +02003668#define SPACE_FOR_FNAME (IOSIZE - 100)
3669#define SPACE_FOR_DIR (IOSIZE - 20)
3670#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003672 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar21554412017-07-24 21:44:43 +02003673#ifdef FEAT_TERMINAL
3674 else if (curbuf->b_term != NULL)
3675 {
3676 vim_strncpy(buf, term_get_status_text(curbuf->b_term),
3677 SPACE_FOR_FNAME);
3678 }
3679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 else
3681 {
3682 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003683 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 }
3686
Bram Moolenaar21554412017-07-24 21:44:43 +02003687#ifdef FEAT_TERMINAL
3688 if (curbuf->b_term == NULL)
3689#endif
3690 switch (bufIsChanged(curbuf)
3691 + (curbuf->b_p_ro * 2)
3692 + (!curbuf->b_p_ma * 4))
3693 {
3694 case 1: STRCAT(buf, " +"); break;
3695 case 2: STRCAT(buf, " ="); break;
3696 case 3: STRCAT(buf, " =+"); break;
3697 case 4:
3698 case 6: STRCAT(buf, " -"); break;
3699 case 5:
3700 case 7: STRCAT(buf, " -+"); break;
3701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702
Bram Moolenaar21554412017-07-24 21:44:43 +02003703 if (curbuf->b_fname != NULL
3704#ifdef FEAT_TERMINAL
3705 && curbuf->b_term == NULL
3706#endif
3707 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 {
3709 /* Get path of file, replace home dir with ~ */
3710 off = (int)STRLEN(buf);
3711 buf[off++] = ' ';
3712 buf[off++] = '(';
3713 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003714 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715#ifdef BACKSLASH_IN_FILENAME
3716 /* avoid "c:/name" to be reduced to "c" */
3717 if (isalpha(buf[off]) && buf[off + 1] == ':')
3718 off += 2;
3719#endif
3720 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003721 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 if (p == buf + off)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003723 {
Bram Moolenaar21554412017-07-24 21:44:43 +02003724 /* must be a help buffer */
3725 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003726 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003730
Bram Moolenaar2c666692012-09-05 13:30:40 +02003731 /* Translate unprintable chars and concatenate. Keep some
3732 * room for the server name. When there is no room (very long
3733 * file name) use (...). */
3734 if (off < SPACE_FOR_DIR)
3735 {
3736 p = transstr(buf + off);
3737 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3738 vim_free(p);
3739 }
3740 else
3741 {
3742 vim_strncpy(buf + off, (char_u *)"...",
3743 (size_t)(SPACE_FOR_ARGNR - off));
3744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 STRCAT(buf, ")");
3746 }
3747
Bram Moolenaar2c666692012-09-05 13:30:40 +02003748 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749
3750#if defined(FEAT_CLIENTSERVER)
3751 if (serverName != NULL)
3752 {
3753 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003754 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 }
3756 else
3757#endif
3758 STRCAT(buf, " - VIM");
3759
3760 if (maxlen > 0)
3761 {
3762 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003763 if (vim_strsize(buf) > maxlen)
3764 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 }
3766 }
3767 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02003768 mustset = value_changed(title_str, &lasttitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769
3770 if (p_icon)
3771 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02003772 icon_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 if (*p_iconstring != NUL)
3774 {
3775#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003776 if (stl_syntax & STL_IN_ICON)
3777 {
3778 int use_sandbox = FALSE;
3779 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003780
3781# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003782 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003783# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003784 called_emsg = FALSE;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003785 build_stl_str_hl(curwin, icon_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003786 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003787 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003788 if (called_emsg)
3789 set_string_option_direct((char_u *)"iconstring", -1,
3790 (char_u *)"", OPT_FREE, SID_ERROR);
3791 called_emsg |= save_called_emsg;
3792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 else
3794#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003795 icon_str = p_iconstring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 }
3797 else
3798 {
3799 if (buf_spname(curbuf) != NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003800 p = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 else /* use file name only in icon */
Bram Moolenaar84a93082018-06-16 22:58:15 +02003802 p = gettail(curbuf->b_ffname);
3803 *icon_str = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 /* Truncate name at 100 bytes. */
Bram Moolenaar84a93082018-06-16 22:58:15 +02003805 len = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 if (len > 100)
3807 {
3808 len -= 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 if (has_mbyte)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003810 len += (*mb_tail_off)(p, p + len) + 1;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003811 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02003813 STRCPY(icon_str, p);
3814 trans_characters(icon_str, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 }
3816 }
3817
Bram Moolenaar84a93082018-06-16 22:58:15 +02003818 mustset |= value_changed(icon_str, &lasticon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819
3820 if (mustset)
3821 resettitle();
3822}
3823
3824/*
3825 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3826 * from "str" if it does.
Bram Moolenaar84a93082018-06-16 22:58:15 +02003827 * Return TRUE if resettitle() is to be called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 */
3829 static int
Bram Moolenaar84a93082018-06-16 22:58:15 +02003830value_changed(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831{
3832 if ((str == NULL) != (*last == NULL)
3833 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3834 {
3835 vim_free(*last);
3836 if (str == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003837 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 *last = NULL;
Bram Moolenaar40385db2018-08-07 22:31:44 +02003839 mch_restore_title(
3840 last == &lasttitle ? SAVE_RESTORE_TITLE : SAVE_RESTORE_ICON);
Bram Moolenaar84a93082018-06-16 22:58:15 +02003841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 else
Bram Moolenaar84a93082018-06-16 22:58:15 +02003843 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 *last = vim_strsave(str);
Bram Moolenaar84a93082018-06-16 22:58:15 +02003845 return TRUE;
3846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 }
3848 return FALSE;
3849}
3850
3851/*
3852 * Put current window title back (used after calling a shell)
3853 */
3854 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003855resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856{
3857 mch_settitle(lasttitle, lasticon);
3858}
Bram Moolenaarea408852005-06-25 22:49:46 +00003859
3860# if defined(EXITFREE) || defined(PROTO)
3861 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003862free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00003863{
3864 vim_free(lasttitle);
3865 vim_free(lasticon);
3866}
3867# endif
3868
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869#endif /* FEAT_TITLE */
3870
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003871#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003873 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 * Return length of string in screen cells.
3875 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003876 * Normally works for window "wp", except when working for 'tabline' then it
3877 * is "curwin".
3878 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 * Items are drawn interspersed with the text that surrounds it
3880 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3881 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3882 *
3883 * If maxwidth is not zero, the string will be filled at any middle marker
3884 * or truncated if too long, fillchar is used for all whitespace.
3885 */
3886 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003887build_stl_str_hl(
3888 win_T *wp,
3889 char_u *out, /* buffer to write into != NameBuff */
3890 size_t outlen, /* length of out[] */
3891 char_u *fmt,
3892 int use_sandbox UNUSED, /* "fmt" was set insecurely, use sandbox */
3893 int fillchar,
3894 int maxwidth,
3895 struct stl_hlrec *hltab, /* return: HL attributes (can be NULL) */
3896 struct stl_hlrec *tabtab) /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897{
Bram Moolenaar10772302019-01-20 18:25:54 +01003898 linenr_T lnum;
3899 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 char_u *p;
3901 char_u *s;
3902 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003903 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904#ifdef FEAT_EVAL
Bram Moolenaarba2929b2017-09-08 13:59:21 +02003905 win_T *save_curwin;
3906 buf_T *save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907#endif
3908 int empty_line;
3909 colnr_T virtcol;
3910 long l;
3911 long n;
3912 int prevchar_isflag;
3913 int prevchar_isitem;
3914 int itemisflag;
3915 int fillable;
3916 char_u *str;
3917 long num;
3918 int width;
3919 int itemcnt;
3920 int curitem;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02003921 int group_end_userhl;
3922 int group_start_userhl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 int groupitem[STL_MAX_ITEM];
3924 int groupdepth;
3925 struct stl_item
3926 {
3927 char_u *start;
3928 int minwid;
3929 int maxwid;
3930 enum
3931 {
3932 Normal,
3933 Empty,
3934 Group,
3935 Middle,
3936 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003937 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 Trunc
3939 } type;
3940 } item[STL_MAX_ITEM];
3941 int minwid;
3942 int maxwid;
3943 int zeropad;
3944 char_u base;
3945 char_u opt;
3946#define TMPLEN 70
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02003947 char_u buf_tmp[TMPLEN];
3948 char_u win_tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003949 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003950 struct stl_hlrec *sp;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02003951 int save_must_redraw = must_redraw;
3952 int save_redr_type = curwin->w_redr_type;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003953
3954#ifdef FEAT_EVAL
3955 /*
3956 * When the format starts with "%!" then evaluate it as an expression and
3957 * use the result as the actual format string.
3958 */
3959 if (fmt[0] == '%' && fmt[1] == '!')
3960 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02003961 typval_T tv;
3962
3963 tv.v_type = VAR_NUMBER;
3964 tv.vval.v_number = wp->w_id;
3965 set_var((char_u *)"g:statusline_winid", &tv, FALSE);
3966
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003967 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3968 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003969 usefmt = fmt;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02003970
3971 do_unlet((char_u *)"g:statusline_winid", TRUE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003972 }
3973#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974
3975 if (fillchar == 0)
3976 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003977 /* Can't handle a multi-byte fill character yet. */
3978 else if (mb_char2len(fillchar) > 1)
3979 fillchar = '-';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003980
Bram Moolenaar10772302019-01-20 18:25:54 +01003981 // The cursor in windows other than the current one isn't always
3982 // up-to-date, esp. because of autocommands and timers.
3983 lnum = wp->w_cursor.lnum;
3984 if (lnum > wp->w_buffer->b_ml.ml_line_count)
3985 {
3986 lnum = wp->w_buffer->b_ml.ml_line_count;
3987 wp->w_cursor.lnum = lnum;
3988 }
3989
3990 // Get line & check if empty (cursorpos will show "0-1"). Note that
3991 // p will become invalid when getting another buffer line.
3992 p = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar567199b2013-04-24 16:52:36 +02003993 empty_line = (*p == NUL);
3994
Bram Moolenaar10772302019-01-20 18:25:54 +01003995 // Get the byte value now, in case we need it below. This is more efficient
3996 // than making a copy of the line.
3997 len = STRLEN(p);
3998 if (wp->w_cursor.col > (colnr_T)len)
3999 {
4000 // Line may have changed since checking the cursor column, or the lnum
4001 // was adjusted above.
4002 wp->w_cursor.col = (colnr_T)len;
Bram Moolenaar10772302019-01-20 18:25:54 +01004003 wp->w_cursor.coladd = 0;
Bram Moolenaar567199b2013-04-24 16:52:36 +02004004 byteval = 0;
Bram Moolenaar10772302019-01-20 18:25:54 +01004005 }
Bram Moolenaar567199b2013-04-24 16:52:36 +02004006 else
Bram Moolenaar567199b2013-04-24 16:52:36 +02004007 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008
4009 groupdepth = 0;
4010 p = out;
4011 curitem = 0;
4012 prevchar_isflag = TRUE;
4013 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004014 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01004016 if (curitem == STL_MAX_ITEM)
4017 {
4018 /* There are too many items. Add the error code to the statusline
4019 * to give the user a hint about what went wrong. */
4020 if (p + 6 < out + outlen)
4021 {
4022 mch_memmove(p, " E541", (size_t)5);
4023 p += 5;
4024 }
4025 break;
4026 }
4027
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 if (*s != NUL && *s != '%')
4029 prevchar_isflag = prevchar_isitem = FALSE;
4030
4031 /*
4032 * Handle up to the next '%' or the end.
4033 */
4034 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
4035 *p++ = *s++;
4036 if (*s == NUL || p + 1 >= out + outlen)
4037 break;
4038
4039 /*
4040 * Handle one '%' item.
4041 */
4042 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01004043 if (*s == NUL) /* ignore trailing % */
4044 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 if (*s == '%')
4046 {
4047 if (p + 1 >= out + outlen)
4048 break;
4049 *p++ = *s++;
4050 prevchar_isflag = prevchar_isitem = FALSE;
4051 continue;
4052 }
4053 if (*s == STL_MIDDLEMARK)
4054 {
4055 s++;
4056 if (groupdepth > 0)
4057 continue;
4058 item[curitem].type = Middle;
4059 item[curitem++].start = p;
4060 continue;
4061 }
4062 if (*s == STL_TRUNCMARK)
4063 {
4064 s++;
4065 item[curitem].type = Trunc;
4066 item[curitem++].start = p;
4067 continue;
4068 }
4069 if (*s == ')')
4070 {
4071 s++;
4072 if (groupdepth < 1)
4073 continue;
4074 groupdepth--;
4075
4076 t = item[groupitem[groupdepth]].start;
4077 *p = NUL;
4078 l = vim_strsize(t);
4079 if (curitem > groupitem[groupdepth] + 1
4080 && item[groupitem[groupdepth]].minwid == 0)
4081 {
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004082 /* remove group if all items are empty and highlight group
4083 * doesn't change */
4084 group_start_userhl = group_end_userhl = 0;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004085 for (n = groupitem[groupdepth] - 1; n >= 0; n--)
4086 {
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004087 if (item[n].type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004088 {
4089 group_start_userhl = group_end_userhl = item[n].minwid;
4090 break;
4091 }
4092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004094 {
4095 if (item[n].type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 break;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004097 if (item[n].type == Highlight)
4098 group_end_userhl = item[n].minwid;
4099 }
4100 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 {
4102 p = t;
4103 l = 0;
4104 }
4105 }
4106 if (l > item[groupitem[groupdepth]].maxwid)
4107 {
4108 /* truncate, remove n bytes of text at the start */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 if (has_mbyte)
4110 {
4111 /* Find the first character that should be included. */
4112 n = 0;
4113 while (l >= item[groupitem[groupdepth]].maxwid)
4114 {
4115 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004116 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 }
4118 }
4119 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004120 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121
4122 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004123 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 p = p - n + 1;
Bram Moolenaar13505972019-01-24 15:04:48 +01004125
4126 // Fill up space left over by half a double-wide char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 while (++l < item[groupitem[groupdepth]].minwid)
4128 *p++ = fillchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129
4130 /* correct the start of the items for the truncation */
4131 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
4132 {
4133 item[l].start -= n;
4134 if (item[l].start < t)
4135 item[l].start = t;
4136 }
4137 }
4138 else if (abs(item[groupitem[groupdepth]].minwid) > l)
4139 {
4140 /* fill */
4141 n = item[groupitem[groupdepth]].minwid;
4142 if (n < 0)
4143 {
4144 /* fill by appending characters */
4145 n = 0 - n;
4146 while (l++ < n && p + 1 < out + outlen)
4147 *p++ = fillchar;
4148 }
4149 else
4150 {
4151 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004152 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 l = n - l;
4154 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004155 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 p += l;
4157 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
4158 item[n].start += l;
4159 for ( ; l > 0; l--)
4160 *t++ = fillchar;
4161 }
4162 }
4163 continue;
4164 }
4165 minwid = 0;
4166 maxwid = 9999;
4167 zeropad = FALSE;
4168 l = 1;
4169 if (*s == '0')
4170 {
4171 s++;
4172 zeropad = TRUE;
4173 }
4174 if (*s == '-')
4175 {
4176 s++;
4177 l = -1;
4178 }
4179 if (VIM_ISDIGIT(*s))
4180 {
4181 minwid = (int)getdigits(&s);
4182 if (minwid < 0) /* overflow */
4183 minwid = 0;
4184 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004185 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 {
4187 item[curitem].type = Highlight;
4188 item[curitem].start = p;
4189 item[curitem].minwid = minwid > 9 ? 1 : minwid;
4190 s++;
4191 curitem++;
4192 continue;
4193 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004194 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4195 {
4196 if (*s == STL_TABCLOSENR)
4197 {
4198 if (minwid == 0)
4199 {
4200 /* %X ends the close label, go back to the previously
4201 * define tab label nr. */
4202 for (n = curitem - 1; n >= 0; --n)
4203 if (item[n].type == TabPage && item[n].minwid >= 0)
4204 {
4205 minwid = item[n].minwid;
4206 break;
4207 }
4208 }
4209 else
4210 /* close nrs are stored as negative values */
4211 minwid = - minwid;
4212 }
4213 item[curitem].type = TabPage;
4214 item[curitem].start = p;
4215 item[curitem].minwid = minwid;
4216 s++;
4217 curitem++;
4218 continue;
4219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 if (*s == '.')
4221 {
4222 s++;
4223 if (VIM_ISDIGIT(*s))
4224 {
4225 maxwid = (int)getdigits(&s);
4226 if (maxwid <= 0) /* overflow */
4227 maxwid = 50;
4228 }
4229 }
4230 minwid = (minwid > 50 ? 50 : minwid) * l;
4231 if (*s == '(')
4232 {
4233 groupitem[groupdepth++] = curitem;
4234 item[curitem].type = Group;
4235 item[curitem].start = p;
4236 item[curitem].minwid = minwid;
4237 item[curitem].maxwid = maxwid;
4238 s++;
4239 curitem++;
4240 continue;
4241 }
4242 if (vim_strchr(STL_ALL, *s) == NULL)
4243 {
4244 s++;
4245 continue;
4246 }
4247 opt = *s++;
4248
4249 /* OK - now for the real work */
4250 base = 'D';
4251 itemisflag = FALSE;
4252 fillable = TRUE;
4253 num = -1;
4254 str = NULL;
4255 switch (opt)
4256 {
4257 case STL_FILEPATH:
4258 case STL_FULLPATH:
4259 case STL_FILENAME:
4260 fillable = FALSE; /* don't change ' ' to fillchar */
4261 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004262 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 else
4264 {
4265 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004266 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4268 }
4269 trans_characters(NameBuff, MAXPATHL);
4270 if (opt != STL_FILENAME)
4271 str = NameBuff;
4272 else
4273 str = gettail(NameBuff);
4274 break;
4275
4276 case STL_VIM_EXPR: /* '{' */
4277 itemisflag = TRUE;
4278 t = p;
4279 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
4280 *p++ = *s++;
4281 if (*s != '}') /* missing '}' or out of space */
4282 break;
4283 s++;
4284 *p = 0;
4285 p = t;
4286
4287#ifdef FEAT_EVAL
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004288 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp),
4289 "%d", curbuf->b_fnum);
4290 set_internal_string_var((char_u *)"g:actual_curbuf", buf_tmp);
4291 vim_snprintf((char *)win_tmp, sizeof(win_tmp), "%d", curwin->w_id);
4292 set_internal_string_var((char_u *)"g:actual_curwin", win_tmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004294 save_curbuf = curbuf;
4295 save_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 curwin = wp;
4297 curbuf = wp->w_buffer;
4298
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004299 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004301 curwin = save_curwin;
4302 curbuf = save_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00004303 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004304 do_unlet((char_u *)"g:actual_curwin", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305
4306 if (str != NULL && *str != 0)
4307 {
4308 if (*skipdigits(str) == NUL)
4309 {
4310 num = atoi((char *)str);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004311 VIM_CLEAR(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 itemisflag = FALSE;
4313 }
4314 }
4315#endif
4316 break;
4317
4318 case STL_LINE:
4319 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4320 ? 0L : (long)(wp->w_cursor.lnum);
4321 break;
4322
4323 case STL_NUMLINES:
4324 num = wp->w_buffer->b_ml.ml_line_count;
4325 break;
4326
4327 case STL_COLUMN:
4328 num = !(State & INSERT) && empty_line
4329 ? 0 : (int)wp->w_cursor.col + 1;
4330 break;
4331
4332 case STL_VIRTCOL:
4333 case STL_VIRTCOL_ALT:
4334 /* In list mode virtcol needs to be recomputed */
4335 virtcol = wp->w_virtcol;
4336 if (wp->w_p_list && lcs_tab1 == NUL)
4337 {
4338 wp->w_p_list = FALSE;
4339 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
4340 wp->w_p_list = TRUE;
4341 }
4342 ++virtcol;
4343 /* Don't display %V if it's the same as %c. */
4344 if (opt == STL_VIRTCOL_ALT
4345 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
4346 ? 0 : (int)wp->w_cursor.col + 1)))
4347 break;
4348 num = (long)virtcol;
4349 break;
4350
4351 case STL_PERCENTAGE:
4352 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4353 (long)wp->w_buffer->b_ml.ml_line_count);
4354 break;
4355
4356 case STL_ALTPERCENT:
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004357 str = buf_tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004358 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 break;
4360
4361 case STL_ARGLISTSTAT:
4362 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004363 buf_tmp[0] = 0;
4364 if (append_arg_number(wp, buf_tmp, (int)sizeof(buf_tmp), FALSE))
4365 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 break;
4367
4368 case STL_KEYMAP:
4369 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004370 if (get_keymap_str(wp, (char_u *)"<%s>", buf_tmp, TMPLEN))
4371 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 break;
4373 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004374#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004375 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376#else
4377 num = 0;
4378#endif
4379 break;
4380
4381 case STL_BUFNO:
4382 num = wp->w_buffer->b_fnum;
4383 break;
4384
4385 case STL_OFFSET_X:
4386 base = 'X';
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004387 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 case STL_OFFSET:
4389#ifdef FEAT_BYTEOFF
4390 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4391 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4392 0L : l + 1 + (!(State & INSERT) && empty_line ?
4393 0 : (int)wp->w_cursor.col);
4394#endif
4395 break;
4396
4397 case STL_BYTEVAL_X:
4398 base = 'X';
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004399 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004401 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 if (num == NL)
4403 num = 0;
4404 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4405 num = NL;
4406 break;
4407
4408 case STL_ROFLAG:
4409 case STL_ROFLAG_ALT:
4410 itemisflag = TRUE;
4411 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004412 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 break;
4414
4415 case STL_HELPFLAG:
4416 case STL_HELPFLAG_ALT:
4417 itemisflag = TRUE;
4418 if (wp->w_buffer->b_help)
4419 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004420 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 break;
4422
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 case STL_FILETYPE:
4424 if (*wp->w_buffer->b_p_ft != NUL
4425 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4426 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004427 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), "[%s]",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004428 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004429 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 }
4431 break;
4432
4433 case STL_FILETYPE_ALT:
4434 itemisflag = TRUE;
4435 if (*wp->w_buffer->b_p_ft != NUL
4436 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4437 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004438 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), ",%s",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004439 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004440 for (t = buf_tmp; *t != 0; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 *t = TOUPPER_LOC(*t);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004442 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 }
4444 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445
Bram Moolenaar4033c552017-09-16 20:54:51 +02004446#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 case STL_PREVIEWFLAG:
4448 case STL_PREVIEWFLAG_ALT:
4449 itemisflag = TRUE;
4450 if (wp->w_p_pvw)
4451 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4452 : _("[Preview]"));
4453 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004454
4455 case STL_QUICKFIX:
4456 if (bt_quickfix(wp->w_buffer))
4457 str = (char_u *)(wp->w_llist_ref
4458 ? _(msg_loclist)
4459 : _(msg_qflist));
4460 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461#endif
4462
4463 case STL_MODIFIED:
4464 case STL_MODIFIED_ALT:
4465 itemisflag = TRUE;
4466 switch ((opt == STL_MODIFIED_ALT)
4467 + bufIsChanged(wp->w_buffer) * 2
4468 + (!wp->w_buffer->b_p_ma) * 4)
4469 {
4470 case 2: str = (char_u *)"[+]"; break;
4471 case 3: str = (char_u *)",+"; break;
4472 case 4: str = (char_u *)"[-]"; break;
4473 case 5: str = (char_u *)",-"; break;
4474 case 6: str = (char_u *)"[+-]"; break;
4475 case 7: str = (char_u *)",+-"; break;
4476 }
4477 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004478
4479 case STL_HIGHLIGHT:
4480 t = s;
4481 while (*s != '#' && *s != NUL)
4482 ++s;
4483 if (*s == '#')
4484 {
4485 item[curitem].type = Highlight;
4486 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004487 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004488 curitem++;
4489 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004490 if (*s != NUL)
4491 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004492 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 }
4494
4495 item[curitem].start = p;
4496 item[curitem].type = Normal;
4497 if (str != NULL && *str)
4498 {
4499 t = str;
4500 if (itemisflag)
4501 {
4502 if ((t[0] && t[1])
4503 && ((!prevchar_isitem && *t == ',')
4504 || (prevchar_isflag && *t == ' ')))
4505 t++;
4506 prevchar_isflag = TRUE;
4507 }
4508 l = vim_strsize(t);
4509 if (l > 0)
4510 prevchar_isitem = TRUE;
4511 if (l > maxwid)
4512 {
4513 while (l >= maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 if (has_mbyte)
4515 {
4516 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004517 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 }
4519 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 l -= byte2cells(*t++);
4521 if (p + 1 >= out + outlen)
4522 break;
4523 *p++ = '<';
4524 }
4525 if (minwid > 0)
4526 {
4527 for (; l < minwid && p + 1 < out + outlen; l++)
4528 {
4529 /* Don't put a "-" in front of a digit. */
4530 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4531 *p++ = ' ';
4532 else
4533 *p++ = fillchar;
4534 }
4535 minwid = 0;
4536 }
4537 else
4538 minwid *= -1;
4539 while (*t && p + 1 < out + outlen)
4540 {
4541 *p++ = *t++;
4542 /* Change a space by fillchar, unless fillchar is '-' and a
4543 * digit follows. */
4544 if (fillable && p[-1] == ' '
4545 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4546 p[-1] = fillchar;
4547 }
4548 for (; l < minwid && p + 1 < out + outlen; l++)
4549 *p++ = fillchar;
4550 }
4551 else if (num >= 0)
4552 {
4553 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4554 char_u nstr[20];
4555
4556 if (p + 20 >= out + outlen)
4557 break; /* not sufficient space */
4558 prevchar_isitem = TRUE;
4559 t = nstr;
4560 if (opt == STL_VIRTCOL_ALT)
4561 {
4562 *t++ = '-';
4563 minwid--;
4564 }
4565 *t++ = '%';
4566 if (zeropad)
4567 *t++ = '0';
4568 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004569 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 *t = 0;
4571
4572 for (n = num, l = 1; n >= nbase; n /= nbase)
4573 l++;
4574 if (opt == STL_VIRTCOL_ALT)
4575 l++;
4576 if (l > maxwid)
4577 {
4578 l += 2;
4579 n = l - maxwid;
4580 while (l-- > maxwid)
4581 num /= nbase;
4582 *t++ = '>';
4583 *t++ = '%';
4584 *t = t[-3];
4585 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004586 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4587 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 }
4589 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004590 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4591 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 p += STRLEN(p);
4593 }
4594 else
4595 item[curitem].type = Empty;
4596
4597 if (opt == STL_VIM_EXPR)
4598 vim_free(str);
4599
4600 if (num >= 0 || (!itemisflag && str && *str))
4601 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4602 curitem++;
4603 }
4604 *p = NUL;
4605 itemcnt = curitem;
4606
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004607#ifdef FEAT_EVAL
4608 if (usefmt != fmt)
4609 vim_free(usefmt);
4610#endif
4611
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 width = vim_strsize(out);
4613 if (maxwidth > 0 && width > maxwidth)
4614 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004615 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 l = 0;
4617 if (itemcnt == 0)
4618 s = out;
4619 else
4620 {
4621 for ( ; l < itemcnt; l++)
4622 if (item[l].type == Trunc)
4623 {
4624 /* Truncate at %< item. */
4625 s = item[l].start;
4626 break;
4627 }
4628 if (l == itemcnt)
4629 {
4630 /* No %< item, truncate first item. */
4631 s = item[0].start;
4632 l = 0;
4633 }
4634 }
4635
4636 if (width - vim_strsize(s) >= maxwidth)
4637 {
4638 /* Truncation mark is beyond max length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 if (has_mbyte)
4640 {
4641 s = out;
4642 width = 0;
4643 for (;;)
4644 {
4645 width += ptr2cells(s);
4646 if (width >= maxwidth)
4647 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004648 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 }
4650 /* Fill up for half a double-wide character. */
4651 while (++width < maxwidth)
4652 *s++ = fillchar;
4653 }
4654 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 s = out + maxwidth - 1;
4656 for (l = 0; l < itemcnt; l++)
4657 if (item[l].start > s)
4658 break;
4659 itemcnt = l;
4660 *s++ = '>';
4661 *s = 0;
4662 }
4663 else
4664 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 if (has_mbyte)
4666 {
4667 n = 0;
4668 while (width >= maxwidth)
4669 {
4670 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004671 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 }
4673 }
4674 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 n = width - maxwidth + 1;
4676 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004677 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 *s = '<';
4679
4680 /* Fill up for half a double-wide character. */
4681 while (++width < maxwidth)
4682 {
4683 s = s + STRLEN(s);
4684 *s++ = fillchar;
4685 *s = NUL;
4686 }
4687
4688 --n; /* count the '<' */
4689 for (; l < itemcnt; l++)
4690 {
4691 if (item[l].start - n >= s)
4692 item[l].start -= n;
4693 else
4694 item[l].start = s;
4695 }
4696 }
4697 width = maxwidth;
4698 }
4699 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4700 {
4701 /* Apply STL_MIDDLE if any */
4702 for (l = 0; l < itemcnt; l++)
4703 if (item[l].type == Middle)
4704 break;
4705 if (l < itemcnt)
4706 {
4707 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004708 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 for (s = item[l].start; s < p; s++)
4710 *s = fillchar;
4711 for (l++; l < itemcnt; l++)
4712 item[l].start += maxwidth - width;
4713 width = maxwidth;
4714 }
4715 }
4716
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004717 /* Store the info about highlighting. */
4718 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004720 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 for (l = 0; l < itemcnt; l++)
4722 {
4723 if (item[l].type == Highlight)
4724 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004725 sp->start = item[l].start;
4726 sp->userhl = item[l].minwid;
4727 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 }
4729 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004730 sp->start = NULL;
4731 sp->userhl = 0;
4732 }
4733
4734 /* Store the info about tab pages labels. */
4735 if (tabtab != NULL)
4736 {
4737 sp = tabtab;
4738 for (l = 0; l < itemcnt; l++)
4739 {
4740 if (item[l].type == TabPage)
4741 {
4742 sp->start = item[l].start;
4743 sp->userhl = item[l].minwid;
4744 sp++;
4745 }
4746 }
4747 sp->start = NULL;
4748 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 }
4750
Bram Moolenaar65ed1362017-09-30 16:00:14 +02004751 /* When inside update_screen we do not want redrawing a stausline, ruler,
4752 * title, etc. to trigger another redraw, it may cause an endless loop. */
4753 if (updating_screen)
4754 {
4755 must_redraw = save_must_redraw;
4756 curwin->w_redr_type = save_redr_type;
4757 }
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004758
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 return width;
4760}
4761#endif /* FEAT_STL_OPT */
4762
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004763#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4764 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004766 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4767 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 */
4769 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004770get_rel_pos(
4771 win_T *wp,
4772 char_u *buf,
4773 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774{
4775 long above; /* number of lines above window */
4776 long below; /* number of lines below window */
4777
Bram Moolenaar0027c212015-01-07 13:31:52 +01004778 if (buflen < 3) /* need at least 3 chars for writing */
4779 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 above = wp->w_topline - 1;
4781#ifdef FEAT_DIFF
4782 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02004783 if (wp->w_topline == 1 && wp->w_topfill >= 1)
4784 above = 0; /* All buffer lines are displayed and there is an
4785 * indication of filler lines, that can be considered
4786 * seeing all lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787#endif
4788 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4789 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004790 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4791 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004793 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004795 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 ? (int)(above / ((above + below) / 100L))
4797 : (int)(above * 100L / (above + below)));
4798}
4799#endif
4800
4801/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004802 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 * Return TRUE if it was appended.
4804 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004805 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004806append_arg_number(
4807 win_T *wp,
4808 char_u *buf,
4809 int buflen,
4810 int add_file) /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811{
4812 char_u *p;
4813
4814 if (ARGCOUNT <= 1) /* nothing to do */
4815 return FALSE;
4816
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004817 p = buf + STRLEN(buf); /* go to the end of the buffer */
4818 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 return FALSE;
4820 *p++ = ' ';
4821 *p++ = '(';
4822 if (add_file)
4823 {
4824 STRCPY(p, "file ");
4825 p += 5;
4826 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004827 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4828 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4830 return TRUE;
4831}
4832
4833/*
4834 * If fname is not a full path, make it a full path.
4835 * Returns pointer to allocated memory (NULL for failure).
4836 */
4837 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004838fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839{
4840 /*
4841 * Force expanding the path always for Unix, because symbolic links may
4842 * mess up the full path name, even though it starts with a '/'.
4843 * Also expand when there is ".." in the file name, try to remove it,
4844 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004845 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 * For MS-Windows also expand names like "longna~1" to "longname".
4847 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004848#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 return FullName_save(fname, TRUE);
4850#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004851 if (!vim_isAbsName(fname)
4852 || strstr((char *)fname, "..") != NULL
4853 || strstr((char *)fname, "//") != NULL
4854# ifdef BACKSLASH_IN_FILENAME
4855 || strstr((char *)fname, "\\\\") != NULL
4856# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004857# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004859# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 )
4861 return FullName_save(fname, FALSE);
4862
4863 fname = vim_strsave(fname);
4864
Bram Moolenaar9b942202007-10-03 12:31:33 +00004865# ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01004866 if (fname != NULL)
4867 fname_case(fname, 0); /* set correct case for file name */
Bram Moolenaar9b942202007-10-03 12:31:33 +00004868# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869
4870 return fname;
4871#endif
4872}
4873
4874/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02004875 * Make "*ffname" a full file name, set "*sfname" to "*ffname" if not NULL.
4876 * "*ffname" becomes a pointer to allocated memory (or NULL).
4877 * When resolving a link both "*sfname" and "*ffname" will point to the same
4878 * allocated memory.
4879 * The "*ffname" and "*sfname" pointer values on call will not be freed.
4880 * Note that the resulting "*ffname" pointer should be considered not allocaed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004883fname_expand(
4884 buf_T *buf UNUSED,
4885 char_u **ffname,
4886 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02004888 if (*ffname == NULL) // no file name given, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 return;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02004890 if (*sfname == NULL) // no short file name given, use ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 *sfname = *ffname;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02004892 *ffname = fix_fname(*ffname); // expand to full path
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893
4894#ifdef FEAT_SHORTCUT
4895 if (!buf->b_p_bin)
4896 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004897 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02004899 // If the file name is a shortcut file, use the file it links to.
Bram Moolenaardce1e892019-02-10 23:18:53 +01004900 rfname = mch_resolve_path(*ffname, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004901 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 {
4903 vim_free(*ffname);
4904 *ffname = rfname;
4905 *sfname = rfname;
4906 }
4907 }
4908#endif
4909}
4910
4911/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 * Open a window for a number of buffers.
4913 */
4914 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004915ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916{
4917 buf_T *buf;
4918 win_T *wp, *wpnext;
4919 int split_ret = OK;
4920 int p_ea_save;
4921 int open_wins = 0;
4922 int r;
4923 int count; /* Maximum number of windows to open. */
4924 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004925 int had_tab = cmdmod.tab;
4926 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927
4928 if (eap->addr_count == 0) /* make as many windows as possible */
4929 count = 9999;
4930 else
4931 count = eap->line2; /* make as many windows as specified */
4932 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4933 all = FALSE;
4934 else
4935 all = TRUE;
4936
4937 setpcmark();
4938
4939#ifdef FEAT_GUI
4940 need_mouse_correct = TRUE;
4941#endif
4942
4943 /*
4944 * Close superfluous windows (two windows for the same buffer).
4945 * Also close windows that are not full-width.
4946 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004947 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004948 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004949 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004951 tpnext = curtab->tp_next;
4952 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004954 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004955 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004956 || ((cmdmod.split & WSP_VERT)
4957 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004958 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004959 : wp->w_width != Columns)
Bram Moolenaar4033c552017-09-16 20:54:51 +02004960 || (had_tab > 0 && wp != firstwin)) && !ONE_WINDOW
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004961 && !(wp->w_closing || wp->w_buffer->b_locked > 0))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004962 {
4963 win_close(wp, FALSE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004964 wpnext = firstwin; /* just in case an autocommand does
4965 something strange with windows */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004966 tpnext = first_tabpage; /* start all over... */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004967 open_wins = 0;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004968 }
4969 else
4970 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004972
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004973 /* Without the ":tab" modifier only do the current tab page. */
4974 if (had_tab == 0 || tpnext == NULL)
4975 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004976 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 }
4978
4979 /*
4980 * Go through the buffer list. When a buffer doesn't have a window yet,
4981 * open one. Otherwise move the window to the right position.
4982 * Watch out for autocommands that delete buffers or windows!
4983 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4985 ++autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 win_enter(lastwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4989 {
4990 /* Check if this buffer needs a window */
4991 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4992 continue;
4993
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004994 if (had_tab != 0)
4995 {
4996 /* With the ":tab" modifier don't move the window. */
4997 if (buf->b_nwindows > 0)
4998 wp = lastwin; /* buffer has a window, skip it */
4999 else
5000 wp = NULL;
5001 }
5002 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005003 {
5004 /* Check if this buffer already has a window */
Bram Moolenaar29323592016-07-24 22:04:11 +02005005 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005006 if (wp->w_buffer == buf)
5007 break;
5008 /* If the buffer already has a window, move it */
5009 if (wp != NULL)
5010 win_move_after(wp, curwin);
5011 }
5012
5013 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005015 bufref_T bufref;
5016
5017 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005018
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 /* Split the window and put the buffer in it */
5020 p_ea_save = p_ea;
5021 p_ea = TRUE; /* use space from all windows */
5022 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5023 ++open_wins;
5024 p_ea = p_ea_save;
5025 if (split_ret == FAIL)
5026 continue;
5027
5028 /* Open the buffer in this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005031 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005033 /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 swap_exists_action = SEA_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 break;
5036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 if (swap_exists_action == SEA_QUIT)
5038 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005039#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005040 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005041
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005042 /* Reset the error/interrupt/exception state here so that
5043 * aborting() returns FALSE when closing a window. */
5044 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005045#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005046
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005047 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 win_close(curwin, TRUE);
5049 --open_wins;
5050 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005051 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005052
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005053#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005054 /* Restore the error/interrupt/exception state if not
5055 * discarded by a new aborting error, interrupt, or uncaught
5056 * exception. */
5057 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 }
5060 else
5061 handle_swap_exists(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 }
5063
5064 ui_breakcheck();
5065 if (got_int)
5066 {
5067 (void)vgetc(); /* only break the file loading, not the rest */
5068 break;
5069 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005070#ifdef FEAT_EVAL
5071 /* Autocommands deleted the buffer or aborted script processing!!! */
5072 if (aborting())
5073 break;
5074#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005075 /* When ":tab" was used open a new tab for a new window repeatedly. */
5076 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5077 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 --autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 win_enter(firstwin, FALSE); /* back to first window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082
5083 /*
5084 * Close superfluous windows.
5085 */
5086 for (wp = lastwin; open_wins > count; )
5087 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005088 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 || autowrite(wp->w_buffer, FALSE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 if (!win_valid(wp))
5091 {
5092 /* BufWrite Autocommands made the window invalid, start over */
5093 wp = lastwin;
5094 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005095 else if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005097 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 --open_wins;
5099 wp = lastwin;
5100 }
5101 else
5102 {
5103 wp = wp->w_prev;
5104 if (wp == NULL)
5105 break;
5106 }
5107 }
5108}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005111static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005112
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113/*
5114 * do_modelines() - process mode lines for the current file
5115 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005116 * "flags" can be:
5117 * OPT_WINONLY only set options local to window
5118 * OPT_NOWIN don't set options local to window
5119 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 * Returns immediately if the "ml" option isn't set.
5121 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005123do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005125 linenr_T lnum;
5126 int nmlines;
5127 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128
5129 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5130 return;
5131
5132 /* Disallow recursive entry here. Can happen when executing a modeline
5133 * triggers an autocommand, which reloads modelines with a ":do". */
5134 if (entered)
5135 return;
5136
5137 ++entered;
5138 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5139 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005140 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 nmlines = 0;
5142
5143 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5144 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005145 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 nmlines = 0;
5147 --entered;
5148}
5149
5150#include "version.h" /* for version number */
5151
5152/*
5153 * chk_modeline() - check a single line for a mode string
5154 * Return FAIL if an error encountered.
5155 */
5156 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005157chk_modeline(
5158 linenr_T lnum,
5159 int flags) /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160{
5161 char_u *s;
5162 char_u *e;
5163 char_u *linecopy; /* local copy of any modeline found */
5164 int prev;
5165 int vers;
5166 int end;
5167 int retval = OK;
5168 char_u *save_sourcing_name;
5169 linenr_T save_sourcing_lnum;
5170#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005171 sctx_T save_current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172#endif
5173
5174 prev = -1;
5175 for (s = ml_get(lnum); *s != NUL; ++s)
5176 {
5177 if (prev == -1 || vim_isspace(prev))
5178 {
5179 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5180 || STRNCMP(s, "vi:", (size_t)3) == 0)
5181 break;
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005182 /* Accept both "vim" and "Vim". */
5183 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 {
5185 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5186 e = s + 4;
5187 else
5188 e = s + 3;
5189 vers = getdigits(&e);
5190 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005191 && (s[0] != 'V'
5192 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 && (s[3] == ':'
5194 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5195 || (VIM_VERSION_100 < vers && s[3] == '<')
5196 || (VIM_VERSION_100 > vers && s[3] == '>')
5197 || (VIM_VERSION_100 == vers && s[3] == '=')))
5198 break;
5199 }
5200 }
5201 prev = *s;
5202 }
5203
5204 if (*s)
5205 {
5206 do /* skip over "ex:", "vi:" or "vim:" */
5207 ++s;
5208 while (s[-1] != ':');
5209
5210 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5211 if (linecopy == NULL)
5212 return FAIL;
5213
5214 save_sourcing_lnum = sourcing_lnum;
5215 save_sourcing_name = sourcing_name;
5216 sourcing_lnum = lnum; /* prepare for emsg() */
5217 sourcing_name = (char_u *)"modelines";
5218
5219 end = FALSE;
5220 while (end == FALSE)
5221 {
5222 s = skipwhite(s);
5223 if (*s == NUL)
5224 break;
5225
5226 /*
5227 * Find end of set command: ':' or end of line.
5228 * Skip over "\:", replacing it with ":".
5229 */
5230 for (e = s; *e != ':' && *e != NUL; ++e)
5231 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005232 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 if (*e == NUL)
5234 end = TRUE;
5235
5236 /*
5237 * If there is a "set" command, require a terminating ':' and
5238 * ignore the stuff after the ':'.
5239 * "vi:set opt opt opt: foo" -- foo not interpreted
5240 * "vi:opt opt opt: foo" -- foo interpreted
5241 * Accept "se" for compatibility with Elvis.
5242 */
5243 if (STRNCMP(s, "set ", (size_t)4) == 0
5244 || STRNCMP(s, "se ", (size_t)3) == 0)
5245 {
5246 if (*e != ':') /* no terminating ':'? */
5247 break;
5248 end = TRUE;
5249 s = vim_strchr(s, ' ') + 1;
5250 }
5251 *e = NUL; /* truncate the set command */
5252
5253 if (*s != NUL) /* skip over an empty "::" */
5254 {
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005255 int secure_save = secure;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005257 save_current_sctx = current_sctx;
5258 current_sctx.sc_sid = SID_MODELINE;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005259 current_sctx.sc_seq = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005260 current_sctx.sc_lnum = 0;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02005261 current_sctx.sc_version = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262#endif
Bram Moolenaar5958f952018-11-20 04:25:21 +01005263 // Make sure no risky things are executed as a side effect.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01005264 secure = 1;
Bram Moolenaar5958f952018-11-20 04:25:21 +01005265
Bram Moolenaara3227e22006-03-08 21:32:40 +00005266 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar5958f952018-11-20 04:25:21 +01005267
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005268 secure = secure_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005270 current_sctx = save_current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271#endif
5272 if (retval == FAIL) /* stop if error found */
5273 break;
5274 }
5275 s = e + 1; /* advance to next part */
5276 }
5277
5278 sourcing_lnum = save_sourcing_lnum;
5279 sourcing_name = save_sourcing_name;
5280
5281 vim_free(linecopy);
5282 }
5283 return retval;
5284}
5285
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005286/*
Bram Moolenaar91335e52018-08-01 17:53:12 +02005287 * Return TRUE if "buf" is a normal buffer, 'buftype' is empty.
5288 */
5289 int
5290bt_normal(buf_T *buf)
5291{
5292 return buf != NULL && buf->b_p_bt[0] == NUL;
5293}
5294
Bram Moolenaar113e1072019-01-20 15:30:40 +01005295#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar91335e52018-08-01 17:53:12 +02005296/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005297 * Return TRUE if "buf" is the quickfix buffer.
5298 */
5299 int
5300bt_quickfix(buf_T *buf)
5301{
5302 return buf != NULL && buf->b_p_bt[0] == 'q';
5303}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005304#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005305
Bram Moolenaar113e1072019-01-20 15:30:40 +01005306#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005307/*
5308 * Return TRUE if "buf" is a terminal buffer.
5309 */
5310 int
5311bt_terminal(buf_T *buf)
5312{
5313 return buf != NULL && buf->b_p_bt[0] == 't';
5314}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005315#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005316
5317/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005318 * Return TRUE if "buf" is a help buffer.
5319 */
5320 int
5321bt_help(buf_T *buf)
5322{
5323 return buf != NULL && buf->b_help;
5324}
5325
5326/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02005327 * Return TRUE if "buf" is a prompt buffer.
5328 */
5329 int
5330bt_prompt(buf_T *buf)
5331{
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005332 return buf != NULL && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'r';
5333}
5334
5335/*
5336 * Return TRUE if "buf" is a buffer for a popup window.
5337 */
5338 int
5339bt_popup(buf_T *buf)
5340{
5341 return buf != NULL && buf->b_p_bt != NULL
5342 && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'o';
Bram Moolenaarf2732452018-06-03 14:47:35 +02005343}
5344
5345/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005346 * Return TRUE if "buf" is a "nofile", "acwrite", "terminal" or "prompt"
5347 * buffer. This means the buffer name is not a file name.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005348 */
5349 int
Bram Moolenaar26910de2019-06-15 19:37:15 +02005350bt_nofilename(buf_T *buf)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005351{
5352 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5353 || buf->b_p_bt[0] == 'a'
Bram Moolenaarf2732452018-06-03 14:47:35 +02005354 || buf->b_p_bt[0] == 't'
5355 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005356}
5357
5358/*
Bram Moolenaar26910de2019-06-15 19:37:15 +02005359 * Return TRUE if "buf" has 'buftype' set to "nofile".
5360 */
5361 int
5362bt_nofile(buf_T *buf)
5363{
5364 return buf != NULL && buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f';
5365}
5366
5367/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005368 * Return TRUE if "buf" is a "nowrite", "nofile", "terminal" or "prompt"
5369 * buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005370 */
5371 int
5372bt_dontwrite(buf_T *buf)
5373{
Bram Moolenaarf2732452018-06-03 14:47:35 +02005374 return buf != NULL && (buf->b_p_bt[0] == 'n'
5375 || buf->b_p_bt[0] == 't'
5376 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005377}
5378
Bram Moolenaar113e1072019-01-20 15:30:40 +01005379#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005380 int
5381bt_dontwrite_msg(buf_T *buf)
5382{
5383 if (bt_dontwrite(buf))
5384 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005385 emsg(_("E382: Cannot write, 'buftype' option is set"));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005386 return TRUE;
5387 }
5388 return FALSE;
5389}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005390#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005391
5392/*
5393 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
5394 * and 'bufhidden'.
5395 */
5396 int
5397buf_hide(buf_T *buf)
5398{
5399 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
5400 switch (buf->b_p_bh[0])
5401 {
5402 case 'u': /* "unload" */
5403 case 'w': /* "wipe" */
5404 case 'd': return FALSE; /* "delete" */
5405 case 'h': return TRUE; /* "hide" */
5406 }
5407 return (p_hid || cmdmod.hide);
5408}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409
5410/*
5411 * Return special buffer name.
5412 * Returns NULL when the buffer has a normal file name.
5413 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005414 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005415buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416{
Bram Moolenaar4033c552017-09-16 20:54:51 +02005417#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005419 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005420 /*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005421 * Differentiate between the quickfix and location list buffers using
5422 * the buffer number stored in the global quickfix stack.
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005423 */
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005424 if (buf->b_fnum == qf_stack_get_bufnr())
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005425 return (char_u *)_(msg_qflist);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005426 else
5427 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02005430
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 /* There is no _file_ when 'buftype' is "nofile", b_sfname
Bram Moolenaar21554412017-07-24 21:44:43 +02005432 * contains the name as specified by the user. */
Bram Moolenaar26910de2019-06-15 19:37:15 +02005433 if (bt_nofilename(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 {
Bram Moolenaar21554412017-07-24 21:44:43 +02005435#ifdef FEAT_TERMINAL
5436 if (buf->b_term != NULL)
5437 return term_get_status_text(buf->b_term);
5438#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02005439 if (buf->b_fname != NULL)
5440 return buf->b_fname;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02005441#ifdef FEAT_JOB_CHANNEL
5442 if (bt_prompt(buf))
5443 return (char_u *)_("[Prompt]");
5444#endif
Bram Moolenaarc6896e22019-05-30 22:32:34 +02005445#ifdef FEAT_TEXT_PROP
5446 if (bt_popup(buf))
5447 return (char_u *)_("[Popup]");
5448#endif
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005449 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 }
Bram Moolenaar21554412017-07-24 21:44:43 +02005451
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005453 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 return NULL;
5455}
5456
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02005457#if defined(FEAT_JOB_CHANNEL) \
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005458 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5459 || defined(PROTO)
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02005460# define SWITCH_TO_WIN
5461
5462/*
5463 * Find a window that contains "buf" and switch to it.
5464 * If there is no such window, use the current window and change "curbuf".
5465 * Caller must initialize save_curbuf to NULL.
5466 * restore_win_for_buf() MUST be called later!
5467 */
5468 void
5469switch_to_win_for_buf(
5470 buf_T *buf,
5471 win_T **save_curwinp,
5472 tabpage_T **save_curtabp,
5473 bufref_T *save_curbuf)
5474{
5475 win_T *wp;
5476 tabpage_T *tp;
5477
5478 if (find_win_for_buf(buf, &wp, &tp) == FAIL)
5479 switch_buffer(save_curbuf, buf);
5480 else if (switch_win(save_curwinp, save_curtabp, wp, tp, TRUE) == FAIL)
5481 {
5482 restore_win(*save_curwinp, *save_curtabp, TRUE);
5483 switch_buffer(save_curbuf, buf);
5484 }
5485}
5486
5487 void
5488restore_win_for_buf(
5489 win_T *save_curwin,
5490 tabpage_T *save_curtab,
5491 bufref_T *save_curbuf)
5492{
5493 if (save_curbuf->br_buf == NULL)
5494 restore_win(save_curwin, save_curtab, TRUE);
5495 else
5496 restore_buffer(save_curbuf);
5497}
5498#endif
5499
Bram Moolenaar4033c552017-09-16 20:54:51 +02005500#if defined(FEAT_QUICKFIX) || defined(SWITCH_TO_WIN) || defined(PROTO)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005501/*
5502 * Find a window for buffer "buf".
5503 * If found OK is returned and "wp" and "tp" are set to the window and tabpage.
5504 * If not found FAIL is returned.
5505 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005506 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005507find_win_for_buf(
5508 buf_T *buf,
5509 win_T **wp,
5510 tabpage_T **tp)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005511{
5512 FOR_ALL_TAB_WINDOWS(*tp, *wp)
5513 if ((*wp)->w_buffer == buf)
5514 goto win_found;
5515 return FAIL;
5516win_found:
5517 return OK;
5518}
5519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521/*
5522 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5523 */
5524 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005525set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526{
5527 if (on != curbuf->b_p_bl)
5528 {
5529 curbuf->b_p_bl = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 if (on)
5531 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5532 else
5533 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 }
5535}
5536
5537/*
5538 * Read the file for "buf" again and check if the contents changed.
5539 * Return TRUE if it changed or this could not be checked.
5540 */
5541 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005542buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543{
5544 buf_T *newbuf;
5545 int differ = TRUE;
5546 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 exarg_T ea;
5549
5550 /* Allocate a buffer without putting it in the buffer list. */
5551 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5552 if (newbuf == NULL)
5553 return TRUE;
5554
5555 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5556 if (prep_exarg(&ea, buf) == FAIL)
5557 {
5558 wipe_buffer(newbuf, FALSE);
5559 return TRUE;
5560 }
5561
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 /* set curwin/curbuf to buf and save a few things */
5563 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564
Bram Moolenaar4770d092006-01-12 23:22:24 +00005565 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 && readfile(buf->b_ffname, buf->b_fname,
5567 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5568 &ea, READ_NEW | READ_DUMMY) == OK)
5569 {
5570 /* compare the two files line by line */
5571 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5572 {
5573 differ = FALSE;
5574 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5575 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5576 {
5577 differ = TRUE;
5578 break;
5579 }
5580 }
5581 }
5582 vim_free(ea.cmd);
5583
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 /* restore curwin/curbuf and a few other things */
5585 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586
5587 if (curbuf != newbuf) /* safety check */
5588 wipe_buffer(newbuf, FALSE);
5589
5590 return differ;
5591}
5592
5593/*
5594 * Wipe out a buffer and decrement the last buffer number if it was used for
5595 * this buffer. Call this to wipe out a temp buffer that does not contain any
5596 * marks.
5597 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005599wipe_buffer(
5600 buf_T *buf,
5601 int aucmd UNUSED) /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602{
5603 if (buf->b_fnum == top_file_num - 1)
5604 --top_file_num;
5605
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005607 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005608
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005609 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005610
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005612 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613}
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005614
5615#if defined(FEAT_EVAL) || defined(PROTO)
5616/*
5617 * Mark references in functions of buffers.
5618 */
5619 int
5620set_ref_in_buffers(int copyID)
5621{
5622 int abort = FALSE;
5623 buf_T *bp;
5624
5625 FOR_ALL_BUFFERS(bp)
5626 {
5627 listener_T *lnr;
5628 typval_T tv;
5629
5630 for (lnr = bp->b_listener; !abort && lnr != NULL; lnr = lnr->lr_next)
5631 {
5632 if (lnr->lr_callback.cb_partial != NULL)
5633 {
5634 tv.v_type = VAR_PARTIAL;
5635 tv.vval.v_partial = lnr->lr_callback.cb_partial;
5636 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
5637 }
5638 }
5639# ifdef FEAT_JOB_CHANNEL
5640 if (!abort && bp->b_prompt_callback.cb_partial != NULL)
5641 {
5642 tv.v_type = VAR_PARTIAL;
5643 tv.vval.v_partial = bp->b_prompt_callback.cb_partial;
5644 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
5645 }
5646 if (!abort && bp->b_prompt_interrupt.cb_partial != NULL)
5647 {
5648 tv.v_type = VAR_PARTIAL;
5649 tv.vval.v_partial = bp->b_prompt_interrupt.cb_partial;
5650 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
5651 }
5652# endif
5653 if (abort)
5654 break;
5655 }
5656 return abort;
5657}
5658#endif