blob: a9aa30c3e3120bba9151227267815379d3cabd0a [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * buffer.c: functions for dealing with the buffer structure
12 */
13
14/*
15 * The buffer list is a double linked list of all buffers.
16 * Each buffer can be in one of these states:
17 * never loaded: BF_NEVERLOADED is set, only the file name is valid
18 * not loaded: b_ml.ml_mfp == NULL, no memfile allocated
19 * hidden: b_nwindows == 0, loaded but not displayed in a window
20 * normal: loaded and displayed in a window
21 *
22 * Instead of storing file names all over the place, each file name is
23 * stored in the buffer list. It can be referenced by a number.
24 *
25 * The current implementation remembers all file names ever used.
26 */
27
Bram Moolenaar071d4272004-06-13 20:20:40 +000028#include "vim.h"
29
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020030static void enter_buffer(buf_T *buf);
31static void buflist_getfpos(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010032static char_u *buflist_match(regmatch_T *rmp, buf_T *buf, int ignore_case);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010033static char_u *fname_match(regmatch_T *rmp, char_u *name, int ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +000034#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +020035static buf_T *buflist_findname_stat(char_u *ffname, stat_T *st);
36static int otherfile_buf(buf_T *buf, char_u *ffname, stat_T *stp);
37static int buf_same_ino(buf_T *buf, stat_T *stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000038#else
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010039static int otherfile_buf(buf_T *buf, char_u *ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000040#endif
41#ifdef FEAT_TITLE
Bram Moolenaar84a93082018-06-16 22:58:15 +020042static int value_changed(char_u *str, char_u **last);
Bram Moolenaar071d4272004-06-13 20:20:40 +000043#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010044static int append_arg_number(win_T *wp, char_u *buf, int buflen, int add_file);
45static void free_buffer(buf_T *);
46static void free_buffer_stuff(buf_T *buf, int free_options);
47static void clear_wininfo(buf_T *buf);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020048#if defined(FEAT_JOB_CHANNEL) \
49 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
50static int find_win_for_buf(buf_T *buf, win_T **wp, tabpage_T **tp);
51#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000052
53#ifdef UNIX
54# define dev_T dev_t
55#else
56# define dev_T unsigned
57#endif
58
Bram Moolenaar4033c552017-09-16 20:54:51 +020059#if defined(FEAT_QUICKFIX)
Bram Moolenaar7fd73202010-07-25 16:58:46 +020060static char *msg_loclist = N_("[Location List]");
61static char *msg_qflist = N_("[Quickfix List]");
62#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +010063static char *e_auabort = N_("E855: Autocommands caused command to abort");
Bram Moolenaar7fd73202010-07-25 16:58:46 +020064
Bram Moolenaarb25f9a92016-07-10 18:21:50 +020065/* Number of times free_buffer() was called. */
66static int buf_free_count = 0;
67
Bram Moolenaarc024b462019-06-08 18:07:21 +020068/*
69 * Read data from buffer for retrying.
70 */
Bram Moolenaarf71d7b92016-08-09 22:14:05 +020071 static int
72read_buffer(
73 int read_stdin, /* read file from stdin, otherwise fifo */
74 exarg_T *eap, /* for forced 'ff' and 'fenc' or NULL */
75 int flags) /* extra flags for readfile() */
76{
77 int retval = OK;
78 linenr_T line_count;
79
80 /*
81 * Read from the buffer which the text is already filled in and append at
82 * the end. This makes it possible to retry when 'fileformat' or
83 * 'fileencoding' was guessed wrong.
84 */
85 line_count = curbuf->b_ml.ml_line_count;
86 retval = readfile(
87 read_stdin ? NULL : curbuf->b_ffname,
88 read_stdin ? NULL : curbuf->b_fname,
89 (linenr_T)line_count, (linenr_T)0, (linenr_T)MAXLNUM, eap,
90 flags | READ_BUFFER);
91 if (retval == OK)
92 {
93 /* Delete the binary lines. */
94 while (--line_count >= 0)
95 ml_delete((linenr_T)1, FALSE);
96 }
97 else
98 {
99 /* Delete the converted lines. */
100 while (curbuf->b_ml.ml_line_count > line_count)
101 ml_delete(line_count, FALSE);
102 }
103 /* Put the cursor on the first line. */
104 curwin->w_cursor.lnum = 1;
105 curwin->w_cursor.col = 0;
106
107 if (read_stdin)
108 {
109 /* Set or reset 'modified' before executing autocommands, so that
110 * it can be changed there. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100111 if (!readonlymode && !BUFEMPTY())
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200112 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100113 else if (retval == OK)
Bram Moolenaarc024b462019-06-08 18:07:21 +0200114 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200115
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100116 if (retval == OK)
117 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100118#ifdef FEAT_EVAL
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100119 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100120 curbuf, &retval);
121#else
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100122 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200123#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100124 }
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200125 }
126 return retval;
127}
128
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129/*
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200130 * Ensure buffer "buf" is loaded. Does not trigger the swap-exists action.
131 */
132 void
133buffer_ensure_loaded(buf_T *buf)
134{
135 if (buf->b_ml.ml_mfp == NULL)
136 {
137 aco_save_T aco;
138
139 aucmd_prepbuf(&aco, buf);
140 swap_exists_action = SEA_NONE;
141 open_buffer(FALSE, NULL, 0);
142 aucmd_restbuf(&aco);
143 }
144}
145
146/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200147 * Open current buffer, that is: open the memfile and read the file into
148 * memory.
149 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150 */
151 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100152open_buffer(
153 int read_stdin, /* read file from stdin */
154 exarg_T *eap, /* for forced 'ff' and 'fenc' or NULL */
155 int flags) /* extra flags for readfile() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156{
157 int retval = OK;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200158 bufref_T old_curbuf;
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100159#ifdef FEAT_SYN_HL
160 long old_tw = curbuf->b_p_tw;
161#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200162 int read_fifo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163
164 /*
165 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
166 * When re-entering the same buffer, it should not change, because the
167 * user may have reset the flag by hand.
168 */
169 if (readonlymode && curbuf->b_ffname != NULL
170 && (curbuf->b_flags & BF_NEVERLOADED))
171 curbuf->b_p_ro = TRUE;
172
Bram Moolenaar4770d092006-01-12 23:22:24 +0000173 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174 {
175 /*
176 * There MUST be a memfile, otherwise we can't do anything
177 * If we can't create one for the current buffer, take another buffer
178 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100179 close_buffer(NULL, curbuf, 0, FALSE);
Bram Moolenaar29323592016-07-24 22:04:11 +0200180 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 if (curbuf->b_ml.ml_mfp != NULL)
182 break;
183 /*
Bram Moolenaar6f10c702019-08-20 22:58:37 +0200184 * If there is no memfile at all, exit.
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000185 * This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186 */
187 if (curbuf == NULL)
188 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100189 emsg(_("E82: Cannot allocate any buffer, exiting..."));
Bram Moolenaar6f10c702019-08-20 22:58:37 +0200190
191 // Don't try to do any saving, with "curbuf" NULL almost nothing
192 // will work.
193 v_dying = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 getout(2);
195 }
Bram Moolenaar6f10c702019-08-20 22:58:37 +0200196
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100197 emsg(_("E83: Cannot allocate buffer, using other one..."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 enter_buffer(curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100199#ifdef FEAT_SYN_HL
200 if (old_tw != curbuf->b_p_tw)
201 check_colorcolumn(curwin);
202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 return FAIL;
204 }
205
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 /* The autocommands in readfile() may change the buffer, but only AFTER
207 * reading the file. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200208 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 modified_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210
211 /* mark cursor position as being invalid */
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100212 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213
214 if (curbuf->b_ffname != NULL
215#ifdef FEAT_NETBEANS_INTG
216 && netbeansReadFile
217#endif
218 )
219 {
Bram Moolenaar426dd022016-03-15 15:09:29 +0100220 int old_msg_silent = msg_silent;
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200221#ifdef UNIX
222 int save_bin = curbuf->b_p_bin;
223 int perm;
224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225#ifdef FEAT_NETBEANS_INTG
226 int oldFire = netbeansFireChanges;
227
228 netbeansFireChanges = 0;
229#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200230#ifdef UNIX
231 perm = mch_getperm(curbuf->b_ffname);
Bram Moolenaard569bb02018-08-11 13:57:20 +0200232 if (perm >= 0 && (S_ISFIFO(perm)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200233 || S_ISSOCK(perm)
Bram Moolenaarf04507d2016-08-20 15:05:39 +0200234# ifdef OPEN_CHR_FILES
235 || (S_ISCHR(perm) && is_dev_fd_file(curbuf->b_ffname))
236# endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200237 ))
238 read_fifo = TRUE;
239 if (read_fifo)
240 curbuf->b_p_bin = TRUE;
241#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100242 if (shortmess(SHM_FILEINFO))
243 msg_silent = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200245 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200246 flags | READ_NEW | (read_fifo ? READ_FIFO : 0));
247#ifdef UNIX
248 if (read_fifo)
249 {
250 curbuf->b_p_bin = save_bin;
251 if (retval == OK)
252 retval = read_buffer(FALSE, eap, flags);
253 }
254#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100255 msg_silent = old_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256#ifdef FEAT_NETBEANS_INTG
257 netbeansFireChanges = oldFire;
258#endif
259 /* Help buffer is filtered. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +0200260 if (bt_help(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 fix_help_buffer();
262 }
263 else if (read_stdin)
264 {
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200265 int save_bin = curbuf->b_p_bin;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266
267 /*
268 * First read the text in binary mode into the buffer.
269 * Then read from that same buffer and append at the end. This makes
270 * it possible to retry when 'fileformat' or 'fileencoding' was
271 * guessed wrong.
272 */
273 curbuf->b_p_bin = TRUE;
274 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200275 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
276 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 curbuf->b_p_bin = save_bin;
278 if (retval == OK)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200279 retval = read_buffer(TRUE, eap, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280 }
281
282 /* if first time loading this buffer, init b_chartab[] */
283 if (curbuf->b_flags & BF_NEVERLOADED)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100284 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 (void)buf_init_chartab(curbuf, FALSE);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100286#ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100287 parse_cino(curbuf);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100288#endif
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290
291 /*
292 * Set/reset the Changed flag first, autocmds may change the buffer.
293 * Apply the automatic commands, before processing the modelines.
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200294 * So the modelines have priority over autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295 */
296 /* When reading stdin, the buffer contents always needs writing, so set
297 * the changed flag. Unless in readonly mode: "ls | gview -".
298 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000299 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 || modified_was_set /* ":set modified" used in autocmd */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100301#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000304 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100306 else if (retval == OK && !read_stdin && !read_fifo)
Bram Moolenaarc024b462019-06-08 18:07:21 +0200307 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 save_file_ff(curbuf); /* keep this fileformat */
309
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100310 /* Set last_changedtick to avoid triggering a TextChanged autocommand right
311 * after it was added. */
312 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100313 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100314
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 /* require "!" to overwrite the file, because it wasn't read completely */
316#ifdef FEAT_EVAL
317 if (aborting())
318#else
319 if (got_int)
320#endif
321 curbuf->b_flags |= BF_READERR;
322
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000323#ifdef FEAT_FOLDING
324 /* Need to update automatic folding. Do this before the autocommands,
325 * they may use the fold info. */
326 foldUpdateAll(curwin);
327#endif
328
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 /* need to set w_topline, unless some autocommand already did that. */
330 if (!(curwin->w_valid & VALID_TOPLINE))
331 {
332 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100333#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100335#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100337#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100339#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341#endif
342
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100343 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 /*
346 * The autocommands may have changed the current buffer. Apply the
347 * modelines to the correct buffer, if it still exists and is loaded.
348 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200349 if (bufref_valid(&old_curbuf) && old_curbuf.br_buf->b_ml.ml_mfp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 {
351 aco_save_T aco;
352
353 /* Go to the buffer that was opened. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200354 aucmd_prepbuf(&aco, old_curbuf.br_buf);
Bram Moolenaara3227e22006-03-08 21:32:40 +0000355 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
357
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100358#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100360 &retval);
361#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364
365 /* restore curwin/curbuf and a few other things */
366 aucmd_restbuf(&aco);
367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 }
369
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 return retval;
371}
372
373/*
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200374 * Store "buf" in "bufref" and set the free count.
375 */
376 void
377set_bufref(bufref_T *bufref, buf_T *buf)
378{
379 bufref->br_buf = buf;
Bram Moolenaarfadacf02017-06-19 20:35:32 +0200380 bufref->br_fnum = buf == NULL ? 0 : buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200381 bufref->br_buf_free_count = buf_free_count;
382}
383
384/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200385 * Return TRUE if "bufref->br_buf" points to the same buffer as when
386 * set_bufref() was called and it is a valid buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200387 * Only goes through the buffer list if buf_free_count changed.
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200388 * Also checks if b_fnum is still the same, a :bwipe followed by :new might get
389 * the same allocated memory, but it's a different buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200390 */
391 int
392bufref_valid(bufref_T *bufref)
393{
394 return bufref->br_buf_free_count == buf_free_count
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200395 ? TRUE : buf_valid(bufref->br_buf)
396 && bufref->br_fnum == bufref->br_buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200397}
398
399/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200401 * This can be slow if there are many buffers, prefer using bufref_valid().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402 */
403 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100404buf_valid(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405{
406 buf_T *bp;
407
Bram Moolenaar82404332016-07-10 17:00:38 +0200408 /* Assume that we more often have a recent buffer, start with the last
409 * one. */
410 for (bp = lastbuf; bp != NULL; bp = bp->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 if (bp == buf)
412 return TRUE;
413 return FALSE;
414}
415
416/*
Bram Moolenaar480778b2016-07-14 22:09:39 +0200417 * A hash table used to quickly lookup a buffer by its number.
418 */
419static hashtab_T buf_hashtab;
420
421 static void
422buf_hashtab_add(buf_T *buf)
423{
424 sprintf((char *)buf->b_key, "%x", buf->b_fnum);
425 if (hash_add(&buf_hashtab, buf->b_key) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100426 emsg(_("E931: Buffer cannot be registered"));
Bram Moolenaar480778b2016-07-14 22:09:39 +0200427}
428
429 static void
430buf_hashtab_remove(buf_T *buf)
431{
432 hashitem_T *hi = hash_find(&buf_hashtab, buf->b_key);
433
434 if (!HASHITEM_EMPTY(hi))
435 hash_remove(&buf_hashtab, hi);
436}
437
Bram Moolenaar94f01952018-09-01 15:30:03 +0200438/*
439 * Return TRUE when buffer "buf" can be unloaded.
440 * Give an error message and return FALSE when the buffer is locked or the
441 * screen is being redrawn and the buffer is in a window.
442 */
443 static int
444can_unload_buffer(buf_T *buf)
445{
446 int can_unload = !buf->b_locked;
447
448 if (can_unload && updating_screen)
449 {
450 win_T *wp;
451
452 FOR_ALL_WINDOWS(wp)
453 if (wp->w_buffer == buf)
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200454 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200455 can_unload = FALSE;
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200456 break;
457 }
Bram Moolenaar94f01952018-09-01 15:30:03 +0200458 }
459 if (!can_unload)
Bram Moolenaardefa0672019-07-21 19:25:37 +0200460 semsg(_("E937: Attempt to delete a buffer that is in use: %s"),
461 buf->b_fname);
Bram Moolenaar94f01952018-09-01 15:30:03 +0200462 return can_unload;
463}
Bram Moolenaara997b452018-04-17 23:24:06 +0200464
Bram Moolenaar480778b2016-07-14 22:09:39 +0200465/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466 * Close the link to a buffer.
467 * "action" is used when there is no longer a window for the buffer.
468 * It can be:
469 * 0 buffer becomes hidden
470 * DOBUF_UNLOAD buffer is unloaded
471 * DOBUF_DELETE buffer is unloaded and removed from buffer list
472 * DOBUF_WIPE buffer is unloaded and really deleted
473 * When doing all but the first one on the current buffer, the caller should
474 * get a new buffer very soon!
475 *
476 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100477 *
478 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
479 * cause there to be only one window with this buffer. e.g. when ":quit" is
480 * supposed to close the window but autocommands close all other windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 */
482 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100483close_buffer(
484 win_T *win, /* if not NULL, set b_last_cursor */
485 buf_T *buf,
486 int action,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200487 int abort_if_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100490 int nwindows;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200491 bufref_T bufref;
Bram Moolenaar3a117e12016-10-30 21:57:52 +0100492 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200493 win_T *the_curwin = curwin;
494 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 int unload_buf = (action != 0);
496 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
497 int wipe_buf = (action == DOBUF_WIPE);
498
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200499 /*
500 * Force unloading or deleting when 'bufhidden' says so.
501 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
502 * "hide" (otherwise we could never free or delete a buffer).
503 */
504 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
505 {
506 del_buf = TRUE;
507 unload_buf = TRUE;
508 }
509 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
510 {
511 del_buf = TRUE;
512 unload_buf = TRUE;
513 wipe_buf = TRUE;
514 }
515 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
516 unload_buf = TRUE;
517
Bram Moolenaar94053a52017-08-01 21:44:33 +0200518#ifdef FEAT_TERMINAL
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200519 if (bt_terminal(buf) && (buf->b_nwindows == 1 || del_buf))
Bram Moolenaar94053a52017-08-01 21:44:33 +0200520 {
521 if (term_job_running(buf->b_term))
522 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200523 if (wipe_buf || unload_buf)
Bram Moolenaara997b452018-04-17 23:24:06 +0200524 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200525 if (!can_unload_buffer(buf))
Bram Moolenaara997b452018-04-17 23:24:06 +0200526 return;
Bram Moolenaar94f01952018-09-01 15:30:03 +0200527
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200528 /* Wiping out or unloading a terminal buffer kills the job. */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200529 free_terminal(buf);
Bram Moolenaara997b452018-04-17 23:24:06 +0200530 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200531 else
532 {
533 /* The job keeps running, hide the buffer. */
534 del_buf = FALSE;
535 unload_buf = FALSE;
536 }
537 }
538 else
539 {
540 /* A terminal buffer is wiped out if the job has finished. */
541 del_buf = TRUE;
542 unload_buf = TRUE;
543 wipe_buf = TRUE;
544 }
545 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200546#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200548 /* Disallow deleting the buffer when it is locked (already being closed or
549 * halfway a command that relies on it). Unloading is allowed. */
Bram Moolenaar94f01952018-09-01 15:30:03 +0200550 if ((del_buf || wipe_buf) && !can_unload_buffer(buf))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200551 return;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200552
Bram Moolenaar4033c552017-09-16 20:54:51 +0200553 /* check no autocommands closed the window */
554 if (win != NULL && win_valid_any_tab(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 {
556 /* Set b_last_cursor when closing the last window for the buffer.
557 * Remember the last cursor position and window options of the buffer.
558 * This used to be only for the current window, but then options like
559 * 'foldmethod' may be lost with a ":only" command. */
560 if (buf->b_nwindows == 1)
561 set_last_cursor(win);
562 buflist_setfpos(buf, win,
563 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
564 win->w_cursor.col, TRUE);
565 }
566
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200567 set_bufref(&bufref, buf);
568
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 /* When the buffer is no longer in a window, trigger BufWinLeave */
570 if (buf->b_nwindows == 1)
571 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200572 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200573 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
574 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200575 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100576 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200577 /* Autocommands deleted the buffer. */
578aucmd_abort:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100579 emsg(_(e_auabort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 return;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100581 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200582 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200583 if (abort_if_last && one_window())
584 /* Autocommands made this the only window. */
585 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586
587 /* When the buffer becomes hidden, but is not unloaded, trigger
588 * BufHidden */
589 if (!unload_buf)
590 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200591 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200592 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
593 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200594 && !bufref_valid(&bufref))
Bram Moolenaar362ce482012-06-06 19:02:45 +0200595 /* Autocommands deleted the buffer. */
596 goto aucmd_abort;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200597 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200598 if (abort_if_last && one_window())
599 /* Autocommands made this the only window. */
600 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100602#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 if (aborting()) /* autocmds may abort script processing */
604 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200607
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200608 /* If the buffer was in curwin and the window has changed, go back to that
609 * window, if it still exists. This avoids that ":edit x" triggering a
610 * "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
611 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
612 {
613 block_autocmds();
614 goto_tabpage_win(the_curtab, the_curwin);
615 unblock_autocmds();
616 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200617
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 nwindows = buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619
620 /* decrease the link count from windows (unless not in any window) */
621 if (buf->b_nwindows > 0)
622 --buf->b_nwindows;
623
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100624#ifdef FEAT_DIFF
625 if (diffopt_hiddenoff() && !unload_buf && buf->b_nwindows == 0)
Bram Moolenaarb7407d32018-02-03 17:36:27 +0100626 diff_buf_delete(buf); /* Clear 'diff' for hidden buffer. */
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100627#endif
628
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 /* Return when a window is displaying the buffer or when it's not
630 * unloaded. */
631 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633
634 /* Always remove the buffer when there is no file name. */
635 if (buf->b_ffname == NULL)
636 del_buf = TRUE;
637
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200638 /* When closing the current buffer stop Visual mode before freeing
639 * anything. */
Bram Moolenaar4930a762016-09-11 14:39:53 +0200640 if (buf == curbuf && VIsual_active
Bram Moolenaar9a27c7f2016-09-09 12:57:09 +0200641#if defined(EXITFREE)
642 && !entered_free_all_mem
643#endif
644 )
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200645 end_visual_mode();
646
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 /*
648 * Free all things allocated for this buffer.
649 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
650 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 /* Remember if we are closing the current buffer. Restore the number of
652 * windows, so that autocommands in buf_freeall() don't get confused. */
653 is_curbuf = (buf == curbuf);
654 buf->b_nwindows = nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200656 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 /* Autocommands may have deleted the buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200659 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100661#ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000662 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100664#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 /*
667 * It's possible that autocommands change curbuf to the one being deleted.
668 * This might cause the previous curbuf to be deleted unexpectedly. But
669 * in some cases it's OK to delete the curbuf, because a new one is
670 * obtained anyway. Therefore only return if curbuf changed to the
671 * deleted buffer.
672 */
673 if (buf == curbuf && !is_curbuf)
674 return;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200675
Bram Moolenaar4033c552017-09-16 20:54:51 +0200676 if (win_valid_any_tab(win) && win->w_buffer == buf)
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200677 win->w_buffer = NULL; /* make sure we don't use the buffer now */
678
679 /* Autocommands may have opened or closed windows for this buffer.
680 * Decrement the count for the close we do here. */
681 if (buf->b_nwindows > 0)
682 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 /*
685 * Remove the buffer from the list.
686 */
687 if (wipe_buf)
688 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +0200689 if (buf->b_sfname != buf->b_ffname)
690 VIM_CLEAR(buf->b_sfname);
691 else
692 buf->b_sfname = NULL;
693 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 if (buf->b_prev == NULL)
695 firstbuf = buf->b_next;
696 else
697 buf->b_prev->b_next = buf->b_next;
698 if (buf->b_next == NULL)
699 lastbuf = buf->b_prev;
700 else
701 buf->b_next->b_prev = buf->b_prev;
702 free_buffer(buf);
703 }
704 else
705 {
706 if (del_buf)
707 {
708 /* Free all internal variables and reset option values, to make
709 * ":bdel" compatible with Vim 5.7. */
710 free_buffer_stuff(buf, TRUE);
711
712 /* Make it look like a new buffer. */
713 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
714
715 /* Init the options when loaded again. */
716 buf->b_p_initialized = FALSE;
717 }
718 buf_clear_file(buf);
719 if (del_buf)
720 buf->b_p_bl = FALSE;
721 }
722}
723
724/*
725 * Make buffer not contain a file.
726 */
727 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100728buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729{
730 buf->b_ml.ml_line_count = 1;
Bram Moolenaarc024b462019-06-08 18:07:21 +0200731 unchanged(buf, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 buf->b_p_eol = TRUE;
734 buf->b_start_eol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000736 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 buf->b_ml.ml_mfp = NULL;
738 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
739#ifdef FEAT_NETBEANS_INTG
740 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741#endif
742}
743
744/*
745 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200746 * the file. Careful: get here with "curwin" NULL when exiting.
747 * flags:
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200748 * BFA_DEL buffer is going to be deleted
749 * BFA_WIPE buffer is going to be wiped out
750 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100753buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200756 bufref_T bufref;
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200757 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaar5a497892016-09-03 16:29:04 +0200758 win_T *the_curwin = curwin;
759 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760
Bram Moolenaar5a497892016-09-03 16:29:04 +0200761 /* Make sure the buffer isn't closed by autocommands. */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200762 ++buf->b_locked;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200763 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200764 if (buf->b_ml.ml_mfp != NULL)
765 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200766 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
767 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200768 && !bufref_valid(&bufref))
769 /* autocommands deleted the buffer */
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200770 return;
771 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200772 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200774 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
775 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200776 && !bufref_valid(&bufref))
777 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 return;
779 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200780 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200782 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
783 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200784 && !bufref_valid(&bufref))
785 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 return;
787 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200788 --buf->b_locked;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200789
Bram Moolenaar5a497892016-09-03 16:29:04 +0200790 /* If the buffer was in curwin and the window has changed, go back to that
791 * window, if it still exists. This avoids that ":edit x" triggering a
792 * "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
793 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
794 {
795 block_autocmds();
796 goto_tabpage_win(the_curtab, the_curwin);
797 unblock_autocmds();
798 }
Bram Moolenaar5a497892016-09-03 16:29:04 +0200799
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100800#ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000801 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804
805 /*
806 * It's possible that autocommands change curbuf to the one being deleted.
807 * This might cause curbuf to be deleted unexpectedly. But in some cases
808 * it's OK to delete the curbuf, because a new one is obtained anyway.
809 * Therefore only return if curbuf changed to the deleted buffer.
810 */
811 if (buf == curbuf && !is_curbuf)
812 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813#ifdef FEAT_DIFF
814 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
815#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200816#ifdef FEAT_SYN_HL
Bram Moolenaar89c71222011-11-30 15:40:56 +0100817 /* Remove any ownsyntax, unless exiting. */
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200818 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaar89c71222011-11-30 15:40:56 +0100819 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200820#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000821
822#ifdef FEAT_FOLDING
823 /* No folds in an empty buffer. */
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000824 {
825 win_T *win;
826 tabpage_T *tp;
827
828 FOR_ALL_TAB_WINDOWS(tp, win)
829 if (win->w_buffer == buf)
830 clearFolding(win);
831 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000832#endif
833
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834#ifdef FEAT_TCL
835 tcl_buffer_free(buf);
836#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 ml_close(buf, TRUE); /* close and delete the memline/memfile */
838 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200839 if ((flags & BFA_KEEP_UNDO) == 0)
840 {
841 u_blockfree(buf); /* free the memory allocated for undo */
842 u_clearall(buf); /* reset all undo information */
843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200845 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100847#ifdef FEAT_TEXT_PROP
848 clear_buf_prop_types(buf);
849#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000850 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851}
852
853/*
854 * Free a buffer structure and the things it contains related to the buffer
855 * itself (not the file, that must have been done already).
856 */
857 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100858free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200860 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200862#ifdef FEAT_EVAL
Bram Moolenaarb2c87502017-10-14 21:15:58 +0200863 /* b:changedtick uses an item in buf_T, remove it now */
864 dictitem_remove(buf->b_vars, (dictitem_T *)&buf->b_ct_di);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200865 unref_var_dict(buf->b_vars);
866#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200867#ifdef FEAT_LUA
868 lua_buffer_free(buf);
869#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000870#ifdef FEAT_MZSCHEME
871 mzscheme_buffer_free(buf);
872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873#ifdef FEAT_PERL
874 perl_buf_free(buf);
875#endif
876#ifdef FEAT_PYTHON
877 python_buffer_free(buf);
878#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200879#ifdef FEAT_PYTHON3
880 python3_buffer_free(buf);
881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882#ifdef FEAT_RUBY
883 ruby_buffer_free(buf);
884#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200885#ifdef FEAT_JOB_CHANNEL
886 channel_buffer_free(buf);
887#endif
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200888#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +0200889 free_terminal(buf);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200890#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +0200891#ifdef FEAT_JOB_CHANNEL
892 vim_free(buf->b_prompt_text);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200893 free_callback(&buf->b_prompt_callback);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200894#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200895
896 buf_hashtab_remove(buf);
897
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200898 aubuflocal_remove(buf);
899
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200900 if (autocmd_busy)
901 {
902 /* Do not free the buffer structure while autocommands are executing,
903 * it's still needed. Free it when autocmd_busy is reset. */
904 buf->b_next = au_pending_free_buf;
905 au_pending_free_buf = buf;
906 }
907 else
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200908 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909}
910
911/*
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100912 * Initializes b:changedtick.
Bram Moolenaar79518e22017-02-17 16:31:35 +0100913 */
914 static void
915init_changedtick(buf_T *buf)
916{
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100917 dictitem_T *di = (dictitem_T *)&buf->b_ct_di;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100918
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100919 di->di_flags = DI_FLAGS_FIX | DI_FLAGS_RO;
920 di->di_tv.v_type = VAR_NUMBER;
921 di->di_tv.v_lock = VAR_FIXED;
922 di->di_tv.vval.v_number = 0;
923
Bram Moolenaar92769c32017-02-25 15:41:37 +0100924#ifdef FEAT_EVAL
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100925 STRCPY(buf->b_ct_di.di_key, "changedtick");
926 (void)dict_add(buf->b_vars, di);
Bram Moolenaar92769c32017-02-25 15:41:37 +0100927#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +0100928}
929
930/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
932 */
933 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100934free_buffer_stuff(
935 buf_T *buf,
936 int free_options) /* free options as well */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937{
938 if (free_options)
939 {
940 clear_wininfo(buf); /* including window-local options */
941 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200942#ifdef FEAT_SPELL
943 ga_clear(&buf->b_s.b_langp);
944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 }
946#ifdef FEAT_EVAL
Bram Moolenaar79518e22017-02-17 16:31:35 +0100947 {
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100948 varnumber_T tick = CHANGEDTICK(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +0100949
950 vars_clear(&buf->b_vars->dv_hashtab); /* free all buffer variables */
951 hash_init(&buf->b_vars->dv_hashtab);
952 init_changedtick(buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100953 CHANGEDTICK(buf) = tick;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955#endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200956 uc_clear(&buf->b_ucmds); // clear local user commands
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957#ifdef FEAT_SIGNS
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200958 buf_delete_signs(buf, (char_u *)"*"); // delete any signs
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000960#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200961 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000962#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
964 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100965 VIM_CLEAR(buf->b_start_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966}
967
968/*
969 * Free the b_wininfo list for buffer "buf".
970 */
971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100972clear_wininfo(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973{
974 wininfo_T *wip;
975
976 while (buf->b_wininfo != NULL)
977 {
978 wip = buf->b_wininfo;
979 buf->b_wininfo = wip->wi_next;
980 if (wip->wi_optset)
981 {
982 clear_winopt(&wip->wi_opt);
983#ifdef FEAT_FOLDING
984 deleteFoldRecurse(&wip->wi_folds);
985#endif
986 }
987 vim_free(wip);
988 }
989}
990
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991/*
992 * Go to another buffer. Handles the result of the ATTENTION dialog.
993 */
994 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100995goto_buffer(
996 exarg_T *eap,
997 int start,
998 int dir,
999 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001001 bufref_T old_curbuf;
1002
1003 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004
1005 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
1007 start, dir, count, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
1009 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001010#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001011 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001012
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001013 /* Reset the error/interrupt/exception state here so that
1014 * aborting() returns FALSE when closing a window. */
1015 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001016#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001017
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001018 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 win_close(curwin, TRUE);
1020 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001021 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001022
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001023#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001024 /* Restore the error/interrupt/exception state if not discarded by a
1025 * new aborting error, interrupt, or uncaught exception. */
1026 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 }
1029 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001030 handle_swap_exists(&old_curbuf);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001031}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033/*
1034 * Handle the situation of swap_exists_action being set.
1035 * It is allowed for "old_curbuf" to be NULL or invalid.
1036 */
1037 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001038handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039{
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001040#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001041 cleanup_T cs;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001042#endif
1043#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001044 long old_tw = curbuf->b_p_tw;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001045#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001046 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001047
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 if (swap_exists_action == SEA_QUIT)
1049 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001050#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001051 /* Reset the error/interrupt/exception state here so that
1052 * aborting() returns FALSE when closing a buffer. */
1053 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001054#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001055
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 /* User selected Quit at ATTENTION prompt. Go back to previous
1057 * buffer. If that buffer is gone or the same as the current one,
1058 * open a new, empty buffer. */
1059 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001060 swap_exists_did_quit = TRUE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001061 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001062 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1063 || old_curbuf->br_buf == curbuf)
1064 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
1065 else
1066 buf = old_curbuf->br_buf;
1067 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001068 {
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001069 int old_msg_silent = msg_silent;
1070
1071 if (shortmess(SHM_FILEINFO))
1072 msg_silent = 1; // prevent fileinfo message
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001073 enter_buffer(buf);
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001074 // restore msg_silent, so that the command line will be shown
1075 msg_silent = old_msg_silent;
1076
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001077#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001078 if (old_tw != curbuf->b_p_tw)
1079 check_colorcolumn(curwin);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001080#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001083
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001084#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001085 /* Restore the error/interrupt/exception state if not discarded by a
1086 * new aborting error, interrupt, or uncaught exception. */
1087 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001088#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 }
1090 else if (swap_exists_action == SEA_RECOVER)
1091 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001092#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001093 /* Reset the error/interrupt/exception state here so that
1094 * aborting() returns FALSE when closing a buffer. */
1095 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001096#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001097
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 /* User selected Recover at ATTENTION prompt. */
1099 msg_scroll = TRUE;
Bram Moolenaar99499b12019-05-23 21:35:48 +02001100 ml_recover(FALSE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001101 msg_puts("\n"); /* don't overwrite the last message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001103 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001104
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001105#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001106 /* Restore the error/interrupt/exception state if not discarded by a
1107 * new aborting error, interrupt, or uncaught exception. */
1108 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 }
1111 swap_exists_action = SEA_NONE;
1112}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114/*
1115 * do_bufdel() - delete or unload buffer(s)
1116 *
1117 * addr_count == 0: ":bdel" - delete current buffer
1118 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1119 * buffer "end_bnr", then any other arguments.
1120 * addr_count == 2: ":N,N bdel" - delete buffers in range
1121 *
1122 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1123 * DOBUF_DEL (":bdel")
1124 *
1125 * Returns error message or NULL
1126 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001127 char *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001128do_bufdel(
1129 int command,
1130 char_u *arg, /* pointer to extra arguments */
1131 int addr_count,
1132 int start_bnr, /* first buffer number in a range */
1133 int end_bnr, /* buffer nr or last buffer nr in a range */
1134 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135{
1136 int do_current = 0; /* delete current buffer? */
1137 int deleted = 0; /* number of buffers deleted */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001138 char *errormsg = NULL; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 int bnr; /* buffer number */
1140 char_u *p;
1141
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 if (addr_count == 0)
1143 {
1144 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1145 }
1146 else
1147 {
1148 if (addr_count == 2)
1149 {
1150 if (*arg) /* both range and argument is not allowed */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001151 return _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 bnr = start_bnr;
1153 }
1154 else /* addr_count == 1 */
1155 bnr = end_bnr;
1156
1157 for ( ;!got_int; ui_breakcheck())
1158 {
1159 /*
1160 * delete the current buffer last, otherwise when the
1161 * current buffer is deleted, the next buffer becomes
1162 * the current one and will be loaded, which may then
1163 * also be deleted, etc.
1164 */
1165 if (bnr == curbuf->b_fnum)
1166 do_current = bnr;
1167 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
1168 forceit) == OK)
1169 ++deleted;
1170
1171 /*
1172 * find next buffer number to delete/unload
1173 */
1174 if (addr_count == 2)
1175 {
1176 if (++bnr > end_bnr)
1177 break;
1178 }
1179 else /* addr_count == 1 */
1180 {
1181 arg = skipwhite(arg);
1182 if (*arg == NUL)
1183 break;
1184 if (!VIM_ISDIGIT(*arg))
1185 {
1186 p = skiptowhite_esc(arg);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01001187 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE,
1188 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 if (bnr < 0) /* failed */
1190 break;
1191 arg = p;
1192 }
1193 else
1194 bnr = getdigits(&arg);
1195 }
1196 }
1197 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1198 FORWARD, do_current, forceit) == OK)
1199 ++deleted;
1200
1201 if (deleted == 0)
1202 {
1203 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001204 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001206 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00001208 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001209 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 }
1211 else if (deleted >= p_report)
1212 {
1213 if (command == DOBUF_UNLOAD)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001214 smsg(NGETTEXT("%d buffer unloaded",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001215 "%d buffers unloaded", deleted), deleted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 else if (command == DOBUF_DEL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001217 smsg(NGETTEXT("%d buffer deleted",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001218 "%d buffers deleted", deleted), deleted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001220 smsg(NGETTEXT("%d buffer wiped out",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001221 "%d buffers wiped out", deleted), deleted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 }
1223 }
1224
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225
1226 return errormsg;
1227}
1228
Bram Moolenaara02471e2014-01-10 16:43:14 +01001229/*
1230 * Make the current buffer empty.
1231 * Used when it is wiped out and it's the last buffer.
1232 */
1233 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001234empty_curbuf(
1235 int close_others,
1236 int forceit,
1237 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001238{
1239 int retval;
1240 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001241 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001242
1243 if (action == DOBUF_UNLOAD)
1244 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001245 emsg(_("E90: Cannot unload last buffer"));
Bram Moolenaara02471e2014-01-10 16:43:14 +01001246 return FAIL;
1247 }
1248
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001249 set_bufref(&bufref, buf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001250 if (close_others)
1251 /* Close any other windows on this buffer, then make it empty. */
Bram Moolenaara02471e2014-01-10 16:43:14 +01001252 close_windows(buf, TRUE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001253
1254 setpcmark();
1255 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1256 forceit ? ECMD_FORCEIT : 0, curwin);
1257
1258 /*
1259 * do_ecmd() may create a new buffer, then we have to delete
1260 * the old one. But do_ecmd() may have done that already, check
1261 * if the buffer still exists.
1262 */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001263 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001264 close_buffer(NULL, buf, action, FALSE);
1265 if (!close_others)
1266 need_fileinfo = FALSE;
1267 return retval;
1268}
Bram Moolenaar94f01952018-09-01 15:30:03 +02001269
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270/*
1271 * Implementation of the commands for the buffer list.
1272 *
1273 * action == DOBUF_GOTO go to specified buffer
1274 * action == DOBUF_SPLIT split window and go to specified buffer
1275 * action == DOBUF_UNLOAD unload specified buffer(s)
1276 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1277 * action == DOBUF_WIPE delete specified buffer(s) really
1278 *
1279 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1280 * start == DOBUF_FIRST go to "count" buffer from first buffer
1281 * start == DOBUF_LAST go to "count" buffer from last buffer
1282 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1283 *
1284 * Return FAIL or OK.
1285 */
1286 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001287do_buffer(
1288 int action,
1289 int start,
1290 int dir, /* FORWARD or BACKWARD */
1291 int count, /* buffer number or number of buffers */
1292 int forceit) /* TRUE for :...! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293{
1294 buf_T *buf;
1295 buf_T *bp;
1296 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1297 || action == DOBUF_WIPE);
1298
1299 switch (start)
1300 {
1301 case DOBUF_FIRST: buf = firstbuf; break;
1302 case DOBUF_LAST: buf = lastbuf; break;
1303 default: buf = curbuf; break;
1304 }
1305 if (start == DOBUF_MOD) /* find next modified buffer */
1306 {
1307 while (count-- > 0)
1308 {
1309 do
1310 {
1311 buf = buf->b_next;
1312 if (buf == NULL)
1313 buf = firstbuf;
1314 }
1315 while (buf != curbuf && !bufIsChanged(buf));
1316 }
1317 if (!bufIsChanged(buf))
1318 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001319 emsg(_("E84: No modified buffer found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 return FAIL;
1321 }
1322 }
1323 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
1324 {
1325 while (buf != NULL && buf->b_fnum != count)
1326 buf = buf->b_next;
1327 }
1328 else
1329 {
1330 bp = NULL;
1331 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1332 {
1333 /* remember the buffer where we start, we come back there when all
1334 * buffers are unlisted. */
1335 if (bp == NULL)
1336 bp = buf;
1337 if (dir == FORWARD)
1338 {
1339 buf = buf->b_next;
1340 if (buf == NULL)
1341 buf = firstbuf;
1342 }
1343 else
1344 {
1345 buf = buf->b_prev;
1346 if (buf == NULL)
1347 buf = lastbuf;
1348 }
1349 /* don't count unlisted buffers */
1350 if (unload || buf->b_p_bl)
1351 {
1352 --count;
1353 bp = NULL; /* use this buffer as new starting point */
1354 }
1355 if (bp == buf)
1356 {
1357 /* back where we started, didn't find anything. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001358 emsg(_("E85: There is no listed buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 return FAIL;
1360 }
1361 }
1362 }
1363
1364 if (buf == NULL) /* could not find it */
1365 {
1366 if (start == DOBUF_FIRST)
1367 {
1368 /* don't warn when deleting */
1369 if (!unload)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001370 semsg(_(e_nobufnr), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 }
1372 else if (dir == FORWARD)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001373 emsg(_("E87: Cannot go beyond last buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001375 emsg(_("E88: Cannot go before first buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 return FAIL;
1377 }
1378
1379#ifdef FEAT_GUI
1380 need_mouse_correct = TRUE;
1381#endif
1382
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 /*
1384 * delete buffer buf from memory and/or the list
1385 */
1386 if (unload)
1387 {
1388 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001389 bufref_T bufref;
1390
Bram Moolenaar94f01952018-09-01 15:30:03 +02001391 if (!can_unload_buffer(buf))
Bram Moolenaara997b452018-04-17 23:24:06 +02001392 return FAIL;
Bram Moolenaara997b452018-04-17 23:24:06 +02001393
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001394 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395
1396 /* When unloading or deleting a buffer that's already unloaded and
1397 * unlisted: fail silently. */
1398 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1399 return FAIL;
1400
1401 if (!forceit && bufIsChanged(buf))
1402 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001403#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 if ((p_confirm || cmdmod.confirm) && p_write)
1405 {
1406 dialog_changed(buf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001407 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 /* Autocommand deleted buffer, oops! It's not changed
1409 * now. */
1410 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001411 /* If it's still changed fail silently, the dialog already
1412 * mentioned why it fails. */
1413 if (bufIsChanged(buf))
1414 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001416 else
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001419 semsg(_("E89: No write since last change for buffer %d (add ! to override)"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 buf->b_fnum);
1421 return FAIL;
1422 }
1423 }
1424
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001425 /* When closing the current buffer stop Visual mode. */
Bram Moolenaar4930a762016-09-11 14:39:53 +02001426 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001427 end_visual_mode();
1428
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 /*
1430 * If deleting the last (listed) buffer, make it empty.
1431 * The last (listed) buffer cannot be unloaded.
1432 */
Bram Moolenaar29323592016-07-24 22:04:11 +02001433 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 if (bp->b_p_bl && bp != buf)
1435 break;
1436 if (bp == NULL && buf == curbuf)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001437 return empty_curbuf(TRUE, forceit, action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 /*
1440 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001441 * (unless it's the only window). Repeat this so long as we end up in
1442 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001444 while (buf == curbuf
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001445 && !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
Bram Moolenaar459ca562016-11-10 18:16:33 +01001446 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001447 {
1448 if (win_close(curwin, FALSE) == FAIL)
1449 break;
1450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451
1452 /*
1453 * If the buffer to be deleted is not the current one, delete it here.
1454 */
1455 if (buf != curbuf)
1456 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001457 close_windows(buf, FALSE);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001458 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows <= 0)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001459 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 return OK;
1461 }
1462
1463 /*
1464 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001465 * There should be another, otherwise it would have been handled
1466 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001467 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 * Then prefer the buffer we most recently visited.
1469 * Else try to find one that is loaded, after the current buffer,
1470 * then before the current buffer.
1471 * Finally use any buffer.
1472 */
1473 buf = NULL; /* selected buffer */
1474 bp = NULL; /* used when no loaded buffer found */
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001475 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1476 buf = au_new_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477#ifdef FEAT_JUMPLIST
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001478 else if (curwin->w_jumplistlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 {
1480 int jumpidx;
1481
1482 jumpidx = curwin->w_jumplistidx - 1;
1483 if (jumpidx < 0)
1484 jumpidx = curwin->w_jumplistlen - 1;
1485
1486 forward = jumpidx;
1487 while (jumpidx != curwin->w_jumplistidx)
1488 {
1489 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1490 if (buf != NULL)
1491 {
1492 if (buf == curbuf || !buf->b_p_bl)
1493 buf = NULL; /* skip current and unlisted bufs */
1494 else if (buf->b_ml.ml_mfp == NULL)
1495 {
1496 /* skip unloaded buf, but may keep it for later */
1497 if (bp == NULL)
1498 bp = buf;
1499 buf = NULL;
1500 }
1501 }
1502 if (buf != NULL) /* found a valid buffer: stop searching */
1503 break;
1504 /* advance to older entry in jump list */
1505 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1506 break;
1507 if (--jumpidx < 0)
1508 jumpidx = curwin->w_jumplistlen - 1;
1509 if (jumpidx == forward) /* List exhausted for sure */
1510 break;
1511 }
1512 }
1513#endif
1514
1515 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1516 {
1517 forward = TRUE;
1518 buf = curbuf->b_next;
1519 for (;;)
1520 {
1521 if (buf == NULL)
1522 {
1523 if (!forward) /* tried both directions */
1524 break;
1525 buf = curbuf->b_prev;
1526 forward = FALSE;
1527 continue;
1528 }
1529 /* in non-help buffer, try to skip help buffers, and vv */
1530 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1531 {
1532 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1533 break;
1534 if (bp == NULL) /* remember unloaded buf for later */
1535 bp = buf;
1536 }
1537 if (forward)
1538 buf = buf->b_next;
1539 else
1540 buf = buf->b_prev;
1541 }
1542 }
1543 if (buf == NULL) /* No loaded buffer, use unloaded one */
1544 buf = bp;
1545 if (buf == NULL) /* No loaded buffer, find listed one */
1546 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001547 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 if (buf->b_p_bl && buf != curbuf)
1549 break;
1550 }
1551 if (buf == NULL) /* Still no buffer, just take one */
1552 {
1553 if (curbuf->b_next != NULL)
1554 buf = curbuf->b_next;
1555 else
1556 buf = curbuf->b_prev;
1557 }
1558 }
1559
Bram Moolenaara02471e2014-01-10 16:43:14 +01001560 if (buf == NULL)
1561 {
1562 /* Autocommands must have wiped out all other buffers. Only option
1563 * now is to make the current buffer empty. */
1564 return empty_curbuf(FALSE, forceit, action);
1565 }
1566
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 /*
1568 * make buf current buffer
1569 */
1570 if (action == DOBUF_SPLIT) /* split window first */
1571 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001572 /* If 'switchbuf' contains "useopen": jump to first window containing
1573 * "buf" if one exists */
1574 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001575 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001576 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001577 * page containing "buf" if one exists */
1578 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 return OK;
1580 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 return FAIL;
1582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583
1584 /* go to current buffer - nothing to do */
1585 if (buf == curbuf)
1586 return OK;
1587
1588 /*
1589 * Check if the current buffer may be abandoned.
1590 */
1591 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1592 {
1593#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1594 if ((p_confirm || cmdmod.confirm) && p_write)
1595 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001596 bufref_T bufref;
1597
1598 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 dialog_changed(curbuf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001600 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 /* Autocommand deleted buffer, oops! */
1602 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 }
1604 if (bufIsChanged(curbuf))
1605#endif
1606 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001607 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 return FAIL;
1609 }
1610 }
1611
1612 /* Go to the other buffer. */
1613 set_curbuf(buf, action);
1614
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001616 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001618#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 if (aborting()) /* autocmds may abort script processing */
1620 return FAIL;
1621#endif
1622
1623 return OK;
1624}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625
1626/*
1627 * Set current buffer to "buf". Executes autocommands and closes current
1628 * buffer. "action" tells how to close the current buffer:
1629 * DOBUF_GOTO free or hide it
1630 * DOBUF_SPLIT nothing
1631 * DOBUF_UNLOAD unload it
1632 * DOBUF_DEL delete it
1633 * DOBUF_WIPE wipe it out
1634 */
1635 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001636set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637{
1638 buf_T *prevbuf;
1639 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1640 || action == DOBUF_WIPE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001641#ifdef FEAT_SYN_HL
1642 long old_tw = curbuf->b_p_tw;
1643#endif
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001644 bufref_T newbufref;
1645 bufref_T prevbufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646
1647 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001648 if (!cmdmod.keepalt)
1649 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001650 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 /* Don't restart Select mode after switching to another buffer. */
1653 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001655 /* close_windows() or apply_autocmds() may change curbuf and wipe out "buf"
1656 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 prevbuf = curbuf;
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001658 set_bufref(&prevbufref, prevbuf);
1659 set_bufref(&newbufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001661 /* Autocommands may delete the curren buffer and/or the buffer we wan to go
1662 * to. In those cases don't close the buffer. */
Bram Moolenaar82404332016-07-10 17:00:38 +02001663 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001664 || (bufref_valid(&prevbufref)
1665 && bufref_valid(&newbufref)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001666#ifdef FEAT_EVAL
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001667 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001669 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001671#ifdef FEAT_SYN_HL
1672 if (prevbuf == curwin->w_buffer)
1673 reset_synblock(curwin);
1674#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001676 close_windows(prevbuf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001677#if defined(FEAT_EVAL)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001678 if (bufref_valid(&prevbufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679#else
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001680 if (bufref_valid(&prevbufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001682 {
Bram Moolenaare25865a2012-07-06 16:22:02 +02001683 win_T *previouswin = curwin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001684 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001685 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1687 unload ? action : (action == DOBUF_GOTO
Bram Moolenaareb44a682017-08-03 22:44:55 +02001688 && !buf_hide(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001689 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaare25865a2012-07-06 16:22:02 +02001690 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaar9e931222012-06-20 11:55:01 +02001691 /* autocommands changed curwin, Grr! */
Bram Moolenaare25865a2012-07-06 16:22:02 +02001692 curwin = previouswin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 }
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001695 /* An autocommand may have deleted "buf", already entered it (e.g., when
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001696 * it did ":bunload") or aborted the script processing.
Bram Moolenaar9e931222012-06-20 11:55:01 +02001697 * If curwin->w_buffer is null, enter_buffer() will make it valid again */
1698 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001699#ifdef FEAT_EVAL
Bram Moolenaar4033c552017-09-16 20:54:51 +02001700 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001702 ) || curwin->w_buffer == NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001703 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001705#ifdef FEAT_SYN_HL
1706 if (old_tw != curbuf->b_p_tw)
1707 check_colorcolumn(curwin);
1708#endif
1709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710}
1711
1712/*
1713 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001714 * Old curbuf must have been abandoned already! This also means "curbuf" may
1715 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001718enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719{
1720 /* Copy buffer and window local option values. Not for a help buffer. */
1721 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1722 if (!buf->b_help)
1723 get_winopts(buf);
1724#ifdef FEAT_FOLDING
1725 else
1726 /* Remove all folds in the window. */
1727 clearFolding(curwin);
1728 foldUpdateAll(curwin); /* update folds (later). */
1729#endif
1730
1731 /* Get the buffer in the current window. */
1732 curwin->w_buffer = buf;
1733 curbuf = buf;
1734 ++curbuf->b_nwindows;
1735
1736#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001737 if (curwin->w_p_diff)
1738 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739#endif
1740
Bram Moolenaara971b822011-09-14 14:43:25 +02001741#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001742 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001743#endif
1744
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 /* Cursor on first line by default. */
1746 curwin->w_cursor.lnum = 1;
1747 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001750 curwin->w_topline_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001752 /* mark cursor position as being invalid */
1753 curwin->w_valid = 0;
1754
Bram Moolenaar9cea87c2018-09-21 16:59:45 +02001755 buflist_setfpos(curbuf, curwin, curbuf->b_last_cursor.lnum,
1756 curbuf->b_last_cursor.col, TRUE);
1757
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 /* Make sure the buffer is loaded. */
1759 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001760 {
1761 /* If there is no filetype, allow for detecting one. Esp. useful for
1762 * ":ball" used in a autocommand. If there already is a filetype we
1763 * might prefer to keep it. */
1764 if (*curbuf->b_p_ft == NUL)
1765 did_filetype = FALSE;
1766
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001767 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 else
1770 {
Bram Moolenaareda65222019-05-16 20:29:44 +02001771 if (!msg_silent && !shortmess(SHM_FILEINFO))
1772 need_fileinfo = TRUE; // display file info after redraw
1773
1774 // check if file changed
1775 (void)buf_check_timestamp(curbuf, FALSE);
1776
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001778#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001780#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1782 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 }
1784
1785 /* If autocommands did not change the cursor position, restore cursor lnum
1786 * and possibly cursor col. */
1787 if (curwin->w_cursor.lnum == 1 && inindent(0))
1788 buflist_getfpos();
1789
1790 check_arg_idx(curwin); /* check for valid arg_idx */
1791#ifdef FEAT_TITLE
1792 maketitle();
1793#endif
Bram Moolenaard4153d42008-11-15 15:06:17 +00001794 /* when autocmds didn't change it */
1795 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1797
Bram Moolenaar009b2592004-10-24 19:18:58 +00001798#ifdef FEAT_NETBEANS_INTG
1799 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001800 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001801#endif
1802
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001803 /* Change directories when the 'acd' option is set. */
Bram Moolenaar6f470022018-04-10 18:47:20 +02001804 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805
1806#ifdef FEAT_KEYMAP
1807 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001808 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001810#ifdef FEAT_SPELL
1811 /* May need to set the spell language. Can only do this after the buffer
1812 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001813 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1814 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001815#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001816#ifdef FEAT_VIMINFO
1817 curbuf->b_last_used = vim_time();
1818#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001819
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 redraw_later(NOT_VALID);
1821}
1822
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001823#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1824/*
1825 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001826 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001827 */
1828 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001829do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001830{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001831 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001832 && curbuf->b_ffname != NULL
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001833 && vim_chdirfile(curbuf->b_ffname, "auto") == OK)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001834 shorten_fnames(TRUE);
1835}
1836#endif
1837
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001838 void
1839no_write_message(void)
1840{
1841#ifdef FEAT_TERMINAL
1842 if (term_job_running(curbuf->b_term))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001843 emsg(_("E948: Job still running (add ! to end the job)"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001844 else
1845#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001846 emsg(_("E37: No write since last change (add ! to override)"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001847}
1848
1849 void
Bram Moolenaar7a760922018-02-19 23:10:02 +01001850no_write_message_nobang(buf_T *buf UNUSED)
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001851{
1852#ifdef FEAT_TERMINAL
Bram Moolenaar7a760922018-02-19 23:10:02 +01001853 if (term_job_running(buf->b_term))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001854 emsg(_("E948: Job still running"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001855 else
1856#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001857 emsg(_("E37: No write since last change"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001858}
1859
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860/*
1861 * functions for dealing with the buffer list
1862 */
1863
Bram Moolenaar480778b2016-07-14 22:09:39 +02001864static int top_file_num = 1; /* highest file number */
1865
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866/*
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001867 * Return TRUE if the current buffer is empty, unnamed, unmodified and used in
1868 * only one window. That means it can be re-used.
1869 */
1870 int
1871curbuf_reusable(void)
1872{
1873 return (curbuf != NULL
1874 && curbuf->b_ffname == NULL
1875 && curbuf->b_nwindows <= 1
1876 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY())
Bram Moolenaard85c3962019-04-07 14:19:14 +02001877#if defined(FEAT_QUICKFIX)
Bram Moolenaar39803d82019-04-07 12:04:51 +02001878 && !bt_quickfix(curbuf)
Bram Moolenaard85c3962019-04-07 14:19:14 +02001879#endif
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001880 && !curbufIsChanged());
1881}
1882
1883/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 * Add a file name to the buffer list. Return a pointer to the buffer.
1885 * If the same file name already exists return a pointer to that buffer.
1886 * If it does not exist, or if fname == NULL, a new entry is created.
1887 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1888 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1889 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001890 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001891 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
1892 * if the buffer already exists.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 * This is the ONLY way to create a new buffer.
1894 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001896buflist_new(
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001897 char_u *ffname_arg, // full path of fname or relative
1898 char_u *sfname_arg, // short fname or NULL
1899 linenr_T lnum, // preferred cursor line
1900 int flags) // BLN_ defines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001902 char_u *ffname = ffname_arg;
1903 char_u *sfname = sfname_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 buf_T *buf;
1905#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001906 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907#endif
1908
Bram Moolenaar480778b2016-07-14 22:09:39 +02001909 if (top_file_num == 1)
1910 hash_init(&buf_hashtab);
1911
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001912 fname_expand(curbuf, &ffname, &sfname); // will allocate ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913
1914 /*
1915 * If file name already exists in the list, update the entry.
1916 */
1917#ifdef UNIX
1918 /* On Unix we can use inode numbers when the file exists. Works better
1919 * for hard links. */
1920 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1921 st.st_dev = (dev_T)-1;
1922#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001923 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924#ifdef UNIX
1925 buflist_findname_stat(ffname, &st)
1926#else
1927 buflist_findname(ffname)
1928#endif
1929 ) != NULL)
1930 {
1931 vim_free(ffname);
1932 if (lnum != 0)
1933 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02001934
1935 if ((flags & BLN_NOOPT) == 0)
1936 /* copy the options now, if 'cpo' doesn't have 's' and not done
1937 * already */
1938 buf_copy_options(buf, 0);
1939
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1941 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001942 bufref_T bufref;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001943
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 buf->b_p_bl = TRUE;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001945 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001947 {
Bram Moolenaar82404332016-07-10 17:00:38 +02001948 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001949 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001950 return NULL;
1951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 }
1953 return buf;
1954 }
1955
1956 /*
1957 * If the current buffer has no name and no contents, use the current
1958 * buffer. Otherwise: Need to allocate a new buffer structure.
1959 *
1960 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001961 * (A spell file buffer is allocated in spell.c, but that's not a normal
1962 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 */
1964 buf = NULL;
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001965 if ((flags & BLN_CURBUF) && curbuf_reusable())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 {
1967 buf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 /* It's like this buffer is deleted. Watch out for autocommands that
1969 * change curbuf! If that happens, allocate a new buffer anyway. */
1970 if (curbuf->b_p_bl)
1971 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1972 if (buf == curbuf)
1973 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001974#ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001975 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 if (buf == curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 {
1980 /* Make sure 'bufhidden' and 'buftype' are empty */
1981 clear_string_option(&buf->b_p_bh);
1982 clear_string_option(&buf->b_p_bt);
1983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 }
1985 if (buf != curbuf || curbuf == NULL)
1986 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001987 buf = ALLOC_CLEAR_ONE(buf_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 if (buf == NULL)
1989 {
1990 vim_free(ffname);
1991 return NULL;
1992 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02001993#ifdef FEAT_EVAL
1994 /* init b: variables */
1995 buf->b_vars = dict_alloc();
1996 if (buf->b_vars == NULL)
1997 {
1998 vim_free(ffname);
1999 vim_free(buf);
2000 return NULL;
2001 }
2002 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
2003#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01002004 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 }
2006
2007 if (ffname != NULL)
2008 {
2009 buf->b_ffname = ffname;
2010 buf->b_sfname = vim_strsave(sfname);
2011 }
2012
2013 clear_wininfo(buf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002014 buf->b_wininfo = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015
2016 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
2017 || buf->b_wininfo == NULL)
2018 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002019 if (buf->b_sfname != buf->b_ffname)
2020 VIM_CLEAR(buf->b_sfname);
2021 else
2022 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01002023 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 if (buf != curbuf)
2025 free_buffer(buf);
2026 return NULL;
2027 }
2028
2029 if (buf == curbuf)
2030 {
2031 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002032 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 if (buf != curbuf) /* autocommands deleted the buffer! */
2034 return NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002035#if defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002036 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 return NULL;
2038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002040
2041 /* Init the options. */
2042 buf->b_p_initialized = FALSE;
2043 buf_copy_options(buf, BCO_ENTER);
2044
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045#ifdef FEAT_KEYMAP
2046 /* need to reload lmaps and set b:keymap_name */
2047 curbuf->b_kmap_state |= KEYMAP_INIT;
2048#endif
2049 }
2050 else
2051 {
2052 /*
2053 * put new buffer at the end of the buffer list
2054 */
2055 buf->b_next = NULL;
2056 if (firstbuf == NULL) /* buffer list is empty */
2057 {
2058 buf->b_prev = NULL;
2059 firstbuf = buf;
2060 }
2061 else /* append new buffer at end of list */
2062 {
2063 lastbuf->b_next = buf;
2064 buf->b_prev = lastbuf;
2065 }
2066 lastbuf = buf;
2067
2068 buf->b_fnum = top_file_num++;
2069 if (top_file_num < 0) /* wrap around (may cause duplicates) */
2070 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002071 emsg(_("W14: Warning: List of file names overflow"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 if (emsg_silent == 0)
2073 {
2074 out_flush();
2075 ui_delay(3000L, TRUE); /* make sure it is noticed */
2076 }
2077 top_file_num = 1;
2078 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002079 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080
2081 /*
2082 * Always copy the options from the current buffer.
2083 */
2084 buf_copy_options(buf, BCO_ALWAYS);
2085 }
2086
2087 buf->b_wininfo->wi_fpos.lnum = lnum;
2088 buf->b_wininfo->wi_win = curwin;
2089
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002090#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002091 hash_init(&buf->b_s.b_keywtab);
2092 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093#endif
2094
2095 buf->b_fname = buf->b_sfname;
2096#ifdef UNIX
2097 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002098 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 else
2100 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002101 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 buf->b_dev = st.st_dev;
2103 buf->b_ino = st.st_ino;
2104 }
2105#endif
2106 buf->b_u_synced = TRUE;
2107 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002108 if (flags & BLN_DUMMY)
2109 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 buf_clear_file(buf);
2111 clrallmarks(buf); /* clear marks */
2112 fmarks_check_names(buf); /* check file marks for this file */
2113 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 if (!(flags & BLN_DUMMY))
2115 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002116 bufref_T bufref;
2117
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01002118 /* Tricky: these autocommands may change the buffer list. They could
2119 * also split the window with re-using the one empty buffer. This may
2120 * result in unexpectedly losing the empty buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002121 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002122 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002123 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002124 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002126 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002127 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002128 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002129 return NULL;
2130 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002131#ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002132 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136
2137 return buf;
2138}
2139
2140/*
2141 * Free the memory for the options of a buffer.
2142 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2143 * 'fileencoding'.
2144 */
2145 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002146free_buf_options(
2147 buf_T *buf,
2148 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149{
2150 if (free_p_ff)
2151 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 clear_string_option(&buf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 clear_string_option(&buf->b_p_bh);
2155 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 }
2157#ifdef FEAT_FIND_ID
2158 clear_string_option(&buf->b_p_def);
2159 clear_string_option(&buf->b_p_inc);
2160# ifdef FEAT_EVAL
2161 clear_string_option(&buf->b_p_inex);
2162# endif
2163#endif
2164#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
2165 clear_string_option(&buf->b_p_inde);
2166 clear_string_option(&buf->b_p_indk);
2167#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002168#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2169 clear_string_option(&buf->b_p_bexpr);
2170#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002171#if defined(FEAT_CRYPT)
2172 clear_string_option(&buf->b_p_cm);
2173#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002174 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002175#if defined(FEAT_EVAL)
2176 clear_string_option(&buf->b_p_fex);
2177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178#ifdef FEAT_CRYPT
2179 clear_string_option(&buf->b_p_key);
2180#endif
2181 clear_string_option(&buf->b_p_kp);
2182 clear_string_option(&buf->b_p_mps);
2183 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002184 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 clear_string_option(&buf->b_p_isk);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002186#ifdef FEAT_VARTABS
2187 clear_string_option(&buf->b_p_vsts);
Bram Moolenaarbdace832019-03-02 10:13:42 +01002188 vim_free(buf->b_p_vsts_nopaste);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002189 buf->b_p_vsts_nopaste = NULL;
Bram Moolenaarbdace832019-03-02 10:13:42 +01002190 vim_free(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002191 buf->b_p_vsts_array = NULL;
2192 clear_string_option(&buf->b_p_vts);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002193 VIM_CLEAR(buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195#ifdef FEAT_KEYMAP
2196 clear_string_option(&buf->b_p_keymap);
Bram Moolenaar50138322018-01-28 17:05:16 +01002197 keymap_clear(&buf->b_kmap_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 ga_clear(&buf->b_kmap_ga);
2199#endif
2200#ifdef FEAT_COMMENTS
2201 clear_string_option(&buf->b_p_com);
2202#endif
2203#ifdef FEAT_FOLDING
2204 clear_string_option(&buf->b_p_cms);
2205#endif
2206 clear_string_option(&buf->b_p_nf);
2207#ifdef FEAT_SYN_HL
2208 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002209 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002210#endif
2211#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002212 clear_string_option(&buf->b_s.b_p_spc);
2213 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002214 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002215 buf->b_s.b_cap_prog = NULL;
2216 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217#endif
2218#ifdef FEAT_SEARCHPATH
2219 clear_string_option(&buf->b_p_sua);
2220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 clear_string_option(&buf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222#ifdef FEAT_CINDENT
2223 clear_string_option(&buf->b_p_cink);
2224 clear_string_option(&buf->b_p_cino);
2225#endif
2226#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
2227 clear_string_option(&buf->b_p_cinw);
2228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 clear_string_option(&buf->b_p_cpt);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002230#ifdef FEAT_COMPL_FUNC
2231 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002232 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002233#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234#ifdef FEAT_QUICKFIX
2235 clear_string_option(&buf->b_p_gp);
2236 clear_string_option(&buf->b_p_mp);
2237 clear_string_option(&buf->b_p_efm);
2238#endif
2239 clear_string_option(&buf->b_p_ep);
2240 clear_string_option(&buf->b_p_path);
2241 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002242 clear_string_option(&buf->b_p_tc);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002243#ifdef FEAT_EVAL
2244 clear_string_option(&buf->b_p_tfu);
2245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 clear_string_option(&buf->b_p_dict);
2247 clear_string_option(&buf->b_p_tsr);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002248#ifdef FEAT_TEXTOBJ
2249 clear_string_option(&buf->b_p_qe);
2250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002252 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002253#ifdef FEAT_LISP
2254 clear_string_option(&buf->b_p_lw);
2255#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002256 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002257 clear_string_option(&buf->b_p_menc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258}
2259
2260/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002261 * Get alternate file "n".
2262 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2263 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 * if (options & GETF_SETMARK) call setpcmark()
2265 * if (options & GETF_ALT) we are jumping to an alternate file.
2266 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2267 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002268 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 */
2270 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002271buflist_getfile(
2272 int n,
2273 linenr_T lnum,
2274 int options,
2275 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276{
2277 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 pos_T *fpos;
2280 colnr_T col;
2281
2282 buf = buflist_findnr(n);
2283 if (buf == NULL)
2284 {
2285 if ((options & GETF_ALT) && n == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002286 emsg(_(e_noalt));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 else
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01002288 semsg(_("E92: Buffer %d not found"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 return FAIL;
2290 }
2291
2292 /* if alternate file is the current buffer, nothing to do */
2293 if (buf == curbuf)
2294 return OK;
2295
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002296 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002297 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002298 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002300 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002301 if (curbuf_locked())
2302 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303
2304 /* altfpos may be changed by getfile(), get it now */
2305 if (lnum == 0)
2306 {
2307 fpos = buflist_findfpos(buf);
2308 lnum = fpos->lnum;
2309 col = fpos->col;
2310 }
2311 else
2312 col = 0;
2313
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 if (options & GETF_SWITCH)
2315 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002316 /* If 'switchbuf' contains "useopen": jump to first window containing
2317 * "buf" if one exists */
2318 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002320
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002321 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00002322 * page containing "buf" if one exists */
2323 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002324 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002325
2326 /* If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2327 * current buffer isn't empty: open new tab or window */
2328 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002329 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002331 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002332 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002333 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2334 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002336 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 }
2338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339
2340 ++RedrawingDisabled;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002341 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2342 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 {
2344 --RedrawingDisabled;
2345
2346 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
2347 if (!p_sol && col != 0)
2348 {
2349 curwin->w_cursor.col = col;
2350 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 curwin->w_set_curswant = TRUE;
2353 }
2354 return OK;
2355 }
2356 --RedrawingDisabled;
2357 return FAIL;
2358}
2359
2360/*
2361 * go to the last know line number for the current buffer
2362 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002363 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002364buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365{
2366 pos_T *fpos;
2367
2368 fpos = buflist_findfpos(curbuf);
2369
2370 curwin->w_cursor.lnum = fpos->lnum;
2371 check_cursor_lnum();
2372
2373 if (p_sol)
2374 curwin->w_cursor.col = 0;
2375 else
2376 {
2377 curwin->w_cursor.col = fpos->col;
2378 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 curwin->w_set_curswant = TRUE;
2381 }
2382}
2383
Bram Moolenaar81695252004-12-29 20:58:21 +00002384#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2385/*
2386 * Find file in buffer list by name (it has to be for the current window).
2387 * Returns NULL if not found.
2388 */
2389 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002390buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002391{
2392 char_u *ffname;
2393 buf_T *buf = NULL;
2394
2395 /* First make the name into a full path name */
2396 ffname = FullName_save(fname,
2397#ifdef UNIX
2398 TRUE /* force expansion, get rid of symbolic links */
2399#else
2400 FALSE
2401#endif
2402 );
2403 if (ffname != NULL)
2404 {
2405 buf = buflist_findname(ffname);
2406 vim_free(ffname);
2407 }
2408 return buf;
2409}
2410#endif
2411
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412/*
2413 * Find file in buffer list by name (it has to be for the current window).
2414 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002415 * Skips dummy buffers.
2416 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 */
2418 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002419buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420{
2421#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002422 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423
2424 if (mch_stat((char *)ffname, &st) < 0)
2425 st.st_dev = (dev_T)-1;
2426 return buflist_findname_stat(ffname, &st);
2427}
2428
2429/*
2430 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2431 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002432 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 */
2434 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002435buflist_findname_stat(
2436 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002437 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438{
2439#endif
2440 buf_T *buf;
2441
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002442 /* Start at the last buffer, expect to find a match sooner. */
2443 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar81695252004-12-29 20:58:21 +00002444 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445#ifdef UNIX
2446 , stp
2447#endif
2448 ))
2449 return buf;
2450 return NULL;
2451}
2452
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453/*
2454 * Find file in buffer list by a regexp pattern.
2455 * Return fnum of the found buffer.
2456 * Return < 0 for error.
2457 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002459buflist_findpat(
2460 char_u *pattern,
2461 char_u *pattern_end, /* pointer to first char after pattern */
2462 int unlisted, /* find unlisted buffers */
2463 int diffmode UNUSED, /* find diff-mode buffers only */
2464 int curtab_only) /* find buffers in current tab only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465{
2466 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 int match = -1;
2468 int find_listed;
2469 char_u *pat;
2470 char_u *patend;
2471 int attempt;
2472 char_u *p;
2473 int toggledollar;
2474
2475 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2476 {
2477 if (*pattern == '%')
2478 match = curbuf->b_fnum;
2479 else
2480 match = curwin->w_alt_fnum;
2481#ifdef FEAT_DIFF
2482 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2483 match = -1;
2484#endif
2485 }
2486
2487 /*
2488 * Try four ways of matching a listed buffer:
2489 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002490 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 * attempt == 2: with '$' at end (only match at end)
2492 * attempt == 3: with '^' at start and '$' at end (only full match)
2493 * Repeat this for finding an unlisted buffer if there was no matching
2494 * listed buffer.
2495 */
2496 else
2497 {
2498 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2499 if (pat == NULL)
2500 return -1;
2501 patend = pat + STRLEN(pat) - 1;
2502 toggledollar = (patend > pat && *patend == '$');
2503
2504 /* First try finding a listed buffer. If not found and "unlisted"
2505 * is TRUE, try finding an unlisted buffer. */
2506 find_listed = TRUE;
2507 for (;;)
2508 {
2509 for (attempt = 0; attempt <= 3; ++attempt)
2510 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002511 regmatch_T regmatch;
2512
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 /* may add '^' and '$' */
2514 if (toggledollar)
2515 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2516 p = pat;
2517 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2518 ++p;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002519 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2520 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 {
2522 vim_free(pat);
2523 return -1;
2524 }
2525
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002526 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 if (buf->b_p_bl == find_listed
2528#ifdef FEAT_DIFF
2529 && (!diffmode || diff_mode_buf(buf))
2530#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002531 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002533 if (curtab_only)
2534 {
2535 /* Ignore the match if the buffer is not open in
2536 * the current tab. */
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002537 win_T *wp;
2538
Bram Moolenaar29323592016-07-24 22:04:11 +02002539 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002540 if (wp->w_buffer == buf)
2541 break;
2542 if (wp == NULL)
2543 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 if (match >= 0) /* already found a match */
2546 {
2547 match = -2;
2548 break;
2549 }
2550 match = buf->b_fnum; /* remember first match */
2551 }
2552
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002553 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 if (match >= 0) /* found one match */
2555 break;
2556 }
2557
2558 /* Only search for unlisted buffers if there was no match with
2559 * a listed buffer. */
2560 if (!unlisted || !find_listed || match != -1)
2561 break;
2562 find_listed = FALSE;
2563 }
2564
2565 vim_free(pat);
2566 }
2567
2568 if (match == -2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002569 semsg(_("E93: More than one match for %s"), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 else if (match < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002571 semsg(_("E94: No matching buffer for %s"), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 return match;
2573}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575/*
2576 * Find all buffer names that match.
2577 * For command line expansion of ":buf" and ":sbuf".
2578 * Return OK if matches found, FAIL otherwise.
2579 */
2580 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002581ExpandBufnames(
2582 char_u *pat,
2583 int *num_file,
2584 char_u ***file,
2585 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586{
2587 int count = 0;
2588 buf_T *buf;
2589 int round;
2590 char_u *p;
2591 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002592 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593
2594 *num_file = 0; /* return values in case of FAIL */
2595 *file = NULL;
2596
Bram Moolenaar05159a02005-02-26 23:04:13 +00002597 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2598 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002600 patc = alloc(STRLEN(pat) + 11);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002601 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002603 STRCPY(patc, "\\(^\\|[\\/]\\)");
2604 STRCPY(patc + 11, pat + 1);
2605 }
2606 else
2607 patc = pat;
2608
2609 /*
2610 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002611 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002612 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002613 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002614 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002615 regmatch_T regmatch;
2616
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002617 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002618 break; /* there was no anchor, no need to try again */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002619 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2620 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002621 {
2622 if (patc != pat)
2623 vim_free(patc);
2624 return FAIL;
2625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626
2627 /*
2628 * round == 1: Count the matches.
2629 * round == 2: Build the array to keep the matches.
2630 */
2631 for (round = 1; round <= 2; ++round)
2632 {
2633 count = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002634 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 {
2636 if (!buf->b_p_bl) /* skip unlisted buffers */
2637 continue;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002638 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 if (p != NULL)
2640 {
2641 if (round == 1)
2642 ++count;
2643 else
2644 {
2645 if (options & WILD_HOME_REPLACE)
2646 p = home_replace_save(buf, p);
2647 else
2648 p = vim_strsave(p);
2649 (*file)[count++] = p;
2650 }
2651 }
2652 }
2653 if (count == 0) /* no match found, break here */
2654 break;
2655 if (round == 1)
2656 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002657 *file = ALLOC_MULT(char_u *, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 if (*file == NULL)
2659 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002660 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002661 if (patc != pat)
2662 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 return FAIL;
2664 }
2665 }
2666 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002667 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 if (count) /* match(es) found, break here */
2669 break;
2670 }
2671
Bram Moolenaar05159a02005-02-26 23:04:13 +00002672 if (patc != pat)
2673 vim_free(patc);
2674
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 *num_file = count;
2676 return (count == 0 ? FAIL : OK);
2677}
2678
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679/*
2680 * Check for a match on the file name for buffer "buf" with regprog "prog".
2681 */
2682 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002683buflist_match(
2684 regmatch_T *rmp,
2685 buf_T *buf,
2686 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687{
2688 char_u *match;
2689
2690 /* First try the short file name, then the long file name. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002691 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002693 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694
2695 return match;
2696}
2697
2698/*
2699 * Try matching the regexp in "prog" with file name "name".
2700 * Return "name" when there is a match, NULL when not.
2701 */
2702 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002703fname_match(
2704 regmatch_T *rmp,
2705 char_u *name,
2706 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707{
2708 char_u *match = NULL;
2709 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710
2711 if (name != NULL)
2712 {
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002713 /* Ignore case when 'fileignorecase' or the argument is set. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002714 rmp->rm_ic = p_fic || ignore_case;
2715 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 match = name;
2717 else
2718 {
2719 /* Replace $(HOME) with '~' and try matching again. */
2720 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002721 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 match = name;
2723 vim_free(p);
2724 }
2725 }
2726
2727 return match;
2728}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729
2730/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02002731 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 */
2733 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002734buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735{
Bram Moolenaar480778b2016-07-14 22:09:39 +02002736 char_u key[VIM_SIZEOF_INT * 2 + 1];
2737 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738
2739 if (nr == 0)
2740 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02002741 sprintf((char *)key, "%x", nr);
2742 hi = hash_find(&buf_hashtab, key);
2743
2744 if (!HASHITEM_EMPTY(hi))
2745 return (buf_T *)(hi->hi_key
2746 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 return NULL;
2748}
2749
2750/*
2751 * Get name of file 'n' in the buffer list.
2752 * When the file has no name an empty string is returned.
2753 * home_replace() is used to shorten the file name (used for marks).
2754 * Returns a pointer to allocated memory, of NULL when failed.
2755 */
2756 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002757buflist_nr2name(
2758 int n,
2759 int fullname,
2760 int helptail) /* for help buffers return tail only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761{
2762 buf_T *buf;
2763
2764 buf = buflist_findnr(n);
2765 if (buf == NULL)
2766 return NULL;
2767 return home_replace_save(helptail ? buf : NULL,
2768 fullname ? buf->b_ffname : buf->b_fname);
2769}
2770
2771/*
2772 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2773 * When "copy_options" is TRUE save the local window option values.
2774 * When "lnum" is 0 only do the options.
2775 */
Bram Moolenaardefa0672019-07-21 19:25:37 +02002776 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002777buflist_setfpos(
2778 buf_T *buf,
2779 win_T *win,
2780 linenr_T lnum,
2781 colnr_T col,
2782 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783{
2784 wininfo_T *wip;
2785
2786 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2787 if (wip->wi_win == win)
2788 break;
2789 if (wip == NULL)
2790 {
2791 /* allocate a new entry */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002792 wip = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 if (wip == NULL)
2794 return;
2795 wip->wi_win = win;
2796 if (lnum == 0) /* set lnum even when it's 0 */
2797 lnum = 1;
2798 }
2799 else
2800 {
2801 /* remove the entry from the list */
2802 if (wip->wi_prev)
2803 wip->wi_prev->wi_next = wip->wi_next;
2804 else
2805 buf->b_wininfo = wip->wi_next;
2806 if (wip->wi_next)
2807 wip->wi_next->wi_prev = wip->wi_prev;
2808 if (copy_options && wip->wi_optset)
2809 {
2810 clear_winopt(&wip->wi_opt);
2811#ifdef FEAT_FOLDING
2812 deleteFoldRecurse(&wip->wi_folds);
2813#endif
2814 }
2815 }
2816 if (lnum != 0)
2817 {
2818 wip->wi_fpos.lnum = lnum;
2819 wip->wi_fpos.col = col;
2820 }
2821 if (copy_options)
2822 {
2823 /* Save the window-specific option values. */
2824 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2825#ifdef FEAT_FOLDING
2826 wip->wi_fold_manual = win->w_fold_manual;
2827 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2828#endif
2829 wip->wi_optset = TRUE;
2830 }
2831
2832 /* insert the entry in front of the list */
2833 wip->wi_next = buf->b_wininfo;
2834 buf->b_wininfo = wip;
2835 wip->wi_prev = NULL;
2836 if (wip->wi_next)
2837 wip->wi_next->wi_prev = wip;
2838
2839 return;
2840}
2841
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002842#ifdef FEAT_DIFF
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002843/*
2844 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2845 * page. That's because a diff is local to a tab page.
2846 */
2847 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002848wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002849{
2850 win_T *wp;
2851
2852 if (wip->wi_opt.wo_diff)
2853 {
Bram Moolenaar29323592016-07-24 22:04:11 +02002854 FOR_ALL_WINDOWS(wp)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002855 /* return FALSE when it's a window in the current tab page, thus
2856 * the buffer was in diff mode here */
2857 if (wip->wi_win == wp)
2858 return FALSE;
2859 return TRUE;
2860 }
2861 return FALSE;
2862}
2863#endif
2864
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865/*
2866 * Find info for the current window in buffer "buf".
2867 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002868 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2869 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 * Returns NULL when there isn't any info.
2871 */
2872 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002873find_wininfo(
2874 buf_T *buf,
2875 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876{
2877 wininfo_T *wip;
2878
2879 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002880 if (wip->wi_win == curwin
2881#ifdef FEAT_DIFF
2882 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2883#endif
2884 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002886
2887 /* If no wininfo for curwin, use the first in the list (that doesn't have
2888 * 'diff' set and is in another tab page). */
2889 if (wip == NULL)
2890 {
2891#ifdef FEAT_DIFF
2892 if (skip_diff_buffer)
2893 {
2894 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2895 if (!wininfo_other_tab_diff(wip))
2896 break;
2897 }
2898 else
2899#endif
2900 wip = buf->b_wininfo;
2901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 return wip;
2903}
2904
2905/*
2906 * Reset the local window options to the values last used in this window.
2907 * If the buffer wasn't used in this window before, use the values from
2908 * the most recently used window. If the values were never set, use the
2909 * global values for the window.
2910 */
2911 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002912get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913{
2914 wininfo_T *wip;
2915
2916 clear_winopt(&curwin->w_onebuf_opt);
2917#ifdef FEAT_FOLDING
2918 clearFolding(curwin);
2919#endif
2920
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002921 wip = find_wininfo(buf, TRUE);
Bram Moolenaar25782a72018-05-13 18:05:33 +02002922 if (wip != NULL && wip->wi_win != NULL
2923 && wip->wi_win != curwin && wip->wi_win->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 {
Bram Moolenaar25782a72018-05-13 18:05:33 +02002925 /* The buffer is currently displayed in the window: use the actual
2926 * option values instead of the saved (possibly outdated) values. */
2927 win_T *wp = wip->wi_win;
2928
2929 copy_winopt(&wp->w_onebuf_opt, &curwin->w_onebuf_opt);
2930#ifdef FEAT_FOLDING
2931 curwin->w_fold_manual = wp->w_fold_manual;
2932 curwin->w_foldinvalid = TRUE;
2933 cloneFoldGrowArray(&wp->w_folds, &curwin->w_folds);
2934#endif
2935 }
2936 else if (wip != NULL && wip->wi_optset)
2937 {
2938 /* the buffer was displayed in the current window earlier */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2940#ifdef FEAT_FOLDING
2941 curwin->w_fold_manual = wip->wi_fold_manual;
2942 curwin->w_foldinvalid = TRUE;
2943 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2944#endif
2945 }
2946 else
2947 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2948
2949#ifdef FEAT_FOLDING
2950 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2951 if (p_fdls >= 0)
2952 curwin->w_p_fdl = p_fdls;
2953#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002954#ifdef FEAT_SYN_HL
2955 check_colorcolumn(curwin);
2956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957}
2958
2959/*
2960 * Find the position (lnum and col) for the buffer 'buf' for the current
2961 * window.
2962 * Returns a pointer to no_position if no position is found.
2963 */
2964 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002965buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966{
2967 wininfo_T *wip;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002968 static pos_T no_position = {1, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002970 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 if (wip != NULL)
2972 return &(wip->wi_fpos);
2973 else
2974 return &no_position;
2975}
2976
2977/*
2978 * Find the lnum for the buffer 'buf' for the current window.
2979 */
2980 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01002981buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982{
2983 return buflist_findfpos(buf)->lnum;
2984}
2985
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002987 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002990buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991{
2992 buf_T *buf;
2993 int len;
2994 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02002995 int ro_char;
2996 int changed_char;
Bram Moolenaar0751f512018-03-29 16:37:16 +02002997#ifdef FEAT_TERMINAL
2998 int job_running;
2999 int job_none_open;
3000#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001
3002 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
3003 {
Bram Moolenaar0751f512018-03-29 16:37:16 +02003004#ifdef FEAT_TERMINAL
3005 job_running = term_job_running(buf->b_term);
3006 job_none_open = job_running && term_none_open(buf->b_term);
3007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 /* skip unlisted buffers, unless ! was used */
Bram Moolenaard51cb702015-07-21 15:03:06 +02003009 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
3010 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
3011 || (vim_strchr(eap->arg, '+')
3012 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
3013 || (vim_strchr(eap->arg, 'a')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003014 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
Bram Moolenaard51cb702015-07-21 15:03:06 +02003015 || (vim_strchr(eap->arg, 'h')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003016 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3017#ifdef FEAT_TERMINAL
3018 || (vim_strchr(eap->arg, 'R')
3019 && (!job_running || (job_running && job_none_open)))
3020 || (vim_strchr(eap->arg, '?')
3021 && (!job_running || (job_running && !job_none_open)))
3022 || (vim_strchr(eap->arg, 'F')
3023 && (job_running || buf->b_term == NULL))
3024#endif
Bram Moolenaard51cb702015-07-21 15:03:06 +02003025 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3026 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3027 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3028 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3029 || (vim_strchr(eap->arg, '#')
3030 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003033 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 else
3035 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003036 if (message_filtered(NameBuff))
3037 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003039 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3040 : (bufIsChanged(buf) ? '+' : ' ');
3041#ifdef FEAT_TERMINAL
3042 if (term_job_running(buf->b_term))
3043 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02003044 if (term_none_open(buf->b_term))
3045 ro_char = '?';
3046 else
3047 ro_char = 'R';
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003048 changed_char = ' '; /* bufIsChanged() returns TRUE to avoid
3049 * closing, but it's not actually changed. */
3050 }
3051 else if (buf->b_term != NULL)
3052 ro_char = 'F';
3053 else
3054#endif
3055 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3056
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003057 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00003058 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 buf->b_fnum,
3060 buf->b_p_bl ? ' ' : 'u',
3061 buf == curbuf ? '%' :
3062 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3063 buf->b_ml.ml_mfp == NULL ? ' ' :
3064 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003065 ro_char,
3066 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003067 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003068 if (len > IOSIZE - 20)
3069 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070
3071 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 i = 40 - vim_strsize(IObuff);
3073 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 IObuff[len++] = ' ';
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003075 while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003076 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3077 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003078 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 msg_outtrans(IObuff);
3080 out_flush(); /* output one line at a time */
3081 ui_breakcheck();
3082 }
3083}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084
3085/*
3086 * Get file name and line number for file 'fnum'.
3087 * Used by DoOneCmd() for translating '%' and '#'.
3088 * Used by insert_reg() and cmdline_paste() for '#' register.
3089 * Return FAIL if not found, OK for success.
3090 */
3091 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003092buflist_name_nr(
3093 int fnum,
3094 char_u **fname,
3095 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096{
3097 buf_T *buf;
3098
3099 buf = buflist_findnr(fnum);
3100 if (buf == NULL || buf->b_fname == NULL)
3101 return FAIL;
3102
3103 *fname = buf->b_fname;
3104 *lnum = buflist_findlnum(buf);
3105
3106 return OK;
3107}
3108
3109/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003110 * Set the file name for "buf"' to "ffname_arg", short file name to
3111 * "sfname_arg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 * The file name with the full path is also remembered, for when :cd is used.
3113 * Returns FAIL for failure (file name already in use by other buffer)
3114 * OK otherwise.
3115 */
3116 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003117setfname(
3118 buf_T *buf,
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003119 char_u *ffname_arg,
3120 char_u *sfname_arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +01003121 int message) /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003123 char_u *ffname = ffname_arg;
3124 char_u *sfname = sfname_arg;
Bram Moolenaar81695252004-12-29 20:58:21 +00003125 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003127 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128#endif
3129
3130 if (ffname == NULL || *ffname == NUL)
3131 {
3132 /* Removing the name. */
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003133 if (buf->b_sfname != buf->b_ffname)
3134 VIM_CLEAR(buf->b_sfname);
3135 else
3136 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003137 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138#ifdef UNIX
3139 st.st_dev = (dev_T)-1;
3140#endif
3141 }
3142 else
3143 {
3144 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
3145 if (ffname == NULL) /* out of memory */
3146 return FAIL;
3147
3148 /*
3149 * if the file name is already used in another buffer:
3150 * - if the buffer is loaded, fail
3151 * - if the buffer is not loaded, delete it from the list
3152 */
3153#ifdef UNIX
3154 if (mch_stat((char *)ffname, &st) < 0)
3155 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003156#endif
3157 if (!(buf->b_flags & BF_DUMMY))
3158#ifdef UNIX
3159 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003161 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162#endif
3163 if (obuf != NULL && obuf != buf)
3164 {
3165 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
3166 {
3167 if (message)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003168 emsg(_("E95: Buffer with this name already exists"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 vim_free(ffname);
3170 return FAIL;
3171 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01003172 /* delete from the list */
3173 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 }
3175 sfname = vim_strsave(sfname);
3176 if (ffname == NULL || sfname == NULL)
3177 {
3178 vim_free(sfname);
3179 vim_free(ffname);
3180 return FAIL;
3181 }
3182#ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003183 fname_case(sfname, 0); /* set correct case for short file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184#endif
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003185 if (buf->b_sfname != buf->b_ffname)
3186 vim_free(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 vim_free(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 buf->b_ffname = ffname;
3189 buf->b_sfname = sfname;
3190 }
3191 buf->b_fname = buf->b_sfname;
3192#ifdef UNIX
3193 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003194 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 else
3196 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003197 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 buf->b_dev = st.st_dev;
3199 buf->b_ino = st.st_ino;
3200 }
3201#endif
3202
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204
3205 buf_name_changed(buf);
3206 return OK;
3207}
3208
3209/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003210 * Crude way of changing the name of a buffer. Use with care!
3211 * The name should be relative to the current directory.
3212 */
3213 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003214buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003215{
3216 buf_T *buf;
3217
3218 buf = buflist_findnr(fnum);
3219 if (buf != NULL)
3220 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003221 if (buf->b_sfname != buf->b_ffname)
3222 vim_free(buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003223 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003224 buf->b_ffname = vim_strsave(name);
3225 buf->b_sfname = NULL;
3226 /* Allocate ffname and expand into full path. Also resolves .lnk
3227 * files on Win32. */
3228 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003229 buf->b_fname = buf->b_sfname;
3230 }
3231}
3232
3233/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 * Take care of what needs to be done when the name of buffer "buf" has
3235 * changed.
3236 */
3237 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003238buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239{
3240 /*
3241 * If the file name changed, also change the name of the swapfile
3242 */
3243 if (buf->b_ml.ml_mfp != NULL)
3244 ml_setname(buf);
3245
3246 if (curwin->w_buffer == buf)
3247 check_arg_idx(curwin); /* check file name for arg list */
3248#ifdef FEAT_TITLE
3249 maketitle(); /* set window title */
3250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 status_redraw_all(); /* status lines need to be redrawn */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 fmarks_check_names(buf); /* check named file marks */
3253 ml_timestamp(buf); /* reset timestamp */
3254}
3255
3256/*
3257 * set alternate file name for current window
3258 *
3259 * Used by do_one_cmd(), do_write() and do_ecmd().
3260 * Return the buffer.
3261 */
3262 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003263setaltfname(
3264 char_u *ffname,
3265 char_u *sfname,
3266 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267{
3268 buf_T *buf;
3269
3270 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
3271 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003272 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 curwin->w_alt_fnum = buf->b_fnum;
3274 return buf;
3275}
3276
3277/*
3278 * Get alternate file name for current window.
3279 * Return NULL if there isn't any, and give error message if requested.
3280 */
3281 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003282getaltfname(
3283 int errmsg) /* give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284{
3285 char_u *fname;
3286 linenr_T dummy;
3287
3288 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3289 {
3290 if (errmsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003291 emsg(_(e_noalt));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 return NULL;
3293 }
3294 return fname;
3295}
3296
3297/*
3298 * Add a file name to the buflist and return its number.
3299 * Uses same flags as buflist_new(), except BLN_DUMMY.
3300 *
3301 * used by qf_init(), main() and doarglist()
3302 */
3303 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003304buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305{
3306 buf_T *buf;
3307
3308 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3309 if (buf != NULL)
3310 return buf->b_fnum;
3311 return 0;
3312}
3313
3314#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3315/*
3316 * Adjust slashes in file names. Called after 'shellslash' was set.
3317 */
3318 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003319buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320{
3321 buf_T *bp;
3322
Bram Moolenaar29323592016-07-24 22:04:11 +02003323 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 {
3325 if (bp->b_ffname != NULL)
3326 slash_adjust(bp->b_ffname);
3327 if (bp->b_sfname != NULL)
3328 slash_adjust(bp->b_sfname);
3329 }
3330}
3331#endif
3332
3333/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003334 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 * Also save the local window option values.
3336 */
3337 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003338buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003340 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341}
3342
3343/*
3344 * Return TRUE if 'ffname' is not the same file as current file.
3345 * Fname must have a full path (expanded by mch_FullName()).
3346 */
3347 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003348otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349{
3350 return otherfile_buf(curbuf, ffname
3351#ifdef UNIX
3352 , NULL
3353#endif
3354 );
3355}
3356
3357 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003358otherfile_buf(
3359 buf_T *buf,
3360 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003362 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003364 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365{
3366 /* no name is different */
3367 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3368 return TRUE;
3369 if (fnamecmp(ffname, buf->b_ffname) == 0)
3370 return FALSE;
3371#ifdef UNIX
3372 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003373 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374
Bram Moolenaar8767f522016-07-01 17:17:39 +02003375 /* If no stat_T given, get it now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 if (stp == NULL)
3377 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003378 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 st.st_dev = (dev_T)-1;
3380 stp = &st;
3381 }
3382 /* Use dev/ino to check if the files are the same, even when the names
3383 * are different (possible with links). Still need to compare the
3384 * name above, for when the file doesn't exist yet.
3385 * Problem: The dev/ino changes when a file is deleted (and created
3386 * again) and remains the same when renamed/moved. We don't want to
3387 * mch_stat() each buffer each time, that would be too slow. Get the
3388 * dev/ino again when they appear to match, but not when they appear
3389 * to be different: Could skip a buffer when it's actually the same
3390 * file. */
3391 if (buf_same_ino(buf, stp))
3392 {
3393 buf_setino(buf);
3394 if (buf_same_ino(buf, stp))
3395 return FALSE;
3396 }
3397 }
3398#endif
3399 return TRUE;
3400}
3401
3402#if defined(UNIX) || defined(PROTO)
3403/*
3404 * Set inode and device number for a buffer.
3405 * Must always be called when b_fname is changed!.
3406 */
3407 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003408buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003410 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411
3412 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3413 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003414 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 buf->b_dev = st.st_dev;
3416 buf->b_ino = st.st_ino;
3417 }
3418 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003419 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420}
3421
3422/*
3423 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3424 */
3425 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003426buf_same_ino(
3427 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003428 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003430 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 && stp->st_dev == buf->b_dev
3432 && stp->st_ino == buf->b_ino);
3433}
3434#endif
3435
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003436/*
3437 * Print info about the current buffer.
3438 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003440fileinfo(
3441 int fullname, /* when non-zero print full path */
3442 int shorthelp,
3443 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444{
3445 char_u *name;
3446 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003447 char *p;
3448 char *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003449 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003451 buffer = alloc(IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 if (buffer == NULL)
3453 return;
3454
3455 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3456 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003457 vim_snprintf(buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 p = buffer + STRLEN(buffer);
3459 }
3460 else
3461 p = buffer;
3462
3463 *p++ = '"';
3464 if (buf_spname(curbuf) != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003465 vim_strncpy((char_u *)p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 else
3467 {
3468 if (!fullname && curbuf->b_fname != NULL)
3469 name = curbuf->b_fname;
3470 else
3471 name = curbuf->b_ffname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003472 home_replace(shorthelp ? curbuf : NULL, name, (char_u *)p,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 (int)(IOSIZE - (p - buffer)), TRUE);
3474 }
3475
Bram Moolenaar32526b32019-01-19 17:43:09 +01003476 vim_snprintf_add(buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 curbufIsChanged() ? (shortmess(SHM_MOD)
3478 ? " [+]" : _(" [Modified]")) : " ",
3479 (curbuf->b_flags & BF_NOTEDITED)
3480#ifdef FEAT_QUICKFIX
3481 && !bt_dontwrite(curbuf)
3482#endif
3483 ? _("[Not edited]") : "",
3484 (curbuf->b_flags & BF_NEW)
3485#ifdef FEAT_QUICKFIX
3486 && !bt_dontwrite(curbuf)
3487#endif
3488 ? _("[New file]") : "",
3489 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003490 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 : _("[readonly]")) : "",
3492 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3493 || curbuf->b_p_ro) ?
3494 " " : "");
3495 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3496 * causes an overflow, thus for large numbers divide instead. */
3497 if (curwin->w_cursor.lnum > 1000000L)
3498 n = (int)(((long)curwin->w_cursor.lnum) /
3499 ((long)curbuf->b_ml.ml_line_count / 100L));
3500 else
3501 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3502 (long)curbuf->b_ml.ml_line_count);
3503 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003504 vim_snprintf_add(buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505#ifdef FEAT_CMDL_INFO
3506 else if (p_ru)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 /* Current line and column are already on the screen -- webb */
Bram Moolenaar32526b32019-01-19 17:43:09 +01003508 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003509 NGETTEXT("%ld line --%d%%--", "%ld lines --%d%%--",
3510 curbuf->b_ml.ml_line_count),
3511 (long)curbuf->b_ml.ml_line_count, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512#endif
3513 else
3514 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003515 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 _("line %ld of %ld --%d%%-- col "),
3517 (long)curwin->w_cursor.lnum,
3518 (long)curbuf->b_ml.ml_line_count,
3519 n);
3520 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003521 len = STRLEN(buffer);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003522 col_print((char_u *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3524 }
3525
Bram Moolenaar32526b32019-01-19 17:43:09 +01003526 (void)append_arg_number(curwin, (char_u *)buffer, IOSIZE,
3527 !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528
3529 if (dont_truncate)
3530 {
3531 /* Temporarily set msg_scroll to avoid the message being truncated.
3532 * First call msg_start() to get the message in the right place. */
3533 msg_start();
3534 n = msg_scroll;
3535 msg_scroll = TRUE;
3536 msg(buffer);
3537 msg_scroll = n;
3538 }
3539 else
3540 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003541 p = (char *)msg_trunc_attr(buffer, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 /* Need to repeat the message after redrawing when:
3544 * - When restart_edit is set (otherwise there will be a delay
3545 * before redrawing).
3546 * - When the screen was scrolled but there is no wait-return
3547 * prompt. */
Bram Moolenaar32526b32019-01-19 17:43:09 +01003548 set_keep_msg((char_u *)p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 }
3550
3551 vim_free(buffer);
3552}
3553
3554 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003555col_print(
3556 char_u *buf,
3557 size_t buflen,
3558 int col,
3559 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560{
3561 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003562 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003564 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565}
3566
3567#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568static char_u *lasttitle = NULL;
3569static char_u *lasticon = NULL;
3570
Bram Moolenaar84a93082018-06-16 22:58:15 +02003571/*
3572 * Put the file name in the title bar and icon of the window.
3573 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003575maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576{
3577 char_u *p;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003578 char_u *title_str = NULL;
3579 char_u *icon_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 int maxlen = 0;
3581 int len;
3582 int mustset;
3583 char_u buf[IOSIZE];
3584 int off;
3585
3586 if (!redrawing())
3587 {
3588 /* Postpone updating the title when 'lazyredraw' is set. */
3589 need_maketitle = TRUE;
3590 return;
3591 }
3592
3593 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003594 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003595 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596
3597 if (p_title)
3598 {
3599 if (p_titlelen > 0)
3600 {
3601 maxlen = p_titlelen * Columns / 100;
3602 if (maxlen < 10)
3603 maxlen = 10;
3604 }
3605
Bram Moolenaar84a93082018-06-16 22:58:15 +02003606 title_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 if (*p_titlestring != NUL)
3608 {
3609#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003610 if (stl_syntax & STL_IN_TITLE)
3611 {
3612 int use_sandbox = FALSE;
3613 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003614
3615# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003616 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003617# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003618 called_emsg = FALSE;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003619 build_stl_str_hl(curwin, title_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003620 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003621 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003622 if (called_emsg)
3623 set_string_option_direct((char_u *)"titlestring", -1,
3624 (char_u *)"", OPT_FREE, SID_ERROR);
3625 called_emsg |= save_called_emsg;
3626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 else
3628#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003629 title_str = p_titlestring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 }
3631 else
3632 {
3633 /* format: "fname + (path) (1 of 2) - VIM" */
3634
Bram Moolenaar2c666692012-09-05 13:30:40 +02003635#define SPACE_FOR_FNAME (IOSIZE - 100)
3636#define SPACE_FOR_DIR (IOSIZE - 20)
3637#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003639 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar21554412017-07-24 21:44:43 +02003640#ifdef FEAT_TERMINAL
3641 else if (curbuf->b_term != NULL)
3642 {
3643 vim_strncpy(buf, term_get_status_text(curbuf->b_term),
3644 SPACE_FOR_FNAME);
3645 }
3646#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 else
3648 {
3649 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003650 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 }
3653
Bram Moolenaar21554412017-07-24 21:44:43 +02003654#ifdef FEAT_TERMINAL
3655 if (curbuf->b_term == NULL)
3656#endif
3657 switch (bufIsChanged(curbuf)
3658 + (curbuf->b_p_ro * 2)
3659 + (!curbuf->b_p_ma * 4))
3660 {
3661 case 1: STRCAT(buf, " +"); break;
3662 case 2: STRCAT(buf, " ="); break;
3663 case 3: STRCAT(buf, " =+"); break;
3664 case 4:
3665 case 6: STRCAT(buf, " -"); break;
3666 case 5:
3667 case 7: STRCAT(buf, " -+"); break;
3668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669
Bram Moolenaar21554412017-07-24 21:44:43 +02003670 if (curbuf->b_fname != NULL
3671#ifdef FEAT_TERMINAL
3672 && curbuf->b_term == NULL
3673#endif
3674 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 {
3676 /* Get path of file, replace home dir with ~ */
3677 off = (int)STRLEN(buf);
3678 buf[off++] = ' ';
3679 buf[off++] = '(';
3680 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003681 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682#ifdef BACKSLASH_IN_FILENAME
3683 /* avoid "c:/name" to be reduced to "c" */
3684 if (isalpha(buf[off]) && buf[off + 1] == ':')
3685 off += 2;
3686#endif
3687 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003688 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 if (p == buf + off)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003690 {
Bram Moolenaar21554412017-07-24 21:44:43 +02003691 /* must be a help buffer */
3692 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003693 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003697
Bram Moolenaar2c666692012-09-05 13:30:40 +02003698 /* Translate unprintable chars and concatenate. Keep some
3699 * room for the server name. When there is no room (very long
3700 * file name) use (...). */
3701 if (off < SPACE_FOR_DIR)
3702 {
3703 p = transstr(buf + off);
3704 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3705 vim_free(p);
3706 }
3707 else
3708 {
3709 vim_strncpy(buf + off, (char_u *)"...",
3710 (size_t)(SPACE_FOR_ARGNR - off));
3711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 STRCAT(buf, ")");
3713 }
3714
Bram Moolenaar2c666692012-09-05 13:30:40 +02003715 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716
3717#if defined(FEAT_CLIENTSERVER)
3718 if (serverName != NULL)
3719 {
3720 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003721 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 }
3723 else
3724#endif
3725 STRCAT(buf, " - VIM");
3726
3727 if (maxlen > 0)
3728 {
3729 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003730 if (vim_strsize(buf) > maxlen)
3731 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 }
3733 }
3734 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02003735 mustset = value_changed(title_str, &lasttitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736
3737 if (p_icon)
3738 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02003739 icon_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 if (*p_iconstring != NUL)
3741 {
3742#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003743 if (stl_syntax & STL_IN_ICON)
3744 {
3745 int use_sandbox = FALSE;
3746 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003747
3748# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003749 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003750# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003751 called_emsg = FALSE;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003752 build_stl_str_hl(curwin, icon_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003753 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003754 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003755 if (called_emsg)
3756 set_string_option_direct((char_u *)"iconstring", -1,
3757 (char_u *)"", OPT_FREE, SID_ERROR);
3758 called_emsg |= save_called_emsg;
3759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 else
3761#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003762 icon_str = p_iconstring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 }
3764 else
3765 {
3766 if (buf_spname(curbuf) != NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003767 p = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 else /* use file name only in icon */
Bram Moolenaar84a93082018-06-16 22:58:15 +02003769 p = gettail(curbuf->b_ffname);
3770 *icon_str = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 /* Truncate name at 100 bytes. */
Bram Moolenaar84a93082018-06-16 22:58:15 +02003772 len = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 if (len > 100)
3774 {
3775 len -= 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 if (has_mbyte)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003777 len += (*mb_tail_off)(p, p + len) + 1;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003778 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02003780 STRCPY(icon_str, p);
3781 trans_characters(icon_str, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 }
3783 }
3784
Bram Moolenaar84a93082018-06-16 22:58:15 +02003785 mustset |= value_changed(icon_str, &lasticon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786
3787 if (mustset)
3788 resettitle();
3789}
3790
3791/*
3792 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3793 * from "str" if it does.
Bram Moolenaar84a93082018-06-16 22:58:15 +02003794 * Return TRUE if resettitle() is to be called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 */
3796 static int
Bram Moolenaar84a93082018-06-16 22:58:15 +02003797value_changed(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798{
3799 if ((str == NULL) != (*last == NULL)
3800 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3801 {
3802 vim_free(*last);
3803 if (str == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003804 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 *last = NULL;
Bram Moolenaar40385db2018-08-07 22:31:44 +02003806 mch_restore_title(
3807 last == &lasttitle ? SAVE_RESTORE_TITLE : SAVE_RESTORE_ICON);
Bram Moolenaar84a93082018-06-16 22:58:15 +02003808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 else
Bram Moolenaar84a93082018-06-16 22:58:15 +02003810 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 *last = vim_strsave(str);
Bram Moolenaar84a93082018-06-16 22:58:15 +02003812 return TRUE;
3813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 }
3815 return FALSE;
3816}
3817
3818/*
3819 * Put current window title back (used after calling a shell)
3820 */
3821 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003822resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823{
3824 mch_settitle(lasttitle, lasticon);
3825}
Bram Moolenaarea408852005-06-25 22:49:46 +00003826
3827# if defined(EXITFREE) || defined(PROTO)
3828 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003829free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00003830{
3831 vim_free(lasttitle);
3832 vim_free(lasticon);
3833}
3834# endif
3835
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836#endif /* FEAT_TITLE */
3837
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003838#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003840 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 * Return length of string in screen cells.
3842 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003843 * Normally works for window "wp", except when working for 'tabline' then it
3844 * is "curwin".
3845 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 * Items are drawn interspersed with the text that surrounds it
3847 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3848 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3849 *
3850 * If maxwidth is not zero, the string will be filled at any middle marker
3851 * or truncated if too long, fillchar is used for all whitespace.
3852 */
3853 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003854build_stl_str_hl(
3855 win_T *wp,
3856 char_u *out, /* buffer to write into != NameBuff */
3857 size_t outlen, /* length of out[] */
3858 char_u *fmt,
3859 int use_sandbox UNUSED, /* "fmt" was set insecurely, use sandbox */
3860 int fillchar,
3861 int maxwidth,
3862 struct stl_hlrec *hltab, /* return: HL attributes (can be NULL) */
3863 struct stl_hlrec *tabtab) /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864{
Bram Moolenaar10772302019-01-20 18:25:54 +01003865 linenr_T lnum;
3866 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 char_u *p;
3868 char_u *s;
3869 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003870 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871#ifdef FEAT_EVAL
Bram Moolenaarba2929b2017-09-08 13:59:21 +02003872 win_T *save_curwin;
3873 buf_T *save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874#endif
3875 int empty_line;
3876 colnr_T virtcol;
3877 long l;
3878 long n;
3879 int prevchar_isflag;
3880 int prevchar_isitem;
3881 int itemisflag;
3882 int fillable;
3883 char_u *str;
3884 long num;
3885 int width;
3886 int itemcnt;
3887 int curitem;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02003888 int group_end_userhl;
3889 int group_start_userhl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 int groupitem[STL_MAX_ITEM];
3891 int groupdepth;
3892 struct stl_item
3893 {
3894 char_u *start;
3895 int minwid;
3896 int maxwid;
3897 enum
3898 {
3899 Normal,
3900 Empty,
3901 Group,
3902 Middle,
3903 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003904 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 Trunc
3906 } type;
3907 } item[STL_MAX_ITEM];
3908 int minwid;
3909 int maxwid;
3910 int zeropad;
3911 char_u base;
3912 char_u opt;
3913#define TMPLEN 70
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02003914 char_u buf_tmp[TMPLEN];
3915 char_u win_tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003916 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003917 struct stl_hlrec *sp;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02003918 int save_must_redraw = must_redraw;
3919 int save_redr_type = curwin->w_redr_type;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003920
3921#ifdef FEAT_EVAL
3922 /*
3923 * When the format starts with "%!" then evaluate it as an expression and
3924 * use the result as the actual format string.
3925 */
3926 if (fmt[0] == '%' && fmt[1] == '!')
3927 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02003928 typval_T tv;
3929
3930 tv.v_type = VAR_NUMBER;
3931 tv.vval.v_number = wp->w_id;
3932 set_var((char_u *)"g:statusline_winid", &tv, FALSE);
3933
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003934 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3935 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003936 usefmt = fmt;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02003937
3938 do_unlet((char_u *)"g:statusline_winid", TRUE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003939 }
3940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941
3942 if (fillchar == 0)
3943 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003944 /* Can't handle a multi-byte fill character yet. */
3945 else if (mb_char2len(fillchar) > 1)
3946 fillchar = '-';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003947
Bram Moolenaar10772302019-01-20 18:25:54 +01003948 // The cursor in windows other than the current one isn't always
3949 // up-to-date, esp. because of autocommands and timers.
3950 lnum = wp->w_cursor.lnum;
3951 if (lnum > wp->w_buffer->b_ml.ml_line_count)
3952 {
3953 lnum = wp->w_buffer->b_ml.ml_line_count;
3954 wp->w_cursor.lnum = lnum;
3955 }
3956
3957 // Get line & check if empty (cursorpos will show "0-1"). Note that
3958 // p will become invalid when getting another buffer line.
3959 p = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar567199b2013-04-24 16:52:36 +02003960 empty_line = (*p == NUL);
3961
Bram Moolenaar10772302019-01-20 18:25:54 +01003962 // Get the byte value now, in case we need it below. This is more efficient
3963 // than making a copy of the line.
3964 len = STRLEN(p);
3965 if (wp->w_cursor.col > (colnr_T)len)
3966 {
3967 // Line may have changed since checking the cursor column, or the lnum
3968 // was adjusted above.
3969 wp->w_cursor.col = (colnr_T)len;
Bram Moolenaar10772302019-01-20 18:25:54 +01003970 wp->w_cursor.coladd = 0;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003971 byteval = 0;
Bram Moolenaar10772302019-01-20 18:25:54 +01003972 }
Bram Moolenaar567199b2013-04-24 16:52:36 +02003973 else
Bram Moolenaar567199b2013-04-24 16:52:36 +02003974 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975
3976 groupdepth = 0;
3977 p = out;
3978 curitem = 0;
3979 prevchar_isflag = TRUE;
3980 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003981 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003983 if (curitem == STL_MAX_ITEM)
3984 {
3985 /* There are too many items. Add the error code to the statusline
3986 * to give the user a hint about what went wrong. */
3987 if (p + 6 < out + outlen)
3988 {
3989 mch_memmove(p, " E541", (size_t)5);
3990 p += 5;
3991 }
3992 break;
3993 }
3994
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 if (*s != NUL && *s != '%')
3996 prevchar_isflag = prevchar_isitem = FALSE;
3997
3998 /*
3999 * Handle up to the next '%' or the end.
4000 */
4001 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
4002 *p++ = *s++;
4003 if (*s == NUL || p + 1 >= out + outlen)
4004 break;
4005
4006 /*
4007 * Handle one '%' item.
4008 */
4009 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01004010 if (*s == NUL) /* ignore trailing % */
4011 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 if (*s == '%')
4013 {
4014 if (p + 1 >= out + outlen)
4015 break;
4016 *p++ = *s++;
4017 prevchar_isflag = prevchar_isitem = FALSE;
4018 continue;
4019 }
4020 if (*s == STL_MIDDLEMARK)
4021 {
4022 s++;
4023 if (groupdepth > 0)
4024 continue;
4025 item[curitem].type = Middle;
4026 item[curitem++].start = p;
4027 continue;
4028 }
4029 if (*s == STL_TRUNCMARK)
4030 {
4031 s++;
4032 item[curitem].type = Trunc;
4033 item[curitem++].start = p;
4034 continue;
4035 }
4036 if (*s == ')')
4037 {
4038 s++;
4039 if (groupdepth < 1)
4040 continue;
4041 groupdepth--;
4042
4043 t = item[groupitem[groupdepth]].start;
4044 *p = NUL;
4045 l = vim_strsize(t);
4046 if (curitem > groupitem[groupdepth] + 1
4047 && item[groupitem[groupdepth]].minwid == 0)
4048 {
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004049 /* remove group if all items are empty and highlight group
4050 * doesn't change */
4051 group_start_userhl = group_end_userhl = 0;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004052 for (n = groupitem[groupdepth] - 1; n >= 0; n--)
4053 {
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004054 if (item[n].type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004055 {
4056 group_start_userhl = group_end_userhl = item[n].minwid;
4057 break;
4058 }
4059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004061 {
4062 if (item[n].type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 break;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004064 if (item[n].type == Highlight)
4065 group_end_userhl = item[n].minwid;
4066 }
4067 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 {
4069 p = t;
4070 l = 0;
4071 }
4072 }
4073 if (l > item[groupitem[groupdepth]].maxwid)
4074 {
4075 /* truncate, remove n bytes of text at the start */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 if (has_mbyte)
4077 {
4078 /* Find the first character that should be included. */
4079 n = 0;
4080 while (l >= item[groupitem[groupdepth]].maxwid)
4081 {
4082 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004083 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 }
4085 }
4086 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004087 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088
4089 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004090 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 p = p - n + 1;
Bram Moolenaar13505972019-01-24 15:04:48 +01004092
4093 // Fill up space left over by half a double-wide char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 while (++l < item[groupitem[groupdepth]].minwid)
4095 *p++ = fillchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096
4097 /* correct the start of the items for the truncation */
4098 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
4099 {
4100 item[l].start -= n;
4101 if (item[l].start < t)
4102 item[l].start = t;
4103 }
4104 }
4105 else if (abs(item[groupitem[groupdepth]].minwid) > l)
4106 {
4107 /* fill */
4108 n = item[groupitem[groupdepth]].minwid;
4109 if (n < 0)
4110 {
4111 /* fill by appending characters */
4112 n = 0 - n;
4113 while (l++ < n && p + 1 < out + outlen)
4114 *p++ = fillchar;
4115 }
4116 else
4117 {
4118 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004119 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 l = n - l;
4121 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004122 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 p += l;
4124 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
4125 item[n].start += l;
4126 for ( ; l > 0; l--)
4127 *t++ = fillchar;
4128 }
4129 }
4130 continue;
4131 }
4132 minwid = 0;
4133 maxwid = 9999;
4134 zeropad = FALSE;
4135 l = 1;
4136 if (*s == '0')
4137 {
4138 s++;
4139 zeropad = TRUE;
4140 }
4141 if (*s == '-')
4142 {
4143 s++;
4144 l = -1;
4145 }
4146 if (VIM_ISDIGIT(*s))
4147 {
4148 minwid = (int)getdigits(&s);
4149 if (minwid < 0) /* overflow */
4150 minwid = 0;
4151 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004152 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 {
4154 item[curitem].type = Highlight;
4155 item[curitem].start = p;
4156 item[curitem].minwid = minwid > 9 ? 1 : minwid;
4157 s++;
4158 curitem++;
4159 continue;
4160 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004161 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4162 {
4163 if (*s == STL_TABCLOSENR)
4164 {
4165 if (minwid == 0)
4166 {
4167 /* %X ends the close label, go back to the previously
4168 * define tab label nr. */
4169 for (n = curitem - 1; n >= 0; --n)
4170 if (item[n].type == TabPage && item[n].minwid >= 0)
4171 {
4172 minwid = item[n].minwid;
4173 break;
4174 }
4175 }
4176 else
4177 /* close nrs are stored as negative values */
4178 minwid = - minwid;
4179 }
4180 item[curitem].type = TabPage;
4181 item[curitem].start = p;
4182 item[curitem].minwid = minwid;
4183 s++;
4184 curitem++;
4185 continue;
4186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 if (*s == '.')
4188 {
4189 s++;
4190 if (VIM_ISDIGIT(*s))
4191 {
4192 maxwid = (int)getdigits(&s);
4193 if (maxwid <= 0) /* overflow */
4194 maxwid = 50;
4195 }
4196 }
4197 minwid = (minwid > 50 ? 50 : minwid) * l;
4198 if (*s == '(')
4199 {
4200 groupitem[groupdepth++] = curitem;
4201 item[curitem].type = Group;
4202 item[curitem].start = p;
4203 item[curitem].minwid = minwid;
4204 item[curitem].maxwid = maxwid;
4205 s++;
4206 curitem++;
4207 continue;
4208 }
4209 if (vim_strchr(STL_ALL, *s) == NULL)
4210 {
4211 s++;
4212 continue;
4213 }
4214 opt = *s++;
4215
4216 /* OK - now for the real work */
4217 base = 'D';
4218 itemisflag = FALSE;
4219 fillable = TRUE;
4220 num = -1;
4221 str = NULL;
4222 switch (opt)
4223 {
4224 case STL_FILEPATH:
4225 case STL_FULLPATH:
4226 case STL_FILENAME:
4227 fillable = FALSE; /* don't change ' ' to fillchar */
4228 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004229 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 else
4231 {
4232 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004233 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4235 }
4236 trans_characters(NameBuff, MAXPATHL);
4237 if (opt != STL_FILENAME)
4238 str = NameBuff;
4239 else
4240 str = gettail(NameBuff);
4241 break;
4242
4243 case STL_VIM_EXPR: /* '{' */
4244 itemisflag = TRUE;
4245 t = p;
4246 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
4247 *p++ = *s++;
4248 if (*s != '}') /* missing '}' or out of space */
4249 break;
4250 s++;
4251 *p = 0;
4252 p = t;
4253
4254#ifdef FEAT_EVAL
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004255 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp),
4256 "%d", curbuf->b_fnum);
4257 set_internal_string_var((char_u *)"g:actual_curbuf", buf_tmp);
4258 vim_snprintf((char *)win_tmp, sizeof(win_tmp), "%d", curwin->w_id);
4259 set_internal_string_var((char_u *)"g:actual_curwin", win_tmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004261 save_curbuf = curbuf;
4262 save_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 curwin = wp;
4264 curbuf = wp->w_buffer;
4265
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004266 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004268 curwin = save_curwin;
4269 curbuf = save_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00004270 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004271 do_unlet((char_u *)"g:actual_curwin", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272
4273 if (str != NULL && *str != 0)
4274 {
4275 if (*skipdigits(str) == NUL)
4276 {
4277 num = atoi((char *)str);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004278 VIM_CLEAR(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 itemisflag = FALSE;
4280 }
4281 }
4282#endif
4283 break;
4284
4285 case STL_LINE:
4286 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4287 ? 0L : (long)(wp->w_cursor.lnum);
4288 break;
4289
4290 case STL_NUMLINES:
4291 num = wp->w_buffer->b_ml.ml_line_count;
4292 break;
4293
4294 case STL_COLUMN:
4295 num = !(State & INSERT) && empty_line
4296 ? 0 : (int)wp->w_cursor.col + 1;
4297 break;
4298
4299 case STL_VIRTCOL:
4300 case STL_VIRTCOL_ALT:
4301 /* In list mode virtcol needs to be recomputed */
4302 virtcol = wp->w_virtcol;
4303 if (wp->w_p_list && lcs_tab1 == NUL)
4304 {
4305 wp->w_p_list = FALSE;
4306 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
4307 wp->w_p_list = TRUE;
4308 }
4309 ++virtcol;
4310 /* Don't display %V if it's the same as %c. */
4311 if (opt == STL_VIRTCOL_ALT
4312 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
4313 ? 0 : (int)wp->w_cursor.col + 1)))
4314 break;
4315 num = (long)virtcol;
4316 break;
4317
4318 case STL_PERCENTAGE:
4319 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4320 (long)wp->w_buffer->b_ml.ml_line_count);
4321 break;
4322
4323 case STL_ALTPERCENT:
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004324 str = buf_tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004325 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 break;
4327
4328 case STL_ARGLISTSTAT:
4329 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004330 buf_tmp[0] = 0;
4331 if (append_arg_number(wp, buf_tmp, (int)sizeof(buf_tmp), FALSE))
4332 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 break;
4334
4335 case STL_KEYMAP:
4336 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004337 if (get_keymap_str(wp, (char_u *)"<%s>", buf_tmp, TMPLEN))
4338 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 break;
4340 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004341#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004342 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343#else
4344 num = 0;
4345#endif
4346 break;
4347
4348 case STL_BUFNO:
4349 num = wp->w_buffer->b_fnum;
4350 break;
4351
4352 case STL_OFFSET_X:
4353 base = 'X';
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004354 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 case STL_OFFSET:
4356#ifdef FEAT_BYTEOFF
4357 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4358 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4359 0L : l + 1 + (!(State & INSERT) && empty_line ?
4360 0 : (int)wp->w_cursor.col);
4361#endif
4362 break;
4363
4364 case STL_BYTEVAL_X:
4365 base = 'X';
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004366 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004368 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 if (num == NL)
4370 num = 0;
4371 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4372 num = NL;
4373 break;
4374
4375 case STL_ROFLAG:
4376 case STL_ROFLAG_ALT:
4377 itemisflag = TRUE;
4378 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004379 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 break;
4381
4382 case STL_HELPFLAG:
4383 case STL_HELPFLAG_ALT:
4384 itemisflag = TRUE;
4385 if (wp->w_buffer->b_help)
4386 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004387 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 break;
4389
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 case STL_FILETYPE:
4391 if (*wp->w_buffer->b_p_ft != NUL
4392 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4393 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004394 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), "[%s]",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004395 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004396 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 }
4398 break;
4399
4400 case STL_FILETYPE_ALT:
4401 itemisflag = TRUE;
4402 if (*wp->w_buffer->b_p_ft != NUL
4403 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4404 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004405 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), ",%s",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004406 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004407 for (t = buf_tmp; *t != 0; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 *t = TOUPPER_LOC(*t);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004409 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 }
4411 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412
Bram Moolenaar4033c552017-09-16 20:54:51 +02004413#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 case STL_PREVIEWFLAG:
4415 case STL_PREVIEWFLAG_ALT:
4416 itemisflag = TRUE;
4417 if (wp->w_p_pvw)
4418 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4419 : _("[Preview]"));
4420 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004421
4422 case STL_QUICKFIX:
4423 if (bt_quickfix(wp->w_buffer))
4424 str = (char_u *)(wp->w_llist_ref
4425 ? _(msg_loclist)
4426 : _(msg_qflist));
4427 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428#endif
4429
4430 case STL_MODIFIED:
4431 case STL_MODIFIED_ALT:
4432 itemisflag = TRUE;
4433 switch ((opt == STL_MODIFIED_ALT)
4434 + bufIsChanged(wp->w_buffer) * 2
4435 + (!wp->w_buffer->b_p_ma) * 4)
4436 {
4437 case 2: str = (char_u *)"[+]"; break;
4438 case 3: str = (char_u *)",+"; break;
4439 case 4: str = (char_u *)"[-]"; break;
4440 case 5: str = (char_u *)",-"; break;
4441 case 6: str = (char_u *)"[+-]"; break;
4442 case 7: str = (char_u *)",+-"; break;
4443 }
4444 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004445
4446 case STL_HIGHLIGHT:
4447 t = s;
4448 while (*s != '#' && *s != NUL)
4449 ++s;
4450 if (*s == '#')
4451 {
4452 item[curitem].type = Highlight;
4453 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004454 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004455 curitem++;
4456 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004457 if (*s != NUL)
4458 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004459 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 }
4461
4462 item[curitem].start = p;
4463 item[curitem].type = Normal;
4464 if (str != NULL && *str)
4465 {
4466 t = str;
4467 if (itemisflag)
4468 {
4469 if ((t[0] && t[1])
4470 && ((!prevchar_isitem && *t == ',')
4471 || (prevchar_isflag && *t == ' ')))
4472 t++;
4473 prevchar_isflag = TRUE;
4474 }
4475 l = vim_strsize(t);
4476 if (l > 0)
4477 prevchar_isitem = TRUE;
4478 if (l > maxwid)
4479 {
4480 while (l >= maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 if (has_mbyte)
4482 {
4483 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004484 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 }
4486 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 l -= byte2cells(*t++);
4488 if (p + 1 >= out + outlen)
4489 break;
4490 *p++ = '<';
4491 }
4492 if (minwid > 0)
4493 {
4494 for (; l < minwid && p + 1 < out + outlen; l++)
4495 {
4496 /* Don't put a "-" in front of a digit. */
4497 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4498 *p++ = ' ';
4499 else
4500 *p++ = fillchar;
4501 }
4502 minwid = 0;
4503 }
4504 else
4505 minwid *= -1;
4506 while (*t && p + 1 < out + outlen)
4507 {
4508 *p++ = *t++;
4509 /* Change a space by fillchar, unless fillchar is '-' and a
4510 * digit follows. */
4511 if (fillable && p[-1] == ' '
4512 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4513 p[-1] = fillchar;
4514 }
4515 for (; l < minwid && p + 1 < out + outlen; l++)
4516 *p++ = fillchar;
4517 }
4518 else if (num >= 0)
4519 {
4520 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4521 char_u nstr[20];
4522
4523 if (p + 20 >= out + outlen)
4524 break; /* not sufficient space */
4525 prevchar_isitem = TRUE;
4526 t = nstr;
4527 if (opt == STL_VIRTCOL_ALT)
4528 {
4529 *t++ = '-';
4530 minwid--;
4531 }
4532 *t++ = '%';
4533 if (zeropad)
4534 *t++ = '0';
4535 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004536 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 *t = 0;
4538
4539 for (n = num, l = 1; n >= nbase; n /= nbase)
4540 l++;
4541 if (opt == STL_VIRTCOL_ALT)
4542 l++;
4543 if (l > maxwid)
4544 {
4545 l += 2;
4546 n = l - maxwid;
4547 while (l-- > maxwid)
4548 num /= nbase;
4549 *t++ = '>';
4550 *t++ = '%';
4551 *t = t[-3];
4552 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004553 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4554 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 }
4556 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004557 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4558 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 p += STRLEN(p);
4560 }
4561 else
4562 item[curitem].type = Empty;
4563
4564 if (opt == STL_VIM_EXPR)
4565 vim_free(str);
4566
4567 if (num >= 0 || (!itemisflag && str && *str))
4568 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4569 curitem++;
4570 }
4571 *p = NUL;
4572 itemcnt = curitem;
4573
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004574#ifdef FEAT_EVAL
4575 if (usefmt != fmt)
4576 vim_free(usefmt);
4577#endif
4578
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 width = vim_strsize(out);
4580 if (maxwidth > 0 && width > maxwidth)
4581 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004582 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 l = 0;
4584 if (itemcnt == 0)
4585 s = out;
4586 else
4587 {
4588 for ( ; l < itemcnt; l++)
4589 if (item[l].type == Trunc)
4590 {
4591 /* Truncate at %< item. */
4592 s = item[l].start;
4593 break;
4594 }
4595 if (l == itemcnt)
4596 {
4597 /* No %< item, truncate first item. */
4598 s = item[0].start;
4599 l = 0;
4600 }
4601 }
4602
4603 if (width - vim_strsize(s) >= maxwidth)
4604 {
4605 /* Truncation mark is beyond max length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 if (has_mbyte)
4607 {
4608 s = out;
4609 width = 0;
4610 for (;;)
4611 {
4612 width += ptr2cells(s);
4613 if (width >= maxwidth)
4614 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004615 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 }
4617 /* Fill up for half a double-wide character. */
4618 while (++width < maxwidth)
4619 *s++ = fillchar;
4620 }
4621 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 s = out + maxwidth - 1;
4623 for (l = 0; l < itemcnt; l++)
4624 if (item[l].start > s)
4625 break;
4626 itemcnt = l;
4627 *s++ = '>';
4628 *s = 0;
4629 }
4630 else
4631 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 if (has_mbyte)
4633 {
4634 n = 0;
4635 while (width >= maxwidth)
4636 {
4637 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004638 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 }
4640 }
4641 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 n = width - maxwidth + 1;
4643 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004644 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 *s = '<';
4646
4647 /* Fill up for half a double-wide character. */
4648 while (++width < maxwidth)
4649 {
4650 s = s + STRLEN(s);
4651 *s++ = fillchar;
4652 *s = NUL;
4653 }
4654
4655 --n; /* count the '<' */
4656 for (; l < itemcnt; l++)
4657 {
4658 if (item[l].start - n >= s)
4659 item[l].start -= n;
4660 else
4661 item[l].start = s;
4662 }
4663 }
4664 width = maxwidth;
4665 }
4666 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4667 {
4668 /* Apply STL_MIDDLE if any */
4669 for (l = 0; l < itemcnt; l++)
4670 if (item[l].type == Middle)
4671 break;
4672 if (l < itemcnt)
4673 {
4674 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004675 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 for (s = item[l].start; s < p; s++)
4677 *s = fillchar;
4678 for (l++; l < itemcnt; l++)
4679 item[l].start += maxwidth - width;
4680 width = maxwidth;
4681 }
4682 }
4683
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004684 /* Store the info about highlighting. */
4685 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004687 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 for (l = 0; l < itemcnt; l++)
4689 {
4690 if (item[l].type == Highlight)
4691 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004692 sp->start = item[l].start;
4693 sp->userhl = item[l].minwid;
4694 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 }
4696 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004697 sp->start = NULL;
4698 sp->userhl = 0;
4699 }
4700
4701 /* Store the info about tab pages labels. */
4702 if (tabtab != NULL)
4703 {
4704 sp = tabtab;
4705 for (l = 0; l < itemcnt; l++)
4706 {
4707 if (item[l].type == TabPage)
4708 {
4709 sp->start = item[l].start;
4710 sp->userhl = item[l].minwid;
4711 sp++;
4712 }
4713 }
4714 sp->start = NULL;
4715 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 }
4717
Bram Moolenaar65ed1362017-09-30 16:00:14 +02004718 /* When inside update_screen we do not want redrawing a stausline, ruler,
4719 * title, etc. to trigger another redraw, it may cause an endless loop. */
4720 if (updating_screen)
4721 {
4722 must_redraw = save_must_redraw;
4723 curwin->w_redr_type = save_redr_type;
4724 }
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004725
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 return width;
4727}
4728#endif /* FEAT_STL_OPT */
4729
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004730#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4731 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004733 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4734 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 */
4736 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004737get_rel_pos(
4738 win_T *wp,
4739 char_u *buf,
4740 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741{
4742 long above; /* number of lines above window */
4743 long below; /* number of lines below window */
4744
Bram Moolenaar0027c212015-01-07 13:31:52 +01004745 if (buflen < 3) /* need at least 3 chars for writing */
4746 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 above = wp->w_topline - 1;
4748#ifdef FEAT_DIFF
4749 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02004750 if (wp->w_topline == 1 && wp->w_topfill >= 1)
4751 above = 0; /* All buffer lines are displayed and there is an
4752 * indication of filler lines, that can be considered
4753 * seeing all lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754#endif
4755 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4756 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004757 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4758 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004760 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004762 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 ? (int)(above / ((above + below) / 100L))
4764 : (int)(above * 100L / (above + below)));
4765}
4766#endif
4767
4768/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004769 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 * Return TRUE if it was appended.
4771 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004772 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004773append_arg_number(
4774 win_T *wp,
4775 char_u *buf,
4776 int buflen,
4777 int add_file) /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778{
4779 char_u *p;
4780
4781 if (ARGCOUNT <= 1) /* nothing to do */
4782 return FALSE;
4783
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004784 p = buf + STRLEN(buf); /* go to the end of the buffer */
4785 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 return FALSE;
4787 *p++ = ' ';
4788 *p++ = '(';
4789 if (add_file)
4790 {
4791 STRCPY(p, "file ");
4792 p += 5;
4793 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004794 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4795 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4797 return TRUE;
4798}
4799
4800/*
4801 * If fname is not a full path, make it a full path.
4802 * Returns pointer to allocated memory (NULL for failure).
4803 */
4804 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004805fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806{
4807 /*
4808 * Force expanding the path always for Unix, because symbolic links may
4809 * mess up the full path name, even though it starts with a '/'.
4810 * Also expand when there is ".." in the file name, try to remove it,
4811 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004812 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 * For MS-Windows also expand names like "longna~1" to "longname".
4814 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004815#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 return FullName_save(fname, TRUE);
4817#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004818 if (!vim_isAbsName(fname)
4819 || strstr((char *)fname, "..") != NULL
4820 || strstr((char *)fname, "//") != NULL
4821# ifdef BACKSLASH_IN_FILENAME
4822 || strstr((char *)fname, "\\\\") != NULL
4823# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004824# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004826# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 )
4828 return FullName_save(fname, FALSE);
4829
4830 fname = vim_strsave(fname);
4831
Bram Moolenaar9b942202007-10-03 12:31:33 +00004832# ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01004833 if (fname != NULL)
4834 fname_case(fname, 0); /* set correct case for file name */
Bram Moolenaar9b942202007-10-03 12:31:33 +00004835# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836
4837 return fname;
4838#endif
4839}
4840
4841/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02004842 * Make "*ffname" a full file name, set "*sfname" to "*ffname" if not NULL.
4843 * "*ffname" becomes a pointer to allocated memory (or NULL).
4844 * When resolving a link both "*sfname" and "*ffname" will point to the same
4845 * allocated memory.
4846 * The "*ffname" and "*sfname" pointer values on call will not be freed.
4847 * Note that the resulting "*ffname" pointer should be considered not allocaed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004850fname_expand(
4851 buf_T *buf UNUSED,
4852 char_u **ffname,
4853 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02004855 if (*ffname == NULL) // no file name given, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 return;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02004857 if (*sfname == NULL) // no short file name given, use ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 *sfname = *ffname;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02004859 *ffname = fix_fname(*ffname); // expand to full path
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860
4861#ifdef FEAT_SHORTCUT
4862 if (!buf->b_p_bin)
4863 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004864 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02004866 // If the file name is a shortcut file, use the file it links to.
Bram Moolenaardce1e892019-02-10 23:18:53 +01004867 rfname = mch_resolve_path(*ffname, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004868 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 {
4870 vim_free(*ffname);
4871 *ffname = rfname;
4872 *sfname = rfname;
4873 }
4874 }
4875#endif
4876}
4877
4878/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 * Open a window for a number of buffers.
4880 */
4881 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004882ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883{
4884 buf_T *buf;
4885 win_T *wp, *wpnext;
4886 int split_ret = OK;
4887 int p_ea_save;
4888 int open_wins = 0;
4889 int r;
4890 int count; /* Maximum number of windows to open. */
4891 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004892 int had_tab = cmdmod.tab;
4893 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894
4895 if (eap->addr_count == 0) /* make as many windows as possible */
4896 count = 9999;
4897 else
4898 count = eap->line2; /* make as many windows as specified */
4899 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4900 all = FALSE;
4901 else
4902 all = TRUE;
4903
4904 setpcmark();
4905
4906#ifdef FEAT_GUI
4907 need_mouse_correct = TRUE;
4908#endif
4909
4910 /*
4911 * Close superfluous windows (two windows for the same buffer).
4912 * Also close windows that are not full-width.
4913 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004914 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004915 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004916 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004918 tpnext = curtab->tp_next;
4919 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004921 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004922 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004923 || ((cmdmod.split & WSP_VERT)
4924 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004925 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004926 : wp->w_width != Columns)
Bram Moolenaar4033c552017-09-16 20:54:51 +02004927 || (had_tab > 0 && wp != firstwin)) && !ONE_WINDOW
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004928 && !(wp->w_closing || wp->w_buffer->b_locked > 0))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004929 {
4930 win_close(wp, FALSE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004931 wpnext = firstwin; /* just in case an autocommand does
4932 something strange with windows */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004933 tpnext = first_tabpage; /* start all over... */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004934 open_wins = 0;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004935 }
4936 else
4937 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004939
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004940 /* Without the ":tab" modifier only do the current tab page. */
4941 if (had_tab == 0 || tpnext == NULL)
4942 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004943 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 }
4945
4946 /*
4947 * Go through the buffer list. When a buffer doesn't have a window yet,
4948 * open one. Otherwise move the window to the right position.
4949 * Watch out for autocommands that delete buffers or windows!
4950 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4952 ++autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 win_enter(lastwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4956 {
4957 /* Check if this buffer needs a window */
4958 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4959 continue;
4960
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004961 if (had_tab != 0)
4962 {
4963 /* With the ":tab" modifier don't move the window. */
4964 if (buf->b_nwindows > 0)
4965 wp = lastwin; /* buffer has a window, skip it */
4966 else
4967 wp = NULL;
4968 }
4969 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004970 {
4971 /* Check if this buffer already has a window */
Bram Moolenaar29323592016-07-24 22:04:11 +02004972 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004973 if (wp->w_buffer == buf)
4974 break;
4975 /* If the buffer already has a window, move it */
4976 if (wp != NULL)
4977 win_move_after(wp, curwin);
4978 }
4979
4980 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02004982 bufref_T bufref;
4983
4984 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004985
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 /* Split the window and put the buffer in it */
4987 p_ea_save = p_ea;
4988 p_ea = TRUE; /* use space from all windows */
4989 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4990 ++open_wins;
4991 p_ea = p_ea_save;
4992 if (split_ret == FAIL)
4993 continue;
4994
4995 /* Open the buffer in this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02004998 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005000 /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 swap_exists_action = SEA_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 break;
5003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 if (swap_exists_action == SEA_QUIT)
5005 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005006#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005007 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005008
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005009 /* Reset the error/interrupt/exception state here so that
5010 * aborting() returns FALSE when closing a window. */
5011 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005012#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005013
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005014 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 win_close(curwin, TRUE);
5016 --open_wins;
5017 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005018 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005019
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005020#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005021 /* Restore the error/interrupt/exception state if not
5022 * discarded by a new aborting error, interrupt, or uncaught
5023 * exception. */
5024 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 }
5027 else
5028 handle_swap_exists(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 }
5030
5031 ui_breakcheck();
5032 if (got_int)
5033 {
5034 (void)vgetc(); /* only break the file loading, not the rest */
5035 break;
5036 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005037#ifdef FEAT_EVAL
5038 /* Autocommands deleted the buffer or aborted script processing!!! */
5039 if (aborting())
5040 break;
5041#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005042 /* When ":tab" was used open a new tab for a new window repeatedly. */
5043 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5044 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 --autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 win_enter(firstwin, FALSE); /* back to first window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049
5050 /*
5051 * Close superfluous windows.
5052 */
5053 for (wp = lastwin; open_wins > count; )
5054 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005055 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 || autowrite(wp->w_buffer, FALSE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 if (!win_valid(wp))
5058 {
5059 /* BufWrite Autocommands made the window invalid, start over */
5060 wp = lastwin;
5061 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005062 else if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005064 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 --open_wins;
5066 wp = lastwin;
5067 }
5068 else
5069 {
5070 wp = wp->w_prev;
5071 if (wp == NULL)
5072 break;
5073 }
5074 }
5075}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005078static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005079
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080/*
5081 * do_modelines() - process mode lines for the current file
5082 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005083 * "flags" can be:
5084 * OPT_WINONLY only set options local to window
5085 * OPT_NOWIN don't set options local to window
5086 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 * Returns immediately if the "ml" option isn't set.
5088 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005090do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005092 linenr_T lnum;
5093 int nmlines;
5094 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095
5096 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5097 return;
5098
5099 /* Disallow recursive entry here. Can happen when executing a modeline
5100 * triggers an autocommand, which reloads modelines with a ":do". */
5101 if (entered)
5102 return;
5103
5104 ++entered;
5105 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5106 ++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
5110 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5111 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005112 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 nmlines = 0;
5114 --entered;
5115}
5116
5117#include "version.h" /* for version number */
5118
5119/*
5120 * chk_modeline() - check a single line for a mode string
5121 * Return FAIL if an error encountered.
5122 */
5123 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005124chk_modeline(
5125 linenr_T lnum,
5126 int flags) /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127{
5128 char_u *s;
5129 char_u *e;
5130 char_u *linecopy; /* local copy of any modeline found */
5131 int prev;
5132 int vers;
5133 int end;
5134 int retval = OK;
5135 char_u *save_sourcing_name;
5136 linenr_T save_sourcing_lnum;
5137#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005138 sctx_T save_current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139#endif
5140
5141 prev = -1;
5142 for (s = ml_get(lnum); *s != NUL; ++s)
5143 {
5144 if (prev == -1 || vim_isspace(prev))
5145 {
5146 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5147 || STRNCMP(s, "vi:", (size_t)3) == 0)
5148 break;
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005149 /* Accept both "vim" and "Vim". */
5150 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 {
5152 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5153 e = s + 4;
5154 else
5155 e = s + 3;
5156 vers = getdigits(&e);
5157 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005158 && (s[0] != 'V'
5159 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 && (s[3] == ':'
5161 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5162 || (VIM_VERSION_100 < vers && s[3] == '<')
5163 || (VIM_VERSION_100 > vers && s[3] == '>')
5164 || (VIM_VERSION_100 == vers && s[3] == '=')))
5165 break;
5166 }
5167 }
5168 prev = *s;
5169 }
5170
5171 if (*s)
5172 {
5173 do /* skip over "ex:", "vi:" or "vim:" */
5174 ++s;
5175 while (s[-1] != ':');
5176
5177 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5178 if (linecopy == NULL)
5179 return FAIL;
5180
5181 save_sourcing_lnum = sourcing_lnum;
5182 save_sourcing_name = sourcing_name;
5183 sourcing_lnum = lnum; /* prepare for emsg() */
5184 sourcing_name = (char_u *)"modelines";
5185
5186 end = FALSE;
5187 while (end == FALSE)
5188 {
5189 s = skipwhite(s);
5190 if (*s == NUL)
5191 break;
5192
5193 /*
5194 * Find end of set command: ':' or end of line.
5195 * Skip over "\:", replacing it with ":".
5196 */
5197 for (e = s; *e != ':' && *e != NUL; ++e)
5198 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005199 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 if (*e == NUL)
5201 end = TRUE;
5202
5203 /*
5204 * If there is a "set" command, require a terminating ':' and
5205 * ignore the stuff after the ':'.
5206 * "vi:set opt opt opt: foo" -- foo not interpreted
5207 * "vi:opt opt opt: foo" -- foo interpreted
5208 * Accept "se" for compatibility with Elvis.
5209 */
5210 if (STRNCMP(s, "set ", (size_t)4) == 0
5211 || STRNCMP(s, "se ", (size_t)3) == 0)
5212 {
5213 if (*e != ':') /* no terminating ':'? */
5214 break;
5215 end = TRUE;
5216 s = vim_strchr(s, ' ') + 1;
5217 }
5218 *e = NUL; /* truncate the set command */
5219
5220 if (*s != NUL) /* skip over an empty "::" */
5221 {
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005222 int secure_save = secure;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005224 save_current_sctx = current_sctx;
5225 current_sctx.sc_sid = SID_MODELINE;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005226 current_sctx.sc_seq = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005227 current_sctx.sc_lnum = 0;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02005228 current_sctx.sc_version = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229#endif
Bram Moolenaar5958f952018-11-20 04:25:21 +01005230 // Make sure no risky things are executed as a side effect.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01005231 secure = 1;
Bram Moolenaar5958f952018-11-20 04:25:21 +01005232
Bram Moolenaara3227e22006-03-08 21:32:40 +00005233 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar5958f952018-11-20 04:25:21 +01005234
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005235 secure = secure_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005237 current_sctx = save_current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238#endif
5239 if (retval == FAIL) /* stop if error found */
5240 break;
5241 }
5242 s = e + 1; /* advance to next part */
5243 }
5244
5245 sourcing_lnum = save_sourcing_lnum;
5246 sourcing_name = save_sourcing_name;
5247
5248 vim_free(linecopy);
5249 }
5250 return retval;
5251}
5252
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005253/*
Bram Moolenaar91335e52018-08-01 17:53:12 +02005254 * Return TRUE if "buf" is a normal buffer, 'buftype' is empty.
5255 */
5256 int
5257bt_normal(buf_T *buf)
5258{
5259 return buf != NULL && buf->b_p_bt[0] == NUL;
5260}
5261
Bram Moolenaar113e1072019-01-20 15:30:40 +01005262#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar91335e52018-08-01 17:53:12 +02005263/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005264 * Return TRUE if "buf" is the quickfix buffer.
5265 */
5266 int
5267bt_quickfix(buf_T *buf)
5268{
5269 return buf != NULL && buf->b_p_bt[0] == 'q';
5270}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005271#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005272
Bram Moolenaar113e1072019-01-20 15:30:40 +01005273#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005274/*
5275 * Return TRUE if "buf" is a terminal buffer.
5276 */
5277 int
5278bt_terminal(buf_T *buf)
5279{
5280 return buf != NULL && buf->b_p_bt[0] == 't';
5281}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005282#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005283
5284/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005285 * Return TRUE if "buf" is a help buffer.
5286 */
5287 int
5288bt_help(buf_T *buf)
5289{
5290 return buf != NULL && buf->b_help;
5291}
5292
5293/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02005294 * Return TRUE if "buf" is a prompt buffer.
5295 */
5296 int
5297bt_prompt(buf_T *buf)
5298{
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005299 return buf != NULL && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'r';
5300}
5301
5302/*
5303 * Return TRUE if "buf" is a buffer for a popup window.
5304 */
5305 int
5306bt_popup(buf_T *buf)
5307{
5308 return buf != NULL && buf->b_p_bt != NULL
5309 && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'o';
Bram Moolenaarf2732452018-06-03 14:47:35 +02005310}
5311
5312/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005313 * Return TRUE if "buf" is a "nofile", "acwrite", "terminal" or "prompt"
5314 * buffer. This means the buffer name is not a file name.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005315 */
5316 int
Bram Moolenaar26910de2019-06-15 19:37:15 +02005317bt_nofilename(buf_T *buf)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005318{
5319 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5320 || buf->b_p_bt[0] == 'a'
Bram Moolenaarf2732452018-06-03 14:47:35 +02005321 || buf->b_p_bt[0] == 't'
5322 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005323}
5324
5325/*
Bram Moolenaar26910de2019-06-15 19:37:15 +02005326 * Return TRUE if "buf" has 'buftype' set to "nofile".
5327 */
5328 int
5329bt_nofile(buf_T *buf)
5330{
5331 return buf != NULL && buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f';
5332}
5333
5334/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005335 * Return TRUE if "buf" is a "nowrite", "nofile", "terminal" or "prompt"
5336 * buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005337 */
5338 int
5339bt_dontwrite(buf_T *buf)
5340{
Bram Moolenaarf2732452018-06-03 14:47:35 +02005341 return buf != NULL && (buf->b_p_bt[0] == 'n'
5342 || buf->b_p_bt[0] == 't'
5343 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005344}
5345
Bram Moolenaar113e1072019-01-20 15:30:40 +01005346#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005347 int
5348bt_dontwrite_msg(buf_T *buf)
5349{
5350 if (bt_dontwrite(buf))
5351 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005352 emsg(_("E382: Cannot write, 'buftype' option is set"));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005353 return TRUE;
5354 }
5355 return FALSE;
5356}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005357#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005358
5359/*
5360 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
5361 * and 'bufhidden'.
5362 */
5363 int
5364buf_hide(buf_T *buf)
5365{
5366 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
5367 switch (buf->b_p_bh[0])
5368 {
5369 case 'u': /* "unload" */
5370 case 'w': /* "wipe" */
5371 case 'd': return FALSE; /* "delete" */
5372 case 'h': return TRUE; /* "hide" */
5373 }
5374 return (p_hid || cmdmod.hide);
5375}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376
5377/*
5378 * Return special buffer name.
5379 * Returns NULL when the buffer has a normal file name.
5380 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005381 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005382buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383{
Bram Moolenaar4033c552017-09-16 20:54:51 +02005384#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005386 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005387 /*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005388 * Differentiate between the quickfix and location list buffers using
5389 * the buffer number stored in the global quickfix stack.
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005390 */
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005391 if (buf->b_fnum == qf_stack_get_bufnr())
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005392 return (char_u *)_(msg_qflist);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005393 else
5394 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02005397
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 /* There is no _file_ when 'buftype' is "nofile", b_sfname
Bram Moolenaar21554412017-07-24 21:44:43 +02005399 * contains the name as specified by the user. */
Bram Moolenaar26910de2019-06-15 19:37:15 +02005400 if (bt_nofilename(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 {
Bram Moolenaar21554412017-07-24 21:44:43 +02005402#ifdef FEAT_TERMINAL
5403 if (buf->b_term != NULL)
5404 return term_get_status_text(buf->b_term);
5405#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02005406 if (buf->b_fname != NULL)
5407 return buf->b_fname;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02005408#ifdef FEAT_JOB_CHANNEL
5409 if (bt_prompt(buf))
5410 return (char_u *)_("[Prompt]");
5411#endif
Bram Moolenaarc6896e22019-05-30 22:32:34 +02005412#ifdef FEAT_TEXT_PROP
5413 if (bt_popup(buf))
5414 return (char_u *)_("[Popup]");
5415#endif
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005416 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 }
Bram Moolenaar21554412017-07-24 21:44:43 +02005418
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005420 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 return NULL;
5422}
5423
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02005424#if defined(FEAT_JOB_CHANNEL) \
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005425 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5426 || defined(PROTO)
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02005427# define SWITCH_TO_WIN
5428
5429/*
5430 * Find a window that contains "buf" and switch to it.
5431 * If there is no such window, use the current window and change "curbuf".
5432 * Caller must initialize save_curbuf to NULL.
5433 * restore_win_for_buf() MUST be called later!
5434 */
5435 void
5436switch_to_win_for_buf(
5437 buf_T *buf,
5438 win_T **save_curwinp,
5439 tabpage_T **save_curtabp,
5440 bufref_T *save_curbuf)
5441{
5442 win_T *wp;
5443 tabpage_T *tp;
5444
5445 if (find_win_for_buf(buf, &wp, &tp) == FAIL)
5446 switch_buffer(save_curbuf, buf);
5447 else if (switch_win(save_curwinp, save_curtabp, wp, tp, TRUE) == FAIL)
5448 {
5449 restore_win(*save_curwinp, *save_curtabp, TRUE);
5450 switch_buffer(save_curbuf, buf);
5451 }
5452}
5453
5454 void
5455restore_win_for_buf(
5456 win_T *save_curwin,
5457 tabpage_T *save_curtab,
5458 bufref_T *save_curbuf)
5459{
5460 if (save_curbuf->br_buf == NULL)
5461 restore_win(save_curwin, save_curtab, TRUE);
5462 else
5463 restore_buffer(save_curbuf);
5464}
5465#endif
5466
Bram Moolenaar4033c552017-09-16 20:54:51 +02005467#if defined(FEAT_QUICKFIX) || defined(SWITCH_TO_WIN) || defined(PROTO)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005468/*
5469 * Find a window for buffer "buf".
5470 * If found OK is returned and "wp" and "tp" are set to the window and tabpage.
5471 * If not found FAIL is returned.
5472 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005473 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005474find_win_for_buf(
5475 buf_T *buf,
5476 win_T **wp,
5477 tabpage_T **tp)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005478{
5479 FOR_ALL_TAB_WINDOWS(*tp, *wp)
5480 if ((*wp)->w_buffer == buf)
5481 goto win_found;
5482 return FAIL;
5483win_found:
5484 return OK;
5485}
5486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488/*
5489 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5490 */
5491 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005492set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493{
5494 if (on != curbuf->b_p_bl)
5495 {
5496 curbuf->b_p_bl = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 if (on)
5498 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5499 else
5500 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 }
5502}
5503
5504/*
5505 * Read the file for "buf" again and check if the contents changed.
5506 * Return TRUE if it changed or this could not be checked.
5507 */
5508 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005509buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510{
5511 buf_T *newbuf;
5512 int differ = TRUE;
5513 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 exarg_T ea;
5516
5517 /* Allocate a buffer without putting it in the buffer list. */
5518 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5519 if (newbuf == NULL)
5520 return TRUE;
5521
5522 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5523 if (prep_exarg(&ea, buf) == FAIL)
5524 {
5525 wipe_buffer(newbuf, FALSE);
5526 return TRUE;
5527 }
5528
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 /* set curwin/curbuf to buf and save a few things */
5530 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531
Bram Moolenaar4770d092006-01-12 23:22:24 +00005532 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 && readfile(buf->b_ffname, buf->b_fname,
5534 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5535 &ea, READ_NEW | READ_DUMMY) == OK)
5536 {
5537 /* compare the two files line by line */
5538 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5539 {
5540 differ = FALSE;
5541 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5542 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5543 {
5544 differ = TRUE;
5545 break;
5546 }
5547 }
5548 }
5549 vim_free(ea.cmd);
5550
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 /* restore curwin/curbuf and a few other things */
5552 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553
5554 if (curbuf != newbuf) /* safety check */
5555 wipe_buffer(newbuf, FALSE);
5556
5557 return differ;
5558}
5559
5560/*
5561 * Wipe out a buffer and decrement the last buffer number if it was used for
5562 * this buffer. Call this to wipe out a temp buffer that does not contain any
5563 * marks.
5564 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005566wipe_buffer(
5567 buf_T *buf,
5568 int aucmd UNUSED) /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569{
5570 if (buf->b_fnum == top_file_num - 1)
5571 --top_file_num;
5572
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005574 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005575
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005576 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005577
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005579 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580}
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005581
5582#if defined(FEAT_EVAL) || defined(PROTO)
5583/*
5584 * Mark references in functions of buffers.
5585 */
5586 int
5587set_ref_in_buffers(int copyID)
5588{
5589 int abort = FALSE;
5590 buf_T *bp;
5591
5592 FOR_ALL_BUFFERS(bp)
5593 {
5594 listener_T *lnr;
5595 typval_T tv;
5596
5597 for (lnr = bp->b_listener; !abort && lnr != NULL; lnr = lnr->lr_next)
5598 {
5599 if (lnr->lr_callback.cb_partial != NULL)
5600 {
5601 tv.v_type = VAR_PARTIAL;
5602 tv.vval.v_partial = lnr->lr_callback.cb_partial;
5603 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
5604 }
5605 }
5606# ifdef FEAT_JOB_CHANNEL
5607 if (!abort && bp->b_prompt_callback.cb_partial != NULL)
5608 {
5609 tv.v_type = VAR_PARTIAL;
5610 tv.vval.v_partial = bp->b_prompt_callback.cb_partial;
5611 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
5612 }
5613 if (!abort && bp->b_prompt_interrupt.cb_partial != NULL)
5614 {
5615 tv.v_type = VAR_PARTIAL;
5616 tv.vval.v_partial = bp->b_prompt_interrupt.cb_partial;
5617 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
5618 }
5619# endif
5620 if (abort)
5621 break;
5622 }
5623 return abort;
5624}
5625#endif