blob: c279c263c1f8364c9723d2e9b63957b1c7e28f83 [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 Moolenaarf28dbce2016-01-29 22:03:47 +010030static char_u *buflist_match(regmatch_T *rmp, buf_T *buf, int ignore_case);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010031static char_u *fname_match(regmatch_T *rmp, char_u *name, int ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +020033static buf_T *buflist_findname_stat(char_u *ffname, stat_T *st);
34static int otherfile_buf(buf_T *buf, char_u *ffname, stat_T *stp);
35static int buf_same_ino(buf_T *buf, stat_T *stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000036#else
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010037static int otherfile_buf(buf_T *buf, char_u *ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000038#endif
39#ifdef FEAT_TITLE
Bram Moolenaar84a93082018-06-16 22:58:15 +020040static int value_changed(char_u *str, char_u **last);
Bram Moolenaar071d4272004-06-13 20:20:40 +000041#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010042static int append_arg_number(win_T *wp, char_u *buf, int buflen, int add_file);
43static void free_buffer(buf_T *);
44static void free_buffer_stuff(buf_T *buf, int free_options);
45static void clear_wininfo(buf_T *buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000046
47#ifdef UNIX
48# define dev_T dev_t
49#else
50# define dev_T unsigned
51#endif
52
Bram Moolenaar4033c552017-09-16 20:54:51 +020053#if defined(FEAT_QUICKFIX)
Bram Moolenaar7fd73202010-07-25 16:58:46 +020054static char *msg_loclist = N_("[Location List]");
55static char *msg_qflist = N_("[Quickfix List]");
56#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +010057static char *e_auabort = N_("E855: Autocommands caused command to abort");
Bram Moolenaar7fd73202010-07-25 16:58:46 +020058
Bram Moolenaarb25f9a92016-07-10 18:21:50 +020059/* Number of times free_buffer() was called. */
60static int buf_free_count = 0;
61
Bram Moolenaarc024b462019-06-08 18:07:21 +020062/*
63 * Read data from buffer for retrying.
64 */
Bram Moolenaarf71d7b92016-08-09 22:14:05 +020065 static int
66read_buffer(
67 int read_stdin, /* read file from stdin, otherwise fifo */
68 exarg_T *eap, /* for forced 'ff' and 'fenc' or NULL */
69 int flags) /* extra flags for readfile() */
70{
71 int retval = OK;
72 linenr_T line_count;
73
74 /*
75 * Read from the buffer which the text is already filled in and append at
76 * the end. This makes it possible to retry when 'fileformat' or
77 * 'fileencoding' was guessed wrong.
78 */
79 line_count = curbuf->b_ml.ml_line_count;
80 retval = readfile(
81 read_stdin ? NULL : curbuf->b_ffname,
82 read_stdin ? NULL : curbuf->b_fname,
83 (linenr_T)line_count, (linenr_T)0, (linenr_T)MAXLNUM, eap,
84 flags | READ_BUFFER);
85 if (retval == OK)
86 {
87 /* Delete the binary lines. */
88 while (--line_count >= 0)
89 ml_delete((linenr_T)1, FALSE);
90 }
91 else
92 {
93 /* Delete the converted lines. */
94 while (curbuf->b_ml.ml_line_count > line_count)
95 ml_delete(line_count, FALSE);
96 }
97 /* Put the cursor on the first line. */
98 curwin->w_cursor.lnum = 1;
99 curwin->w_cursor.col = 0;
100
101 if (read_stdin)
102 {
103 /* Set or reset 'modified' before executing autocommands, so that
104 * it can be changed there. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100105 if (!readonlymode && !BUFEMPTY())
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200106 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100107 else if (retval == OK)
Bram Moolenaarc024b462019-06-08 18:07:21 +0200108 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200109
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100110 if (retval == OK)
111 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100112#ifdef FEAT_EVAL
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100113 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100114 curbuf, &retval);
115#else
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100116 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200117#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100118 }
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200119 }
120 return retval;
121}
122
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123/*
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200124 * Ensure buffer "buf" is loaded. Does not trigger the swap-exists action.
125 */
126 void
127buffer_ensure_loaded(buf_T *buf)
128{
129 if (buf->b_ml.ml_mfp == NULL)
130 {
131 aco_save_T aco;
132
133 aucmd_prepbuf(&aco, buf);
134 swap_exists_action = SEA_NONE;
135 open_buffer(FALSE, NULL, 0);
136 aucmd_restbuf(&aco);
137 }
138}
139
140/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200141 * Open current buffer, that is: open the memfile and read the file into
142 * memory.
143 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144 */
145 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100146open_buffer(
147 int read_stdin, /* read file from stdin */
148 exarg_T *eap, /* for forced 'ff' and 'fenc' or NULL */
149 int flags) /* extra flags for readfile() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150{
151 int retval = OK;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200152 bufref_T old_curbuf;
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100153#ifdef FEAT_SYN_HL
154 long old_tw = curbuf->b_p_tw;
155#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200156 int read_fifo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157
158 /*
159 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
160 * When re-entering the same buffer, it should not change, because the
161 * user may have reset the flag by hand.
162 */
163 if (readonlymode && curbuf->b_ffname != NULL
164 && (curbuf->b_flags & BF_NEVERLOADED))
165 curbuf->b_p_ro = TRUE;
166
Bram Moolenaar4770d092006-01-12 23:22:24 +0000167 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168 {
169 /*
170 * There MUST be a memfile, otherwise we can't do anything
171 * If we can't create one for the current buffer, take another buffer
172 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100173 close_buffer(NULL, curbuf, 0, FALSE);
Bram Moolenaar29323592016-07-24 22:04:11 +0200174 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175 if (curbuf->b_ml.ml_mfp != NULL)
176 break;
177 /*
178 * if there is no memfile at all, exit
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000179 * This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 */
181 if (curbuf == NULL)
182 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100183 emsg(_("E82: Cannot allocate any buffer, exiting..."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 getout(2);
185 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100186 emsg(_("E83: Cannot allocate buffer, using other one..."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 enter_buffer(curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100188#ifdef FEAT_SYN_HL
189 if (old_tw != curbuf->b_p_tw)
190 check_colorcolumn(curwin);
191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 return FAIL;
193 }
194
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 /* The autocommands in readfile() may change the buffer, but only AFTER
196 * reading the file. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200197 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 modified_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199
200 /* mark cursor position as being invalid */
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100201 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202
203 if (curbuf->b_ffname != NULL
204#ifdef FEAT_NETBEANS_INTG
205 && netbeansReadFile
206#endif
207 )
208 {
Bram Moolenaar426dd022016-03-15 15:09:29 +0100209 int old_msg_silent = msg_silent;
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200210#ifdef UNIX
211 int save_bin = curbuf->b_p_bin;
212 int perm;
213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214#ifdef FEAT_NETBEANS_INTG
215 int oldFire = netbeansFireChanges;
216
217 netbeansFireChanges = 0;
218#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200219#ifdef UNIX
220 perm = mch_getperm(curbuf->b_ffname);
Bram Moolenaard569bb02018-08-11 13:57:20 +0200221 if (perm >= 0 && (S_ISFIFO(perm)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200222 || S_ISSOCK(perm)
Bram Moolenaarf04507d2016-08-20 15:05:39 +0200223# ifdef OPEN_CHR_FILES
224 || (S_ISCHR(perm) && is_dev_fd_file(curbuf->b_ffname))
225# endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200226 ))
227 read_fifo = TRUE;
228 if (read_fifo)
229 curbuf->b_p_bin = TRUE;
230#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100231 if (shortmess(SHM_FILEINFO))
232 msg_silent = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200234 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200235 flags | READ_NEW | (read_fifo ? READ_FIFO : 0));
236#ifdef UNIX
237 if (read_fifo)
238 {
239 curbuf->b_p_bin = save_bin;
240 if (retval == OK)
241 retval = read_buffer(FALSE, eap, flags);
242 }
243#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100244 msg_silent = old_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245#ifdef FEAT_NETBEANS_INTG
246 netbeansFireChanges = oldFire;
247#endif
248 /* Help buffer is filtered. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +0200249 if (bt_help(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250 fix_help_buffer();
251 }
252 else if (read_stdin)
253 {
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200254 int save_bin = curbuf->b_p_bin;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255
256 /*
257 * First read the text in binary mode into the buffer.
258 * Then read from that same buffer and append at the end. This makes
259 * it possible to retry when 'fileformat' or 'fileencoding' was
260 * guessed wrong.
261 */
262 curbuf->b_p_bin = TRUE;
263 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200264 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
265 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266 curbuf->b_p_bin = save_bin;
267 if (retval == OK)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200268 retval = read_buffer(TRUE, eap, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 }
270
271 /* if first time loading this buffer, init b_chartab[] */
272 if (curbuf->b_flags & BF_NEVERLOADED)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100273 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 (void)buf_init_chartab(curbuf, FALSE);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100275#ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100276 parse_cino(curbuf);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100277#endif
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279
280 /*
281 * Set/reset the Changed flag first, autocmds may change the buffer.
282 * Apply the automatic commands, before processing the modelines.
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200283 * So the modelines have priority over autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284 */
285 /* When reading stdin, the buffer contents always needs writing, so set
286 * the changed flag. Unless in readonly mode: "ls | gview -".
287 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000288 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 || modified_was_set /* ":set modified" used in autocmd */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100290#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000293 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100295 else if (retval == OK && !read_stdin && !read_fifo)
Bram Moolenaarc024b462019-06-08 18:07:21 +0200296 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297 save_file_ff(curbuf); /* keep this fileformat */
298
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100299 /* Set last_changedtick to avoid triggering a TextChanged autocommand right
300 * after it was added. */
301 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
302#ifdef FEAT_INS_EXPAND
303 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
304#endif
305
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 /* require "!" to overwrite the file, because it wasn't read completely */
307#ifdef FEAT_EVAL
308 if (aborting())
309#else
310 if (got_int)
311#endif
312 curbuf->b_flags |= BF_READERR;
313
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000314#ifdef FEAT_FOLDING
315 /* Need to update automatic folding. Do this before the autocommands,
316 * they may use the fold info. */
317 foldUpdateAll(curwin);
318#endif
319
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 /* need to set w_topline, unless some autocommand already did that. */
321 if (!(curwin->w_valid & VALID_TOPLINE))
322 {
323 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100324#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100328#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100330#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332#endif
333
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100334 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 /*
337 * The autocommands may have changed the current buffer. Apply the
338 * modelines to the correct buffer, if it still exists and is loaded.
339 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200340 if (bufref_valid(&old_curbuf) && old_curbuf.br_buf->b_ml.ml_mfp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 {
342 aco_save_T aco;
343
344 /* Go to the buffer that was opened. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200345 aucmd_prepbuf(&aco, old_curbuf.br_buf);
Bram Moolenaara3227e22006-03-08 21:32:40 +0000346 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
348
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100349#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100351 &retval);
352#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355
356 /* restore curwin/curbuf and a few other things */
357 aucmd_restbuf(&aco);
358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 }
360
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 return retval;
362}
363
364/*
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200365 * Store "buf" in "bufref" and set the free count.
366 */
367 void
368set_bufref(bufref_T *bufref, buf_T *buf)
369{
370 bufref->br_buf = buf;
Bram Moolenaarfadacf02017-06-19 20:35:32 +0200371 bufref->br_fnum = buf == NULL ? 0 : buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200372 bufref->br_buf_free_count = buf_free_count;
373}
374
375/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200376 * Return TRUE if "bufref->br_buf" points to the same buffer as when
377 * set_bufref() was called and it is a valid buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200378 * Only goes through the buffer list if buf_free_count changed.
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200379 * Also checks if b_fnum is still the same, a :bwipe followed by :new might get
380 * the same allocated memory, but it's a different buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200381 */
382 int
383bufref_valid(bufref_T *bufref)
384{
385 return bufref->br_buf_free_count == buf_free_count
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200386 ? TRUE : buf_valid(bufref->br_buf)
387 && bufref->br_fnum == bufref->br_buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200388}
389
390/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200392 * This can be slow if there are many buffers, prefer using bufref_valid().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 */
394 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100395buf_valid(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396{
397 buf_T *bp;
398
Bram Moolenaar82404332016-07-10 17:00:38 +0200399 /* Assume that we more often have a recent buffer, start with the last
400 * one. */
401 for (bp = lastbuf; bp != NULL; bp = bp->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402 if (bp == buf)
403 return TRUE;
404 return FALSE;
405}
406
407/*
Bram Moolenaar480778b2016-07-14 22:09:39 +0200408 * A hash table used to quickly lookup a buffer by its number.
409 */
410static hashtab_T buf_hashtab;
411
412 static void
413buf_hashtab_add(buf_T *buf)
414{
415 sprintf((char *)buf->b_key, "%x", buf->b_fnum);
416 if (hash_add(&buf_hashtab, buf->b_key) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100417 emsg(_("E931: Buffer cannot be registered"));
Bram Moolenaar480778b2016-07-14 22:09:39 +0200418}
419
420 static void
421buf_hashtab_remove(buf_T *buf)
422{
423 hashitem_T *hi = hash_find(&buf_hashtab, buf->b_key);
424
425 if (!HASHITEM_EMPTY(hi))
426 hash_remove(&buf_hashtab, hi);
427}
428
Bram Moolenaar94f01952018-09-01 15:30:03 +0200429/*
430 * Return TRUE when buffer "buf" can be unloaded.
431 * Give an error message and return FALSE when the buffer is locked or the
432 * screen is being redrawn and the buffer is in a window.
433 */
434 static int
435can_unload_buffer(buf_T *buf)
436{
437 int can_unload = !buf->b_locked;
438
439 if (can_unload && updating_screen)
440 {
441 win_T *wp;
442
443 FOR_ALL_WINDOWS(wp)
444 if (wp->w_buffer == buf)
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200445 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200446 can_unload = FALSE;
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200447 break;
448 }
Bram Moolenaar94f01952018-09-01 15:30:03 +0200449 }
450 if (!can_unload)
Bram Moolenaardefa0672019-07-21 19:25:37 +0200451 semsg(_("E937: Attempt to delete a buffer that is in use: %s"),
452 buf->b_fname);
Bram Moolenaar94f01952018-09-01 15:30:03 +0200453 return can_unload;
454}
Bram Moolenaara997b452018-04-17 23:24:06 +0200455
Bram Moolenaar480778b2016-07-14 22:09:39 +0200456/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 * Close the link to a buffer.
458 * "action" is used when there is no longer a window for the buffer.
459 * It can be:
460 * 0 buffer becomes hidden
461 * DOBUF_UNLOAD buffer is unloaded
462 * DOBUF_DELETE buffer is unloaded and removed from buffer list
463 * DOBUF_WIPE buffer is unloaded and really deleted
464 * When doing all but the first one on the current buffer, the caller should
465 * get a new buffer very soon!
466 *
467 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100468 *
469 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
470 * cause there to be only one window with this buffer. e.g. when ":quit" is
471 * supposed to close the window but autocommands close all other windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472 */
473 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100474close_buffer(
475 win_T *win, /* if not NULL, set b_last_cursor */
476 buf_T *buf,
477 int action,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200478 int abort_if_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100481 int nwindows;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200482 bufref_T bufref;
Bram Moolenaar3a117e12016-10-30 21:57:52 +0100483 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200484 win_T *the_curwin = curwin;
485 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 int unload_buf = (action != 0);
487 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
488 int wipe_buf = (action == DOBUF_WIPE);
489
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200490 /*
491 * Force unloading or deleting when 'bufhidden' says so.
492 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
493 * "hide" (otherwise we could never free or delete a buffer).
494 */
495 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
496 {
497 del_buf = TRUE;
498 unload_buf = TRUE;
499 }
500 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
501 {
502 del_buf = TRUE;
503 unload_buf = TRUE;
504 wipe_buf = TRUE;
505 }
506 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
507 unload_buf = TRUE;
508
Bram Moolenaar94053a52017-08-01 21:44:33 +0200509#ifdef FEAT_TERMINAL
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200510 if (bt_terminal(buf) && (buf->b_nwindows == 1 || del_buf))
Bram Moolenaar94053a52017-08-01 21:44:33 +0200511 {
512 if (term_job_running(buf->b_term))
513 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200514 if (wipe_buf || unload_buf)
Bram Moolenaara997b452018-04-17 23:24:06 +0200515 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200516 if (!can_unload_buffer(buf))
Bram Moolenaara997b452018-04-17 23:24:06 +0200517 return;
Bram Moolenaar94f01952018-09-01 15:30:03 +0200518
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200519 /* Wiping out or unloading a terminal buffer kills the job. */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200520 free_terminal(buf);
Bram Moolenaara997b452018-04-17 23:24:06 +0200521 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200522 else
523 {
524 /* The job keeps running, hide the buffer. */
525 del_buf = FALSE;
526 unload_buf = FALSE;
527 }
528 }
529 else
530 {
531 /* A terminal buffer is wiped out if the job has finished. */
532 del_buf = TRUE;
533 unload_buf = TRUE;
534 wipe_buf = TRUE;
535 }
536 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200537#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200539 /* Disallow deleting the buffer when it is locked (already being closed or
540 * halfway a command that relies on it). Unloading is allowed. */
Bram Moolenaar94f01952018-09-01 15:30:03 +0200541 if ((del_buf || wipe_buf) && !can_unload_buffer(buf))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200542 return;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200543
Bram Moolenaar4033c552017-09-16 20:54:51 +0200544 /* check no autocommands closed the window */
545 if (win != NULL && win_valid_any_tab(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 {
547 /* Set b_last_cursor when closing the last window for the buffer.
548 * Remember the last cursor position and window options of the buffer.
549 * This used to be only for the current window, but then options like
550 * 'foldmethod' may be lost with a ":only" command. */
551 if (buf->b_nwindows == 1)
552 set_last_cursor(win);
553 buflist_setfpos(buf, win,
554 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
555 win->w_cursor.col, TRUE);
556 }
557
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200558 set_bufref(&bufref, buf);
559
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 /* When the buffer is no longer in a window, trigger BufWinLeave */
561 if (buf->b_nwindows == 1)
562 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200563 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200564 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
565 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200566 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100567 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200568 /* Autocommands deleted the buffer. */
569aucmd_abort:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100570 emsg(_(e_auabort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 return;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100572 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200573 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200574 if (abort_if_last && one_window())
575 /* Autocommands made this the only window. */
576 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577
578 /* When the buffer becomes hidden, but is not unloaded, trigger
579 * BufHidden */
580 if (!unload_buf)
581 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200582 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200583 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
584 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200585 && !bufref_valid(&bufref))
Bram Moolenaar362ce482012-06-06 19:02:45 +0200586 /* Autocommands deleted the buffer. */
587 goto aucmd_abort;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200588 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200589 if (abort_if_last && one_window())
590 /* Autocommands made this the only window. */
591 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100593#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 if (aborting()) /* autocmds may abort script processing */
595 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200598
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200599 /* If the buffer was in curwin and the window has changed, go back to that
600 * window, if it still exists. This avoids that ":edit x" triggering a
601 * "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
602 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
603 {
604 block_autocmds();
605 goto_tabpage_win(the_curtab, the_curwin);
606 unblock_autocmds();
607 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200608
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 nwindows = buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610
611 /* decrease the link count from windows (unless not in any window) */
612 if (buf->b_nwindows > 0)
613 --buf->b_nwindows;
614
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100615#ifdef FEAT_DIFF
616 if (diffopt_hiddenoff() && !unload_buf && buf->b_nwindows == 0)
Bram Moolenaarb7407d32018-02-03 17:36:27 +0100617 diff_buf_delete(buf); /* Clear 'diff' for hidden buffer. */
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100618#endif
619
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 /* Return when a window is displaying the buffer or when it's not
621 * unloaded. */
622 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624
625 /* Always remove the buffer when there is no file name. */
626 if (buf->b_ffname == NULL)
627 del_buf = TRUE;
628
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200629 /* When closing the current buffer stop Visual mode before freeing
630 * anything. */
Bram Moolenaar4930a762016-09-11 14:39:53 +0200631 if (buf == curbuf && VIsual_active
Bram Moolenaar9a27c7f2016-09-09 12:57:09 +0200632#if defined(EXITFREE)
633 && !entered_free_all_mem
634#endif
635 )
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200636 end_visual_mode();
637
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 /*
639 * Free all things allocated for this buffer.
640 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
641 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 /* Remember if we are closing the current buffer. Restore the number of
643 * windows, so that autocommands in buf_freeall() don't get confused. */
644 is_curbuf = (buf == curbuf);
645 buf->b_nwindows = nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200647 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 /* Autocommands may have deleted the buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200650 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100652#ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000653 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100655#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 /*
658 * It's possible that autocommands change curbuf to the one being deleted.
659 * This might cause the previous curbuf to be deleted unexpectedly. But
660 * in some cases it's OK to delete the curbuf, because a new one is
661 * obtained anyway. Therefore only return if curbuf changed to the
662 * deleted buffer.
663 */
664 if (buf == curbuf && !is_curbuf)
665 return;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200666
Bram Moolenaar4033c552017-09-16 20:54:51 +0200667 if (win_valid_any_tab(win) && win->w_buffer == buf)
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200668 win->w_buffer = NULL; /* make sure we don't use the buffer now */
669
670 /* Autocommands may have opened or closed windows for this buffer.
671 * Decrement the count for the close we do here. */
672 if (buf->b_nwindows > 0)
673 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 /*
676 * Remove the buffer from the list.
677 */
678 if (wipe_buf)
679 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +0200680 if (buf->b_sfname != buf->b_ffname)
681 VIM_CLEAR(buf->b_sfname);
682 else
683 buf->b_sfname = NULL;
684 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 if (buf->b_prev == NULL)
686 firstbuf = buf->b_next;
687 else
688 buf->b_prev->b_next = buf->b_next;
689 if (buf->b_next == NULL)
690 lastbuf = buf->b_prev;
691 else
692 buf->b_next->b_prev = buf->b_prev;
693 free_buffer(buf);
694 }
695 else
696 {
697 if (del_buf)
698 {
699 /* Free all internal variables and reset option values, to make
700 * ":bdel" compatible with Vim 5.7. */
701 free_buffer_stuff(buf, TRUE);
702
703 /* Make it look like a new buffer. */
704 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
705
706 /* Init the options when loaded again. */
707 buf->b_p_initialized = FALSE;
708 }
709 buf_clear_file(buf);
710 if (del_buf)
711 buf->b_p_bl = FALSE;
712 }
713}
714
715/*
716 * Make buffer not contain a file.
717 */
718 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100719buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720{
721 buf->b_ml.ml_line_count = 1;
Bram Moolenaarc024b462019-06-08 18:07:21 +0200722 unchanged(buf, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 buf->b_p_eol = TRUE;
725 buf->b_start_eol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000727 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 buf->b_ml.ml_mfp = NULL;
729 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
730#ifdef FEAT_NETBEANS_INTG
731 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732#endif
733}
734
735/*
736 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200737 * the file. Careful: get here with "curwin" NULL when exiting.
738 * flags:
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200739 * BFA_DEL buffer is going to be deleted
740 * BFA_WIPE buffer is going to be wiped out
741 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100744buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200747 bufref_T bufref;
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200748 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaar5a497892016-09-03 16:29:04 +0200749 win_T *the_curwin = curwin;
750 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751
Bram Moolenaar5a497892016-09-03 16:29:04 +0200752 /* Make sure the buffer isn't closed by autocommands. */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200753 ++buf->b_locked;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200754 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200755 if (buf->b_ml.ml_mfp != NULL)
756 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200757 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
758 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200759 && !bufref_valid(&bufref))
760 /* autocommands deleted the buffer */
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200761 return;
762 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200763 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200765 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
766 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200767 && !bufref_valid(&bufref))
768 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 return;
770 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200771 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200773 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
774 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200775 && !bufref_valid(&bufref))
776 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 return;
778 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200779 --buf->b_locked;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200780
Bram Moolenaar5a497892016-09-03 16:29:04 +0200781 /* If the buffer was in curwin and the window has changed, go back to that
782 * window, if it still exists. This avoids that ":edit x" triggering a
783 * "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
784 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
785 {
786 block_autocmds();
787 goto_tabpage_win(the_curtab, the_curwin);
788 unblock_autocmds();
789 }
Bram Moolenaar5a497892016-09-03 16:29:04 +0200790
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100791#ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000792 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795
796 /*
797 * It's possible that autocommands change curbuf to the one being deleted.
798 * This might cause curbuf to be deleted unexpectedly. But in some cases
799 * it's OK to delete the curbuf, because a new one is obtained anyway.
800 * Therefore only return if curbuf changed to the deleted buffer.
801 */
802 if (buf == curbuf && !is_curbuf)
803 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804#ifdef FEAT_DIFF
805 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
806#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200807#ifdef FEAT_SYN_HL
Bram Moolenaar89c71222011-11-30 15:40:56 +0100808 /* Remove any ownsyntax, unless exiting. */
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200809 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaar89c71222011-11-30 15:40:56 +0100810 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200811#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000812
813#ifdef FEAT_FOLDING
814 /* No folds in an empty buffer. */
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000815 {
816 win_T *win;
817 tabpage_T *tp;
818
819 FOR_ALL_TAB_WINDOWS(tp, win)
820 if (win->w_buffer == buf)
821 clearFolding(win);
822 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000823#endif
824
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825#ifdef FEAT_TCL
826 tcl_buffer_free(buf);
827#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 ml_close(buf, TRUE); /* close and delete the memline/memfile */
829 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200830 if ((flags & BFA_KEEP_UNDO) == 0)
831 {
832 u_blockfree(buf); /* free the memory allocated for undo */
833 u_clearall(buf); /* reset all undo information */
834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200836 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100838#ifdef FEAT_TEXT_PROP
839 clear_buf_prop_types(buf);
840#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000841 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842}
843
844/*
845 * Free a buffer structure and the things it contains related to the buffer
846 * itself (not the file, that must have been done already).
847 */
848 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100849free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200851 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200853#ifdef FEAT_EVAL
Bram Moolenaarb2c87502017-10-14 21:15:58 +0200854 /* b:changedtick uses an item in buf_T, remove it now */
855 dictitem_remove(buf->b_vars, (dictitem_T *)&buf->b_ct_di);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200856 unref_var_dict(buf->b_vars);
857#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200858#ifdef FEAT_LUA
859 lua_buffer_free(buf);
860#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000861#ifdef FEAT_MZSCHEME
862 mzscheme_buffer_free(buf);
863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864#ifdef FEAT_PERL
865 perl_buf_free(buf);
866#endif
867#ifdef FEAT_PYTHON
868 python_buffer_free(buf);
869#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200870#ifdef FEAT_PYTHON3
871 python3_buffer_free(buf);
872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873#ifdef FEAT_RUBY
874 ruby_buffer_free(buf);
875#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200876#ifdef FEAT_JOB_CHANNEL
877 channel_buffer_free(buf);
878#endif
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200879#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +0200880 free_terminal(buf);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200881#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +0200882#ifdef FEAT_JOB_CHANNEL
883 vim_free(buf->b_prompt_text);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200884 free_callback(&buf->b_prompt_callback);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200885#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200886
887 buf_hashtab_remove(buf);
888
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200889 aubuflocal_remove(buf);
890
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200891 if (autocmd_busy)
892 {
893 /* Do not free the buffer structure while autocommands are executing,
894 * it's still needed. Free it when autocmd_busy is reset. */
895 buf->b_next = au_pending_free_buf;
896 au_pending_free_buf = buf;
897 }
898 else
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200899 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900}
901
902/*
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100903 * Initializes b:changedtick.
Bram Moolenaar79518e22017-02-17 16:31:35 +0100904 */
905 static void
906init_changedtick(buf_T *buf)
907{
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100908 dictitem_T *di = (dictitem_T *)&buf->b_ct_di;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100909
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100910 di->di_flags = DI_FLAGS_FIX | DI_FLAGS_RO;
911 di->di_tv.v_type = VAR_NUMBER;
912 di->di_tv.v_lock = VAR_FIXED;
913 di->di_tv.vval.v_number = 0;
914
Bram Moolenaar92769c32017-02-25 15:41:37 +0100915#ifdef FEAT_EVAL
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100916 STRCPY(buf->b_ct_di.di_key, "changedtick");
917 (void)dict_add(buf->b_vars, di);
Bram Moolenaar92769c32017-02-25 15:41:37 +0100918#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +0100919}
920
921/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
923 */
924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100925free_buffer_stuff(
926 buf_T *buf,
927 int free_options) /* free options as well */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928{
929 if (free_options)
930 {
931 clear_wininfo(buf); /* including window-local options */
932 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200933#ifdef FEAT_SPELL
934 ga_clear(&buf->b_s.b_langp);
935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 }
937#ifdef FEAT_EVAL
Bram Moolenaar79518e22017-02-17 16:31:35 +0100938 {
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100939 varnumber_T tick = CHANGEDTICK(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +0100940
941 vars_clear(&buf->b_vars->dv_hashtab); /* free all buffer variables */
942 hash_init(&buf->b_vars->dv_hashtab);
943 init_changedtick(buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100944 CHANGEDTICK(buf) = tick;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946#endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200947 uc_clear(&buf->b_ucmds); // clear local user commands
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948#ifdef FEAT_SIGNS
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200949 buf_delete_signs(buf, (char_u *)"*"); // delete any signs
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000951#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200952 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
955 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100956 VIM_CLEAR(buf->b_start_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957}
958
959/*
960 * Free the b_wininfo list for buffer "buf".
961 */
962 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100963clear_wininfo(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964{
965 wininfo_T *wip;
966
967 while (buf->b_wininfo != NULL)
968 {
969 wip = buf->b_wininfo;
970 buf->b_wininfo = wip->wi_next;
971 if (wip->wi_optset)
972 {
973 clear_winopt(&wip->wi_opt);
974#ifdef FEAT_FOLDING
975 deleteFoldRecurse(&wip->wi_folds);
976#endif
977 }
978 vim_free(wip);
979 }
980}
981
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982/*
983 * Go to another buffer. Handles the result of the ATTENTION dialog.
984 */
985 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100986goto_buffer(
987 exarg_T *eap,
988 int start,
989 int dir,
990 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200992 bufref_T old_curbuf;
993
994 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995
996 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
998 start, dir, count, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
1000 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001001#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001002 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001003
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001004 /* Reset the error/interrupt/exception state here so that
1005 * aborting() returns FALSE when closing a window. */
1006 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001007#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001008
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001009 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 win_close(curwin, TRUE);
1011 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001012 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001013
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001014#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001015 /* Restore the error/interrupt/exception state if not discarded by a
1016 * new aborting error, interrupt, or uncaught exception. */
1017 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 }
1020 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001021 handle_swap_exists(&old_curbuf);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001022}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024/*
1025 * Handle the situation of swap_exists_action being set.
1026 * It is allowed for "old_curbuf" to be NULL or invalid.
1027 */
1028 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001029handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030{
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001031#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001032 cleanup_T cs;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001033#endif
1034#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001035 long old_tw = curbuf->b_p_tw;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001036#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001037 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001038
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 if (swap_exists_action == SEA_QUIT)
1040 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001041#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001042 /* Reset the error/interrupt/exception state here so that
1043 * aborting() returns FALSE when closing a buffer. */
1044 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001045#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001046
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 /* User selected Quit at ATTENTION prompt. Go back to previous
1048 * buffer. If that buffer is gone or the same as the current one,
1049 * open a new, empty buffer. */
1050 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001051 swap_exists_did_quit = TRUE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001052 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001053 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1054 || old_curbuf->br_buf == curbuf)
1055 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
1056 else
1057 buf = old_curbuf->br_buf;
1058 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001059 {
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001060 int old_msg_silent = msg_silent;
1061
1062 if (shortmess(SHM_FILEINFO))
1063 msg_silent = 1; // prevent fileinfo message
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001064 enter_buffer(buf);
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001065 // restore msg_silent, so that the command line will be shown
1066 msg_silent = old_msg_silent;
1067
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001068#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001069 if (old_tw != curbuf->b_p_tw)
1070 check_colorcolumn(curwin);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001071#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001074
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001075#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001076 /* Restore the error/interrupt/exception state if not discarded by a
1077 * new aborting error, interrupt, or uncaught exception. */
1078 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 }
1081 else if (swap_exists_action == SEA_RECOVER)
1082 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001083#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001084 /* Reset the error/interrupt/exception state here so that
1085 * aborting() returns FALSE when closing a buffer. */
1086 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001087#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001088
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 /* User selected Recover at ATTENTION prompt. */
1090 msg_scroll = TRUE;
Bram Moolenaar99499b12019-05-23 21:35:48 +02001091 ml_recover(FALSE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001092 msg_puts("\n"); /* don't overwrite the last message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001094 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001095
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001096#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001097 /* Restore the error/interrupt/exception state if not discarded by a
1098 * new aborting error, interrupt, or uncaught exception. */
1099 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 }
1102 swap_exists_action = SEA_NONE;
1103}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105/*
1106 * do_bufdel() - delete or unload buffer(s)
1107 *
1108 * addr_count == 0: ":bdel" - delete current buffer
1109 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1110 * buffer "end_bnr", then any other arguments.
1111 * addr_count == 2: ":N,N bdel" - delete buffers in range
1112 *
1113 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1114 * DOBUF_DEL (":bdel")
1115 *
1116 * Returns error message or NULL
1117 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001118 char *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001119do_bufdel(
1120 int command,
1121 char_u *arg, /* pointer to extra arguments */
1122 int addr_count,
1123 int start_bnr, /* first buffer number in a range */
1124 int end_bnr, /* buffer nr or last buffer nr in a range */
1125 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126{
1127 int do_current = 0; /* delete current buffer? */
1128 int deleted = 0; /* number of buffers deleted */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001129 char *errormsg = NULL; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 int bnr; /* buffer number */
1131 char_u *p;
1132
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 if (addr_count == 0)
1134 {
1135 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1136 }
1137 else
1138 {
1139 if (addr_count == 2)
1140 {
1141 if (*arg) /* both range and argument is not allowed */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001142 return _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 bnr = start_bnr;
1144 }
1145 else /* addr_count == 1 */
1146 bnr = end_bnr;
1147
1148 for ( ;!got_int; ui_breakcheck())
1149 {
1150 /*
1151 * delete the current buffer last, otherwise when the
1152 * current buffer is deleted, the next buffer becomes
1153 * the current one and will be loaded, which may then
1154 * also be deleted, etc.
1155 */
1156 if (bnr == curbuf->b_fnum)
1157 do_current = bnr;
1158 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
1159 forceit) == OK)
1160 ++deleted;
1161
1162 /*
1163 * find next buffer number to delete/unload
1164 */
1165 if (addr_count == 2)
1166 {
1167 if (++bnr > end_bnr)
1168 break;
1169 }
1170 else /* addr_count == 1 */
1171 {
1172 arg = skipwhite(arg);
1173 if (*arg == NUL)
1174 break;
1175 if (!VIM_ISDIGIT(*arg))
1176 {
1177 p = skiptowhite_esc(arg);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01001178 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE,
1179 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 if (bnr < 0) /* failed */
1181 break;
1182 arg = p;
1183 }
1184 else
1185 bnr = getdigits(&arg);
1186 }
1187 }
1188 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1189 FORWARD, do_current, forceit) == OK)
1190 ++deleted;
1191
1192 if (deleted == 0)
1193 {
1194 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001195 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001197 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00001199 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001200 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 }
1202 else if (deleted >= p_report)
1203 {
1204 if (command == DOBUF_UNLOAD)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001205 smsg(NGETTEXT("%d buffer unloaded",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001206 "%d buffers unloaded", deleted), deleted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 else if (command == DOBUF_DEL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001208 smsg(NGETTEXT("%d buffer deleted",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001209 "%d buffers deleted", deleted), deleted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001211 smsg(NGETTEXT("%d buffer wiped out",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001212 "%d buffers wiped out", deleted), deleted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 }
1214 }
1215
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216
1217 return errormsg;
1218}
1219
Bram Moolenaara02471e2014-01-10 16:43:14 +01001220/*
1221 * Make the current buffer empty.
1222 * Used when it is wiped out and it's the last buffer.
1223 */
1224 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001225empty_curbuf(
1226 int close_others,
1227 int forceit,
1228 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001229{
1230 int retval;
1231 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001232 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001233
1234 if (action == DOBUF_UNLOAD)
1235 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001236 emsg(_("E90: Cannot unload last buffer"));
Bram Moolenaara02471e2014-01-10 16:43:14 +01001237 return FAIL;
1238 }
1239
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001240 set_bufref(&bufref, buf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001241 if (close_others)
1242 /* Close any other windows on this buffer, then make it empty. */
Bram Moolenaara02471e2014-01-10 16:43:14 +01001243 close_windows(buf, TRUE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001244
1245 setpcmark();
1246 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1247 forceit ? ECMD_FORCEIT : 0, curwin);
1248
1249 /*
1250 * do_ecmd() may create a new buffer, then we have to delete
1251 * the old one. But do_ecmd() may have done that already, check
1252 * if the buffer still exists.
1253 */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001254 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001255 close_buffer(NULL, buf, action, FALSE);
1256 if (!close_others)
1257 need_fileinfo = FALSE;
1258 return retval;
1259}
Bram Moolenaar94f01952018-09-01 15:30:03 +02001260
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261/*
1262 * Implementation of the commands for the buffer list.
1263 *
1264 * action == DOBUF_GOTO go to specified buffer
1265 * action == DOBUF_SPLIT split window and go to specified buffer
1266 * action == DOBUF_UNLOAD unload specified buffer(s)
1267 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1268 * action == DOBUF_WIPE delete specified buffer(s) really
1269 *
1270 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1271 * start == DOBUF_FIRST go to "count" buffer from first buffer
1272 * start == DOBUF_LAST go to "count" buffer from last buffer
1273 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1274 *
1275 * Return FAIL or OK.
1276 */
1277 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001278do_buffer(
1279 int action,
1280 int start,
1281 int dir, /* FORWARD or BACKWARD */
1282 int count, /* buffer number or number of buffers */
1283 int forceit) /* TRUE for :...! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284{
1285 buf_T *buf;
1286 buf_T *bp;
1287 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1288 || action == DOBUF_WIPE);
1289
1290 switch (start)
1291 {
1292 case DOBUF_FIRST: buf = firstbuf; break;
1293 case DOBUF_LAST: buf = lastbuf; break;
1294 default: buf = curbuf; break;
1295 }
1296 if (start == DOBUF_MOD) /* find next modified buffer */
1297 {
1298 while (count-- > 0)
1299 {
1300 do
1301 {
1302 buf = buf->b_next;
1303 if (buf == NULL)
1304 buf = firstbuf;
1305 }
1306 while (buf != curbuf && !bufIsChanged(buf));
1307 }
1308 if (!bufIsChanged(buf))
1309 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001310 emsg(_("E84: No modified buffer found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 return FAIL;
1312 }
1313 }
1314 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
1315 {
1316 while (buf != NULL && buf->b_fnum != count)
1317 buf = buf->b_next;
1318 }
1319 else
1320 {
1321 bp = NULL;
1322 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1323 {
1324 /* remember the buffer where we start, we come back there when all
1325 * buffers are unlisted. */
1326 if (bp == NULL)
1327 bp = buf;
1328 if (dir == FORWARD)
1329 {
1330 buf = buf->b_next;
1331 if (buf == NULL)
1332 buf = firstbuf;
1333 }
1334 else
1335 {
1336 buf = buf->b_prev;
1337 if (buf == NULL)
1338 buf = lastbuf;
1339 }
1340 /* don't count unlisted buffers */
1341 if (unload || buf->b_p_bl)
1342 {
1343 --count;
1344 bp = NULL; /* use this buffer as new starting point */
1345 }
1346 if (bp == buf)
1347 {
1348 /* back where we started, didn't find anything. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001349 emsg(_("E85: There is no listed buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 return FAIL;
1351 }
1352 }
1353 }
1354
1355 if (buf == NULL) /* could not find it */
1356 {
1357 if (start == DOBUF_FIRST)
1358 {
1359 /* don't warn when deleting */
1360 if (!unload)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001361 semsg(_(e_nobufnr), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 }
1363 else if (dir == FORWARD)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001364 emsg(_("E87: Cannot go beyond last buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001366 emsg(_("E88: Cannot go before first buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 return FAIL;
1368 }
1369
1370#ifdef FEAT_GUI
1371 need_mouse_correct = TRUE;
1372#endif
1373
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 /*
1375 * delete buffer buf from memory and/or the list
1376 */
1377 if (unload)
1378 {
1379 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001380 bufref_T bufref;
1381
Bram Moolenaar94f01952018-09-01 15:30:03 +02001382 if (!can_unload_buffer(buf))
Bram Moolenaara997b452018-04-17 23:24:06 +02001383 return FAIL;
Bram Moolenaara997b452018-04-17 23:24:06 +02001384
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001385 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386
1387 /* When unloading or deleting a buffer that's already unloaded and
1388 * unlisted: fail silently. */
1389 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1390 return FAIL;
1391
1392 if (!forceit && bufIsChanged(buf))
1393 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001394#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 if ((p_confirm || cmdmod.confirm) && p_write)
1396 {
1397 dialog_changed(buf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001398 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 /* Autocommand deleted buffer, oops! It's not changed
1400 * now. */
1401 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001402 /* If it's still changed fail silently, the dialog already
1403 * mentioned why it fails. */
1404 if (bufIsChanged(buf))
1405 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001407 else
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001408#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001410 semsg(_("E89: No write since last change for buffer %d (add ! to override)"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 buf->b_fnum);
1412 return FAIL;
1413 }
1414 }
1415
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001416 /* When closing the current buffer stop Visual mode. */
Bram Moolenaar4930a762016-09-11 14:39:53 +02001417 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001418 end_visual_mode();
1419
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 /*
1421 * If deleting the last (listed) buffer, make it empty.
1422 * The last (listed) buffer cannot be unloaded.
1423 */
Bram Moolenaar29323592016-07-24 22:04:11 +02001424 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 if (bp->b_p_bl && bp != buf)
1426 break;
1427 if (bp == NULL && buf == curbuf)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001428 return empty_curbuf(TRUE, forceit, action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 /*
1431 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001432 * (unless it's the only window). Repeat this so long as we end up in
1433 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001435 while (buf == curbuf
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001436 && !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
Bram Moolenaar459ca562016-11-10 18:16:33 +01001437 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001438 {
1439 if (win_close(curwin, FALSE) == FAIL)
1440 break;
1441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442
1443 /*
1444 * If the buffer to be deleted is not the current one, delete it here.
1445 */
1446 if (buf != curbuf)
1447 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001448 close_windows(buf, FALSE);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001449 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows <= 0)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001450 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 return OK;
1452 }
1453
1454 /*
1455 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001456 * There should be another, otherwise it would have been handled
1457 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001458 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 * Then prefer the buffer we most recently visited.
1460 * Else try to find one that is loaded, after the current buffer,
1461 * then before the current buffer.
1462 * Finally use any buffer.
1463 */
1464 buf = NULL; /* selected buffer */
1465 bp = NULL; /* used when no loaded buffer found */
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001466 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1467 buf = au_new_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468#ifdef FEAT_JUMPLIST
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001469 else if (curwin->w_jumplistlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 {
1471 int jumpidx;
1472
1473 jumpidx = curwin->w_jumplistidx - 1;
1474 if (jumpidx < 0)
1475 jumpidx = curwin->w_jumplistlen - 1;
1476
1477 forward = jumpidx;
1478 while (jumpidx != curwin->w_jumplistidx)
1479 {
1480 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1481 if (buf != NULL)
1482 {
1483 if (buf == curbuf || !buf->b_p_bl)
1484 buf = NULL; /* skip current and unlisted bufs */
1485 else if (buf->b_ml.ml_mfp == NULL)
1486 {
1487 /* skip unloaded buf, but may keep it for later */
1488 if (bp == NULL)
1489 bp = buf;
1490 buf = NULL;
1491 }
1492 }
1493 if (buf != NULL) /* found a valid buffer: stop searching */
1494 break;
1495 /* advance to older entry in jump list */
1496 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1497 break;
1498 if (--jumpidx < 0)
1499 jumpidx = curwin->w_jumplistlen - 1;
1500 if (jumpidx == forward) /* List exhausted for sure */
1501 break;
1502 }
1503 }
1504#endif
1505
1506 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1507 {
1508 forward = TRUE;
1509 buf = curbuf->b_next;
1510 for (;;)
1511 {
1512 if (buf == NULL)
1513 {
1514 if (!forward) /* tried both directions */
1515 break;
1516 buf = curbuf->b_prev;
1517 forward = FALSE;
1518 continue;
1519 }
1520 /* in non-help buffer, try to skip help buffers, and vv */
1521 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1522 {
1523 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1524 break;
1525 if (bp == NULL) /* remember unloaded buf for later */
1526 bp = buf;
1527 }
1528 if (forward)
1529 buf = buf->b_next;
1530 else
1531 buf = buf->b_prev;
1532 }
1533 }
1534 if (buf == NULL) /* No loaded buffer, use unloaded one */
1535 buf = bp;
1536 if (buf == NULL) /* No loaded buffer, find listed one */
1537 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001538 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 if (buf->b_p_bl && buf != curbuf)
1540 break;
1541 }
1542 if (buf == NULL) /* Still no buffer, just take one */
1543 {
1544 if (curbuf->b_next != NULL)
1545 buf = curbuf->b_next;
1546 else
1547 buf = curbuf->b_prev;
1548 }
1549 }
1550
Bram Moolenaara02471e2014-01-10 16:43:14 +01001551 if (buf == NULL)
1552 {
1553 /* Autocommands must have wiped out all other buffers. Only option
1554 * now is to make the current buffer empty. */
1555 return empty_curbuf(FALSE, forceit, action);
1556 }
1557
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 /*
1559 * make buf current buffer
1560 */
1561 if (action == DOBUF_SPLIT) /* split window first */
1562 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001563 /* If 'switchbuf' contains "useopen": jump to first window containing
1564 * "buf" if one exists */
1565 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001566 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001567 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001568 * page containing "buf" if one exists */
1569 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 return OK;
1571 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 return FAIL;
1573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574
1575 /* go to current buffer - nothing to do */
1576 if (buf == curbuf)
1577 return OK;
1578
1579 /*
1580 * Check if the current buffer may be abandoned.
1581 */
1582 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1583 {
1584#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1585 if ((p_confirm || cmdmod.confirm) && p_write)
1586 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001587 bufref_T bufref;
1588
1589 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 dialog_changed(curbuf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001591 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 /* Autocommand deleted buffer, oops! */
1593 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 }
1595 if (bufIsChanged(curbuf))
1596#endif
1597 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001598 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 return FAIL;
1600 }
1601 }
1602
1603 /* Go to the other buffer. */
1604 set_curbuf(buf, action);
1605
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001607 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001609#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 if (aborting()) /* autocmds may abort script processing */
1611 return FAIL;
1612#endif
1613
1614 return OK;
1615}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616
1617/*
1618 * Set current buffer to "buf". Executes autocommands and closes current
1619 * buffer. "action" tells how to close the current buffer:
1620 * DOBUF_GOTO free or hide it
1621 * DOBUF_SPLIT nothing
1622 * DOBUF_UNLOAD unload it
1623 * DOBUF_DEL delete it
1624 * DOBUF_WIPE wipe it out
1625 */
1626 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001627set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628{
1629 buf_T *prevbuf;
1630 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1631 || action == DOBUF_WIPE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001632#ifdef FEAT_SYN_HL
1633 long old_tw = curbuf->b_p_tw;
1634#endif
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001635 bufref_T newbufref;
1636 bufref_T prevbufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637
1638 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001639 if (!cmdmod.keepalt)
1640 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001641 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 /* Don't restart Select mode after switching to another buffer. */
1644 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001646 /* close_windows() or apply_autocmds() may change curbuf and wipe out "buf"
1647 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 prevbuf = curbuf;
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001649 set_bufref(&prevbufref, prevbuf);
1650 set_bufref(&newbufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001652 /* Autocommands may delete the curren buffer and/or the buffer we wan to go
1653 * to. In those cases don't close the buffer. */
Bram Moolenaar82404332016-07-10 17:00:38 +02001654 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001655 || (bufref_valid(&prevbufref)
1656 && bufref_valid(&newbufref)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001657#ifdef FEAT_EVAL
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001658 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001660 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001662#ifdef FEAT_SYN_HL
1663 if (prevbuf == curwin->w_buffer)
1664 reset_synblock(curwin);
1665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001667 close_windows(prevbuf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001668#if defined(FEAT_EVAL)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001669 if (bufref_valid(&prevbufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670#else
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001671 if (bufref_valid(&prevbufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001673 {
Bram Moolenaare25865a2012-07-06 16:22:02 +02001674 win_T *previouswin = curwin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001675 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001676 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1678 unload ? action : (action == DOBUF_GOTO
Bram Moolenaareb44a682017-08-03 22:44:55 +02001679 && !buf_hide(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001680 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaare25865a2012-07-06 16:22:02 +02001681 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaar9e931222012-06-20 11:55:01 +02001682 /* autocommands changed curwin, Grr! */
Bram Moolenaare25865a2012-07-06 16:22:02 +02001683 curwin = previouswin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 }
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001686 /* An autocommand may have deleted "buf", already entered it (e.g., when
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001687 * it did ":bunload") or aborted the script processing.
Bram Moolenaar9e931222012-06-20 11:55:01 +02001688 * If curwin->w_buffer is null, enter_buffer() will make it valid again */
1689 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001690#ifdef FEAT_EVAL
Bram Moolenaar4033c552017-09-16 20:54:51 +02001691 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001693 ) || curwin->w_buffer == NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001694 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001696#ifdef FEAT_SYN_HL
1697 if (old_tw != curbuf->b_p_tw)
1698 check_colorcolumn(curwin);
1699#endif
1700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701}
1702
1703/*
1704 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001705 * Old curbuf must have been abandoned already! This also means "curbuf" may
1706 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 */
1708 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001709enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710{
1711 /* Copy buffer and window local option values. Not for a help buffer. */
1712 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1713 if (!buf->b_help)
1714 get_winopts(buf);
1715#ifdef FEAT_FOLDING
1716 else
1717 /* Remove all folds in the window. */
1718 clearFolding(curwin);
1719 foldUpdateAll(curwin); /* update folds (later). */
1720#endif
1721
1722 /* Get the buffer in the current window. */
1723 curwin->w_buffer = buf;
1724 curbuf = buf;
1725 ++curbuf->b_nwindows;
1726
1727#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001728 if (curwin->w_p_diff)
1729 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730#endif
1731
Bram Moolenaara971b822011-09-14 14:43:25 +02001732#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001733 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001734#endif
1735
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 /* Cursor on first line by default. */
1737 curwin->w_cursor.lnum = 1;
1738 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001741 curwin->w_topline_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001743 /* mark cursor position as being invalid */
1744 curwin->w_valid = 0;
1745
Bram Moolenaar9cea87c2018-09-21 16:59:45 +02001746 buflist_setfpos(curbuf, curwin, curbuf->b_last_cursor.lnum,
1747 curbuf->b_last_cursor.col, TRUE);
1748
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 /* Make sure the buffer is loaded. */
1750 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001751 {
1752 /* If there is no filetype, allow for detecting one. Esp. useful for
1753 * ":ball" used in a autocommand. If there already is a filetype we
1754 * might prefer to keep it. */
1755 if (*curbuf->b_p_ft == NUL)
1756 did_filetype = FALSE;
1757
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001758 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 else
1761 {
Bram Moolenaareda65222019-05-16 20:29:44 +02001762 if (!msg_silent && !shortmess(SHM_FILEINFO))
1763 need_fileinfo = TRUE; // display file info after redraw
1764
1765 // check if file changed
1766 (void)buf_check_timestamp(curbuf, FALSE);
1767
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001769#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1773 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 }
1775
1776 /* If autocommands did not change the cursor position, restore cursor lnum
1777 * and possibly cursor col. */
1778 if (curwin->w_cursor.lnum == 1 && inindent(0))
1779 buflist_getfpos();
1780
1781 check_arg_idx(curwin); /* check for valid arg_idx */
1782#ifdef FEAT_TITLE
1783 maketitle();
1784#endif
Bram Moolenaard4153d42008-11-15 15:06:17 +00001785 /* when autocmds didn't change it */
1786 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1788
Bram Moolenaar009b2592004-10-24 19:18:58 +00001789#ifdef FEAT_NETBEANS_INTG
1790 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001791 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001792#endif
1793
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001794 /* Change directories when the 'acd' option is set. */
Bram Moolenaar6f470022018-04-10 18:47:20 +02001795 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796
1797#ifdef FEAT_KEYMAP
1798 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001799 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001801#ifdef FEAT_SPELL
1802 /* May need to set the spell language. Can only do this after the buffer
1803 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001804 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1805 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001806#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001807#ifdef FEAT_VIMINFO
1808 curbuf->b_last_used = vim_time();
1809#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001810
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 redraw_later(NOT_VALID);
1812}
1813
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001814#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1815/*
1816 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001817 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001818 */
1819 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001820do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001821{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001822 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001823 && curbuf->b_ffname != NULL
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001824 && vim_chdirfile(curbuf->b_ffname, "auto") == OK)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001825 shorten_fnames(TRUE);
1826}
1827#endif
1828
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001829 void
1830no_write_message(void)
1831{
1832#ifdef FEAT_TERMINAL
1833 if (term_job_running(curbuf->b_term))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001834 emsg(_("E948: Job still running (add ! to end the job)"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001835 else
1836#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001837 emsg(_("E37: No write since last change (add ! to override)"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001838}
1839
1840 void
Bram Moolenaar7a760922018-02-19 23:10:02 +01001841no_write_message_nobang(buf_T *buf UNUSED)
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001842{
1843#ifdef FEAT_TERMINAL
Bram Moolenaar7a760922018-02-19 23:10:02 +01001844 if (term_job_running(buf->b_term))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001845 emsg(_("E948: Job still running"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001846 else
1847#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001848 emsg(_("E37: No write since last change"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001849}
1850
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851/*
1852 * functions for dealing with the buffer list
1853 */
1854
Bram Moolenaar480778b2016-07-14 22:09:39 +02001855static int top_file_num = 1; /* highest file number */
1856
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857/*
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001858 * Return TRUE if the current buffer is empty, unnamed, unmodified and used in
1859 * only one window. That means it can be re-used.
1860 */
1861 int
1862curbuf_reusable(void)
1863{
1864 return (curbuf != NULL
1865 && curbuf->b_ffname == NULL
1866 && curbuf->b_nwindows <= 1
1867 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY())
Bram Moolenaard85c3962019-04-07 14:19:14 +02001868#if defined(FEAT_QUICKFIX)
Bram Moolenaar39803d82019-04-07 12:04:51 +02001869 && !bt_quickfix(curbuf)
Bram Moolenaard85c3962019-04-07 14:19:14 +02001870#endif
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001871 && !curbufIsChanged());
1872}
1873
1874/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 * Add a file name to the buffer list. Return a pointer to the buffer.
1876 * If the same file name already exists return a pointer to that buffer.
1877 * If it does not exist, or if fname == NULL, a new entry is created.
1878 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1879 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1880 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001881 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001882 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
1883 * if the buffer already exists.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 * This is the ONLY way to create a new buffer.
1885 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001887buflist_new(
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001888 char_u *ffname_arg, // full path of fname or relative
1889 char_u *sfname_arg, // short fname or NULL
1890 linenr_T lnum, // preferred cursor line
1891 int flags) // BLN_ defines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001893 char_u *ffname = ffname_arg;
1894 char_u *sfname = sfname_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 buf_T *buf;
1896#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001897 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898#endif
1899
Bram Moolenaar480778b2016-07-14 22:09:39 +02001900 if (top_file_num == 1)
1901 hash_init(&buf_hashtab);
1902
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001903 fname_expand(curbuf, &ffname, &sfname); // will allocate ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904
1905 /*
1906 * If file name already exists in the list, update the entry.
1907 */
1908#ifdef UNIX
1909 /* On Unix we can use inode numbers when the file exists. Works better
1910 * for hard links. */
1911 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1912 st.st_dev = (dev_T)-1;
1913#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001914 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915#ifdef UNIX
1916 buflist_findname_stat(ffname, &st)
1917#else
1918 buflist_findname(ffname)
1919#endif
1920 ) != NULL)
1921 {
1922 vim_free(ffname);
1923 if (lnum != 0)
1924 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02001925
1926 if ((flags & BLN_NOOPT) == 0)
1927 /* copy the options now, if 'cpo' doesn't have 's' and not done
1928 * already */
1929 buf_copy_options(buf, 0);
1930
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1932 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001933 bufref_T bufref;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001934
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 buf->b_p_bl = TRUE;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001936 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001938 {
Bram Moolenaar82404332016-07-10 17:00:38 +02001939 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001940 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001941 return NULL;
1942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 }
1944 return buf;
1945 }
1946
1947 /*
1948 * If the current buffer has no name and no contents, use the current
1949 * buffer. Otherwise: Need to allocate a new buffer structure.
1950 *
1951 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001952 * (A spell file buffer is allocated in spell.c, but that's not a normal
1953 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 */
1955 buf = NULL;
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001956 if ((flags & BLN_CURBUF) && curbuf_reusable())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 {
1958 buf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 /* It's like this buffer is deleted. Watch out for autocommands that
1960 * change curbuf! If that happens, allocate a new buffer anyway. */
1961 if (curbuf->b_p_bl)
1962 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1963 if (buf == curbuf)
1964 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001965#ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001966 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 if (buf == curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 {
1971 /* Make sure 'bufhidden' and 'buftype' are empty */
1972 clear_string_option(&buf->b_p_bh);
1973 clear_string_option(&buf->b_p_bt);
1974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 }
1976 if (buf != curbuf || curbuf == NULL)
1977 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001978 buf = ALLOC_CLEAR_ONE(buf_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 if (buf == NULL)
1980 {
1981 vim_free(ffname);
1982 return NULL;
1983 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02001984#ifdef FEAT_EVAL
1985 /* init b: variables */
1986 buf->b_vars = dict_alloc();
1987 if (buf->b_vars == NULL)
1988 {
1989 vim_free(ffname);
1990 vim_free(buf);
1991 return NULL;
1992 }
1993 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
1994#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01001995 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 }
1997
1998 if (ffname != NULL)
1999 {
2000 buf->b_ffname = ffname;
2001 buf->b_sfname = vim_strsave(sfname);
2002 }
2003
2004 clear_wininfo(buf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002005 buf->b_wininfo = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006
2007 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
2008 || buf->b_wininfo == NULL)
2009 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002010 if (buf->b_sfname != buf->b_ffname)
2011 VIM_CLEAR(buf->b_sfname);
2012 else
2013 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01002014 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 if (buf != curbuf)
2016 free_buffer(buf);
2017 return NULL;
2018 }
2019
2020 if (buf == curbuf)
2021 {
2022 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002023 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 if (buf != curbuf) /* autocommands deleted the buffer! */
2025 return NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002026#if defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002027 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 return NULL;
2029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002031
2032 /* Init the options. */
2033 buf->b_p_initialized = FALSE;
2034 buf_copy_options(buf, BCO_ENTER);
2035
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036#ifdef FEAT_KEYMAP
2037 /* need to reload lmaps and set b:keymap_name */
2038 curbuf->b_kmap_state |= KEYMAP_INIT;
2039#endif
2040 }
2041 else
2042 {
2043 /*
2044 * put new buffer at the end of the buffer list
2045 */
2046 buf->b_next = NULL;
2047 if (firstbuf == NULL) /* buffer list is empty */
2048 {
2049 buf->b_prev = NULL;
2050 firstbuf = buf;
2051 }
2052 else /* append new buffer at end of list */
2053 {
2054 lastbuf->b_next = buf;
2055 buf->b_prev = lastbuf;
2056 }
2057 lastbuf = buf;
2058
2059 buf->b_fnum = top_file_num++;
2060 if (top_file_num < 0) /* wrap around (may cause duplicates) */
2061 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002062 emsg(_("W14: Warning: List of file names overflow"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 if (emsg_silent == 0)
2064 {
2065 out_flush();
2066 ui_delay(3000L, TRUE); /* make sure it is noticed */
2067 }
2068 top_file_num = 1;
2069 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002070 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071
2072 /*
2073 * Always copy the options from the current buffer.
2074 */
2075 buf_copy_options(buf, BCO_ALWAYS);
2076 }
2077
2078 buf->b_wininfo->wi_fpos.lnum = lnum;
2079 buf->b_wininfo->wi_win = curwin;
2080
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002081#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002082 hash_init(&buf->b_s.b_keywtab);
2083 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084#endif
2085
2086 buf->b_fname = buf->b_sfname;
2087#ifdef UNIX
2088 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002089 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 else
2091 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002092 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 buf->b_dev = st.st_dev;
2094 buf->b_ino = st.st_ino;
2095 }
2096#endif
2097 buf->b_u_synced = TRUE;
2098 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002099 if (flags & BLN_DUMMY)
2100 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 buf_clear_file(buf);
2102 clrallmarks(buf); /* clear marks */
2103 fmarks_check_names(buf); /* check file marks for this file */
2104 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 if (!(flags & BLN_DUMMY))
2106 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002107 bufref_T bufref;
2108
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01002109 /* Tricky: these autocommands may change the buffer list. They could
2110 * also split the window with re-using the one empty buffer. This may
2111 * result in unexpectedly losing the empty buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002112 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002113 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002114 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002115 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002117 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002118 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002119 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002120 return NULL;
2121 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002122#ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002123 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127
2128 return buf;
2129}
2130
2131/*
2132 * Free the memory for the options of a buffer.
2133 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2134 * 'fileencoding'.
2135 */
2136 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002137free_buf_options(
2138 buf_T *buf,
2139 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140{
2141 if (free_p_ff)
2142 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 clear_string_option(&buf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 clear_string_option(&buf->b_p_bh);
2146 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 }
2148#ifdef FEAT_FIND_ID
2149 clear_string_option(&buf->b_p_def);
2150 clear_string_option(&buf->b_p_inc);
2151# ifdef FEAT_EVAL
2152 clear_string_option(&buf->b_p_inex);
2153# endif
2154#endif
2155#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
2156 clear_string_option(&buf->b_p_inde);
2157 clear_string_option(&buf->b_p_indk);
2158#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002159#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2160 clear_string_option(&buf->b_p_bexpr);
2161#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002162#if defined(FEAT_CRYPT)
2163 clear_string_option(&buf->b_p_cm);
2164#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002165 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002166#if defined(FEAT_EVAL)
2167 clear_string_option(&buf->b_p_fex);
2168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169#ifdef FEAT_CRYPT
2170 clear_string_option(&buf->b_p_key);
2171#endif
2172 clear_string_option(&buf->b_p_kp);
2173 clear_string_option(&buf->b_p_mps);
2174 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002175 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 clear_string_option(&buf->b_p_isk);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002177#ifdef FEAT_VARTABS
2178 clear_string_option(&buf->b_p_vsts);
Bram Moolenaarbdace832019-03-02 10:13:42 +01002179 vim_free(buf->b_p_vsts_nopaste);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002180 buf->b_p_vsts_nopaste = NULL;
Bram Moolenaarbdace832019-03-02 10:13:42 +01002181 vim_free(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002182 buf->b_p_vsts_array = NULL;
2183 clear_string_option(&buf->b_p_vts);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002184 VIM_CLEAR(buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002185#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186#ifdef FEAT_KEYMAP
2187 clear_string_option(&buf->b_p_keymap);
Bram Moolenaar50138322018-01-28 17:05:16 +01002188 keymap_clear(&buf->b_kmap_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 ga_clear(&buf->b_kmap_ga);
2190#endif
2191#ifdef FEAT_COMMENTS
2192 clear_string_option(&buf->b_p_com);
2193#endif
2194#ifdef FEAT_FOLDING
2195 clear_string_option(&buf->b_p_cms);
2196#endif
2197 clear_string_option(&buf->b_p_nf);
2198#ifdef FEAT_SYN_HL
2199 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002200 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002201#endif
2202#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002203 clear_string_option(&buf->b_s.b_p_spc);
2204 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002205 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002206 buf->b_s.b_cap_prog = NULL;
2207 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208#endif
2209#ifdef FEAT_SEARCHPATH
2210 clear_string_option(&buf->b_p_sua);
2211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 clear_string_option(&buf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213#ifdef FEAT_CINDENT
2214 clear_string_option(&buf->b_p_cink);
2215 clear_string_option(&buf->b_p_cino);
2216#endif
2217#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
2218 clear_string_option(&buf->b_p_cinw);
2219#endif
2220#ifdef FEAT_INS_EXPAND
2221 clear_string_option(&buf->b_p_cpt);
2222#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002223#ifdef FEAT_COMPL_FUNC
2224 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002225 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227#ifdef FEAT_QUICKFIX
2228 clear_string_option(&buf->b_p_gp);
2229 clear_string_option(&buf->b_p_mp);
2230 clear_string_option(&buf->b_p_efm);
2231#endif
2232 clear_string_option(&buf->b_p_ep);
2233 clear_string_option(&buf->b_p_path);
2234 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002235 clear_string_option(&buf->b_p_tc);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002236#ifdef FEAT_EVAL
2237 clear_string_option(&buf->b_p_tfu);
2238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239#ifdef FEAT_INS_EXPAND
2240 clear_string_option(&buf->b_p_dict);
2241 clear_string_option(&buf->b_p_tsr);
2242#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002243#ifdef FEAT_TEXTOBJ
2244 clear_string_option(&buf->b_p_qe);
2245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002247 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002248#ifdef FEAT_LISP
2249 clear_string_option(&buf->b_p_lw);
2250#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002251 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002252 clear_string_option(&buf->b_p_menc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253}
2254
2255/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002256 * Get alternate file "n".
2257 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2258 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 * if (options & GETF_SETMARK) call setpcmark()
2260 * if (options & GETF_ALT) we are jumping to an alternate file.
2261 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2262 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002263 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 */
2265 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002266buflist_getfile(
2267 int n,
2268 linenr_T lnum,
2269 int options,
2270 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271{
2272 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 pos_T *fpos;
2275 colnr_T col;
2276
2277 buf = buflist_findnr(n);
2278 if (buf == NULL)
2279 {
2280 if ((options & GETF_ALT) && n == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002281 emsg(_(e_noalt));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 else
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01002283 semsg(_("E92: Buffer %d not found"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 return FAIL;
2285 }
2286
2287 /* if alternate file is the current buffer, nothing to do */
2288 if (buf == curbuf)
2289 return OK;
2290
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002291 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002292 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002293 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002295 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002296 if (curbuf_locked())
2297 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298
2299 /* altfpos may be changed by getfile(), get it now */
2300 if (lnum == 0)
2301 {
2302 fpos = buflist_findfpos(buf);
2303 lnum = fpos->lnum;
2304 col = fpos->col;
2305 }
2306 else
2307 col = 0;
2308
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 if (options & GETF_SWITCH)
2310 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002311 /* If 'switchbuf' contains "useopen": jump to first window containing
2312 * "buf" if one exists */
2313 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002315
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002316 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00002317 * page containing "buf" if one exists */
2318 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002319 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002320
2321 /* If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2322 * current buffer isn't empty: open new tab or window */
2323 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002324 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002326 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002327 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002328 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2329 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002331 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 }
2333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334
2335 ++RedrawingDisabled;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002336 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2337 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 {
2339 --RedrawingDisabled;
2340
2341 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
2342 if (!p_sol && col != 0)
2343 {
2344 curwin->w_cursor.col = col;
2345 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 curwin->w_set_curswant = TRUE;
2348 }
2349 return OK;
2350 }
2351 --RedrawingDisabled;
2352 return FAIL;
2353}
2354
2355/*
2356 * go to the last know line number for the current buffer
2357 */
2358 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002359buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360{
2361 pos_T *fpos;
2362
2363 fpos = buflist_findfpos(curbuf);
2364
2365 curwin->w_cursor.lnum = fpos->lnum;
2366 check_cursor_lnum();
2367
2368 if (p_sol)
2369 curwin->w_cursor.col = 0;
2370 else
2371 {
2372 curwin->w_cursor.col = fpos->col;
2373 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 curwin->w_set_curswant = TRUE;
2376 }
2377}
2378
Bram Moolenaar81695252004-12-29 20:58:21 +00002379#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2380/*
2381 * Find file in buffer list by name (it has to be for the current window).
2382 * Returns NULL if not found.
2383 */
2384 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002385buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002386{
2387 char_u *ffname;
2388 buf_T *buf = NULL;
2389
2390 /* First make the name into a full path name */
2391 ffname = FullName_save(fname,
2392#ifdef UNIX
2393 TRUE /* force expansion, get rid of symbolic links */
2394#else
2395 FALSE
2396#endif
2397 );
2398 if (ffname != NULL)
2399 {
2400 buf = buflist_findname(ffname);
2401 vim_free(ffname);
2402 }
2403 return buf;
2404}
2405#endif
2406
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407/*
2408 * Find file in buffer list by name (it has to be for the current window).
2409 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002410 * Skips dummy buffers.
2411 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 */
2413 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002414buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415{
2416#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002417 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418
2419 if (mch_stat((char *)ffname, &st) < 0)
2420 st.st_dev = (dev_T)-1;
2421 return buflist_findname_stat(ffname, &st);
2422}
2423
2424/*
2425 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2426 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002427 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 */
2429 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002430buflist_findname_stat(
2431 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002432 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433{
2434#endif
2435 buf_T *buf;
2436
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002437 /* Start at the last buffer, expect to find a match sooner. */
2438 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar81695252004-12-29 20:58:21 +00002439 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440#ifdef UNIX
2441 , stp
2442#endif
2443 ))
2444 return buf;
2445 return NULL;
2446}
2447
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448/*
2449 * Find file in buffer list by a regexp pattern.
2450 * Return fnum of the found buffer.
2451 * Return < 0 for error.
2452 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002454buflist_findpat(
2455 char_u *pattern,
2456 char_u *pattern_end, /* pointer to first char after pattern */
2457 int unlisted, /* find unlisted buffers */
2458 int diffmode UNUSED, /* find diff-mode buffers only */
2459 int curtab_only) /* find buffers in current tab only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460{
2461 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 int match = -1;
2463 int find_listed;
2464 char_u *pat;
2465 char_u *patend;
2466 int attempt;
2467 char_u *p;
2468 int toggledollar;
2469
2470 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2471 {
2472 if (*pattern == '%')
2473 match = curbuf->b_fnum;
2474 else
2475 match = curwin->w_alt_fnum;
2476#ifdef FEAT_DIFF
2477 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2478 match = -1;
2479#endif
2480 }
2481
2482 /*
2483 * Try four ways of matching a listed buffer:
2484 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002485 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 * attempt == 2: with '$' at end (only match at end)
2487 * attempt == 3: with '^' at start and '$' at end (only full match)
2488 * Repeat this for finding an unlisted buffer if there was no matching
2489 * listed buffer.
2490 */
2491 else
2492 {
2493 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2494 if (pat == NULL)
2495 return -1;
2496 patend = pat + STRLEN(pat) - 1;
2497 toggledollar = (patend > pat && *patend == '$');
2498
2499 /* First try finding a listed buffer. If not found and "unlisted"
2500 * is TRUE, try finding an unlisted buffer. */
2501 find_listed = TRUE;
2502 for (;;)
2503 {
2504 for (attempt = 0; attempt <= 3; ++attempt)
2505 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002506 regmatch_T regmatch;
2507
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 /* may add '^' and '$' */
2509 if (toggledollar)
2510 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2511 p = pat;
2512 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2513 ++p;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002514 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2515 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 {
2517 vim_free(pat);
2518 return -1;
2519 }
2520
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002521 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 if (buf->b_p_bl == find_listed
2523#ifdef FEAT_DIFF
2524 && (!diffmode || diff_mode_buf(buf))
2525#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002526 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002528 if (curtab_only)
2529 {
2530 /* Ignore the match if the buffer is not open in
2531 * the current tab. */
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002532 win_T *wp;
2533
Bram Moolenaar29323592016-07-24 22:04:11 +02002534 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002535 if (wp->w_buffer == buf)
2536 break;
2537 if (wp == NULL)
2538 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 if (match >= 0) /* already found a match */
2541 {
2542 match = -2;
2543 break;
2544 }
2545 match = buf->b_fnum; /* remember first match */
2546 }
2547
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002548 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 if (match >= 0) /* found one match */
2550 break;
2551 }
2552
2553 /* Only search for unlisted buffers if there was no match with
2554 * a listed buffer. */
2555 if (!unlisted || !find_listed || match != -1)
2556 break;
2557 find_listed = FALSE;
2558 }
2559
2560 vim_free(pat);
2561 }
2562
2563 if (match == -2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002564 semsg(_("E93: More than one match for %s"), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 else if (match < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002566 semsg(_("E94: No matching buffer for %s"), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 return match;
2568}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570/*
2571 * Find all buffer names that match.
2572 * For command line expansion of ":buf" and ":sbuf".
2573 * Return OK if matches found, FAIL otherwise.
2574 */
2575 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002576ExpandBufnames(
2577 char_u *pat,
2578 int *num_file,
2579 char_u ***file,
2580 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581{
2582 int count = 0;
2583 buf_T *buf;
2584 int round;
2585 char_u *p;
2586 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002587 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588
2589 *num_file = 0; /* return values in case of FAIL */
2590 *file = NULL;
2591
Bram Moolenaar05159a02005-02-26 23:04:13 +00002592 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2593 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002595 patc = alloc(STRLEN(pat) + 11);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002596 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002598 STRCPY(patc, "\\(^\\|[\\/]\\)");
2599 STRCPY(patc + 11, pat + 1);
2600 }
2601 else
2602 patc = pat;
2603
2604 /*
2605 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002606 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002607 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002608 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002609 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002610 regmatch_T regmatch;
2611
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002612 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002613 break; /* there was no anchor, no need to try again */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002614 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2615 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002616 {
2617 if (patc != pat)
2618 vim_free(patc);
2619 return FAIL;
2620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621
2622 /*
2623 * round == 1: Count the matches.
2624 * round == 2: Build the array to keep the matches.
2625 */
2626 for (round = 1; round <= 2; ++round)
2627 {
2628 count = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002629 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 {
2631 if (!buf->b_p_bl) /* skip unlisted buffers */
2632 continue;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002633 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 if (p != NULL)
2635 {
2636 if (round == 1)
2637 ++count;
2638 else
2639 {
2640 if (options & WILD_HOME_REPLACE)
2641 p = home_replace_save(buf, p);
2642 else
2643 p = vim_strsave(p);
2644 (*file)[count++] = p;
2645 }
2646 }
2647 }
2648 if (count == 0) /* no match found, break here */
2649 break;
2650 if (round == 1)
2651 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002652 *file = ALLOC_MULT(char_u *, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 if (*file == NULL)
2654 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002655 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002656 if (patc != pat)
2657 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 return FAIL;
2659 }
2660 }
2661 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002662 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 if (count) /* match(es) found, break here */
2664 break;
2665 }
2666
Bram Moolenaar05159a02005-02-26 23:04:13 +00002667 if (patc != pat)
2668 vim_free(patc);
2669
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 *num_file = count;
2671 return (count == 0 ? FAIL : OK);
2672}
2673
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674/*
2675 * Check for a match on the file name for buffer "buf" with regprog "prog".
2676 */
2677 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002678buflist_match(
2679 regmatch_T *rmp,
2680 buf_T *buf,
2681 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682{
2683 char_u *match;
2684
2685 /* First try the short file name, then the long file name. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002686 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002688 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689
2690 return match;
2691}
2692
2693/*
2694 * Try matching the regexp in "prog" with file name "name".
2695 * Return "name" when there is a match, NULL when not.
2696 */
2697 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002698fname_match(
2699 regmatch_T *rmp,
2700 char_u *name,
2701 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702{
2703 char_u *match = NULL;
2704 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705
2706 if (name != NULL)
2707 {
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002708 /* Ignore case when 'fileignorecase' or the argument is set. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002709 rmp->rm_ic = p_fic || ignore_case;
2710 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 match = name;
2712 else
2713 {
2714 /* Replace $(HOME) with '~' and try matching again. */
2715 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002716 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 match = name;
2718 vim_free(p);
2719 }
2720 }
2721
2722 return match;
2723}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724
2725/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02002726 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 */
2728 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002729buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730{
Bram Moolenaar480778b2016-07-14 22:09:39 +02002731 char_u key[VIM_SIZEOF_INT * 2 + 1];
2732 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733
2734 if (nr == 0)
2735 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02002736 sprintf((char *)key, "%x", nr);
2737 hi = hash_find(&buf_hashtab, key);
2738
2739 if (!HASHITEM_EMPTY(hi))
2740 return (buf_T *)(hi->hi_key
2741 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 return NULL;
2743}
2744
2745/*
2746 * Get name of file 'n' in the buffer list.
2747 * When the file has no name an empty string is returned.
2748 * home_replace() is used to shorten the file name (used for marks).
2749 * Returns a pointer to allocated memory, of NULL when failed.
2750 */
2751 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002752buflist_nr2name(
2753 int n,
2754 int fullname,
2755 int helptail) /* for help buffers return tail only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756{
2757 buf_T *buf;
2758
2759 buf = buflist_findnr(n);
2760 if (buf == NULL)
2761 return NULL;
2762 return home_replace_save(helptail ? buf : NULL,
2763 fullname ? buf->b_ffname : buf->b_fname);
2764}
2765
2766/*
2767 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2768 * When "copy_options" is TRUE save the local window option values.
2769 * When "lnum" is 0 only do the options.
2770 */
Bram Moolenaardefa0672019-07-21 19:25:37 +02002771 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002772buflist_setfpos(
2773 buf_T *buf,
2774 win_T *win,
2775 linenr_T lnum,
2776 colnr_T col,
2777 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778{
2779 wininfo_T *wip;
2780
2781 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2782 if (wip->wi_win == win)
2783 break;
2784 if (wip == NULL)
2785 {
2786 /* allocate a new entry */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002787 wip = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 if (wip == NULL)
2789 return;
2790 wip->wi_win = win;
2791 if (lnum == 0) /* set lnum even when it's 0 */
2792 lnum = 1;
2793 }
2794 else
2795 {
2796 /* remove the entry from the list */
2797 if (wip->wi_prev)
2798 wip->wi_prev->wi_next = wip->wi_next;
2799 else
2800 buf->b_wininfo = wip->wi_next;
2801 if (wip->wi_next)
2802 wip->wi_next->wi_prev = wip->wi_prev;
2803 if (copy_options && wip->wi_optset)
2804 {
2805 clear_winopt(&wip->wi_opt);
2806#ifdef FEAT_FOLDING
2807 deleteFoldRecurse(&wip->wi_folds);
2808#endif
2809 }
2810 }
2811 if (lnum != 0)
2812 {
2813 wip->wi_fpos.lnum = lnum;
2814 wip->wi_fpos.col = col;
2815 }
2816 if (copy_options)
2817 {
2818 /* Save the window-specific option values. */
2819 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2820#ifdef FEAT_FOLDING
2821 wip->wi_fold_manual = win->w_fold_manual;
2822 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2823#endif
2824 wip->wi_optset = TRUE;
2825 }
2826
2827 /* insert the entry in front of the list */
2828 wip->wi_next = buf->b_wininfo;
2829 buf->b_wininfo = wip;
2830 wip->wi_prev = NULL;
2831 if (wip->wi_next)
2832 wip->wi_next->wi_prev = wip;
2833
2834 return;
2835}
2836
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002837#ifdef FEAT_DIFF
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002838/*
2839 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2840 * page. That's because a diff is local to a tab page.
2841 */
2842 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002843wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002844{
2845 win_T *wp;
2846
2847 if (wip->wi_opt.wo_diff)
2848 {
Bram Moolenaar29323592016-07-24 22:04:11 +02002849 FOR_ALL_WINDOWS(wp)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002850 /* return FALSE when it's a window in the current tab page, thus
2851 * the buffer was in diff mode here */
2852 if (wip->wi_win == wp)
2853 return FALSE;
2854 return TRUE;
2855 }
2856 return FALSE;
2857}
2858#endif
2859
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860/*
2861 * Find info for the current window in buffer "buf".
2862 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002863 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2864 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 * Returns NULL when there isn't any info.
2866 */
2867 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002868find_wininfo(
2869 buf_T *buf,
2870 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871{
2872 wininfo_T *wip;
2873
2874 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002875 if (wip->wi_win == curwin
2876#ifdef FEAT_DIFF
2877 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2878#endif
2879 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002881
2882 /* If no wininfo for curwin, use the first in the list (that doesn't have
2883 * 'diff' set and is in another tab page). */
2884 if (wip == NULL)
2885 {
2886#ifdef FEAT_DIFF
2887 if (skip_diff_buffer)
2888 {
2889 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2890 if (!wininfo_other_tab_diff(wip))
2891 break;
2892 }
2893 else
2894#endif
2895 wip = buf->b_wininfo;
2896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 return wip;
2898}
2899
2900/*
2901 * Reset the local window options to the values last used in this window.
2902 * If the buffer wasn't used in this window before, use the values from
2903 * the most recently used window. If the values were never set, use the
2904 * global values for the window.
2905 */
2906 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002907get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908{
2909 wininfo_T *wip;
2910
2911 clear_winopt(&curwin->w_onebuf_opt);
2912#ifdef FEAT_FOLDING
2913 clearFolding(curwin);
2914#endif
2915
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002916 wip = find_wininfo(buf, TRUE);
Bram Moolenaar25782a72018-05-13 18:05:33 +02002917 if (wip != NULL && wip->wi_win != NULL
2918 && wip->wi_win != curwin && wip->wi_win->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 {
Bram Moolenaar25782a72018-05-13 18:05:33 +02002920 /* The buffer is currently displayed in the window: use the actual
2921 * option values instead of the saved (possibly outdated) values. */
2922 win_T *wp = wip->wi_win;
2923
2924 copy_winopt(&wp->w_onebuf_opt, &curwin->w_onebuf_opt);
2925#ifdef FEAT_FOLDING
2926 curwin->w_fold_manual = wp->w_fold_manual;
2927 curwin->w_foldinvalid = TRUE;
2928 cloneFoldGrowArray(&wp->w_folds, &curwin->w_folds);
2929#endif
2930 }
2931 else if (wip != NULL && wip->wi_optset)
2932 {
2933 /* the buffer was displayed in the current window earlier */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2935#ifdef FEAT_FOLDING
2936 curwin->w_fold_manual = wip->wi_fold_manual;
2937 curwin->w_foldinvalid = TRUE;
2938 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2939#endif
2940 }
2941 else
2942 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2943
2944#ifdef FEAT_FOLDING
2945 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2946 if (p_fdls >= 0)
2947 curwin->w_p_fdl = p_fdls;
2948#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002949#ifdef FEAT_SYN_HL
2950 check_colorcolumn(curwin);
2951#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952}
2953
2954/*
2955 * Find the position (lnum and col) for the buffer 'buf' for the current
2956 * window.
2957 * Returns a pointer to no_position if no position is found.
2958 */
2959 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002960buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961{
2962 wininfo_T *wip;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002963 static pos_T no_position = {1, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002965 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 if (wip != NULL)
2967 return &(wip->wi_fpos);
2968 else
2969 return &no_position;
2970}
2971
2972/*
2973 * Find the lnum for the buffer 'buf' for the current window.
2974 */
2975 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01002976buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977{
2978 return buflist_findfpos(buf)->lnum;
2979}
2980
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002982 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002985buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986{
2987 buf_T *buf;
2988 int len;
2989 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02002990 int ro_char;
2991 int changed_char;
Bram Moolenaar0751f512018-03-29 16:37:16 +02002992#ifdef FEAT_TERMINAL
2993 int job_running;
2994 int job_none_open;
2995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996
2997 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2998 {
Bram Moolenaar0751f512018-03-29 16:37:16 +02002999#ifdef FEAT_TERMINAL
3000 job_running = term_job_running(buf->b_term);
3001 job_none_open = job_running && term_none_open(buf->b_term);
3002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 /* skip unlisted buffers, unless ! was used */
Bram Moolenaard51cb702015-07-21 15:03:06 +02003004 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
3005 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
3006 || (vim_strchr(eap->arg, '+')
3007 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
3008 || (vim_strchr(eap->arg, 'a')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003009 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
Bram Moolenaard51cb702015-07-21 15:03:06 +02003010 || (vim_strchr(eap->arg, 'h')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003011 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3012#ifdef FEAT_TERMINAL
3013 || (vim_strchr(eap->arg, 'R')
3014 && (!job_running || (job_running && job_none_open)))
3015 || (vim_strchr(eap->arg, '?')
3016 && (!job_running || (job_running && !job_none_open)))
3017 || (vim_strchr(eap->arg, 'F')
3018 && (job_running || buf->b_term == NULL))
3019#endif
Bram Moolenaard51cb702015-07-21 15:03:06 +02003020 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3021 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3022 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3023 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3024 || (vim_strchr(eap->arg, '#')
3025 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003028 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 else
3030 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003031 if (message_filtered(NameBuff))
3032 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003034 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3035 : (bufIsChanged(buf) ? '+' : ' ');
3036#ifdef FEAT_TERMINAL
3037 if (term_job_running(buf->b_term))
3038 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02003039 if (term_none_open(buf->b_term))
3040 ro_char = '?';
3041 else
3042 ro_char = 'R';
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003043 changed_char = ' '; /* bufIsChanged() returns TRUE to avoid
3044 * closing, but it's not actually changed. */
3045 }
3046 else if (buf->b_term != NULL)
3047 ro_char = 'F';
3048 else
3049#endif
3050 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3051
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003052 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00003053 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 buf->b_fnum,
3055 buf->b_p_bl ? ' ' : 'u',
3056 buf == curbuf ? '%' :
3057 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3058 buf->b_ml.ml_mfp == NULL ? ' ' :
3059 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003060 ro_char,
3061 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003062 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003063 if (len > IOSIZE - 20)
3064 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065
3066 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 i = 40 - vim_strsize(IObuff);
3068 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 IObuff[len++] = ' ';
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003070 while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003071 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3072 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003073 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 msg_outtrans(IObuff);
3075 out_flush(); /* output one line at a time */
3076 ui_breakcheck();
3077 }
3078}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079
3080/*
3081 * Get file name and line number for file 'fnum'.
3082 * Used by DoOneCmd() for translating '%' and '#'.
3083 * Used by insert_reg() and cmdline_paste() for '#' register.
3084 * Return FAIL if not found, OK for success.
3085 */
3086 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003087buflist_name_nr(
3088 int fnum,
3089 char_u **fname,
3090 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091{
3092 buf_T *buf;
3093
3094 buf = buflist_findnr(fnum);
3095 if (buf == NULL || buf->b_fname == NULL)
3096 return FAIL;
3097
3098 *fname = buf->b_fname;
3099 *lnum = buflist_findlnum(buf);
3100
3101 return OK;
3102}
3103
3104/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003105 * Set the file name for "buf"' to "ffname_arg", short file name to
3106 * "sfname_arg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 * The file name with the full path is also remembered, for when :cd is used.
3108 * Returns FAIL for failure (file name already in use by other buffer)
3109 * OK otherwise.
3110 */
3111 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003112setfname(
3113 buf_T *buf,
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003114 char_u *ffname_arg,
3115 char_u *sfname_arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +01003116 int message) /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003118 char_u *ffname = ffname_arg;
3119 char_u *sfname = sfname_arg;
Bram Moolenaar81695252004-12-29 20:58:21 +00003120 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003122 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123#endif
3124
3125 if (ffname == NULL || *ffname == NUL)
3126 {
3127 /* Removing the name. */
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003128 if (buf->b_sfname != buf->b_ffname)
3129 VIM_CLEAR(buf->b_sfname);
3130 else
3131 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003132 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133#ifdef UNIX
3134 st.st_dev = (dev_T)-1;
3135#endif
3136 }
3137 else
3138 {
3139 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
3140 if (ffname == NULL) /* out of memory */
3141 return FAIL;
3142
3143 /*
3144 * if the file name is already used in another buffer:
3145 * - if the buffer is loaded, fail
3146 * - if the buffer is not loaded, delete it from the list
3147 */
3148#ifdef UNIX
3149 if (mch_stat((char *)ffname, &st) < 0)
3150 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003151#endif
3152 if (!(buf->b_flags & BF_DUMMY))
3153#ifdef UNIX
3154 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003156 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157#endif
3158 if (obuf != NULL && obuf != buf)
3159 {
3160 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
3161 {
3162 if (message)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003163 emsg(_("E95: Buffer with this name already exists"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 vim_free(ffname);
3165 return FAIL;
3166 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01003167 /* delete from the list */
3168 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 }
3170 sfname = vim_strsave(sfname);
3171 if (ffname == NULL || sfname == NULL)
3172 {
3173 vim_free(sfname);
3174 vim_free(ffname);
3175 return FAIL;
3176 }
3177#ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003178 fname_case(sfname, 0); /* set correct case for short file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179#endif
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003180 if (buf->b_sfname != buf->b_ffname)
3181 vim_free(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 vim_free(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 buf->b_ffname = ffname;
3184 buf->b_sfname = sfname;
3185 }
3186 buf->b_fname = buf->b_sfname;
3187#ifdef UNIX
3188 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003189 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 else
3191 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003192 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 buf->b_dev = st.st_dev;
3194 buf->b_ino = st.st_ino;
3195 }
3196#endif
3197
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199
3200 buf_name_changed(buf);
3201 return OK;
3202}
3203
3204/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003205 * Crude way of changing the name of a buffer. Use with care!
3206 * The name should be relative to the current directory.
3207 */
3208 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003209buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003210{
3211 buf_T *buf;
3212
3213 buf = buflist_findnr(fnum);
3214 if (buf != NULL)
3215 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003216 if (buf->b_sfname != buf->b_ffname)
3217 vim_free(buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003218 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003219 buf->b_ffname = vim_strsave(name);
3220 buf->b_sfname = NULL;
3221 /* Allocate ffname and expand into full path. Also resolves .lnk
3222 * files on Win32. */
3223 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003224 buf->b_fname = buf->b_sfname;
3225 }
3226}
3227
3228/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 * Take care of what needs to be done when the name of buffer "buf" has
3230 * changed.
3231 */
3232 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003233buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234{
3235 /*
3236 * If the file name changed, also change the name of the swapfile
3237 */
3238 if (buf->b_ml.ml_mfp != NULL)
3239 ml_setname(buf);
3240
3241 if (curwin->w_buffer == buf)
3242 check_arg_idx(curwin); /* check file name for arg list */
3243#ifdef FEAT_TITLE
3244 maketitle(); /* set window title */
3245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 status_redraw_all(); /* status lines need to be redrawn */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 fmarks_check_names(buf); /* check named file marks */
3248 ml_timestamp(buf); /* reset timestamp */
3249}
3250
3251/*
3252 * set alternate file name for current window
3253 *
3254 * Used by do_one_cmd(), do_write() and do_ecmd().
3255 * Return the buffer.
3256 */
3257 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003258setaltfname(
3259 char_u *ffname,
3260 char_u *sfname,
3261 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262{
3263 buf_T *buf;
3264
3265 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
3266 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003267 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 curwin->w_alt_fnum = buf->b_fnum;
3269 return buf;
3270}
3271
3272/*
3273 * Get alternate file name for current window.
3274 * Return NULL if there isn't any, and give error message if requested.
3275 */
3276 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003277getaltfname(
3278 int errmsg) /* give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279{
3280 char_u *fname;
3281 linenr_T dummy;
3282
3283 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3284 {
3285 if (errmsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003286 emsg(_(e_noalt));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 return NULL;
3288 }
3289 return fname;
3290}
3291
3292/*
3293 * Add a file name to the buflist and return its number.
3294 * Uses same flags as buflist_new(), except BLN_DUMMY.
3295 *
3296 * used by qf_init(), main() and doarglist()
3297 */
3298 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003299buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300{
3301 buf_T *buf;
3302
3303 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3304 if (buf != NULL)
3305 return buf->b_fnum;
3306 return 0;
3307}
3308
3309#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3310/*
3311 * Adjust slashes in file names. Called after 'shellslash' was set.
3312 */
3313 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003314buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315{
3316 buf_T *bp;
3317
Bram Moolenaar29323592016-07-24 22:04:11 +02003318 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 {
3320 if (bp->b_ffname != NULL)
3321 slash_adjust(bp->b_ffname);
3322 if (bp->b_sfname != NULL)
3323 slash_adjust(bp->b_sfname);
3324 }
3325}
3326#endif
3327
3328/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003329 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 * Also save the local window option values.
3331 */
3332 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003333buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003335 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336}
3337
3338/*
3339 * Return TRUE if 'ffname' is not the same file as current file.
3340 * Fname must have a full path (expanded by mch_FullName()).
3341 */
3342 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003343otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344{
3345 return otherfile_buf(curbuf, ffname
3346#ifdef UNIX
3347 , NULL
3348#endif
3349 );
3350}
3351
3352 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003353otherfile_buf(
3354 buf_T *buf,
3355 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003357 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003359 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360{
3361 /* no name is different */
3362 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3363 return TRUE;
3364 if (fnamecmp(ffname, buf->b_ffname) == 0)
3365 return FALSE;
3366#ifdef UNIX
3367 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003368 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369
Bram Moolenaar8767f522016-07-01 17:17:39 +02003370 /* If no stat_T given, get it now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 if (stp == NULL)
3372 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003373 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 st.st_dev = (dev_T)-1;
3375 stp = &st;
3376 }
3377 /* Use dev/ino to check if the files are the same, even when the names
3378 * are different (possible with links). Still need to compare the
3379 * name above, for when the file doesn't exist yet.
3380 * Problem: The dev/ino changes when a file is deleted (and created
3381 * again) and remains the same when renamed/moved. We don't want to
3382 * mch_stat() each buffer each time, that would be too slow. Get the
3383 * dev/ino again when they appear to match, but not when they appear
3384 * to be different: Could skip a buffer when it's actually the same
3385 * file. */
3386 if (buf_same_ino(buf, stp))
3387 {
3388 buf_setino(buf);
3389 if (buf_same_ino(buf, stp))
3390 return FALSE;
3391 }
3392 }
3393#endif
3394 return TRUE;
3395}
3396
3397#if defined(UNIX) || defined(PROTO)
3398/*
3399 * Set inode and device number for a buffer.
3400 * Must always be called when b_fname is changed!.
3401 */
3402 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003403buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003405 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406
3407 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3408 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003409 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 buf->b_dev = st.st_dev;
3411 buf->b_ino = st.st_ino;
3412 }
3413 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003414 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415}
3416
3417/*
3418 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3419 */
3420 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003421buf_same_ino(
3422 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003423 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003425 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 && stp->st_dev == buf->b_dev
3427 && stp->st_ino == buf->b_ino);
3428}
3429#endif
3430
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003431/*
3432 * Print info about the current buffer.
3433 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003435fileinfo(
3436 int fullname, /* when non-zero print full path */
3437 int shorthelp,
3438 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439{
3440 char_u *name;
3441 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003442 char *p;
3443 char *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003444 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003446 buffer = alloc(IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 if (buffer == NULL)
3448 return;
3449
3450 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3451 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003452 vim_snprintf(buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 p = buffer + STRLEN(buffer);
3454 }
3455 else
3456 p = buffer;
3457
3458 *p++ = '"';
3459 if (buf_spname(curbuf) != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003460 vim_strncpy((char_u *)p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 else
3462 {
3463 if (!fullname && curbuf->b_fname != NULL)
3464 name = curbuf->b_fname;
3465 else
3466 name = curbuf->b_ffname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003467 home_replace(shorthelp ? curbuf : NULL, name, (char_u *)p,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 (int)(IOSIZE - (p - buffer)), TRUE);
3469 }
3470
Bram Moolenaar32526b32019-01-19 17:43:09 +01003471 vim_snprintf_add(buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 curbufIsChanged() ? (shortmess(SHM_MOD)
3473 ? " [+]" : _(" [Modified]")) : " ",
3474 (curbuf->b_flags & BF_NOTEDITED)
3475#ifdef FEAT_QUICKFIX
3476 && !bt_dontwrite(curbuf)
3477#endif
3478 ? _("[Not edited]") : "",
3479 (curbuf->b_flags & BF_NEW)
3480#ifdef FEAT_QUICKFIX
3481 && !bt_dontwrite(curbuf)
3482#endif
3483 ? _("[New file]") : "",
3484 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003485 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 : _("[readonly]")) : "",
3487 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3488 || curbuf->b_p_ro) ?
3489 " " : "");
3490 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3491 * causes an overflow, thus for large numbers divide instead. */
3492 if (curwin->w_cursor.lnum > 1000000L)
3493 n = (int)(((long)curwin->w_cursor.lnum) /
3494 ((long)curbuf->b_ml.ml_line_count / 100L));
3495 else
3496 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3497 (long)curbuf->b_ml.ml_line_count);
3498 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003499 vim_snprintf_add(buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500#ifdef FEAT_CMDL_INFO
3501 else if (p_ru)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 /* Current line and column are already on the screen -- webb */
Bram Moolenaar32526b32019-01-19 17:43:09 +01003503 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003504 NGETTEXT("%ld line --%d%%--", "%ld lines --%d%%--",
3505 curbuf->b_ml.ml_line_count),
3506 (long)curbuf->b_ml.ml_line_count, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507#endif
3508 else
3509 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003510 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 _("line %ld of %ld --%d%%-- col "),
3512 (long)curwin->w_cursor.lnum,
3513 (long)curbuf->b_ml.ml_line_count,
3514 n);
3515 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003516 len = STRLEN(buffer);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003517 col_print((char_u *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3519 }
3520
Bram Moolenaar32526b32019-01-19 17:43:09 +01003521 (void)append_arg_number(curwin, (char_u *)buffer, IOSIZE,
3522 !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523
3524 if (dont_truncate)
3525 {
3526 /* Temporarily set msg_scroll to avoid the message being truncated.
3527 * First call msg_start() to get the message in the right place. */
3528 msg_start();
3529 n = msg_scroll;
3530 msg_scroll = TRUE;
3531 msg(buffer);
3532 msg_scroll = n;
3533 }
3534 else
3535 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003536 p = (char *)msg_trunc_attr(buffer, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 /* Need to repeat the message after redrawing when:
3539 * - When restart_edit is set (otherwise there will be a delay
3540 * before redrawing).
3541 * - When the screen was scrolled but there is no wait-return
3542 * prompt. */
Bram Moolenaar32526b32019-01-19 17:43:09 +01003543 set_keep_msg((char_u *)p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 }
3545
3546 vim_free(buffer);
3547}
3548
3549 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003550col_print(
3551 char_u *buf,
3552 size_t buflen,
3553 int col,
3554 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555{
3556 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003557 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003559 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560}
3561
3562#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563static char_u *lasttitle = NULL;
3564static char_u *lasticon = NULL;
3565
Bram Moolenaar84a93082018-06-16 22:58:15 +02003566/*
3567 * Put the file name in the title bar and icon of the window.
3568 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003570maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571{
3572 char_u *p;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003573 char_u *title_str = NULL;
3574 char_u *icon_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 int maxlen = 0;
3576 int len;
3577 int mustset;
3578 char_u buf[IOSIZE];
3579 int off;
3580
3581 if (!redrawing())
3582 {
3583 /* Postpone updating the title when 'lazyredraw' is set. */
3584 need_maketitle = TRUE;
3585 return;
3586 }
3587
3588 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003589 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003590 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591
3592 if (p_title)
3593 {
3594 if (p_titlelen > 0)
3595 {
3596 maxlen = p_titlelen * Columns / 100;
3597 if (maxlen < 10)
3598 maxlen = 10;
3599 }
3600
Bram Moolenaar84a93082018-06-16 22:58:15 +02003601 title_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 if (*p_titlestring != NUL)
3603 {
3604#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003605 if (stl_syntax & STL_IN_TITLE)
3606 {
3607 int use_sandbox = FALSE;
3608 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003609
3610# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003611 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003612# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003613 called_emsg = FALSE;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003614 build_stl_str_hl(curwin, title_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003615 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003616 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003617 if (called_emsg)
3618 set_string_option_direct((char_u *)"titlestring", -1,
3619 (char_u *)"", OPT_FREE, SID_ERROR);
3620 called_emsg |= save_called_emsg;
3621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 else
3623#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003624 title_str = p_titlestring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 }
3626 else
3627 {
3628 /* format: "fname + (path) (1 of 2) - VIM" */
3629
Bram Moolenaar2c666692012-09-05 13:30:40 +02003630#define SPACE_FOR_FNAME (IOSIZE - 100)
3631#define SPACE_FOR_DIR (IOSIZE - 20)
3632#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003634 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar21554412017-07-24 21:44:43 +02003635#ifdef FEAT_TERMINAL
3636 else if (curbuf->b_term != NULL)
3637 {
3638 vim_strncpy(buf, term_get_status_text(curbuf->b_term),
3639 SPACE_FOR_FNAME);
3640 }
3641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 else
3643 {
3644 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003645 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 }
3648
Bram Moolenaar21554412017-07-24 21:44:43 +02003649#ifdef FEAT_TERMINAL
3650 if (curbuf->b_term == NULL)
3651#endif
3652 switch (bufIsChanged(curbuf)
3653 + (curbuf->b_p_ro * 2)
3654 + (!curbuf->b_p_ma * 4))
3655 {
3656 case 1: STRCAT(buf, " +"); break;
3657 case 2: STRCAT(buf, " ="); break;
3658 case 3: STRCAT(buf, " =+"); break;
3659 case 4:
3660 case 6: STRCAT(buf, " -"); break;
3661 case 5:
3662 case 7: STRCAT(buf, " -+"); break;
3663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664
Bram Moolenaar21554412017-07-24 21:44:43 +02003665 if (curbuf->b_fname != NULL
3666#ifdef FEAT_TERMINAL
3667 && curbuf->b_term == NULL
3668#endif
3669 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 {
3671 /* Get path of file, replace home dir with ~ */
3672 off = (int)STRLEN(buf);
3673 buf[off++] = ' ';
3674 buf[off++] = '(';
3675 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003676 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677#ifdef BACKSLASH_IN_FILENAME
3678 /* avoid "c:/name" to be reduced to "c" */
3679 if (isalpha(buf[off]) && buf[off + 1] == ':')
3680 off += 2;
3681#endif
3682 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003683 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 if (p == buf + off)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003685 {
Bram Moolenaar21554412017-07-24 21:44:43 +02003686 /* must be a help buffer */
3687 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003688 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003692
Bram Moolenaar2c666692012-09-05 13:30:40 +02003693 /* Translate unprintable chars and concatenate. Keep some
3694 * room for the server name. When there is no room (very long
3695 * file name) use (...). */
3696 if (off < SPACE_FOR_DIR)
3697 {
3698 p = transstr(buf + off);
3699 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3700 vim_free(p);
3701 }
3702 else
3703 {
3704 vim_strncpy(buf + off, (char_u *)"...",
3705 (size_t)(SPACE_FOR_ARGNR - off));
3706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 STRCAT(buf, ")");
3708 }
3709
Bram Moolenaar2c666692012-09-05 13:30:40 +02003710 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711
3712#if defined(FEAT_CLIENTSERVER)
3713 if (serverName != NULL)
3714 {
3715 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003716 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 }
3718 else
3719#endif
3720 STRCAT(buf, " - VIM");
3721
3722 if (maxlen > 0)
3723 {
3724 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003725 if (vim_strsize(buf) > maxlen)
3726 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 }
3728 }
3729 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02003730 mustset = value_changed(title_str, &lasttitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731
3732 if (p_icon)
3733 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02003734 icon_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 if (*p_iconstring != NUL)
3736 {
3737#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003738 if (stl_syntax & STL_IN_ICON)
3739 {
3740 int use_sandbox = FALSE;
3741 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003742
3743# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003744 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003745# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003746 called_emsg = FALSE;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003747 build_stl_str_hl(curwin, icon_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003748 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003749 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003750 if (called_emsg)
3751 set_string_option_direct((char_u *)"iconstring", -1,
3752 (char_u *)"", OPT_FREE, SID_ERROR);
3753 called_emsg |= save_called_emsg;
3754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 else
3756#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003757 icon_str = p_iconstring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 }
3759 else
3760 {
3761 if (buf_spname(curbuf) != NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003762 p = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 else /* use file name only in icon */
Bram Moolenaar84a93082018-06-16 22:58:15 +02003764 p = gettail(curbuf->b_ffname);
3765 *icon_str = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 /* Truncate name at 100 bytes. */
Bram Moolenaar84a93082018-06-16 22:58:15 +02003767 len = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 if (len > 100)
3769 {
3770 len -= 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 if (has_mbyte)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003772 len += (*mb_tail_off)(p, p + len) + 1;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003773 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02003775 STRCPY(icon_str, p);
3776 trans_characters(icon_str, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 }
3778 }
3779
Bram Moolenaar84a93082018-06-16 22:58:15 +02003780 mustset |= value_changed(icon_str, &lasticon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781
3782 if (mustset)
3783 resettitle();
3784}
3785
3786/*
3787 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3788 * from "str" if it does.
Bram Moolenaar84a93082018-06-16 22:58:15 +02003789 * Return TRUE if resettitle() is to be called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 */
3791 static int
Bram Moolenaar84a93082018-06-16 22:58:15 +02003792value_changed(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793{
3794 if ((str == NULL) != (*last == NULL)
3795 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3796 {
3797 vim_free(*last);
3798 if (str == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003799 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 *last = NULL;
Bram Moolenaar40385db2018-08-07 22:31:44 +02003801 mch_restore_title(
3802 last == &lasttitle ? SAVE_RESTORE_TITLE : SAVE_RESTORE_ICON);
Bram Moolenaar84a93082018-06-16 22:58:15 +02003803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 else
Bram Moolenaar84a93082018-06-16 22:58:15 +02003805 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 *last = vim_strsave(str);
Bram Moolenaar84a93082018-06-16 22:58:15 +02003807 return TRUE;
3808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 }
3810 return FALSE;
3811}
3812
3813/*
3814 * Put current window title back (used after calling a shell)
3815 */
3816 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003817resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818{
3819 mch_settitle(lasttitle, lasticon);
3820}
Bram Moolenaarea408852005-06-25 22:49:46 +00003821
3822# if defined(EXITFREE) || defined(PROTO)
3823 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003824free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00003825{
3826 vim_free(lasttitle);
3827 vim_free(lasticon);
3828}
3829# endif
3830
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831#endif /* FEAT_TITLE */
3832
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003833#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003835 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 * Return length of string in screen cells.
3837 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003838 * Normally works for window "wp", except when working for 'tabline' then it
3839 * is "curwin".
3840 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 * Items are drawn interspersed with the text that surrounds it
3842 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3843 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3844 *
3845 * If maxwidth is not zero, the string will be filled at any middle marker
3846 * or truncated if too long, fillchar is used for all whitespace.
3847 */
3848 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003849build_stl_str_hl(
3850 win_T *wp,
3851 char_u *out, /* buffer to write into != NameBuff */
3852 size_t outlen, /* length of out[] */
3853 char_u *fmt,
3854 int use_sandbox UNUSED, /* "fmt" was set insecurely, use sandbox */
3855 int fillchar,
3856 int maxwidth,
3857 struct stl_hlrec *hltab, /* return: HL attributes (can be NULL) */
3858 struct stl_hlrec *tabtab) /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859{
Bram Moolenaar10772302019-01-20 18:25:54 +01003860 linenr_T lnum;
3861 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 char_u *p;
3863 char_u *s;
3864 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003865 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866#ifdef FEAT_EVAL
Bram Moolenaarba2929b2017-09-08 13:59:21 +02003867 win_T *save_curwin;
3868 buf_T *save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869#endif
3870 int empty_line;
3871 colnr_T virtcol;
3872 long l;
3873 long n;
3874 int prevchar_isflag;
3875 int prevchar_isitem;
3876 int itemisflag;
3877 int fillable;
3878 char_u *str;
3879 long num;
3880 int width;
3881 int itemcnt;
3882 int curitem;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02003883 int group_end_userhl;
3884 int group_start_userhl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 int groupitem[STL_MAX_ITEM];
3886 int groupdepth;
3887 struct stl_item
3888 {
3889 char_u *start;
3890 int minwid;
3891 int maxwid;
3892 enum
3893 {
3894 Normal,
3895 Empty,
3896 Group,
3897 Middle,
3898 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003899 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 Trunc
3901 } type;
3902 } item[STL_MAX_ITEM];
3903 int minwid;
3904 int maxwid;
3905 int zeropad;
3906 char_u base;
3907 char_u opt;
3908#define TMPLEN 70
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02003909 char_u buf_tmp[TMPLEN];
3910 char_u win_tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003911 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003912 struct stl_hlrec *sp;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02003913 int save_must_redraw = must_redraw;
3914 int save_redr_type = curwin->w_redr_type;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003915
3916#ifdef FEAT_EVAL
3917 /*
3918 * When the format starts with "%!" then evaluate it as an expression and
3919 * use the result as the actual format string.
3920 */
3921 if (fmt[0] == '%' && fmt[1] == '!')
3922 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02003923 typval_T tv;
3924
3925 tv.v_type = VAR_NUMBER;
3926 tv.vval.v_number = wp->w_id;
3927 set_var((char_u *)"g:statusline_winid", &tv, FALSE);
3928
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003929 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3930 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003931 usefmt = fmt;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02003932
3933 do_unlet((char_u *)"g:statusline_winid", TRUE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003934 }
3935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936
3937 if (fillchar == 0)
3938 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003939 /* Can't handle a multi-byte fill character yet. */
3940 else if (mb_char2len(fillchar) > 1)
3941 fillchar = '-';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003942
Bram Moolenaar10772302019-01-20 18:25:54 +01003943 // The cursor in windows other than the current one isn't always
3944 // up-to-date, esp. because of autocommands and timers.
3945 lnum = wp->w_cursor.lnum;
3946 if (lnum > wp->w_buffer->b_ml.ml_line_count)
3947 {
3948 lnum = wp->w_buffer->b_ml.ml_line_count;
3949 wp->w_cursor.lnum = lnum;
3950 }
3951
3952 // Get line & check if empty (cursorpos will show "0-1"). Note that
3953 // p will become invalid when getting another buffer line.
3954 p = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar567199b2013-04-24 16:52:36 +02003955 empty_line = (*p == NUL);
3956
Bram Moolenaar10772302019-01-20 18:25:54 +01003957 // Get the byte value now, in case we need it below. This is more efficient
3958 // than making a copy of the line.
3959 len = STRLEN(p);
3960 if (wp->w_cursor.col > (colnr_T)len)
3961 {
3962 // Line may have changed since checking the cursor column, or the lnum
3963 // was adjusted above.
3964 wp->w_cursor.col = (colnr_T)len;
Bram Moolenaar10772302019-01-20 18:25:54 +01003965 wp->w_cursor.coladd = 0;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003966 byteval = 0;
Bram Moolenaar10772302019-01-20 18:25:54 +01003967 }
Bram Moolenaar567199b2013-04-24 16:52:36 +02003968 else
Bram Moolenaar567199b2013-04-24 16:52:36 +02003969 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970
3971 groupdepth = 0;
3972 p = out;
3973 curitem = 0;
3974 prevchar_isflag = TRUE;
3975 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003976 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003978 if (curitem == STL_MAX_ITEM)
3979 {
3980 /* There are too many items. Add the error code to the statusline
3981 * to give the user a hint about what went wrong. */
3982 if (p + 6 < out + outlen)
3983 {
3984 mch_memmove(p, " E541", (size_t)5);
3985 p += 5;
3986 }
3987 break;
3988 }
3989
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 if (*s != NUL && *s != '%')
3991 prevchar_isflag = prevchar_isitem = FALSE;
3992
3993 /*
3994 * Handle up to the next '%' or the end.
3995 */
3996 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3997 *p++ = *s++;
3998 if (*s == NUL || p + 1 >= out + outlen)
3999 break;
4000
4001 /*
4002 * Handle one '%' item.
4003 */
4004 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01004005 if (*s == NUL) /* ignore trailing % */
4006 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 if (*s == '%')
4008 {
4009 if (p + 1 >= out + outlen)
4010 break;
4011 *p++ = *s++;
4012 prevchar_isflag = prevchar_isitem = FALSE;
4013 continue;
4014 }
4015 if (*s == STL_MIDDLEMARK)
4016 {
4017 s++;
4018 if (groupdepth > 0)
4019 continue;
4020 item[curitem].type = Middle;
4021 item[curitem++].start = p;
4022 continue;
4023 }
4024 if (*s == STL_TRUNCMARK)
4025 {
4026 s++;
4027 item[curitem].type = Trunc;
4028 item[curitem++].start = p;
4029 continue;
4030 }
4031 if (*s == ')')
4032 {
4033 s++;
4034 if (groupdepth < 1)
4035 continue;
4036 groupdepth--;
4037
4038 t = item[groupitem[groupdepth]].start;
4039 *p = NUL;
4040 l = vim_strsize(t);
4041 if (curitem > groupitem[groupdepth] + 1
4042 && item[groupitem[groupdepth]].minwid == 0)
4043 {
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004044 /* remove group if all items are empty and highlight group
4045 * doesn't change */
4046 group_start_userhl = group_end_userhl = 0;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004047 for (n = groupitem[groupdepth] - 1; n >= 0; n--)
4048 {
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004049 if (item[n].type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004050 {
4051 group_start_userhl = group_end_userhl = item[n].minwid;
4052 break;
4053 }
4054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004056 {
4057 if (item[n].type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 break;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004059 if (item[n].type == Highlight)
4060 group_end_userhl = item[n].minwid;
4061 }
4062 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 {
4064 p = t;
4065 l = 0;
4066 }
4067 }
4068 if (l > item[groupitem[groupdepth]].maxwid)
4069 {
4070 /* truncate, remove n bytes of text at the start */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 if (has_mbyte)
4072 {
4073 /* Find the first character that should be included. */
4074 n = 0;
4075 while (l >= item[groupitem[groupdepth]].maxwid)
4076 {
4077 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004078 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 }
4080 }
4081 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004082 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083
4084 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004085 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 p = p - n + 1;
Bram Moolenaar13505972019-01-24 15:04:48 +01004087
4088 // Fill up space left over by half a double-wide char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 while (++l < item[groupitem[groupdepth]].minwid)
4090 *p++ = fillchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091
4092 /* correct the start of the items for the truncation */
4093 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
4094 {
4095 item[l].start -= n;
4096 if (item[l].start < t)
4097 item[l].start = t;
4098 }
4099 }
4100 else if (abs(item[groupitem[groupdepth]].minwid) > l)
4101 {
4102 /* fill */
4103 n = item[groupitem[groupdepth]].minwid;
4104 if (n < 0)
4105 {
4106 /* fill by appending characters */
4107 n = 0 - n;
4108 while (l++ < n && p + 1 < out + outlen)
4109 *p++ = fillchar;
4110 }
4111 else
4112 {
4113 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004114 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 l = n - l;
4116 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004117 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 p += l;
4119 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
4120 item[n].start += l;
4121 for ( ; l > 0; l--)
4122 *t++ = fillchar;
4123 }
4124 }
4125 continue;
4126 }
4127 minwid = 0;
4128 maxwid = 9999;
4129 zeropad = FALSE;
4130 l = 1;
4131 if (*s == '0')
4132 {
4133 s++;
4134 zeropad = TRUE;
4135 }
4136 if (*s == '-')
4137 {
4138 s++;
4139 l = -1;
4140 }
4141 if (VIM_ISDIGIT(*s))
4142 {
4143 minwid = (int)getdigits(&s);
4144 if (minwid < 0) /* overflow */
4145 minwid = 0;
4146 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004147 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 {
4149 item[curitem].type = Highlight;
4150 item[curitem].start = p;
4151 item[curitem].minwid = minwid > 9 ? 1 : minwid;
4152 s++;
4153 curitem++;
4154 continue;
4155 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004156 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4157 {
4158 if (*s == STL_TABCLOSENR)
4159 {
4160 if (minwid == 0)
4161 {
4162 /* %X ends the close label, go back to the previously
4163 * define tab label nr. */
4164 for (n = curitem - 1; n >= 0; --n)
4165 if (item[n].type == TabPage && item[n].minwid >= 0)
4166 {
4167 minwid = item[n].minwid;
4168 break;
4169 }
4170 }
4171 else
4172 /* close nrs are stored as negative values */
4173 minwid = - minwid;
4174 }
4175 item[curitem].type = TabPage;
4176 item[curitem].start = p;
4177 item[curitem].minwid = minwid;
4178 s++;
4179 curitem++;
4180 continue;
4181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 if (*s == '.')
4183 {
4184 s++;
4185 if (VIM_ISDIGIT(*s))
4186 {
4187 maxwid = (int)getdigits(&s);
4188 if (maxwid <= 0) /* overflow */
4189 maxwid = 50;
4190 }
4191 }
4192 minwid = (minwid > 50 ? 50 : minwid) * l;
4193 if (*s == '(')
4194 {
4195 groupitem[groupdepth++] = curitem;
4196 item[curitem].type = Group;
4197 item[curitem].start = p;
4198 item[curitem].minwid = minwid;
4199 item[curitem].maxwid = maxwid;
4200 s++;
4201 curitem++;
4202 continue;
4203 }
4204 if (vim_strchr(STL_ALL, *s) == NULL)
4205 {
4206 s++;
4207 continue;
4208 }
4209 opt = *s++;
4210
4211 /* OK - now for the real work */
4212 base = 'D';
4213 itemisflag = FALSE;
4214 fillable = TRUE;
4215 num = -1;
4216 str = NULL;
4217 switch (opt)
4218 {
4219 case STL_FILEPATH:
4220 case STL_FULLPATH:
4221 case STL_FILENAME:
4222 fillable = FALSE; /* don't change ' ' to fillchar */
4223 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004224 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 else
4226 {
4227 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004228 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4230 }
4231 trans_characters(NameBuff, MAXPATHL);
4232 if (opt != STL_FILENAME)
4233 str = NameBuff;
4234 else
4235 str = gettail(NameBuff);
4236 break;
4237
4238 case STL_VIM_EXPR: /* '{' */
4239 itemisflag = TRUE;
4240 t = p;
4241 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
4242 *p++ = *s++;
4243 if (*s != '}') /* missing '}' or out of space */
4244 break;
4245 s++;
4246 *p = 0;
4247 p = t;
4248
4249#ifdef FEAT_EVAL
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004250 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp),
4251 "%d", curbuf->b_fnum);
4252 set_internal_string_var((char_u *)"g:actual_curbuf", buf_tmp);
4253 vim_snprintf((char *)win_tmp, sizeof(win_tmp), "%d", curwin->w_id);
4254 set_internal_string_var((char_u *)"g:actual_curwin", win_tmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004256 save_curbuf = curbuf;
4257 save_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 curwin = wp;
4259 curbuf = wp->w_buffer;
4260
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004261 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004263 curwin = save_curwin;
4264 curbuf = save_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00004265 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004266 do_unlet((char_u *)"g:actual_curwin", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267
4268 if (str != NULL && *str != 0)
4269 {
4270 if (*skipdigits(str) == NUL)
4271 {
4272 num = atoi((char *)str);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004273 VIM_CLEAR(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 itemisflag = FALSE;
4275 }
4276 }
4277#endif
4278 break;
4279
4280 case STL_LINE:
4281 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4282 ? 0L : (long)(wp->w_cursor.lnum);
4283 break;
4284
4285 case STL_NUMLINES:
4286 num = wp->w_buffer->b_ml.ml_line_count;
4287 break;
4288
4289 case STL_COLUMN:
4290 num = !(State & INSERT) && empty_line
4291 ? 0 : (int)wp->w_cursor.col + 1;
4292 break;
4293
4294 case STL_VIRTCOL:
4295 case STL_VIRTCOL_ALT:
4296 /* In list mode virtcol needs to be recomputed */
4297 virtcol = wp->w_virtcol;
4298 if (wp->w_p_list && lcs_tab1 == NUL)
4299 {
4300 wp->w_p_list = FALSE;
4301 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
4302 wp->w_p_list = TRUE;
4303 }
4304 ++virtcol;
4305 /* Don't display %V if it's the same as %c. */
4306 if (opt == STL_VIRTCOL_ALT
4307 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
4308 ? 0 : (int)wp->w_cursor.col + 1)))
4309 break;
4310 num = (long)virtcol;
4311 break;
4312
4313 case STL_PERCENTAGE:
4314 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4315 (long)wp->w_buffer->b_ml.ml_line_count);
4316 break;
4317
4318 case STL_ALTPERCENT:
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004319 str = buf_tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004320 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 break;
4322
4323 case STL_ARGLISTSTAT:
4324 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004325 buf_tmp[0] = 0;
4326 if (append_arg_number(wp, buf_tmp, (int)sizeof(buf_tmp), FALSE))
4327 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 break;
4329
4330 case STL_KEYMAP:
4331 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004332 if (get_keymap_str(wp, (char_u *)"<%s>", buf_tmp, TMPLEN))
4333 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 break;
4335 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004336#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004337 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338#else
4339 num = 0;
4340#endif
4341 break;
4342
4343 case STL_BUFNO:
4344 num = wp->w_buffer->b_fnum;
4345 break;
4346
4347 case STL_OFFSET_X:
4348 base = 'X';
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004349 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 case STL_OFFSET:
4351#ifdef FEAT_BYTEOFF
4352 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4353 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4354 0L : l + 1 + (!(State & INSERT) && empty_line ?
4355 0 : (int)wp->w_cursor.col);
4356#endif
4357 break;
4358
4359 case STL_BYTEVAL_X:
4360 base = 'X';
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004361 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004363 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 if (num == NL)
4365 num = 0;
4366 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4367 num = NL;
4368 break;
4369
4370 case STL_ROFLAG:
4371 case STL_ROFLAG_ALT:
4372 itemisflag = TRUE;
4373 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004374 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 break;
4376
4377 case STL_HELPFLAG:
4378 case STL_HELPFLAG_ALT:
4379 itemisflag = TRUE;
4380 if (wp->w_buffer->b_help)
4381 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004382 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 break;
4384
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 case STL_FILETYPE:
4386 if (*wp->w_buffer->b_p_ft != NUL
4387 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4388 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004389 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), "[%s]",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004390 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004391 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 }
4393 break;
4394
4395 case STL_FILETYPE_ALT:
4396 itemisflag = TRUE;
4397 if (*wp->w_buffer->b_p_ft != NUL
4398 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4399 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004400 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), ",%s",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004401 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004402 for (t = buf_tmp; *t != 0; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 *t = TOUPPER_LOC(*t);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004404 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 }
4406 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407
Bram Moolenaar4033c552017-09-16 20:54:51 +02004408#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 case STL_PREVIEWFLAG:
4410 case STL_PREVIEWFLAG_ALT:
4411 itemisflag = TRUE;
4412 if (wp->w_p_pvw)
4413 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4414 : _("[Preview]"));
4415 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004416
4417 case STL_QUICKFIX:
4418 if (bt_quickfix(wp->w_buffer))
4419 str = (char_u *)(wp->w_llist_ref
4420 ? _(msg_loclist)
4421 : _(msg_qflist));
4422 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423#endif
4424
4425 case STL_MODIFIED:
4426 case STL_MODIFIED_ALT:
4427 itemisflag = TRUE;
4428 switch ((opt == STL_MODIFIED_ALT)
4429 + bufIsChanged(wp->w_buffer) * 2
4430 + (!wp->w_buffer->b_p_ma) * 4)
4431 {
4432 case 2: str = (char_u *)"[+]"; break;
4433 case 3: str = (char_u *)",+"; break;
4434 case 4: str = (char_u *)"[-]"; break;
4435 case 5: str = (char_u *)",-"; break;
4436 case 6: str = (char_u *)"[+-]"; break;
4437 case 7: str = (char_u *)",+-"; break;
4438 }
4439 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004440
4441 case STL_HIGHLIGHT:
4442 t = s;
4443 while (*s != '#' && *s != NUL)
4444 ++s;
4445 if (*s == '#')
4446 {
4447 item[curitem].type = Highlight;
4448 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004449 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004450 curitem++;
4451 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004452 if (*s != NUL)
4453 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004454 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 }
4456
4457 item[curitem].start = p;
4458 item[curitem].type = Normal;
4459 if (str != NULL && *str)
4460 {
4461 t = str;
4462 if (itemisflag)
4463 {
4464 if ((t[0] && t[1])
4465 && ((!prevchar_isitem && *t == ',')
4466 || (prevchar_isflag && *t == ' ')))
4467 t++;
4468 prevchar_isflag = TRUE;
4469 }
4470 l = vim_strsize(t);
4471 if (l > 0)
4472 prevchar_isitem = TRUE;
4473 if (l > maxwid)
4474 {
4475 while (l >= maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 if (has_mbyte)
4477 {
4478 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004479 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 }
4481 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 l -= byte2cells(*t++);
4483 if (p + 1 >= out + outlen)
4484 break;
4485 *p++ = '<';
4486 }
4487 if (minwid > 0)
4488 {
4489 for (; l < minwid && p + 1 < out + outlen; l++)
4490 {
4491 /* Don't put a "-" in front of a digit. */
4492 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4493 *p++ = ' ';
4494 else
4495 *p++ = fillchar;
4496 }
4497 minwid = 0;
4498 }
4499 else
4500 minwid *= -1;
4501 while (*t && p + 1 < out + outlen)
4502 {
4503 *p++ = *t++;
4504 /* Change a space by fillchar, unless fillchar is '-' and a
4505 * digit follows. */
4506 if (fillable && p[-1] == ' '
4507 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4508 p[-1] = fillchar;
4509 }
4510 for (; l < minwid && p + 1 < out + outlen; l++)
4511 *p++ = fillchar;
4512 }
4513 else if (num >= 0)
4514 {
4515 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4516 char_u nstr[20];
4517
4518 if (p + 20 >= out + outlen)
4519 break; /* not sufficient space */
4520 prevchar_isitem = TRUE;
4521 t = nstr;
4522 if (opt == STL_VIRTCOL_ALT)
4523 {
4524 *t++ = '-';
4525 minwid--;
4526 }
4527 *t++ = '%';
4528 if (zeropad)
4529 *t++ = '0';
4530 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004531 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 *t = 0;
4533
4534 for (n = num, l = 1; n >= nbase; n /= nbase)
4535 l++;
4536 if (opt == STL_VIRTCOL_ALT)
4537 l++;
4538 if (l > maxwid)
4539 {
4540 l += 2;
4541 n = l - maxwid;
4542 while (l-- > maxwid)
4543 num /= nbase;
4544 *t++ = '>';
4545 *t++ = '%';
4546 *t = t[-3];
4547 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004548 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4549 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 }
4551 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004552 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4553 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 p += STRLEN(p);
4555 }
4556 else
4557 item[curitem].type = Empty;
4558
4559 if (opt == STL_VIM_EXPR)
4560 vim_free(str);
4561
4562 if (num >= 0 || (!itemisflag && str && *str))
4563 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4564 curitem++;
4565 }
4566 *p = NUL;
4567 itemcnt = curitem;
4568
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004569#ifdef FEAT_EVAL
4570 if (usefmt != fmt)
4571 vim_free(usefmt);
4572#endif
4573
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 width = vim_strsize(out);
4575 if (maxwidth > 0 && width > maxwidth)
4576 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004577 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 l = 0;
4579 if (itemcnt == 0)
4580 s = out;
4581 else
4582 {
4583 for ( ; l < itemcnt; l++)
4584 if (item[l].type == Trunc)
4585 {
4586 /* Truncate at %< item. */
4587 s = item[l].start;
4588 break;
4589 }
4590 if (l == itemcnt)
4591 {
4592 /* No %< item, truncate first item. */
4593 s = item[0].start;
4594 l = 0;
4595 }
4596 }
4597
4598 if (width - vim_strsize(s) >= maxwidth)
4599 {
4600 /* Truncation mark is beyond max length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 if (has_mbyte)
4602 {
4603 s = out;
4604 width = 0;
4605 for (;;)
4606 {
4607 width += ptr2cells(s);
4608 if (width >= maxwidth)
4609 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004610 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 }
4612 /* Fill up for half a double-wide character. */
4613 while (++width < maxwidth)
4614 *s++ = fillchar;
4615 }
4616 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 s = out + maxwidth - 1;
4618 for (l = 0; l < itemcnt; l++)
4619 if (item[l].start > s)
4620 break;
4621 itemcnt = l;
4622 *s++ = '>';
4623 *s = 0;
4624 }
4625 else
4626 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 if (has_mbyte)
4628 {
4629 n = 0;
4630 while (width >= maxwidth)
4631 {
4632 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004633 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 }
4635 }
4636 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 n = width - maxwidth + 1;
4638 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004639 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 *s = '<';
4641
4642 /* Fill up for half a double-wide character. */
4643 while (++width < maxwidth)
4644 {
4645 s = s + STRLEN(s);
4646 *s++ = fillchar;
4647 *s = NUL;
4648 }
4649
4650 --n; /* count the '<' */
4651 for (; l < itemcnt; l++)
4652 {
4653 if (item[l].start - n >= s)
4654 item[l].start -= n;
4655 else
4656 item[l].start = s;
4657 }
4658 }
4659 width = maxwidth;
4660 }
4661 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4662 {
4663 /* Apply STL_MIDDLE if any */
4664 for (l = 0; l < itemcnt; l++)
4665 if (item[l].type == Middle)
4666 break;
4667 if (l < itemcnt)
4668 {
4669 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004670 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 for (s = item[l].start; s < p; s++)
4672 *s = fillchar;
4673 for (l++; l < itemcnt; l++)
4674 item[l].start += maxwidth - width;
4675 width = maxwidth;
4676 }
4677 }
4678
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004679 /* Store the info about highlighting. */
4680 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004682 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 for (l = 0; l < itemcnt; l++)
4684 {
4685 if (item[l].type == Highlight)
4686 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004687 sp->start = item[l].start;
4688 sp->userhl = item[l].minwid;
4689 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 }
4691 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004692 sp->start = NULL;
4693 sp->userhl = 0;
4694 }
4695
4696 /* Store the info about tab pages labels. */
4697 if (tabtab != NULL)
4698 {
4699 sp = tabtab;
4700 for (l = 0; l < itemcnt; l++)
4701 {
4702 if (item[l].type == TabPage)
4703 {
4704 sp->start = item[l].start;
4705 sp->userhl = item[l].minwid;
4706 sp++;
4707 }
4708 }
4709 sp->start = NULL;
4710 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 }
4712
Bram Moolenaar65ed1362017-09-30 16:00:14 +02004713 /* When inside update_screen we do not want redrawing a stausline, ruler,
4714 * title, etc. to trigger another redraw, it may cause an endless loop. */
4715 if (updating_screen)
4716 {
4717 must_redraw = save_must_redraw;
4718 curwin->w_redr_type = save_redr_type;
4719 }
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004720
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 return width;
4722}
4723#endif /* FEAT_STL_OPT */
4724
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004725#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4726 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004728 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4729 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 */
4731 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004732get_rel_pos(
4733 win_T *wp,
4734 char_u *buf,
4735 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736{
4737 long above; /* number of lines above window */
4738 long below; /* number of lines below window */
4739
Bram Moolenaar0027c212015-01-07 13:31:52 +01004740 if (buflen < 3) /* need at least 3 chars for writing */
4741 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 above = wp->w_topline - 1;
4743#ifdef FEAT_DIFF
4744 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02004745 if (wp->w_topline == 1 && wp->w_topfill >= 1)
4746 above = 0; /* All buffer lines are displayed and there is an
4747 * indication of filler lines, that can be considered
4748 * seeing all lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749#endif
4750 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4751 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004752 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4753 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004755 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004757 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 ? (int)(above / ((above + below) / 100L))
4759 : (int)(above * 100L / (above + below)));
4760}
4761#endif
4762
4763/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004764 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 * Return TRUE if it was appended.
4766 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004767 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004768append_arg_number(
4769 win_T *wp,
4770 char_u *buf,
4771 int buflen,
4772 int add_file) /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773{
4774 char_u *p;
4775
4776 if (ARGCOUNT <= 1) /* nothing to do */
4777 return FALSE;
4778
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004779 p = buf + STRLEN(buf); /* go to the end of the buffer */
4780 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 return FALSE;
4782 *p++ = ' ';
4783 *p++ = '(';
4784 if (add_file)
4785 {
4786 STRCPY(p, "file ");
4787 p += 5;
4788 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004789 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4790 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4792 return TRUE;
4793}
4794
4795/*
4796 * If fname is not a full path, make it a full path.
4797 * Returns pointer to allocated memory (NULL for failure).
4798 */
4799 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004800fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801{
4802 /*
4803 * Force expanding the path always for Unix, because symbolic links may
4804 * mess up the full path name, even though it starts with a '/'.
4805 * Also expand when there is ".." in the file name, try to remove it,
4806 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004807 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 * For MS-Windows also expand names like "longna~1" to "longname".
4809 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004810#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 return FullName_save(fname, TRUE);
4812#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004813 if (!vim_isAbsName(fname)
4814 || strstr((char *)fname, "..") != NULL
4815 || strstr((char *)fname, "//") != NULL
4816# ifdef BACKSLASH_IN_FILENAME
4817 || strstr((char *)fname, "\\\\") != NULL
4818# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004819# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004821# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 )
4823 return FullName_save(fname, FALSE);
4824
4825 fname = vim_strsave(fname);
4826
Bram Moolenaar9b942202007-10-03 12:31:33 +00004827# ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01004828 if (fname != NULL)
4829 fname_case(fname, 0); /* set correct case for file name */
Bram Moolenaar9b942202007-10-03 12:31:33 +00004830# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831
4832 return fname;
4833#endif
4834}
4835
4836/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02004837 * Make "*ffname" a full file name, set "*sfname" to "*ffname" if not NULL.
4838 * "*ffname" becomes a pointer to allocated memory (or NULL).
4839 * When resolving a link both "*sfname" and "*ffname" will point to the same
4840 * allocated memory.
4841 * The "*ffname" and "*sfname" pointer values on call will not be freed.
4842 * Note that the resulting "*ffname" pointer should be considered not allocaed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004845fname_expand(
4846 buf_T *buf UNUSED,
4847 char_u **ffname,
4848 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02004850 if (*ffname == NULL) // no file name given, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 return;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02004852 if (*sfname == NULL) // no short file name given, use ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 *sfname = *ffname;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02004854 *ffname = fix_fname(*ffname); // expand to full path
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855
4856#ifdef FEAT_SHORTCUT
4857 if (!buf->b_p_bin)
4858 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004859 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02004861 // If the file name is a shortcut file, use the file it links to.
Bram Moolenaardce1e892019-02-10 23:18:53 +01004862 rfname = mch_resolve_path(*ffname, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004863 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 {
4865 vim_free(*ffname);
4866 *ffname = rfname;
4867 *sfname = rfname;
4868 }
4869 }
4870#endif
4871}
4872
4873/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 * Open a window for a number of buffers.
4875 */
4876 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004877ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878{
4879 buf_T *buf;
4880 win_T *wp, *wpnext;
4881 int split_ret = OK;
4882 int p_ea_save;
4883 int open_wins = 0;
4884 int r;
4885 int count; /* Maximum number of windows to open. */
4886 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004887 int had_tab = cmdmod.tab;
4888 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889
4890 if (eap->addr_count == 0) /* make as many windows as possible */
4891 count = 9999;
4892 else
4893 count = eap->line2; /* make as many windows as specified */
4894 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4895 all = FALSE;
4896 else
4897 all = TRUE;
4898
4899 setpcmark();
4900
4901#ifdef FEAT_GUI
4902 need_mouse_correct = TRUE;
4903#endif
4904
4905 /*
4906 * Close superfluous windows (two windows for the same buffer).
4907 * Also close windows that are not full-width.
4908 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004909 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004910 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004911 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004913 tpnext = curtab->tp_next;
4914 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004916 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004917 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004918 || ((cmdmod.split & WSP_VERT)
4919 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004920 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004921 : wp->w_width != Columns)
Bram Moolenaar4033c552017-09-16 20:54:51 +02004922 || (had_tab > 0 && wp != firstwin)) && !ONE_WINDOW
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004923 && !(wp->w_closing || wp->w_buffer->b_locked > 0))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004924 {
4925 win_close(wp, FALSE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004926 wpnext = firstwin; /* just in case an autocommand does
4927 something strange with windows */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004928 tpnext = first_tabpage; /* start all over... */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004929 open_wins = 0;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004930 }
4931 else
4932 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004934
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004935 /* Without the ":tab" modifier only do the current tab page. */
4936 if (had_tab == 0 || tpnext == NULL)
4937 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004938 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 }
4940
4941 /*
4942 * Go through the buffer list. When a buffer doesn't have a window yet,
4943 * open one. Otherwise move the window to the right position.
4944 * Watch out for autocommands that delete buffers or windows!
4945 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4947 ++autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 win_enter(lastwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4951 {
4952 /* Check if this buffer needs a window */
4953 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4954 continue;
4955
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004956 if (had_tab != 0)
4957 {
4958 /* With the ":tab" modifier don't move the window. */
4959 if (buf->b_nwindows > 0)
4960 wp = lastwin; /* buffer has a window, skip it */
4961 else
4962 wp = NULL;
4963 }
4964 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004965 {
4966 /* Check if this buffer already has a window */
Bram Moolenaar29323592016-07-24 22:04:11 +02004967 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004968 if (wp->w_buffer == buf)
4969 break;
4970 /* If the buffer already has a window, move it */
4971 if (wp != NULL)
4972 win_move_after(wp, curwin);
4973 }
4974
4975 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02004977 bufref_T bufref;
4978
4979 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004980
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 /* Split the window and put the buffer in it */
4982 p_ea_save = p_ea;
4983 p_ea = TRUE; /* use space from all windows */
4984 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4985 ++open_wins;
4986 p_ea = p_ea_save;
4987 if (split_ret == FAIL)
4988 continue;
4989
4990 /* Open the buffer in this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02004993 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02004995 /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 swap_exists_action = SEA_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 break;
4998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 if (swap_exists_action == SEA_QUIT)
5000 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005001#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005002 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005003
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005004 /* Reset the error/interrupt/exception state here so that
5005 * aborting() returns FALSE when closing a window. */
5006 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005007#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005008
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005009 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 win_close(curwin, TRUE);
5011 --open_wins;
5012 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005013 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005014
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005015#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005016 /* Restore the error/interrupt/exception state if not
5017 * discarded by a new aborting error, interrupt, or uncaught
5018 * exception. */
5019 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005020#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 }
5022 else
5023 handle_swap_exists(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 }
5025
5026 ui_breakcheck();
5027 if (got_int)
5028 {
5029 (void)vgetc(); /* only break the file loading, not the rest */
5030 break;
5031 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005032#ifdef FEAT_EVAL
5033 /* Autocommands deleted the buffer or aborted script processing!!! */
5034 if (aborting())
5035 break;
5036#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005037 /* When ":tab" was used open a new tab for a new window repeatedly. */
5038 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5039 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 --autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 win_enter(firstwin, FALSE); /* back to first window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044
5045 /*
5046 * Close superfluous windows.
5047 */
5048 for (wp = lastwin; open_wins > count; )
5049 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005050 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 || autowrite(wp->w_buffer, FALSE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 if (!win_valid(wp))
5053 {
5054 /* BufWrite Autocommands made the window invalid, start over */
5055 wp = lastwin;
5056 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005057 else if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005059 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 --open_wins;
5061 wp = lastwin;
5062 }
5063 else
5064 {
5065 wp = wp->w_prev;
5066 if (wp == NULL)
5067 break;
5068 }
5069 }
5070}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005073static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005074
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075/*
5076 * do_modelines() - process mode lines for the current file
5077 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005078 * "flags" can be:
5079 * OPT_WINONLY only set options local to window
5080 * OPT_NOWIN don't set options local to window
5081 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 * Returns immediately if the "ml" option isn't set.
5083 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005085do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005087 linenr_T lnum;
5088 int nmlines;
5089 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090
5091 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5092 return;
5093
5094 /* Disallow recursive entry here. Can happen when executing a modeline
5095 * triggers an autocommand, which reloads modelines with a ":do". */
5096 if (entered)
5097 return;
5098
5099 ++entered;
5100 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5101 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005102 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 nmlines = 0;
5104
5105 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5106 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005107 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 nmlines = 0;
5109 --entered;
5110}
5111
5112#include "version.h" /* for version number */
5113
5114/*
5115 * chk_modeline() - check a single line for a mode string
5116 * Return FAIL if an error encountered.
5117 */
5118 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005119chk_modeline(
5120 linenr_T lnum,
5121 int flags) /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122{
5123 char_u *s;
5124 char_u *e;
5125 char_u *linecopy; /* local copy of any modeline found */
5126 int prev;
5127 int vers;
5128 int end;
5129 int retval = OK;
5130 char_u *save_sourcing_name;
5131 linenr_T save_sourcing_lnum;
5132#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005133 sctx_T save_current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134#endif
5135
5136 prev = -1;
5137 for (s = ml_get(lnum); *s != NUL; ++s)
5138 {
5139 if (prev == -1 || vim_isspace(prev))
5140 {
5141 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5142 || STRNCMP(s, "vi:", (size_t)3) == 0)
5143 break;
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005144 /* Accept both "vim" and "Vim". */
5145 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 {
5147 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5148 e = s + 4;
5149 else
5150 e = s + 3;
5151 vers = getdigits(&e);
5152 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005153 && (s[0] != 'V'
5154 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 && (s[3] == ':'
5156 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5157 || (VIM_VERSION_100 < vers && s[3] == '<')
5158 || (VIM_VERSION_100 > vers && s[3] == '>')
5159 || (VIM_VERSION_100 == vers && s[3] == '=')))
5160 break;
5161 }
5162 }
5163 prev = *s;
5164 }
5165
5166 if (*s)
5167 {
5168 do /* skip over "ex:", "vi:" or "vim:" */
5169 ++s;
5170 while (s[-1] != ':');
5171
5172 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5173 if (linecopy == NULL)
5174 return FAIL;
5175
5176 save_sourcing_lnum = sourcing_lnum;
5177 save_sourcing_name = sourcing_name;
5178 sourcing_lnum = lnum; /* prepare for emsg() */
5179 sourcing_name = (char_u *)"modelines";
5180
5181 end = FALSE;
5182 while (end == FALSE)
5183 {
5184 s = skipwhite(s);
5185 if (*s == NUL)
5186 break;
5187
5188 /*
5189 * Find end of set command: ':' or end of line.
5190 * Skip over "\:", replacing it with ":".
5191 */
5192 for (e = s; *e != ':' && *e != NUL; ++e)
5193 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005194 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 if (*e == NUL)
5196 end = TRUE;
5197
5198 /*
5199 * If there is a "set" command, require a terminating ':' and
5200 * ignore the stuff after the ':'.
5201 * "vi:set opt opt opt: foo" -- foo not interpreted
5202 * "vi:opt opt opt: foo" -- foo interpreted
5203 * Accept "se" for compatibility with Elvis.
5204 */
5205 if (STRNCMP(s, "set ", (size_t)4) == 0
5206 || STRNCMP(s, "se ", (size_t)3) == 0)
5207 {
5208 if (*e != ':') /* no terminating ':'? */
5209 break;
5210 end = TRUE;
5211 s = vim_strchr(s, ' ') + 1;
5212 }
5213 *e = NUL; /* truncate the set command */
5214
5215 if (*s != NUL) /* skip over an empty "::" */
5216 {
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005217 int secure_save = secure;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005219 save_current_sctx = current_sctx;
5220 current_sctx.sc_sid = SID_MODELINE;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005221 current_sctx.sc_seq = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005222 current_sctx.sc_lnum = 0;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02005223 current_sctx.sc_version = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224#endif
Bram Moolenaar5958f952018-11-20 04:25:21 +01005225 // Make sure no risky things are executed as a side effect.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01005226 secure = 1;
Bram Moolenaar5958f952018-11-20 04:25:21 +01005227
Bram Moolenaara3227e22006-03-08 21:32:40 +00005228 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar5958f952018-11-20 04:25:21 +01005229
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005230 secure = secure_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005232 current_sctx = save_current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233#endif
5234 if (retval == FAIL) /* stop if error found */
5235 break;
5236 }
5237 s = e + 1; /* advance to next part */
5238 }
5239
5240 sourcing_lnum = save_sourcing_lnum;
5241 sourcing_name = save_sourcing_name;
5242
5243 vim_free(linecopy);
5244 }
5245 return retval;
5246}
5247
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005248/*
Bram Moolenaar91335e52018-08-01 17:53:12 +02005249 * Return TRUE if "buf" is a normal buffer, 'buftype' is empty.
5250 */
5251 int
5252bt_normal(buf_T *buf)
5253{
5254 return buf != NULL && buf->b_p_bt[0] == NUL;
5255}
5256
Bram Moolenaar113e1072019-01-20 15:30:40 +01005257#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar91335e52018-08-01 17:53:12 +02005258/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005259 * Return TRUE if "buf" is the quickfix buffer.
5260 */
5261 int
5262bt_quickfix(buf_T *buf)
5263{
5264 return buf != NULL && buf->b_p_bt[0] == 'q';
5265}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005266#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005267
Bram Moolenaar113e1072019-01-20 15:30:40 +01005268#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005269/*
5270 * Return TRUE if "buf" is a terminal buffer.
5271 */
5272 int
5273bt_terminal(buf_T *buf)
5274{
5275 return buf != NULL && buf->b_p_bt[0] == 't';
5276}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005277#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005278
5279/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005280 * Return TRUE if "buf" is a help buffer.
5281 */
5282 int
5283bt_help(buf_T *buf)
5284{
5285 return buf != NULL && buf->b_help;
5286}
5287
5288/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02005289 * Return TRUE if "buf" is a prompt buffer.
5290 */
5291 int
5292bt_prompt(buf_T *buf)
5293{
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005294 return buf != NULL && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'r';
5295}
5296
5297/*
5298 * Return TRUE if "buf" is a buffer for a popup window.
5299 */
5300 int
5301bt_popup(buf_T *buf)
5302{
5303 return buf != NULL && buf->b_p_bt != NULL
5304 && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'o';
Bram Moolenaarf2732452018-06-03 14:47:35 +02005305}
5306
5307/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005308 * Return TRUE if "buf" is a "nofile", "acwrite", "terminal" or "prompt"
5309 * buffer. This means the buffer name is not a file name.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005310 */
5311 int
Bram Moolenaar26910de2019-06-15 19:37:15 +02005312bt_nofilename(buf_T *buf)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005313{
5314 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5315 || buf->b_p_bt[0] == 'a'
Bram Moolenaarf2732452018-06-03 14:47:35 +02005316 || buf->b_p_bt[0] == 't'
5317 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005318}
5319
5320/*
Bram Moolenaar26910de2019-06-15 19:37:15 +02005321 * Return TRUE if "buf" has 'buftype' set to "nofile".
5322 */
5323 int
5324bt_nofile(buf_T *buf)
5325{
5326 return buf != NULL && buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f';
5327}
5328
5329/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005330 * Return TRUE if "buf" is a "nowrite", "nofile", "terminal" or "prompt"
5331 * buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005332 */
5333 int
5334bt_dontwrite(buf_T *buf)
5335{
Bram Moolenaarf2732452018-06-03 14:47:35 +02005336 return buf != NULL && (buf->b_p_bt[0] == 'n'
5337 || buf->b_p_bt[0] == 't'
5338 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005339}
5340
Bram Moolenaar113e1072019-01-20 15:30:40 +01005341#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005342 int
5343bt_dontwrite_msg(buf_T *buf)
5344{
5345 if (bt_dontwrite(buf))
5346 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005347 emsg(_("E382: Cannot write, 'buftype' option is set"));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005348 return TRUE;
5349 }
5350 return FALSE;
5351}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005352#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005353
5354/*
5355 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
5356 * and 'bufhidden'.
5357 */
5358 int
5359buf_hide(buf_T *buf)
5360{
5361 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
5362 switch (buf->b_p_bh[0])
5363 {
5364 case 'u': /* "unload" */
5365 case 'w': /* "wipe" */
5366 case 'd': return FALSE; /* "delete" */
5367 case 'h': return TRUE; /* "hide" */
5368 }
5369 return (p_hid || cmdmod.hide);
5370}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371
5372/*
5373 * Return special buffer name.
5374 * Returns NULL when the buffer has a normal file name.
5375 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005376 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005377buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378{
Bram Moolenaar4033c552017-09-16 20:54:51 +02005379#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005381 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005382 /*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005383 * Differentiate between the quickfix and location list buffers using
5384 * the buffer number stored in the global quickfix stack.
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005385 */
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005386 if (buf->b_fnum == qf_stack_get_bufnr())
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005387 return (char_u *)_(msg_qflist);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005388 else
5389 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02005392
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 /* There is no _file_ when 'buftype' is "nofile", b_sfname
Bram Moolenaar21554412017-07-24 21:44:43 +02005394 * contains the name as specified by the user. */
Bram Moolenaar26910de2019-06-15 19:37:15 +02005395 if (bt_nofilename(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 {
Bram Moolenaar21554412017-07-24 21:44:43 +02005397#ifdef FEAT_TERMINAL
5398 if (buf->b_term != NULL)
5399 return term_get_status_text(buf->b_term);
5400#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02005401 if (buf->b_fname != NULL)
5402 return buf->b_fname;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02005403#ifdef FEAT_JOB_CHANNEL
5404 if (bt_prompt(buf))
5405 return (char_u *)_("[Prompt]");
5406#endif
Bram Moolenaarc6896e22019-05-30 22:32:34 +02005407#ifdef FEAT_TEXT_PROP
5408 if (bt_popup(buf))
5409 return (char_u *)_("[Popup]");
5410#endif
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005411 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 }
Bram Moolenaar21554412017-07-24 21:44:43 +02005413
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005415 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 return NULL;
5417}
5418
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02005419#if defined(FEAT_JOB_CHANNEL) \
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005420 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5421 || defined(PROTO)
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02005422# define SWITCH_TO_WIN
5423
5424/*
5425 * Find a window that contains "buf" and switch to it.
5426 * If there is no such window, use the current window and change "curbuf".
5427 * Caller must initialize save_curbuf to NULL.
5428 * restore_win_for_buf() MUST be called later!
5429 */
5430 void
5431switch_to_win_for_buf(
5432 buf_T *buf,
5433 win_T **save_curwinp,
5434 tabpage_T **save_curtabp,
5435 bufref_T *save_curbuf)
5436{
5437 win_T *wp;
5438 tabpage_T *tp;
5439
5440 if (find_win_for_buf(buf, &wp, &tp) == FAIL)
5441 switch_buffer(save_curbuf, buf);
5442 else if (switch_win(save_curwinp, save_curtabp, wp, tp, TRUE) == FAIL)
5443 {
5444 restore_win(*save_curwinp, *save_curtabp, TRUE);
5445 switch_buffer(save_curbuf, buf);
5446 }
5447}
5448
5449 void
5450restore_win_for_buf(
5451 win_T *save_curwin,
5452 tabpage_T *save_curtab,
5453 bufref_T *save_curbuf)
5454{
5455 if (save_curbuf->br_buf == NULL)
5456 restore_win(save_curwin, save_curtab, TRUE);
5457 else
5458 restore_buffer(save_curbuf);
5459}
5460#endif
5461
Bram Moolenaar4033c552017-09-16 20:54:51 +02005462#if defined(FEAT_QUICKFIX) || defined(SWITCH_TO_WIN) || defined(PROTO)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005463/*
5464 * Find a window for buffer "buf".
5465 * If found OK is returned and "wp" and "tp" are set to the window and tabpage.
5466 * If not found FAIL is returned.
5467 */
5468 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005469find_win_for_buf(
5470 buf_T *buf,
5471 win_T **wp,
5472 tabpage_T **tp)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005473{
5474 FOR_ALL_TAB_WINDOWS(*tp, *wp)
5475 if ((*wp)->w_buffer == buf)
5476 goto win_found;
5477 return FAIL;
5478win_found:
5479 return OK;
5480}
5481#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483/*
5484 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5485 */
5486 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005487set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488{
5489 if (on != curbuf->b_p_bl)
5490 {
5491 curbuf->b_p_bl = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 if (on)
5493 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5494 else
5495 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 }
5497}
5498
5499/*
5500 * Read the file for "buf" again and check if the contents changed.
5501 * Return TRUE if it changed or this could not be checked.
5502 */
5503 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005504buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505{
5506 buf_T *newbuf;
5507 int differ = TRUE;
5508 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 exarg_T ea;
5511
5512 /* Allocate a buffer without putting it in the buffer list. */
5513 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5514 if (newbuf == NULL)
5515 return TRUE;
5516
5517 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5518 if (prep_exarg(&ea, buf) == FAIL)
5519 {
5520 wipe_buffer(newbuf, FALSE);
5521 return TRUE;
5522 }
5523
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 /* set curwin/curbuf to buf and save a few things */
5525 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526
Bram Moolenaar4770d092006-01-12 23:22:24 +00005527 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 && readfile(buf->b_ffname, buf->b_fname,
5529 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5530 &ea, READ_NEW | READ_DUMMY) == OK)
5531 {
5532 /* compare the two files line by line */
5533 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5534 {
5535 differ = FALSE;
5536 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5537 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5538 {
5539 differ = TRUE;
5540 break;
5541 }
5542 }
5543 }
5544 vim_free(ea.cmd);
5545
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 /* restore curwin/curbuf and a few other things */
5547 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548
5549 if (curbuf != newbuf) /* safety check */
5550 wipe_buffer(newbuf, FALSE);
5551
5552 return differ;
5553}
5554
5555/*
5556 * Wipe out a buffer and decrement the last buffer number if it was used for
5557 * this buffer. Call this to wipe out a temp buffer that does not contain any
5558 * marks.
5559 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005561wipe_buffer(
5562 buf_T *buf,
5563 int aucmd UNUSED) /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564{
5565 if (buf->b_fnum == top_file_num - 1)
5566 --top_file_num;
5567
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005569 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005570
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005571 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005572
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005574 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575}
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005576
5577#if defined(FEAT_EVAL) || defined(PROTO)
5578/*
5579 * Mark references in functions of buffers.
5580 */
5581 int
5582set_ref_in_buffers(int copyID)
5583{
5584 int abort = FALSE;
5585 buf_T *bp;
5586
5587 FOR_ALL_BUFFERS(bp)
5588 {
5589 listener_T *lnr;
5590 typval_T tv;
5591
5592 for (lnr = bp->b_listener; !abort && lnr != NULL; lnr = lnr->lr_next)
5593 {
5594 if (lnr->lr_callback.cb_partial != NULL)
5595 {
5596 tv.v_type = VAR_PARTIAL;
5597 tv.vval.v_partial = lnr->lr_callback.cb_partial;
5598 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
5599 }
5600 }
5601# ifdef FEAT_JOB_CHANNEL
5602 if (!abort && bp->b_prompt_callback.cb_partial != NULL)
5603 {
5604 tv.v_type = VAR_PARTIAL;
5605 tv.vval.v_partial = bp->b_prompt_callback.cb_partial;
5606 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
5607 }
5608 if (!abort && bp->b_prompt_interrupt.cb_partial != NULL)
5609 {
5610 tv.v_type = VAR_PARTIAL;
5611 tv.vval.v_partial = bp->b_prompt_interrupt.cb_partial;
5612 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
5613 }
5614# endif
5615 if (abort)
5616 break;
5617 }
5618 return abort;
5619}
5620#endif