blob: f9686a7bd8cc3c8b46d66b25029d71f001265b39 [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);
313#ifdef FEAT_INS_EXPAND
314 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
315#endif
316
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 /* require "!" to overwrite the file, because it wasn't read completely */
318#ifdef FEAT_EVAL
319 if (aborting())
320#else
321 if (got_int)
322#endif
323 curbuf->b_flags |= BF_READERR;
324
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000325#ifdef FEAT_FOLDING
326 /* Need to update automatic folding. Do this before the autocommands,
327 * they may use the fold info. */
328 foldUpdateAll(curwin);
329#endif
330
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 /* need to set w_topline, unless some autocommand already did that. */
332 if (!(curwin->w_valid & VALID_TOPLINE))
333 {
334 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100335#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100337#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100339#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100341#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343#endif
344
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100345 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 /*
348 * The autocommands may have changed the current buffer. Apply the
349 * modelines to the correct buffer, if it still exists and is loaded.
350 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200351 if (bufref_valid(&old_curbuf) && old_curbuf.br_buf->b_ml.ml_mfp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 {
353 aco_save_T aco;
354
355 /* Go to the buffer that was opened. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200356 aucmd_prepbuf(&aco, old_curbuf.br_buf);
Bram Moolenaara3227e22006-03-08 21:32:40 +0000357 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
359
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100360#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100362 &retval);
363#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366
367 /* restore curwin/curbuf and a few other things */
368 aucmd_restbuf(&aco);
369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 }
371
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 return retval;
373}
374
375/*
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200376 * Store "buf" in "bufref" and set the free count.
377 */
378 void
379set_bufref(bufref_T *bufref, buf_T *buf)
380{
381 bufref->br_buf = buf;
Bram Moolenaarfadacf02017-06-19 20:35:32 +0200382 bufref->br_fnum = buf == NULL ? 0 : buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200383 bufref->br_buf_free_count = buf_free_count;
384}
385
386/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200387 * Return TRUE if "bufref->br_buf" points to the same buffer as when
388 * set_bufref() was called and it is a valid buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200389 * Only goes through the buffer list if buf_free_count changed.
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200390 * Also checks if b_fnum is still the same, a :bwipe followed by :new might get
391 * the same allocated memory, but it's a different buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200392 */
393 int
394bufref_valid(bufref_T *bufref)
395{
396 return bufref->br_buf_free_count == buf_free_count
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200397 ? TRUE : buf_valid(bufref->br_buf)
398 && bufref->br_fnum == bufref->br_buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200399}
400
401/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200403 * This can be slow if there are many buffers, prefer using bufref_valid().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 */
405 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100406buf_valid(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407{
408 buf_T *bp;
409
Bram Moolenaar82404332016-07-10 17:00:38 +0200410 /* Assume that we more often have a recent buffer, start with the last
411 * one. */
412 for (bp = lastbuf; bp != NULL; bp = bp->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413 if (bp == buf)
414 return TRUE;
415 return FALSE;
416}
417
418/*
Bram Moolenaar480778b2016-07-14 22:09:39 +0200419 * A hash table used to quickly lookup a buffer by its number.
420 */
421static hashtab_T buf_hashtab;
422
423 static void
424buf_hashtab_add(buf_T *buf)
425{
426 sprintf((char *)buf->b_key, "%x", buf->b_fnum);
427 if (hash_add(&buf_hashtab, buf->b_key) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100428 emsg(_("E931: Buffer cannot be registered"));
Bram Moolenaar480778b2016-07-14 22:09:39 +0200429}
430
431 static void
432buf_hashtab_remove(buf_T *buf)
433{
434 hashitem_T *hi = hash_find(&buf_hashtab, buf->b_key);
435
436 if (!HASHITEM_EMPTY(hi))
437 hash_remove(&buf_hashtab, hi);
438}
439
Bram Moolenaar94f01952018-09-01 15:30:03 +0200440/*
441 * Return TRUE when buffer "buf" can be unloaded.
442 * Give an error message and return FALSE when the buffer is locked or the
443 * screen is being redrawn and the buffer is in a window.
444 */
445 static int
446can_unload_buffer(buf_T *buf)
447{
448 int can_unload = !buf->b_locked;
449
450 if (can_unload && updating_screen)
451 {
452 win_T *wp;
453
454 FOR_ALL_WINDOWS(wp)
455 if (wp->w_buffer == buf)
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200456 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200457 can_unload = FALSE;
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200458 break;
459 }
Bram Moolenaar94f01952018-09-01 15:30:03 +0200460 }
461 if (!can_unload)
Bram Moolenaardefa0672019-07-21 19:25:37 +0200462 semsg(_("E937: Attempt to delete a buffer that is in use: %s"),
463 buf->b_fname);
Bram Moolenaar94f01952018-09-01 15:30:03 +0200464 return can_unload;
465}
Bram Moolenaara997b452018-04-17 23:24:06 +0200466
Bram Moolenaar480778b2016-07-14 22:09:39 +0200467/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468 * Close the link to a buffer.
469 * "action" is used when there is no longer a window for the buffer.
470 * It can be:
471 * 0 buffer becomes hidden
472 * DOBUF_UNLOAD buffer is unloaded
473 * DOBUF_DELETE buffer is unloaded and removed from buffer list
474 * DOBUF_WIPE buffer is unloaded and really deleted
475 * When doing all but the first one on the current buffer, the caller should
476 * get a new buffer very soon!
477 *
478 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100479 *
480 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
481 * cause there to be only one window with this buffer. e.g. when ":quit" is
482 * supposed to close the window but autocommands close all other windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 */
484 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100485close_buffer(
486 win_T *win, /* if not NULL, set b_last_cursor */
487 buf_T *buf,
488 int action,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200489 int abort_if_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100492 int nwindows;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200493 bufref_T bufref;
Bram Moolenaar3a117e12016-10-30 21:57:52 +0100494 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200495 win_T *the_curwin = curwin;
496 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 int unload_buf = (action != 0);
498 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
499 int wipe_buf = (action == DOBUF_WIPE);
500
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200501 /*
502 * Force unloading or deleting when 'bufhidden' says so.
503 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
504 * "hide" (otherwise we could never free or delete a buffer).
505 */
506 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
507 {
508 del_buf = TRUE;
509 unload_buf = TRUE;
510 }
511 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
512 {
513 del_buf = TRUE;
514 unload_buf = TRUE;
515 wipe_buf = TRUE;
516 }
517 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
518 unload_buf = TRUE;
519
Bram Moolenaar94053a52017-08-01 21:44:33 +0200520#ifdef FEAT_TERMINAL
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200521 if (bt_terminal(buf) && (buf->b_nwindows == 1 || del_buf))
Bram Moolenaar94053a52017-08-01 21:44:33 +0200522 {
523 if (term_job_running(buf->b_term))
524 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200525 if (wipe_buf || unload_buf)
Bram Moolenaara997b452018-04-17 23:24:06 +0200526 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200527 if (!can_unload_buffer(buf))
Bram Moolenaara997b452018-04-17 23:24:06 +0200528 return;
Bram Moolenaar94f01952018-09-01 15:30:03 +0200529
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200530 /* Wiping out or unloading a terminal buffer kills the job. */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200531 free_terminal(buf);
Bram Moolenaara997b452018-04-17 23:24:06 +0200532 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200533 else
534 {
535 /* The job keeps running, hide the buffer. */
536 del_buf = FALSE;
537 unload_buf = FALSE;
538 }
539 }
540 else
541 {
542 /* A terminal buffer is wiped out if the job has finished. */
543 del_buf = TRUE;
544 unload_buf = TRUE;
545 wipe_buf = TRUE;
546 }
547 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200548#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200550 /* Disallow deleting the buffer when it is locked (already being closed or
551 * halfway a command that relies on it). Unloading is allowed. */
Bram Moolenaar94f01952018-09-01 15:30:03 +0200552 if ((del_buf || wipe_buf) && !can_unload_buffer(buf))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200553 return;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200554
Bram Moolenaar4033c552017-09-16 20:54:51 +0200555 /* check no autocommands closed the window */
556 if (win != NULL && win_valid_any_tab(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 {
558 /* Set b_last_cursor when closing the last window for the buffer.
559 * Remember the last cursor position and window options of the buffer.
560 * This used to be only for the current window, but then options like
561 * 'foldmethod' may be lost with a ":only" command. */
562 if (buf->b_nwindows == 1)
563 set_last_cursor(win);
564 buflist_setfpos(buf, win,
565 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
566 win->w_cursor.col, TRUE);
567 }
568
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200569 set_bufref(&bufref, buf);
570
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 /* When the buffer is no longer in a window, trigger BufWinLeave */
572 if (buf->b_nwindows == 1)
573 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200574 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200575 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
576 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200577 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100578 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200579 /* Autocommands deleted the buffer. */
580aucmd_abort:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100581 emsg(_(e_auabort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 return;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100583 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200584 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200585 if (abort_if_last && one_window())
586 /* Autocommands made this the only window. */
587 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588
589 /* When the buffer becomes hidden, but is not unloaded, trigger
590 * BufHidden */
591 if (!unload_buf)
592 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200593 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200594 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
595 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200596 && !bufref_valid(&bufref))
Bram Moolenaar362ce482012-06-06 19:02:45 +0200597 /* Autocommands deleted the buffer. */
598 goto aucmd_abort;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200599 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200600 if (abort_if_last && one_window())
601 /* Autocommands made this the only window. */
602 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100604#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 if (aborting()) /* autocmds may abort script processing */
606 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200609
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200610 /* If the buffer was in curwin and the window has changed, go back to that
611 * window, if it still exists. This avoids that ":edit x" triggering a
612 * "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
613 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
614 {
615 block_autocmds();
616 goto_tabpage_win(the_curtab, the_curwin);
617 unblock_autocmds();
618 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200619
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 nwindows = buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621
622 /* decrease the link count from windows (unless not in any window) */
623 if (buf->b_nwindows > 0)
624 --buf->b_nwindows;
625
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100626#ifdef FEAT_DIFF
627 if (diffopt_hiddenoff() && !unload_buf && buf->b_nwindows == 0)
Bram Moolenaarb7407d32018-02-03 17:36:27 +0100628 diff_buf_delete(buf); /* Clear 'diff' for hidden buffer. */
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100629#endif
630
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 /* Return when a window is displaying the buffer or when it's not
632 * unloaded. */
633 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635
636 /* Always remove the buffer when there is no file name. */
637 if (buf->b_ffname == NULL)
638 del_buf = TRUE;
639
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200640 /* When closing the current buffer stop Visual mode before freeing
641 * anything. */
Bram Moolenaar4930a762016-09-11 14:39:53 +0200642 if (buf == curbuf && VIsual_active
Bram Moolenaar9a27c7f2016-09-09 12:57:09 +0200643#if defined(EXITFREE)
644 && !entered_free_all_mem
645#endif
646 )
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200647 end_visual_mode();
648
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 /*
650 * Free all things allocated for this buffer.
651 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
652 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 /* Remember if we are closing the current buffer. Restore the number of
654 * windows, so that autocommands in buf_freeall() don't get confused. */
655 is_curbuf = (buf == curbuf);
656 buf->b_nwindows = nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200658 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 /* Autocommands may have deleted the buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200661 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100663#ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000664 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 /*
669 * It's possible that autocommands change curbuf to the one being deleted.
670 * This might cause the previous curbuf to be deleted unexpectedly. But
671 * in some cases it's OK to delete the curbuf, because a new one is
672 * obtained anyway. Therefore only return if curbuf changed to the
673 * deleted buffer.
674 */
675 if (buf == curbuf && !is_curbuf)
676 return;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200677
Bram Moolenaar4033c552017-09-16 20:54:51 +0200678 if (win_valid_any_tab(win) && win->w_buffer == buf)
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200679 win->w_buffer = NULL; /* make sure we don't use the buffer now */
680
681 /* Autocommands may have opened or closed windows for this buffer.
682 * Decrement the count for the close we do here. */
683 if (buf->b_nwindows > 0)
684 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 /*
687 * Remove the buffer from the list.
688 */
689 if (wipe_buf)
690 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +0200691 if (buf->b_sfname != buf->b_ffname)
692 VIM_CLEAR(buf->b_sfname);
693 else
694 buf->b_sfname = NULL;
695 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 if (buf->b_prev == NULL)
697 firstbuf = buf->b_next;
698 else
699 buf->b_prev->b_next = buf->b_next;
700 if (buf->b_next == NULL)
701 lastbuf = buf->b_prev;
702 else
703 buf->b_next->b_prev = buf->b_prev;
704 free_buffer(buf);
705 }
706 else
707 {
708 if (del_buf)
709 {
710 /* Free all internal variables and reset option values, to make
711 * ":bdel" compatible with Vim 5.7. */
712 free_buffer_stuff(buf, TRUE);
713
714 /* Make it look like a new buffer. */
715 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
716
717 /* Init the options when loaded again. */
718 buf->b_p_initialized = FALSE;
719 }
720 buf_clear_file(buf);
721 if (del_buf)
722 buf->b_p_bl = FALSE;
723 }
724}
725
726/*
727 * Make buffer not contain a file.
728 */
729 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100730buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731{
732 buf->b_ml.ml_line_count = 1;
Bram Moolenaarc024b462019-06-08 18:07:21 +0200733 unchanged(buf, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 buf->b_p_eol = TRUE;
736 buf->b_start_eol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000738 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 buf->b_ml.ml_mfp = NULL;
740 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
741#ifdef FEAT_NETBEANS_INTG
742 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743#endif
744}
745
746/*
747 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200748 * the file. Careful: get here with "curwin" NULL when exiting.
749 * flags:
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200750 * BFA_DEL buffer is going to be deleted
751 * BFA_WIPE buffer is going to be wiped out
752 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100755buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200758 bufref_T bufref;
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200759 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaar5a497892016-09-03 16:29:04 +0200760 win_T *the_curwin = curwin;
761 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762
Bram Moolenaar5a497892016-09-03 16:29:04 +0200763 /* Make sure the buffer isn't closed by autocommands. */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200764 ++buf->b_locked;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200765 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200766 if (buf->b_ml.ml_mfp != NULL)
767 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200768 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
769 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200770 && !bufref_valid(&bufref))
771 /* autocommands deleted the buffer */
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200772 return;
773 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200774 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200776 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
777 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200778 && !bufref_valid(&bufref))
779 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 return;
781 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200782 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200784 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
785 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200786 && !bufref_valid(&bufref))
787 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 return;
789 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200790 --buf->b_locked;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200791
Bram Moolenaar5a497892016-09-03 16:29:04 +0200792 /* If the buffer was in curwin and the window has changed, go back to that
793 * window, if it still exists. This avoids that ":edit x" triggering a
794 * "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
795 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
796 {
797 block_autocmds();
798 goto_tabpage_win(the_curtab, the_curwin);
799 unblock_autocmds();
800 }
Bram Moolenaar5a497892016-09-03 16:29:04 +0200801
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100802#ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000803 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806
807 /*
808 * It's possible that autocommands change curbuf to the one being deleted.
809 * This might cause curbuf to be deleted unexpectedly. But in some cases
810 * it's OK to delete the curbuf, because a new one is obtained anyway.
811 * Therefore only return if curbuf changed to the deleted buffer.
812 */
813 if (buf == curbuf && !is_curbuf)
814 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815#ifdef FEAT_DIFF
816 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
817#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200818#ifdef FEAT_SYN_HL
Bram Moolenaar89c71222011-11-30 15:40:56 +0100819 /* Remove any ownsyntax, unless exiting. */
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200820 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaar89c71222011-11-30 15:40:56 +0100821 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200822#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000823
824#ifdef FEAT_FOLDING
825 /* No folds in an empty buffer. */
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000826 {
827 win_T *win;
828 tabpage_T *tp;
829
830 FOR_ALL_TAB_WINDOWS(tp, win)
831 if (win->w_buffer == buf)
832 clearFolding(win);
833 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000834#endif
835
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836#ifdef FEAT_TCL
837 tcl_buffer_free(buf);
838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 ml_close(buf, TRUE); /* close and delete the memline/memfile */
840 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200841 if ((flags & BFA_KEEP_UNDO) == 0)
842 {
843 u_blockfree(buf); /* free the memory allocated for undo */
844 u_clearall(buf); /* reset all undo information */
845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200847 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100849#ifdef FEAT_TEXT_PROP
850 clear_buf_prop_types(buf);
851#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000852 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853}
854
855/*
856 * Free a buffer structure and the things it contains related to the buffer
857 * itself (not the file, that must have been done already).
858 */
859 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100860free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200862 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200864#ifdef FEAT_EVAL
Bram Moolenaarb2c87502017-10-14 21:15:58 +0200865 /* b:changedtick uses an item in buf_T, remove it now */
866 dictitem_remove(buf->b_vars, (dictitem_T *)&buf->b_ct_di);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200867 unref_var_dict(buf->b_vars);
868#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200869#ifdef FEAT_LUA
870 lua_buffer_free(buf);
871#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000872#ifdef FEAT_MZSCHEME
873 mzscheme_buffer_free(buf);
874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875#ifdef FEAT_PERL
876 perl_buf_free(buf);
877#endif
878#ifdef FEAT_PYTHON
879 python_buffer_free(buf);
880#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200881#ifdef FEAT_PYTHON3
882 python3_buffer_free(buf);
883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884#ifdef FEAT_RUBY
885 ruby_buffer_free(buf);
886#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200887#ifdef FEAT_JOB_CHANNEL
888 channel_buffer_free(buf);
889#endif
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200890#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +0200891 free_terminal(buf);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200892#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +0200893#ifdef FEAT_JOB_CHANNEL
894 vim_free(buf->b_prompt_text);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200895 free_callback(&buf->b_prompt_callback);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200896#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200897
898 buf_hashtab_remove(buf);
899
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200900 aubuflocal_remove(buf);
901
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200902 if (autocmd_busy)
903 {
904 /* Do not free the buffer structure while autocommands are executing,
905 * it's still needed. Free it when autocmd_busy is reset. */
906 buf->b_next = au_pending_free_buf;
907 au_pending_free_buf = buf;
908 }
909 else
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200910 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911}
912
913/*
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100914 * Initializes b:changedtick.
Bram Moolenaar79518e22017-02-17 16:31:35 +0100915 */
916 static void
917init_changedtick(buf_T *buf)
918{
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100919 dictitem_T *di = (dictitem_T *)&buf->b_ct_di;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100920
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100921 di->di_flags = DI_FLAGS_FIX | DI_FLAGS_RO;
922 di->di_tv.v_type = VAR_NUMBER;
923 di->di_tv.v_lock = VAR_FIXED;
924 di->di_tv.vval.v_number = 0;
925
Bram Moolenaar92769c32017-02-25 15:41:37 +0100926#ifdef FEAT_EVAL
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100927 STRCPY(buf->b_ct_di.di_key, "changedtick");
928 (void)dict_add(buf->b_vars, di);
Bram Moolenaar92769c32017-02-25 15:41:37 +0100929#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +0100930}
931
932/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
934 */
935 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100936free_buffer_stuff(
937 buf_T *buf,
938 int free_options) /* free options as well */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939{
940 if (free_options)
941 {
942 clear_wininfo(buf); /* including window-local options */
943 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200944#ifdef FEAT_SPELL
945 ga_clear(&buf->b_s.b_langp);
946#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 }
948#ifdef FEAT_EVAL
Bram Moolenaar79518e22017-02-17 16:31:35 +0100949 {
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100950 varnumber_T tick = CHANGEDTICK(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +0100951
952 vars_clear(&buf->b_vars->dv_hashtab); /* free all buffer variables */
953 hash_init(&buf->b_vars->dv_hashtab);
954 init_changedtick(buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100955 CHANGEDTICK(buf) = tick;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957#endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200958 uc_clear(&buf->b_ucmds); // clear local user commands
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959#ifdef FEAT_SIGNS
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200960 buf_delete_signs(buf, (char_u *)"*"); // delete any signs
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000962#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200963 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
966 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100967 VIM_CLEAR(buf->b_start_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968}
969
970/*
971 * Free the b_wininfo list for buffer "buf".
972 */
973 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100974clear_wininfo(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975{
976 wininfo_T *wip;
977
978 while (buf->b_wininfo != NULL)
979 {
980 wip = buf->b_wininfo;
981 buf->b_wininfo = wip->wi_next;
982 if (wip->wi_optset)
983 {
984 clear_winopt(&wip->wi_opt);
985#ifdef FEAT_FOLDING
986 deleteFoldRecurse(&wip->wi_folds);
987#endif
988 }
989 vim_free(wip);
990 }
991}
992
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993/*
994 * Go to another buffer. Handles the result of the ATTENTION dialog.
995 */
996 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100997goto_buffer(
998 exarg_T *eap,
999 int start,
1000 int dir,
1001 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001003 bufref_T old_curbuf;
1004
1005 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006
1007 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
1009 start, dir, count, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
1011 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001012#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001013 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001014
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001015 /* Reset the error/interrupt/exception state here so that
1016 * aborting() returns FALSE when closing a window. */
1017 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001018#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001019
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001020 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 win_close(curwin, TRUE);
1022 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001023 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001024
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001025#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001026 /* Restore the error/interrupt/exception state if not discarded by a
1027 * new aborting error, interrupt, or uncaught exception. */
1028 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 }
1031 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001032 handle_swap_exists(&old_curbuf);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001033}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035/*
1036 * Handle the situation of swap_exists_action being set.
1037 * It is allowed for "old_curbuf" to be NULL or invalid.
1038 */
1039 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001040handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041{
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001042#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001043 cleanup_T cs;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001044#endif
1045#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001046 long old_tw = curbuf->b_p_tw;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001047#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001048 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001049
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 if (swap_exists_action == SEA_QUIT)
1051 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001052#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001053 /* Reset the error/interrupt/exception state here so that
1054 * aborting() returns FALSE when closing a buffer. */
1055 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001056#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001057
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 /* User selected Quit at ATTENTION prompt. Go back to previous
1059 * buffer. If that buffer is gone or the same as the current one,
1060 * open a new, empty buffer. */
1061 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001062 swap_exists_did_quit = TRUE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001063 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001064 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1065 || old_curbuf->br_buf == curbuf)
1066 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
1067 else
1068 buf = old_curbuf->br_buf;
1069 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001070 {
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001071 int old_msg_silent = msg_silent;
1072
1073 if (shortmess(SHM_FILEINFO))
1074 msg_silent = 1; // prevent fileinfo message
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001075 enter_buffer(buf);
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001076 // restore msg_silent, so that the command line will be shown
1077 msg_silent = old_msg_silent;
1078
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001079#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001080 if (old_tw != curbuf->b_p_tw)
1081 check_colorcolumn(curwin);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001082#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001085
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001086#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001087 /* Restore the error/interrupt/exception state if not discarded by a
1088 * new aborting error, interrupt, or uncaught exception. */
1089 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 }
1092 else if (swap_exists_action == SEA_RECOVER)
1093 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001094#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001095 /* Reset the error/interrupt/exception state here so that
1096 * aborting() returns FALSE when closing a buffer. */
1097 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001098#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001099
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 /* User selected Recover at ATTENTION prompt. */
1101 msg_scroll = TRUE;
Bram Moolenaar99499b12019-05-23 21:35:48 +02001102 ml_recover(FALSE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001103 msg_puts("\n"); /* don't overwrite the last message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001105 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001106
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001107#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001108 /* Restore the error/interrupt/exception state if not discarded by a
1109 * new aborting error, interrupt, or uncaught exception. */
1110 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001111#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 }
1113 swap_exists_action = SEA_NONE;
1114}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116/*
1117 * do_bufdel() - delete or unload buffer(s)
1118 *
1119 * addr_count == 0: ":bdel" - delete current buffer
1120 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1121 * buffer "end_bnr", then any other arguments.
1122 * addr_count == 2: ":N,N bdel" - delete buffers in range
1123 *
1124 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1125 * DOBUF_DEL (":bdel")
1126 *
1127 * Returns error message or NULL
1128 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001129 char *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001130do_bufdel(
1131 int command,
1132 char_u *arg, /* pointer to extra arguments */
1133 int addr_count,
1134 int start_bnr, /* first buffer number in a range */
1135 int end_bnr, /* buffer nr or last buffer nr in a range */
1136 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137{
1138 int do_current = 0; /* delete current buffer? */
1139 int deleted = 0; /* number of buffers deleted */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001140 char *errormsg = NULL; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 int bnr; /* buffer number */
1142 char_u *p;
1143
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 if (addr_count == 0)
1145 {
1146 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1147 }
1148 else
1149 {
1150 if (addr_count == 2)
1151 {
1152 if (*arg) /* both range and argument is not allowed */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001153 return _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 bnr = start_bnr;
1155 }
1156 else /* addr_count == 1 */
1157 bnr = end_bnr;
1158
1159 for ( ;!got_int; ui_breakcheck())
1160 {
1161 /*
1162 * delete the current buffer last, otherwise when the
1163 * current buffer is deleted, the next buffer becomes
1164 * the current one and will be loaded, which may then
1165 * also be deleted, etc.
1166 */
1167 if (bnr == curbuf->b_fnum)
1168 do_current = bnr;
1169 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
1170 forceit) == OK)
1171 ++deleted;
1172
1173 /*
1174 * find next buffer number to delete/unload
1175 */
1176 if (addr_count == 2)
1177 {
1178 if (++bnr > end_bnr)
1179 break;
1180 }
1181 else /* addr_count == 1 */
1182 {
1183 arg = skipwhite(arg);
1184 if (*arg == NUL)
1185 break;
1186 if (!VIM_ISDIGIT(*arg))
1187 {
1188 p = skiptowhite_esc(arg);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01001189 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE,
1190 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 if (bnr < 0) /* failed */
1192 break;
1193 arg = p;
1194 }
1195 else
1196 bnr = getdigits(&arg);
1197 }
1198 }
1199 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1200 FORWARD, do_current, forceit) == OK)
1201 ++deleted;
1202
1203 if (deleted == 0)
1204 {
1205 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001206 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001208 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00001210 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001211 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 }
1213 else if (deleted >= p_report)
1214 {
1215 if (command == DOBUF_UNLOAD)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001216 smsg(NGETTEXT("%d buffer unloaded",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001217 "%d buffers unloaded", deleted), deleted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 else if (command == DOBUF_DEL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001219 smsg(NGETTEXT("%d buffer deleted",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001220 "%d buffers deleted", deleted), deleted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001222 smsg(NGETTEXT("%d buffer wiped out",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001223 "%d buffers wiped out", deleted), deleted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 }
1225 }
1226
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227
1228 return errormsg;
1229}
1230
Bram Moolenaara02471e2014-01-10 16:43:14 +01001231/*
1232 * Make the current buffer empty.
1233 * Used when it is wiped out and it's the last buffer.
1234 */
1235 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001236empty_curbuf(
1237 int close_others,
1238 int forceit,
1239 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001240{
1241 int retval;
1242 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001243 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001244
1245 if (action == DOBUF_UNLOAD)
1246 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001247 emsg(_("E90: Cannot unload last buffer"));
Bram Moolenaara02471e2014-01-10 16:43:14 +01001248 return FAIL;
1249 }
1250
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001251 set_bufref(&bufref, buf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001252 if (close_others)
1253 /* Close any other windows on this buffer, then make it empty. */
Bram Moolenaara02471e2014-01-10 16:43:14 +01001254 close_windows(buf, TRUE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001255
1256 setpcmark();
1257 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1258 forceit ? ECMD_FORCEIT : 0, curwin);
1259
1260 /*
1261 * do_ecmd() may create a new buffer, then we have to delete
1262 * the old one. But do_ecmd() may have done that already, check
1263 * if the buffer still exists.
1264 */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001265 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001266 close_buffer(NULL, buf, action, FALSE);
1267 if (!close_others)
1268 need_fileinfo = FALSE;
1269 return retval;
1270}
Bram Moolenaar94f01952018-09-01 15:30:03 +02001271
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272/*
1273 * Implementation of the commands for the buffer list.
1274 *
1275 * action == DOBUF_GOTO go to specified buffer
1276 * action == DOBUF_SPLIT split window and go to specified buffer
1277 * action == DOBUF_UNLOAD unload specified buffer(s)
1278 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1279 * action == DOBUF_WIPE delete specified buffer(s) really
1280 *
1281 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1282 * start == DOBUF_FIRST go to "count" buffer from first buffer
1283 * start == DOBUF_LAST go to "count" buffer from last buffer
1284 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1285 *
1286 * Return FAIL or OK.
1287 */
1288 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001289do_buffer(
1290 int action,
1291 int start,
1292 int dir, /* FORWARD or BACKWARD */
1293 int count, /* buffer number or number of buffers */
1294 int forceit) /* TRUE for :...! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295{
1296 buf_T *buf;
1297 buf_T *bp;
1298 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1299 || action == DOBUF_WIPE);
1300
1301 switch (start)
1302 {
1303 case DOBUF_FIRST: buf = firstbuf; break;
1304 case DOBUF_LAST: buf = lastbuf; break;
1305 default: buf = curbuf; break;
1306 }
1307 if (start == DOBUF_MOD) /* find next modified buffer */
1308 {
1309 while (count-- > 0)
1310 {
1311 do
1312 {
1313 buf = buf->b_next;
1314 if (buf == NULL)
1315 buf = firstbuf;
1316 }
1317 while (buf != curbuf && !bufIsChanged(buf));
1318 }
1319 if (!bufIsChanged(buf))
1320 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001321 emsg(_("E84: No modified buffer found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 return FAIL;
1323 }
1324 }
1325 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
1326 {
1327 while (buf != NULL && buf->b_fnum != count)
1328 buf = buf->b_next;
1329 }
1330 else
1331 {
1332 bp = NULL;
1333 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1334 {
1335 /* remember the buffer where we start, we come back there when all
1336 * buffers are unlisted. */
1337 if (bp == NULL)
1338 bp = buf;
1339 if (dir == FORWARD)
1340 {
1341 buf = buf->b_next;
1342 if (buf == NULL)
1343 buf = firstbuf;
1344 }
1345 else
1346 {
1347 buf = buf->b_prev;
1348 if (buf == NULL)
1349 buf = lastbuf;
1350 }
1351 /* don't count unlisted buffers */
1352 if (unload || buf->b_p_bl)
1353 {
1354 --count;
1355 bp = NULL; /* use this buffer as new starting point */
1356 }
1357 if (bp == buf)
1358 {
1359 /* back where we started, didn't find anything. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001360 emsg(_("E85: There is no listed buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 return FAIL;
1362 }
1363 }
1364 }
1365
1366 if (buf == NULL) /* could not find it */
1367 {
1368 if (start == DOBUF_FIRST)
1369 {
1370 /* don't warn when deleting */
1371 if (!unload)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001372 semsg(_(e_nobufnr), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 }
1374 else if (dir == FORWARD)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001375 emsg(_("E87: Cannot go beyond last buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001377 emsg(_("E88: Cannot go before first buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 return FAIL;
1379 }
1380
1381#ifdef FEAT_GUI
1382 need_mouse_correct = TRUE;
1383#endif
1384
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 /*
1386 * delete buffer buf from memory and/or the list
1387 */
1388 if (unload)
1389 {
1390 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001391 bufref_T bufref;
1392
Bram Moolenaar94f01952018-09-01 15:30:03 +02001393 if (!can_unload_buffer(buf))
Bram Moolenaara997b452018-04-17 23:24:06 +02001394 return FAIL;
Bram Moolenaara997b452018-04-17 23:24:06 +02001395
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001396 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397
1398 /* When unloading or deleting a buffer that's already unloaded and
1399 * unlisted: fail silently. */
1400 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1401 return FAIL;
1402
1403 if (!forceit && bufIsChanged(buf))
1404 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001405#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 if ((p_confirm || cmdmod.confirm) && p_write)
1407 {
1408 dialog_changed(buf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001409 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 /* Autocommand deleted buffer, oops! It's not changed
1411 * now. */
1412 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001413 /* If it's still changed fail silently, the dialog already
1414 * mentioned why it fails. */
1415 if (bufIsChanged(buf))
1416 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001418 else
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001421 semsg(_("E89: No write since last change for buffer %d (add ! to override)"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 buf->b_fnum);
1423 return FAIL;
1424 }
1425 }
1426
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001427 /* When closing the current buffer stop Visual mode. */
Bram Moolenaar4930a762016-09-11 14:39:53 +02001428 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001429 end_visual_mode();
1430
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 /*
1432 * If deleting the last (listed) buffer, make it empty.
1433 * The last (listed) buffer cannot be unloaded.
1434 */
Bram Moolenaar29323592016-07-24 22:04:11 +02001435 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 if (bp->b_p_bl && bp != buf)
1437 break;
1438 if (bp == NULL && buf == curbuf)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001439 return empty_curbuf(TRUE, forceit, action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 /*
1442 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001443 * (unless it's the only window). Repeat this so long as we end up in
1444 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001446 while (buf == curbuf
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001447 && !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
Bram Moolenaar459ca562016-11-10 18:16:33 +01001448 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001449 {
1450 if (win_close(curwin, FALSE) == FAIL)
1451 break;
1452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453
1454 /*
1455 * If the buffer to be deleted is not the current one, delete it here.
1456 */
1457 if (buf != curbuf)
1458 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001459 close_windows(buf, FALSE);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001460 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows <= 0)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001461 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 return OK;
1463 }
1464
1465 /*
1466 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001467 * There should be another, otherwise it would have been handled
1468 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001469 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 * Then prefer the buffer we most recently visited.
1471 * Else try to find one that is loaded, after the current buffer,
1472 * then before the current buffer.
1473 * Finally use any buffer.
1474 */
1475 buf = NULL; /* selected buffer */
1476 bp = NULL; /* used when no loaded buffer found */
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001477 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1478 buf = au_new_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479#ifdef FEAT_JUMPLIST
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001480 else if (curwin->w_jumplistlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 {
1482 int jumpidx;
1483
1484 jumpidx = curwin->w_jumplistidx - 1;
1485 if (jumpidx < 0)
1486 jumpidx = curwin->w_jumplistlen - 1;
1487
1488 forward = jumpidx;
1489 while (jumpidx != curwin->w_jumplistidx)
1490 {
1491 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1492 if (buf != NULL)
1493 {
1494 if (buf == curbuf || !buf->b_p_bl)
1495 buf = NULL; /* skip current and unlisted bufs */
1496 else if (buf->b_ml.ml_mfp == NULL)
1497 {
1498 /* skip unloaded buf, but may keep it for later */
1499 if (bp == NULL)
1500 bp = buf;
1501 buf = NULL;
1502 }
1503 }
1504 if (buf != NULL) /* found a valid buffer: stop searching */
1505 break;
1506 /* advance to older entry in jump list */
1507 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1508 break;
1509 if (--jumpidx < 0)
1510 jumpidx = curwin->w_jumplistlen - 1;
1511 if (jumpidx == forward) /* List exhausted for sure */
1512 break;
1513 }
1514 }
1515#endif
1516
1517 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1518 {
1519 forward = TRUE;
1520 buf = curbuf->b_next;
1521 for (;;)
1522 {
1523 if (buf == NULL)
1524 {
1525 if (!forward) /* tried both directions */
1526 break;
1527 buf = curbuf->b_prev;
1528 forward = FALSE;
1529 continue;
1530 }
1531 /* in non-help buffer, try to skip help buffers, and vv */
1532 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1533 {
1534 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1535 break;
1536 if (bp == NULL) /* remember unloaded buf for later */
1537 bp = buf;
1538 }
1539 if (forward)
1540 buf = buf->b_next;
1541 else
1542 buf = buf->b_prev;
1543 }
1544 }
1545 if (buf == NULL) /* No loaded buffer, use unloaded one */
1546 buf = bp;
1547 if (buf == NULL) /* No loaded buffer, find listed one */
1548 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001549 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 if (buf->b_p_bl && buf != curbuf)
1551 break;
1552 }
1553 if (buf == NULL) /* Still no buffer, just take one */
1554 {
1555 if (curbuf->b_next != NULL)
1556 buf = curbuf->b_next;
1557 else
1558 buf = curbuf->b_prev;
1559 }
1560 }
1561
Bram Moolenaara02471e2014-01-10 16:43:14 +01001562 if (buf == NULL)
1563 {
1564 /* Autocommands must have wiped out all other buffers. Only option
1565 * now is to make the current buffer empty. */
1566 return empty_curbuf(FALSE, forceit, action);
1567 }
1568
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 /*
1570 * make buf current buffer
1571 */
1572 if (action == DOBUF_SPLIT) /* split window first */
1573 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001574 /* If 'switchbuf' contains "useopen": jump to first window containing
1575 * "buf" if one exists */
1576 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001577 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001578 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001579 * page containing "buf" if one exists */
1580 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 return OK;
1582 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 return FAIL;
1584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585
1586 /* go to current buffer - nothing to do */
1587 if (buf == curbuf)
1588 return OK;
1589
1590 /*
1591 * Check if the current buffer may be abandoned.
1592 */
1593 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1594 {
1595#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1596 if ((p_confirm || cmdmod.confirm) && p_write)
1597 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001598 bufref_T bufref;
1599
1600 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 dialog_changed(curbuf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001602 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 /* Autocommand deleted buffer, oops! */
1604 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 }
1606 if (bufIsChanged(curbuf))
1607#endif
1608 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001609 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 return FAIL;
1611 }
1612 }
1613
1614 /* Go to the other buffer. */
1615 set_curbuf(buf, action);
1616
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001618 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001620#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 if (aborting()) /* autocmds may abort script processing */
1622 return FAIL;
1623#endif
1624
1625 return OK;
1626}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627
1628/*
1629 * Set current buffer to "buf". Executes autocommands and closes current
1630 * buffer. "action" tells how to close the current buffer:
1631 * DOBUF_GOTO free or hide it
1632 * DOBUF_SPLIT nothing
1633 * DOBUF_UNLOAD unload it
1634 * DOBUF_DEL delete it
1635 * DOBUF_WIPE wipe it out
1636 */
1637 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001638set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639{
1640 buf_T *prevbuf;
1641 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1642 || action == DOBUF_WIPE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001643#ifdef FEAT_SYN_HL
1644 long old_tw = curbuf->b_p_tw;
1645#endif
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001646 bufref_T newbufref;
1647 bufref_T prevbufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648
1649 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001650 if (!cmdmod.keepalt)
1651 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001652 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 /* Don't restart Select mode after switching to another buffer. */
1655 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001657 /* close_windows() or apply_autocmds() may change curbuf and wipe out "buf"
1658 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 prevbuf = curbuf;
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001660 set_bufref(&prevbufref, prevbuf);
1661 set_bufref(&newbufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001663 /* Autocommands may delete the curren buffer and/or the buffer we wan to go
1664 * to. In those cases don't close the buffer. */
Bram Moolenaar82404332016-07-10 17:00:38 +02001665 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001666 || (bufref_valid(&prevbufref)
1667 && bufref_valid(&newbufref)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001668#ifdef FEAT_EVAL
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001669 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001671 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001673#ifdef FEAT_SYN_HL
1674 if (prevbuf == curwin->w_buffer)
1675 reset_synblock(curwin);
1676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001678 close_windows(prevbuf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001679#if defined(FEAT_EVAL)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001680 if (bufref_valid(&prevbufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681#else
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001682 if (bufref_valid(&prevbufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001684 {
Bram Moolenaare25865a2012-07-06 16:22:02 +02001685 win_T *previouswin = curwin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001686 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001687 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1689 unload ? action : (action == DOBUF_GOTO
Bram Moolenaareb44a682017-08-03 22:44:55 +02001690 && !buf_hide(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001691 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaare25865a2012-07-06 16:22:02 +02001692 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaar9e931222012-06-20 11:55:01 +02001693 /* autocommands changed curwin, Grr! */
Bram Moolenaare25865a2012-07-06 16:22:02 +02001694 curwin = previouswin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 }
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001697 /* An autocommand may have deleted "buf", already entered it (e.g., when
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001698 * it did ":bunload") or aborted the script processing.
Bram Moolenaar9e931222012-06-20 11:55:01 +02001699 * If curwin->w_buffer is null, enter_buffer() will make it valid again */
1700 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001701#ifdef FEAT_EVAL
Bram Moolenaar4033c552017-09-16 20:54:51 +02001702 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001704 ) || curwin->w_buffer == NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001705 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001707#ifdef FEAT_SYN_HL
1708 if (old_tw != curbuf->b_p_tw)
1709 check_colorcolumn(curwin);
1710#endif
1711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712}
1713
1714/*
1715 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001716 * Old curbuf must have been abandoned already! This also means "curbuf" may
1717 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001719 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001720enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721{
1722 /* Copy buffer and window local option values. Not for a help buffer. */
1723 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1724 if (!buf->b_help)
1725 get_winopts(buf);
1726#ifdef FEAT_FOLDING
1727 else
1728 /* Remove all folds in the window. */
1729 clearFolding(curwin);
1730 foldUpdateAll(curwin); /* update folds (later). */
1731#endif
1732
1733 /* Get the buffer in the current window. */
1734 curwin->w_buffer = buf;
1735 curbuf = buf;
1736 ++curbuf->b_nwindows;
1737
1738#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001739 if (curwin->w_p_diff)
1740 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741#endif
1742
Bram Moolenaara971b822011-09-14 14:43:25 +02001743#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001744 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001745#endif
1746
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 /* Cursor on first line by default. */
1748 curwin->w_cursor.lnum = 1;
1749 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001752 curwin->w_topline_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001754 /* mark cursor position as being invalid */
1755 curwin->w_valid = 0;
1756
Bram Moolenaar9cea87c2018-09-21 16:59:45 +02001757 buflist_setfpos(curbuf, curwin, curbuf->b_last_cursor.lnum,
1758 curbuf->b_last_cursor.col, TRUE);
1759
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 /* Make sure the buffer is loaded. */
1761 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001762 {
1763 /* If there is no filetype, allow for detecting one. Esp. useful for
1764 * ":ball" used in a autocommand. If there already is a filetype we
1765 * might prefer to keep it. */
1766 if (*curbuf->b_p_ft == NUL)
1767 did_filetype = FALSE;
1768
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001769 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 else
1772 {
Bram Moolenaareda65222019-05-16 20:29:44 +02001773 if (!msg_silent && !shortmess(SHM_FILEINFO))
1774 need_fileinfo = TRUE; // display file info after redraw
1775
1776 // check if file changed
1777 (void)buf_check_timestamp(curbuf, FALSE);
1778
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001780#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001782#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1784 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 }
1786
1787 /* If autocommands did not change the cursor position, restore cursor lnum
1788 * and possibly cursor col. */
1789 if (curwin->w_cursor.lnum == 1 && inindent(0))
1790 buflist_getfpos();
1791
1792 check_arg_idx(curwin); /* check for valid arg_idx */
1793#ifdef FEAT_TITLE
1794 maketitle();
1795#endif
Bram Moolenaard4153d42008-11-15 15:06:17 +00001796 /* when autocmds didn't change it */
1797 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1799
Bram Moolenaar009b2592004-10-24 19:18:58 +00001800#ifdef FEAT_NETBEANS_INTG
1801 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001802 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001803#endif
1804
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001805 /* Change directories when the 'acd' option is set. */
Bram Moolenaar6f470022018-04-10 18:47:20 +02001806 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807
1808#ifdef FEAT_KEYMAP
1809 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001810 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001812#ifdef FEAT_SPELL
1813 /* May need to set the spell language. Can only do this after the buffer
1814 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001815 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1816 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001817#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001818#ifdef FEAT_VIMINFO
1819 curbuf->b_last_used = vim_time();
1820#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001821
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 redraw_later(NOT_VALID);
1823}
1824
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001825#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1826/*
1827 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001828 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001829 */
1830 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001831do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001832{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001833 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001834 && curbuf->b_ffname != NULL
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001835 && vim_chdirfile(curbuf->b_ffname, "auto") == OK)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001836 shorten_fnames(TRUE);
1837}
1838#endif
1839
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001840 void
1841no_write_message(void)
1842{
1843#ifdef FEAT_TERMINAL
1844 if (term_job_running(curbuf->b_term))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001845 emsg(_("E948: Job still running (add ! to end the job)"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001846 else
1847#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001848 emsg(_("E37: No write since last change (add ! to override)"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001849}
1850
1851 void
Bram Moolenaar7a760922018-02-19 23:10:02 +01001852no_write_message_nobang(buf_T *buf UNUSED)
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001853{
1854#ifdef FEAT_TERMINAL
Bram Moolenaar7a760922018-02-19 23:10:02 +01001855 if (term_job_running(buf->b_term))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001856 emsg(_("E948: Job still running"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001857 else
1858#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001859 emsg(_("E37: No write since last change"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001860}
1861
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862/*
1863 * functions for dealing with the buffer list
1864 */
1865
Bram Moolenaar480778b2016-07-14 22:09:39 +02001866static int top_file_num = 1; /* highest file number */
1867
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868/*
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001869 * Return TRUE if the current buffer is empty, unnamed, unmodified and used in
1870 * only one window. That means it can be re-used.
1871 */
1872 int
1873curbuf_reusable(void)
1874{
1875 return (curbuf != NULL
1876 && curbuf->b_ffname == NULL
1877 && curbuf->b_nwindows <= 1
1878 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY())
Bram Moolenaard85c3962019-04-07 14:19:14 +02001879#if defined(FEAT_QUICKFIX)
Bram Moolenaar39803d82019-04-07 12:04:51 +02001880 && !bt_quickfix(curbuf)
Bram Moolenaard85c3962019-04-07 14:19:14 +02001881#endif
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001882 && !curbufIsChanged());
1883}
1884
1885/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 * Add a file name to the buffer list. Return a pointer to the buffer.
1887 * If the same file name already exists return a pointer to that buffer.
1888 * If it does not exist, or if fname == NULL, a new entry is created.
1889 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1890 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1891 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001892 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001893 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
1894 * if the buffer already exists.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 * This is the ONLY way to create a new buffer.
1896 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001898buflist_new(
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001899 char_u *ffname_arg, // full path of fname or relative
1900 char_u *sfname_arg, // short fname or NULL
1901 linenr_T lnum, // preferred cursor line
1902 int flags) // BLN_ defines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001904 char_u *ffname = ffname_arg;
1905 char_u *sfname = sfname_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 buf_T *buf;
1907#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001908 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909#endif
1910
Bram Moolenaar480778b2016-07-14 22:09:39 +02001911 if (top_file_num == 1)
1912 hash_init(&buf_hashtab);
1913
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001914 fname_expand(curbuf, &ffname, &sfname); // will allocate ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915
1916 /*
1917 * If file name already exists in the list, update the entry.
1918 */
1919#ifdef UNIX
1920 /* On Unix we can use inode numbers when the file exists. Works better
1921 * for hard links. */
1922 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1923 st.st_dev = (dev_T)-1;
1924#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001925 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926#ifdef UNIX
1927 buflist_findname_stat(ffname, &st)
1928#else
1929 buflist_findname(ffname)
1930#endif
1931 ) != NULL)
1932 {
1933 vim_free(ffname);
1934 if (lnum != 0)
1935 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02001936
1937 if ((flags & BLN_NOOPT) == 0)
1938 /* copy the options now, if 'cpo' doesn't have 's' and not done
1939 * already */
1940 buf_copy_options(buf, 0);
1941
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1943 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001944 bufref_T bufref;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001945
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 buf->b_p_bl = TRUE;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001947 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001949 {
Bram Moolenaar82404332016-07-10 17:00:38 +02001950 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001951 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001952 return NULL;
1953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 }
1955 return buf;
1956 }
1957
1958 /*
1959 * If the current buffer has no name and no contents, use the current
1960 * buffer. Otherwise: Need to allocate a new buffer structure.
1961 *
1962 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001963 * (A spell file buffer is allocated in spell.c, but that's not a normal
1964 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 */
1966 buf = NULL;
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001967 if ((flags & BLN_CURBUF) && curbuf_reusable())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 {
1969 buf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 /* It's like this buffer is deleted. Watch out for autocommands that
1971 * change curbuf! If that happens, allocate a new buffer anyway. */
1972 if (curbuf->b_p_bl)
1973 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1974 if (buf == curbuf)
1975 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001976#ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001977 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 if (buf == curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 {
1982 /* Make sure 'bufhidden' and 'buftype' are empty */
1983 clear_string_option(&buf->b_p_bh);
1984 clear_string_option(&buf->b_p_bt);
1985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 }
1987 if (buf != curbuf || curbuf == NULL)
1988 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001989 buf = ALLOC_CLEAR_ONE(buf_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 if (buf == NULL)
1991 {
1992 vim_free(ffname);
1993 return NULL;
1994 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02001995#ifdef FEAT_EVAL
1996 /* init b: variables */
1997 buf->b_vars = dict_alloc();
1998 if (buf->b_vars == NULL)
1999 {
2000 vim_free(ffname);
2001 vim_free(buf);
2002 return NULL;
2003 }
2004 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
2005#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01002006 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 }
2008
2009 if (ffname != NULL)
2010 {
2011 buf->b_ffname = ffname;
2012 buf->b_sfname = vim_strsave(sfname);
2013 }
2014
2015 clear_wininfo(buf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002016 buf->b_wininfo = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017
2018 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
2019 || buf->b_wininfo == NULL)
2020 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002021 if (buf->b_sfname != buf->b_ffname)
2022 VIM_CLEAR(buf->b_sfname);
2023 else
2024 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01002025 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 if (buf != curbuf)
2027 free_buffer(buf);
2028 return NULL;
2029 }
2030
2031 if (buf == curbuf)
2032 {
2033 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002034 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 if (buf != curbuf) /* autocommands deleted the buffer! */
2036 return NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002037#if defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002038 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 return NULL;
2040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002042
2043 /* Init the options. */
2044 buf->b_p_initialized = FALSE;
2045 buf_copy_options(buf, BCO_ENTER);
2046
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047#ifdef FEAT_KEYMAP
2048 /* need to reload lmaps and set b:keymap_name */
2049 curbuf->b_kmap_state |= KEYMAP_INIT;
2050#endif
2051 }
2052 else
2053 {
2054 /*
2055 * put new buffer at the end of the buffer list
2056 */
2057 buf->b_next = NULL;
2058 if (firstbuf == NULL) /* buffer list is empty */
2059 {
2060 buf->b_prev = NULL;
2061 firstbuf = buf;
2062 }
2063 else /* append new buffer at end of list */
2064 {
2065 lastbuf->b_next = buf;
2066 buf->b_prev = lastbuf;
2067 }
2068 lastbuf = buf;
2069
2070 buf->b_fnum = top_file_num++;
2071 if (top_file_num < 0) /* wrap around (may cause duplicates) */
2072 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002073 emsg(_("W14: Warning: List of file names overflow"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 if (emsg_silent == 0)
2075 {
2076 out_flush();
2077 ui_delay(3000L, TRUE); /* make sure it is noticed */
2078 }
2079 top_file_num = 1;
2080 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002081 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082
2083 /*
2084 * Always copy the options from the current buffer.
2085 */
2086 buf_copy_options(buf, BCO_ALWAYS);
2087 }
2088
2089 buf->b_wininfo->wi_fpos.lnum = lnum;
2090 buf->b_wininfo->wi_win = curwin;
2091
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002092#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002093 hash_init(&buf->b_s.b_keywtab);
2094 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095#endif
2096
2097 buf->b_fname = buf->b_sfname;
2098#ifdef UNIX
2099 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002100 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 else
2102 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002103 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 buf->b_dev = st.st_dev;
2105 buf->b_ino = st.st_ino;
2106 }
2107#endif
2108 buf->b_u_synced = TRUE;
2109 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002110 if (flags & BLN_DUMMY)
2111 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 buf_clear_file(buf);
2113 clrallmarks(buf); /* clear marks */
2114 fmarks_check_names(buf); /* check file marks for this file */
2115 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 if (!(flags & BLN_DUMMY))
2117 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002118 bufref_T bufref;
2119
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01002120 /* Tricky: these autocommands may change the buffer list. They could
2121 * also split the window with re-using the one empty buffer. This may
2122 * result in unexpectedly losing the empty buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002123 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002124 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002125 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002126 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002128 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002129 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002130 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002131 return NULL;
2132 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002133#ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002134 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138
2139 return buf;
2140}
2141
2142/*
2143 * Free the memory for the options of a buffer.
2144 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2145 * 'fileencoding'.
2146 */
2147 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002148free_buf_options(
2149 buf_T *buf,
2150 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151{
2152 if (free_p_ff)
2153 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 clear_string_option(&buf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 clear_string_option(&buf->b_p_bh);
2157 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 }
2159#ifdef FEAT_FIND_ID
2160 clear_string_option(&buf->b_p_def);
2161 clear_string_option(&buf->b_p_inc);
2162# ifdef FEAT_EVAL
2163 clear_string_option(&buf->b_p_inex);
2164# endif
2165#endif
2166#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
2167 clear_string_option(&buf->b_p_inde);
2168 clear_string_option(&buf->b_p_indk);
2169#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002170#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2171 clear_string_option(&buf->b_p_bexpr);
2172#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002173#if defined(FEAT_CRYPT)
2174 clear_string_option(&buf->b_p_cm);
2175#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002176 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002177#if defined(FEAT_EVAL)
2178 clear_string_option(&buf->b_p_fex);
2179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180#ifdef FEAT_CRYPT
2181 clear_string_option(&buf->b_p_key);
2182#endif
2183 clear_string_option(&buf->b_p_kp);
2184 clear_string_option(&buf->b_p_mps);
2185 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002186 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 clear_string_option(&buf->b_p_isk);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002188#ifdef FEAT_VARTABS
2189 clear_string_option(&buf->b_p_vsts);
Bram Moolenaarbdace832019-03-02 10:13:42 +01002190 vim_free(buf->b_p_vsts_nopaste);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002191 buf->b_p_vsts_nopaste = NULL;
Bram Moolenaarbdace832019-03-02 10:13:42 +01002192 vim_free(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002193 buf->b_p_vsts_array = NULL;
2194 clear_string_option(&buf->b_p_vts);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002195 VIM_CLEAR(buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197#ifdef FEAT_KEYMAP
2198 clear_string_option(&buf->b_p_keymap);
Bram Moolenaar50138322018-01-28 17:05:16 +01002199 keymap_clear(&buf->b_kmap_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 ga_clear(&buf->b_kmap_ga);
2201#endif
2202#ifdef FEAT_COMMENTS
2203 clear_string_option(&buf->b_p_com);
2204#endif
2205#ifdef FEAT_FOLDING
2206 clear_string_option(&buf->b_p_cms);
2207#endif
2208 clear_string_option(&buf->b_p_nf);
2209#ifdef FEAT_SYN_HL
2210 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002211 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002212#endif
2213#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002214 clear_string_option(&buf->b_s.b_p_spc);
2215 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002216 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002217 buf->b_s.b_cap_prog = NULL;
2218 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219#endif
2220#ifdef FEAT_SEARCHPATH
2221 clear_string_option(&buf->b_p_sua);
2222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 clear_string_option(&buf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224#ifdef FEAT_CINDENT
2225 clear_string_option(&buf->b_p_cink);
2226 clear_string_option(&buf->b_p_cino);
2227#endif
2228#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
2229 clear_string_option(&buf->b_p_cinw);
2230#endif
2231#ifdef FEAT_INS_EXPAND
2232 clear_string_option(&buf->b_p_cpt);
2233#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002234#ifdef FEAT_COMPL_FUNC
2235 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002236 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238#ifdef FEAT_QUICKFIX
2239 clear_string_option(&buf->b_p_gp);
2240 clear_string_option(&buf->b_p_mp);
2241 clear_string_option(&buf->b_p_efm);
2242#endif
2243 clear_string_option(&buf->b_p_ep);
2244 clear_string_option(&buf->b_p_path);
2245 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002246 clear_string_option(&buf->b_p_tc);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002247#ifdef FEAT_EVAL
2248 clear_string_option(&buf->b_p_tfu);
2249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250#ifdef FEAT_INS_EXPAND
2251 clear_string_option(&buf->b_p_dict);
2252 clear_string_option(&buf->b_p_tsr);
2253#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002254#ifdef FEAT_TEXTOBJ
2255 clear_string_option(&buf->b_p_qe);
2256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002258 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002259#ifdef FEAT_LISP
2260 clear_string_option(&buf->b_p_lw);
2261#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002262 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002263 clear_string_option(&buf->b_p_menc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264}
2265
2266/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002267 * Get alternate file "n".
2268 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2269 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 * if (options & GETF_SETMARK) call setpcmark()
2271 * if (options & GETF_ALT) we are jumping to an alternate file.
2272 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2273 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002274 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 */
2276 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002277buflist_getfile(
2278 int n,
2279 linenr_T lnum,
2280 int options,
2281 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282{
2283 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 pos_T *fpos;
2286 colnr_T col;
2287
2288 buf = buflist_findnr(n);
2289 if (buf == NULL)
2290 {
2291 if ((options & GETF_ALT) && n == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002292 emsg(_(e_noalt));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 else
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01002294 semsg(_("E92: Buffer %d not found"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 return FAIL;
2296 }
2297
2298 /* if alternate file is the current buffer, nothing to do */
2299 if (buf == curbuf)
2300 return OK;
2301
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002302 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002303 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002304 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002306 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002307 if (curbuf_locked())
2308 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309
2310 /* altfpos may be changed by getfile(), get it now */
2311 if (lnum == 0)
2312 {
2313 fpos = buflist_findfpos(buf);
2314 lnum = fpos->lnum;
2315 col = fpos->col;
2316 }
2317 else
2318 col = 0;
2319
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 if (options & GETF_SWITCH)
2321 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002322 /* If 'switchbuf' contains "useopen": jump to first window containing
2323 * "buf" if one exists */
2324 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002326
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002327 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00002328 * page containing "buf" if one exists */
2329 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002330 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002331
2332 /* If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2333 * current buffer isn't empty: open new tab or window */
2334 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002335 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002337 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002338 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002339 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2340 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002342 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 }
2344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345
2346 ++RedrawingDisabled;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002347 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2348 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 {
2350 --RedrawingDisabled;
2351
2352 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
2353 if (!p_sol && col != 0)
2354 {
2355 curwin->w_cursor.col = col;
2356 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 curwin->w_set_curswant = TRUE;
2359 }
2360 return OK;
2361 }
2362 --RedrawingDisabled;
2363 return FAIL;
2364}
2365
2366/*
2367 * go to the last know line number for the current buffer
2368 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02002369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002370buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371{
2372 pos_T *fpos;
2373
2374 fpos = buflist_findfpos(curbuf);
2375
2376 curwin->w_cursor.lnum = fpos->lnum;
2377 check_cursor_lnum();
2378
2379 if (p_sol)
2380 curwin->w_cursor.col = 0;
2381 else
2382 {
2383 curwin->w_cursor.col = fpos->col;
2384 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 curwin->w_set_curswant = TRUE;
2387 }
2388}
2389
Bram Moolenaar81695252004-12-29 20:58:21 +00002390#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2391/*
2392 * Find file in buffer list by name (it has to be for the current window).
2393 * Returns NULL if not found.
2394 */
2395 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002396buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002397{
2398 char_u *ffname;
2399 buf_T *buf = NULL;
2400
2401 /* First make the name into a full path name */
2402 ffname = FullName_save(fname,
2403#ifdef UNIX
2404 TRUE /* force expansion, get rid of symbolic links */
2405#else
2406 FALSE
2407#endif
2408 );
2409 if (ffname != NULL)
2410 {
2411 buf = buflist_findname(ffname);
2412 vim_free(ffname);
2413 }
2414 return buf;
2415}
2416#endif
2417
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418/*
2419 * Find file in buffer list by name (it has to be for the current window).
2420 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002421 * Skips dummy buffers.
2422 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 */
2424 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002425buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426{
2427#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002428 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429
2430 if (mch_stat((char *)ffname, &st) < 0)
2431 st.st_dev = (dev_T)-1;
2432 return buflist_findname_stat(ffname, &st);
2433}
2434
2435/*
2436 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2437 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002438 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 */
2440 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002441buflist_findname_stat(
2442 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002443 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444{
2445#endif
2446 buf_T *buf;
2447
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002448 /* Start at the last buffer, expect to find a match sooner. */
2449 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar81695252004-12-29 20:58:21 +00002450 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451#ifdef UNIX
2452 , stp
2453#endif
2454 ))
2455 return buf;
2456 return NULL;
2457}
2458
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459/*
2460 * Find file in buffer list by a regexp pattern.
2461 * Return fnum of the found buffer.
2462 * Return < 0 for error.
2463 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002465buflist_findpat(
2466 char_u *pattern,
2467 char_u *pattern_end, /* pointer to first char after pattern */
2468 int unlisted, /* find unlisted buffers */
2469 int diffmode UNUSED, /* find diff-mode buffers only */
2470 int curtab_only) /* find buffers in current tab only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471{
2472 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 int match = -1;
2474 int find_listed;
2475 char_u *pat;
2476 char_u *patend;
2477 int attempt;
2478 char_u *p;
2479 int toggledollar;
2480
2481 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2482 {
2483 if (*pattern == '%')
2484 match = curbuf->b_fnum;
2485 else
2486 match = curwin->w_alt_fnum;
2487#ifdef FEAT_DIFF
2488 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2489 match = -1;
2490#endif
2491 }
2492
2493 /*
2494 * Try four ways of matching a listed buffer:
2495 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002496 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 * attempt == 2: with '$' at end (only match at end)
2498 * attempt == 3: with '^' at start and '$' at end (only full match)
2499 * Repeat this for finding an unlisted buffer if there was no matching
2500 * listed buffer.
2501 */
2502 else
2503 {
2504 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2505 if (pat == NULL)
2506 return -1;
2507 patend = pat + STRLEN(pat) - 1;
2508 toggledollar = (patend > pat && *patend == '$');
2509
2510 /* First try finding a listed buffer. If not found and "unlisted"
2511 * is TRUE, try finding an unlisted buffer. */
2512 find_listed = TRUE;
2513 for (;;)
2514 {
2515 for (attempt = 0; attempt <= 3; ++attempt)
2516 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002517 regmatch_T regmatch;
2518
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 /* may add '^' and '$' */
2520 if (toggledollar)
2521 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2522 p = pat;
2523 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2524 ++p;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002525 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2526 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 {
2528 vim_free(pat);
2529 return -1;
2530 }
2531
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002532 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 if (buf->b_p_bl == find_listed
2534#ifdef FEAT_DIFF
2535 && (!diffmode || diff_mode_buf(buf))
2536#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002537 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002539 if (curtab_only)
2540 {
2541 /* Ignore the match if the buffer is not open in
2542 * the current tab. */
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002543 win_T *wp;
2544
Bram Moolenaar29323592016-07-24 22:04:11 +02002545 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002546 if (wp->w_buffer == buf)
2547 break;
2548 if (wp == NULL)
2549 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 if (match >= 0) /* already found a match */
2552 {
2553 match = -2;
2554 break;
2555 }
2556 match = buf->b_fnum; /* remember first match */
2557 }
2558
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002559 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 if (match >= 0) /* found one match */
2561 break;
2562 }
2563
2564 /* Only search for unlisted buffers if there was no match with
2565 * a listed buffer. */
2566 if (!unlisted || !find_listed || match != -1)
2567 break;
2568 find_listed = FALSE;
2569 }
2570
2571 vim_free(pat);
2572 }
2573
2574 if (match == -2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002575 semsg(_("E93: More than one match for %s"), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 else if (match < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002577 semsg(_("E94: No matching buffer for %s"), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 return match;
2579}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581/*
2582 * Find all buffer names that match.
2583 * For command line expansion of ":buf" and ":sbuf".
2584 * Return OK if matches found, FAIL otherwise.
2585 */
2586 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002587ExpandBufnames(
2588 char_u *pat,
2589 int *num_file,
2590 char_u ***file,
2591 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592{
2593 int count = 0;
2594 buf_T *buf;
2595 int round;
2596 char_u *p;
2597 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002598 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599
2600 *num_file = 0; /* return values in case of FAIL */
2601 *file = NULL;
2602
Bram Moolenaar05159a02005-02-26 23:04:13 +00002603 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2604 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002606 patc = alloc(STRLEN(pat) + 11);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002607 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002609 STRCPY(patc, "\\(^\\|[\\/]\\)");
2610 STRCPY(patc + 11, pat + 1);
2611 }
2612 else
2613 patc = pat;
2614
2615 /*
2616 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002617 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002618 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002619 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002620 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002621 regmatch_T regmatch;
2622
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002623 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002624 break; /* there was no anchor, no need to try again */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002625 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2626 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002627 {
2628 if (patc != pat)
2629 vim_free(patc);
2630 return FAIL;
2631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632
2633 /*
2634 * round == 1: Count the matches.
2635 * round == 2: Build the array to keep the matches.
2636 */
2637 for (round = 1; round <= 2; ++round)
2638 {
2639 count = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002640 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 {
2642 if (!buf->b_p_bl) /* skip unlisted buffers */
2643 continue;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002644 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 if (p != NULL)
2646 {
2647 if (round == 1)
2648 ++count;
2649 else
2650 {
2651 if (options & WILD_HOME_REPLACE)
2652 p = home_replace_save(buf, p);
2653 else
2654 p = vim_strsave(p);
2655 (*file)[count++] = p;
2656 }
2657 }
2658 }
2659 if (count == 0) /* no match found, break here */
2660 break;
2661 if (round == 1)
2662 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002663 *file = ALLOC_MULT(char_u *, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 if (*file == NULL)
2665 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002666 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002667 if (patc != pat)
2668 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 return FAIL;
2670 }
2671 }
2672 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002673 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 if (count) /* match(es) found, break here */
2675 break;
2676 }
2677
Bram Moolenaar05159a02005-02-26 23:04:13 +00002678 if (patc != pat)
2679 vim_free(patc);
2680
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 *num_file = count;
2682 return (count == 0 ? FAIL : OK);
2683}
2684
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685/*
2686 * Check for a match on the file name for buffer "buf" with regprog "prog".
2687 */
2688 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002689buflist_match(
2690 regmatch_T *rmp,
2691 buf_T *buf,
2692 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693{
2694 char_u *match;
2695
2696 /* First try the short file name, then the long file name. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002697 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002699 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700
2701 return match;
2702}
2703
2704/*
2705 * Try matching the regexp in "prog" with file name "name".
2706 * Return "name" when there is a match, NULL when not.
2707 */
2708 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002709fname_match(
2710 regmatch_T *rmp,
2711 char_u *name,
2712 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713{
2714 char_u *match = NULL;
2715 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716
2717 if (name != NULL)
2718 {
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002719 /* Ignore case when 'fileignorecase' or the argument is set. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002720 rmp->rm_ic = p_fic || ignore_case;
2721 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 match = name;
2723 else
2724 {
2725 /* Replace $(HOME) with '~' and try matching again. */
2726 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002727 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 match = name;
2729 vim_free(p);
2730 }
2731 }
2732
2733 return match;
2734}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735
2736/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02002737 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 */
2739 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002740buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741{
Bram Moolenaar480778b2016-07-14 22:09:39 +02002742 char_u key[VIM_SIZEOF_INT * 2 + 1];
2743 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744
2745 if (nr == 0)
2746 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02002747 sprintf((char *)key, "%x", nr);
2748 hi = hash_find(&buf_hashtab, key);
2749
2750 if (!HASHITEM_EMPTY(hi))
2751 return (buf_T *)(hi->hi_key
2752 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 return NULL;
2754}
2755
2756/*
2757 * Get name of file 'n' in the buffer list.
2758 * When the file has no name an empty string is returned.
2759 * home_replace() is used to shorten the file name (used for marks).
2760 * Returns a pointer to allocated memory, of NULL when failed.
2761 */
2762 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002763buflist_nr2name(
2764 int n,
2765 int fullname,
2766 int helptail) /* for help buffers return tail only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767{
2768 buf_T *buf;
2769
2770 buf = buflist_findnr(n);
2771 if (buf == NULL)
2772 return NULL;
2773 return home_replace_save(helptail ? buf : NULL,
2774 fullname ? buf->b_ffname : buf->b_fname);
2775}
2776
2777/*
2778 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2779 * When "copy_options" is TRUE save the local window option values.
2780 * When "lnum" is 0 only do the options.
2781 */
Bram Moolenaardefa0672019-07-21 19:25:37 +02002782 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002783buflist_setfpos(
2784 buf_T *buf,
2785 win_T *win,
2786 linenr_T lnum,
2787 colnr_T col,
2788 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789{
2790 wininfo_T *wip;
2791
2792 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2793 if (wip->wi_win == win)
2794 break;
2795 if (wip == NULL)
2796 {
2797 /* allocate a new entry */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002798 wip = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 if (wip == NULL)
2800 return;
2801 wip->wi_win = win;
2802 if (lnum == 0) /* set lnum even when it's 0 */
2803 lnum = 1;
2804 }
2805 else
2806 {
2807 /* remove the entry from the list */
2808 if (wip->wi_prev)
2809 wip->wi_prev->wi_next = wip->wi_next;
2810 else
2811 buf->b_wininfo = wip->wi_next;
2812 if (wip->wi_next)
2813 wip->wi_next->wi_prev = wip->wi_prev;
2814 if (copy_options && wip->wi_optset)
2815 {
2816 clear_winopt(&wip->wi_opt);
2817#ifdef FEAT_FOLDING
2818 deleteFoldRecurse(&wip->wi_folds);
2819#endif
2820 }
2821 }
2822 if (lnum != 0)
2823 {
2824 wip->wi_fpos.lnum = lnum;
2825 wip->wi_fpos.col = col;
2826 }
2827 if (copy_options)
2828 {
2829 /* Save the window-specific option values. */
2830 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2831#ifdef FEAT_FOLDING
2832 wip->wi_fold_manual = win->w_fold_manual;
2833 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2834#endif
2835 wip->wi_optset = TRUE;
2836 }
2837
2838 /* insert the entry in front of the list */
2839 wip->wi_next = buf->b_wininfo;
2840 buf->b_wininfo = wip;
2841 wip->wi_prev = NULL;
2842 if (wip->wi_next)
2843 wip->wi_next->wi_prev = wip;
2844
2845 return;
2846}
2847
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002848#ifdef FEAT_DIFF
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002849/*
2850 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2851 * page. That's because a diff is local to a tab page.
2852 */
2853 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002854wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002855{
2856 win_T *wp;
2857
2858 if (wip->wi_opt.wo_diff)
2859 {
Bram Moolenaar29323592016-07-24 22:04:11 +02002860 FOR_ALL_WINDOWS(wp)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002861 /* return FALSE when it's a window in the current tab page, thus
2862 * the buffer was in diff mode here */
2863 if (wip->wi_win == wp)
2864 return FALSE;
2865 return TRUE;
2866 }
2867 return FALSE;
2868}
2869#endif
2870
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871/*
2872 * Find info for the current window in buffer "buf".
2873 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002874 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2875 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 * Returns NULL when there isn't any info.
2877 */
2878 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002879find_wininfo(
2880 buf_T *buf,
2881 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882{
2883 wininfo_T *wip;
2884
2885 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002886 if (wip->wi_win == curwin
2887#ifdef FEAT_DIFF
2888 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2889#endif
2890 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002892
2893 /* If no wininfo for curwin, use the first in the list (that doesn't have
2894 * 'diff' set and is in another tab page). */
2895 if (wip == NULL)
2896 {
2897#ifdef FEAT_DIFF
2898 if (skip_diff_buffer)
2899 {
2900 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2901 if (!wininfo_other_tab_diff(wip))
2902 break;
2903 }
2904 else
2905#endif
2906 wip = buf->b_wininfo;
2907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 return wip;
2909}
2910
2911/*
2912 * Reset the local window options to the values last used in this window.
2913 * If the buffer wasn't used in this window before, use the values from
2914 * the most recently used window. If the values were never set, use the
2915 * global values for the window.
2916 */
2917 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002918get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919{
2920 wininfo_T *wip;
2921
2922 clear_winopt(&curwin->w_onebuf_opt);
2923#ifdef FEAT_FOLDING
2924 clearFolding(curwin);
2925#endif
2926
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002927 wip = find_wininfo(buf, TRUE);
Bram Moolenaar25782a72018-05-13 18:05:33 +02002928 if (wip != NULL && wip->wi_win != NULL
2929 && wip->wi_win != curwin && wip->wi_win->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 {
Bram Moolenaar25782a72018-05-13 18:05:33 +02002931 /* The buffer is currently displayed in the window: use the actual
2932 * option values instead of the saved (possibly outdated) values. */
2933 win_T *wp = wip->wi_win;
2934
2935 copy_winopt(&wp->w_onebuf_opt, &curwin->w_onebuf_opt);
2936#ifdef FEAT_FOLDING
2937 curwin->w_fold_manual = wp->w_fold_manual;
2938 curwin->w_foldinvalid = TRUE;
2939 cloneFoldGrowArray(&wp->w_folds, &curwin->w_folds);
2940#endif
2941 }
2942 else if (wip != NULL && wip->wi_optset)
2943 {
2944 /* the buffer was displayed in the current window earlier */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2946#ifdef FEAT_FOLDING
2947 curwin->w_fold_manual = wip->wi_fold_manual;
2948 curwin->w_foldinvalid = TRUE;
2949 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2950#endif
2951 }
2952 else
2953 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2954
2955#ifdef FEAT_FOLDING
2956 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2957 if (p_fdls >= 0)
2958 curwin->w_p_fdl = p_fdls;
2959#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002960#ifdef FEAT_SYN_HL
2961 check_colorcolumn(curwin);
2962#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963}
2964
2965/*
2966 * Find the position (lnum and col) for the buffer 'buf' for the current
2967 * window.
2968 * Returns a pointer to no_position if no position is found.
2969 */
2970 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002971buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972{
2973 wininfo_T *wip;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002974 static pos_T no_position = {1, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002976 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 if (wip != NULL)
2978 return &(wip->wi_fpos);
2979 else
2980 return &no_position;
2981}
2982
2983/*
2984 * Find the lnum for the buffer 'buf' for the current window.
2985 */
2986 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01002987buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988{
2989 return buflist_findfpos(buf)->lnum;
2990}
2991
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002993 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002996buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997{
2998 buf_T *buf;
2999 int len;
3000 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003001 int ro_char;
3002 int changed_char;
Bram Moolenaar0751f512018-03-29 16:37:16 +02003003#ifdef FEAT_TERMINAL
3004 int job_running;
3005 int job_none_open;
3006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007
3008 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
3009 {
Bram Moolenaar0751f512018-03-29 16:37:16 +02003010#ifdef FEAT_TERMINAL
3011 job_running = term_job_running(buf->b_term);
3012 job_none_open = job_running && term_none_open(buf->b_term);
3013#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 /* skip unlisted buffers, unless ! was used */
Bram Moolenaard51cb702015-07-21 15:03:06 +02003015 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
3016 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
3017 || (vim_strchr(eap->arg, '+')
3018 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
3019 || (vim_strchr(eap->arg, 'a')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003020 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
Bram Moolenaard51cb702015-07-21 15:03:06 +02003021 || (vim_strchr(eap->arg, 'h')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003022 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3023#ifdef FEAT_TERMINAL
3024 || (vim_strchr(eap->arg, 'R')
3025 && (!job_running || (job_running && job_none_open)))
3026 || (vim_strchr(eap->arg, '?')
3027 && (!job_running || (job_running && !job_none_open)))
3028 || (vim_strchr(eap->arg, 'F')
3029 && (job_running || buf->b_term == NULL))
3030#endif
Bram Moolenaard51cb702015-07-21 15:03:06 +02003031 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3032 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3033 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3034 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3035 || (vim_strchr(eap->arg, '#')
3036 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003039 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 else
3041 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003042 if (message_filtered(NameBuff))
3043 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003045 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3046 : (bufIsChanged(buf) ? '+' : ' ');
3047#ifdef FEAT_TERMINAL
3048 if (term_job_running(buf->b_term))
3049 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02003050 if (term_none_open(buf->b_term))
3051 ro_char = '?';
3052 else
3053 ro_char = 'R';
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003054 changed_char = ' '; /* bufIsChanged() returns TRUE to avoid
3055 * closing, but it's not actually changed. */
3056 }
3057 else if (buf->b_term != NULL)
3058 ro_char = 'F';
3059 else
3060#endif
3061 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3062
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003063 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00003064 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 buf->b_fnum,
3066 buf->b_p_bl ? ' ' : 'u',
3067 buf == curbuf ? '%' :
3068 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3069 buf->b_ml.ml_mfp == NULL ? ' ' :
3070 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003071 ro_char,
3072 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003073 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003074 if (len > IOSIZE - 20)
3075 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076
3077 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 i = 40 - vim_strsize(IObuff);
3079 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 IObuff[len++] = ' ';
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003081 while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003082 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3083 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003084 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 msg_outtrans(IObuff);
3086 out_flush(); /* output one line at a time */
3087 ui_breakcheck();
3088 }
3089}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090
3091/*
3092 * Get file name and line number for file 'fnum'.
3093 * Used by DoOneCmd() for translating '%' and '#'.
3094 * Used by insert_reg() and cmdline_paste() for '#' register.
3095 * Return FAIL if not found, OK for success.
3096 */
3097 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003098buflist_name_nr(
3099 int fnum,
3100 char_u **fname,
3101 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102{
3103 buf_T *buf;
3104
3105 buf = buflist_findnr(fnum);
3106 if (buf == NULL || buf->b_fname == NULL)
3107 return FAIL;
3108
3109 *fname = buf->b_fname;
3110 *lnum = buflist_findlnum(buf);
3111
3112 return OK;
3113}
3114
3115/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003116 * Set the file name for "buf"' to "ffname_arg", short file name to
3117 * "sfname_arg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 * The file name with the full path is also remembered, for when :cd is used.
3119 * Returns FAIL for failure (file name already in use by other buffer)
3120 * OK otherwise.
3121 */
3122 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003123setfname(
3124 buf_T *buf,
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003125 char_u *ffname_arg,
3126 char_u *sfname_arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +01003127 int message) /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003129 char_u *ffname = ffname_arg;
3130 char_u *sfname = sfname_arg;
Bram Moolenaar81695252004-12-29 20:58:21 +00003131 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003133 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134#endif
3135
3136 if (ffname == NULL || *ffname == NUL)
3137 {
3138 /* Removing the name. */
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003139 if (buf->b_sfname != buf->b_ffname)
3140 VIM_CLEAR(buf->b_sfname);
3141 else
3142 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003143 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144#ifdef UNIX
3145 st.st_dev = (dev_T)-1;
3146#endif
3147 }
3148 else
3149 {
3150 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
3151 if (ffname == NULL) /* out of memory */
3152 return FAIL;
3153
3154 /*
3155 * if the file name is already used in another buffer:
3156 * - if the buffer is loaded, fail
3157 * - if the buffer is not loaded, delete it from the list
3158 */
3159#ifdef UNIX
3160 if (mch_stat((char *)ffname, &st) < 0)
3161 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003162#endif
3163 if (!(buf->b_flags & BF_DUMMY))
3164#ifdef UNIX
3165 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003167 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168#endif
3169 if (obuf != NULL && obuf != buf)
3170 {
3171 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
3172 {
3173 if (message)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003174 emsg(_("E95: Buffer with this name already exists"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 vim_free(ffname);
3176 return FAIL;
3177 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01003178 /* delete from the list */
3179 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 }
3181 sfname = vim_strsave(sfname);
3182 if (ffname == NULL || sfname == NULL)
3183 {
3184 vim_free(sfname);
3185 vim_free(ffname);
3186 return FAIL;
3187 }
3188#ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003189 fname_case(sfname, 0); /* set correct case for short file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190#endif
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003191 if (buf->b_sfname != buf->b_ffname)
3192 vim_free(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 vim_free(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 buf->b_ffname = ffname;
3195 buf->b_sfname = sfname;
3196 }
3197 buf->b_fname = buf->b_sfname;
3198#ifdef UNIX
3199 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003200 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 else
3202 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003203 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 buf->b_dev = st.st_dev;
3205 buf->b_ino = st.st_ino;
3206 }
3207#endif
3208
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210
3211 buf_name_changed(buf);
3212 return OK;
3213}
3214
3215/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003216 * Crude way of changing the name of a buffer. Use with care!
3217 * The name should be relative to the current directory.
3218 */
3219 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003220buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003221{
3222 buf_T *buf;
3223
3224 buf = buflist_findnr(fnum);
3225 if (buf != NULL)
3226 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003227 if (buf->b_sfname != buf->b_ffname)
3228 vim_free(buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003229 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003230 buf->b_ffname = vim_strsave(name);
3231 buf->b_sfname = NULL;
3232 /* Allocate ffname and expand into full path. Also resolves .lnk
3233 * files on Win32. */
3234 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003235 buf->b_fname = buf->b_sfname;
3236 }
3237}
3238
3239/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 * Take care of what needs to be done when the name of buffer "buf" has
3241 * changed.
3242 */
3243 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003244buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245{
3246 /*
3247 * If the file name changed, also change the name of the swapfile
3248 */
3249 if (buf->b_ml.ml_mfp != NULL)
3250 ml_setname(buf);
3251
3252 if (curwin->w_buffer == buf)
3253 check_arg_idx(curwin); /* check file name for arg list */
3254#ifdef FEAT_TITLE
3255 maketitle(); /* set window title */
3256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 status_redraw_all(); /* status lines need to be redrawn */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 fmarks_check_names(buf); /* check named file marks */
3259 ml_timestamp(buf); /* reset timestamp */
3260}
3261
3262/*
3263 * set alternate file name for current window
3264 *
3265 * Used by do_one_cmd(), do_write() and do_ecmd().
3266 * Return the buffer.
3267 */
3268 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003269setaltfname(
3270 char_u *ffname,
3271 char_u *sfname,
3272 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273{
3274 buf_T *buf;
3275
3276 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
3277 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003278 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 curwin->w_alt_fnum = buf->b_fnum;
3280 return buf;
3281}
3282
3283/*
3284 * Get alternate file name for current window.
3285 * Return NULL if there isn't any, and give error message if requested.
3286 */
3287 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003288getaltfname(
3289 int errmsg) /* give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290{
3291 char_u *fname;
3292 linenr_T dummy;
3293
3294 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3295 {
3296 if (errmsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003297 emsg(_(e_noalt));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 return NULL;
3299 }
3300 return fname;
3301}
3302
3303/*
3304 * Add a file name to the buflist and return its number.
3305 * Uses same flags as buflist_new(), except BLN_DUMMY.
3306 *
3307 * used by qf_init(), main() and doarglist()
3308 */
3309 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003310buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311{
3312 buf_T *buf;
3313
3314 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3315 if (buf != NULL)
3316 return buf->b_fnum;
3317 return 0;
3318}
3319
3320#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3321/*
3322 * Adjust slashes in file names. Called after 'shellslash' was set.
3323 */
3324 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003325buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326{
3327 buf_T *bp;
3328
Bram Moolenaar29323592016-07-24 22:04:11 +02003329 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 {
3331 if (bp->b_ffname != NULL)
3332 slash_adjust(bp->b_ffname);
3333 if (bp->b_sfname != NULL)
3334 slash_adjust(bp->b_sfname);
3335 }
3336}
3337#endif
3338
3339/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003340 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 * Also save the local window option values.
3342 */
3343 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003344buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003346 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347}
3348
3349/*
3350 * Return TRUE if 'ffname' is not the same file as current file.
3351 * Fname must have a full path (expanded by mch_FullName()).
3352 */
3353 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003354otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355{
3356 return otherfile_buf(curbuf, ffname
3357#ifdef UNIX
3358 , NULL
3359#endif
3360 );
3361}
3362
3363 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003364otherfile_buf(
3365 buf_T *buf,
3366 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003368 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003370 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371{
3372 /* no name is different */
3373 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3374 return TRUE;
3375 if (fnamecmp(ffname, buf->b_ffname) == 0)
3376 return FALSE;
3377#ifdef UNIX
3378 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003379 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380
Bram Moolenaar8767f522016-07-01 17:17:39 +02003381 /* If no stat_T given, get it now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 if (stp == NULL)
3383 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003384 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 st.st_dev = (dev_T)-1;
3386 stp = &st;
3387 }
3388 /* Use dev/ino to check if the files are the same, even when the names
3389 * are different (possible with links). Still need to compare the
3390 * name above, for when the file doesn't exist yet.
3391 * Problem: The dev/ino changes when a file is deleted (and created
3392 * again) and remains the same when renamed/moved. We don't want to
3393 * mch_stat() each buffer each time, that would be too slow. Get the
3394 * dev/ino again when they appear to match, but not when they appear
3395 * to be different: Could skip a buffer when it's actually the same
3396 * file. */
3397 if (buf_same_ino(buf, stp))
3398 {
3399 buf_setino(buf);
3400 if (buf_same_ino(buf, stp))
3401 return FALSE;
3402 }
3403 }
3404#endif
3405 return TRUE;
3406}
3407
3408#if defined(UNIX) || defined(PROTO)
3409/*
3410 * Set inode and device number for a buffer.
3411 * Must always be called when b_fname is changed!.
3412 */
3413 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003414buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003416 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417
3418 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3419 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003420 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 buf->b_dev = st.st_dev;
3422 buf->b_ino = st.st_ino;
3423 }
3424 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003425 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426}
3427
3428/*
3429 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3430 */
3431 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003432buf_same_ino(
3433 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003434 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003436 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 && stp->st_dev == buf->b_dev
3438 && stp->st_ino == buf->b_ino);
3439}
3440#endif
3441
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003442/*
3443 * Print info about the current buffer.
3444 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003446fileinfo(
3447 int fullname, /* when non-zero print full path */
3448 int shorthelp,
3449 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450{
3451 char_u *name;
3452 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003453 char *p;
3454 char *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003455 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003457 buffer = alloc(IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 if (buffer == NULL)
3459 return;
3460
3461 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3462 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003463 vim_snprintf(buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 p = buffer + STRLEN(buffer);
3465 }
3466 else
3467 p = buffer;
3468
3469 *p++ = '"';
3470 if (buf_spname(curbuf) != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003471 vim_strncpy((char_u *)p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 else
3473 {
3474 if (!fullname && curbuf->b_fname != NULL)
3475 name = curbuf->b_fname;
3476 else
3477 name = curbuf->b_ffname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003478 home_replace(shorthelp ? curbuf : NULL, name, (char_u *)p,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 (int)(IOSIZE - (p - buffer)), TRUE);
3480 }
3481
Bram Moolenaar32526b32019-01-19 17:43:09 +01003482 vim_snprintf_add(buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 curbufIsChanged() ? (shortmess(SHM_MOD)
3484 ? " [+]" : _(" [Modified]")) : " ",
3485 (curbuf->b_flags & BF_NOTEDITED)
3486#ifdef FEAT_QUICKFIX
3487 && !bt_dontwrite(curbuf)
3488#endif
3489 ? _("[Not edited]") : "",
3490 (curbuf->b_flags & BF_NEW)
3491#ifdef FEAT_QUICKFIX
3492 && !bt_dontwrite(curbuf)
3493#endif
3494 ? _("[New file]") : "",
3495 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003496 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 : _("[readonly]")) : "",
3498 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3499 || curbuf->b_p_ro) ?
3500 " " : "");
3501 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3502 * causes an overflow, thus for large numbers divide instead. */
3503 if (curwin->w_cursor.lnum > 1000000L)
3504 n = (int)(((long)curwin->w_cursor.lnum) /
3505 ((long)curbuf->b_ml.ml_line_count / 100L));
3506 else
3507 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3508 (long)curbuf->b_ml.ml_line_count);
3509 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003510 vim_snprintf_add(buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511#ifdef FEAT_CMDL_INFO
3512 else if (p_ru)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 /* Current line and column are already on the screen -- webb */
Bram Moolenaar32526b32019-01-19 17:43:09 +01003514 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003515 NGETTEXT("%ld line --%d%%--", "%ld lines --%d%%--",
3516 curbuf->b_ml.ml_line_count),
3517 (long)curbuf->b_ml.ml_line_count, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518#endif
3519 else
3520 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003521 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 _("line %ld of %ld --%d%%-- col "),
3523 (long)curwin->w_cursor.lnum,
3524 (long)curbuf->b_ml.ml_line_count,
3525 n);
3526 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003527 len = STRLEN(buffer);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003528 col_print((char_u *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3530 }
3531
Bram Moolenaar32526b32019-01-19 17:43:09 +01003532 (void)append_arg_number(curwin, (char_u *)buffer, IOSIZE,
3533 !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534
3535 if (dont_truncate)
3536 {
3537 /* Temporarily set msg_scroll to avoid the message being truncated.
3538 * First call msg_start() to get the message in the right place. */
3539 msg_start();
3540 n = msg_scroll;
3541 msg_scroll = TRUE;
3542 msg(buffer);
3543 msg_scroll = n;
3544 }
3545 else
3546 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003547 p = (char *)msg_trunc_attr(buffer, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 /* Need to repeat the message after redrawing when:
3550 * - When restart_edit is set (otherwise there will be a delay
3551 * before redrawing).
3552 * - When the screen was scrolled but there is no wait-return
3553 * prompt. */
Bram Moolenaar32526b32019-01-19 17:43:09 +01003554 set_keep_msg((char_u *)p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 }
3556
3557 vim_free(buffer);
3558}
3559
3560 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003561col_print(
3562 char_u *buf,
3563 size_t buflen,
3564 int col,
3565 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566{
3567 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003568 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003570 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571}
3572
3573#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574static char_u *lasttitle = NULL;
3575static char_u *lasticon = NULL;
3576
Bram Moolenaar84a93082018-06-16 22:58:15 +02003577/*
3578 * Put the file name in the title bar and icon of the window.
3579 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003581maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582{
3583 char_u *p;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003584 char_u *title_str = NULL;
3585 char_u *icon_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 int maxlen = 0;
3587 int len;
3588 int mustset;
3589 char_u buf[IOSIZE];
3590 int off;
3591
3592 if (!redrawing())
3593 {
3594 /* Postpone updating the title when 'lazyredraw' is set. */
3595 need_maketitle = TRUE;
3596 return;
3597 }
3598
3599 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003600 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003601 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602
3603 if (p_title)
3604 {
3605 if (p_titlelen > 0)
3606 {
3607 maxlen = p_titlelen * Columns / 100;
3608 if (maxlen < 10)
3609 maxlen = 10;
3610 }
3611
Bram Moolenaar84a93082018-06-16 22:58:15 +02003612 title_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 if (*p_titlestring != NUL)
3614 {
3615#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003616 if (stl_syntax & STL_IN_TITLE)
3617 {
3618 int use_sandbox = FALSE;
3619 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003620
3621# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003622 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003623# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003624 called_emsg = FALSE;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003625 build_stl_str_hl(curwin, title_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003626 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003627 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003628 if (called_emsg)
3629 set_string_option_direct((char_u *)"titlestring", -1,
3630 (char_u *)"", OPT_FREE, SID_ERROR);
3631 called_emsg |= save_called_emsg;
3632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 else
3634#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003635 title_str = p_titlestring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 }
3637 else
3638 {
3639 /* format: "fname + (path) (1 of 2) - VIM" */
3640
Bram Moolenaar2c666692012-09-05 13:30:40 +02003641#define SPACE_FOR_FNAME (IOSIZE - 100)
3642#define SPACE_FOR_DIR (IOSIZE - 20)
3643#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003645 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar21554412017-07-24 21:44:43 +02003646#ifdef FEAT_TERMINAL
3647 else if (curbuf->b_term != NULL)
3648 {
3649 vim_strncpy(buf, term_get_status_text(curbuf->b_term),
3650 SPACE_FOR_FNAME);
3651 }
3652#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 else
3654 {
3655 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003656 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 }
3659
Bram Moolenaar21554412017-07-24 21:44:43 +02003660#ifdef FEAT_TERMINAL
3661 if (curbuf->b_term == NULL)
3662#endif
3663 switch (bufIsChanged(curbuf)
3664 + (curbuf->b_p_ro * 2)
3665 + (!curbuf->b_p_ma * 4))
3666 {
3667 case 1: STRCAT(buf, " +"); break;
3668 case 2: STRCAT(buf, " ="); break;
3669 case 3: STRCAT(buf, " =+"); break;
3670 case 4:
3671 case 6: STRCAT(buf, " -"); break;
3672 case 5:
3673 case 7: STRCAT(buf, " -+"); break;
3674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675
Bram Moolenaar21554412017-07-24 21:44:43 +02003676 if (curbuf->b_fname != NULL
3677#ifdef FEAT_TERMINAL
3678 && curbuf->b_term == NULL
3679#endif
3680 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 {
3682 /* Get path of file, replace home dir with ~ */
3683 off = (int)STRLEN(buf);
3684 buf[off++] = ' ';
3685 buf[off++] = '(';
3686 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003687 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688#ifdef BACKSLASH_IN_FILENAME
3689 /* avoid "c:/name" to be reduced to "c" */
3690 if (isalpha(buf[off]) && buf[off + 1] == ':')
3691 off += 2;
3692#endif
3693 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003694 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 if (p == buf + off)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003696 {
Bram Moolenaar21554412017-07-24 21:44:43 +02003697 /* must be a help buffer */
3698 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003699 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003703
Bram Moolenaar2c666692012-09-05 13:30:40 +02003704 /* Translate unprintable chars and concatenate. Keep some
3705 * room for the server name. When there is no room (very long
3706 * file name) use (...). */
3707 if (off < SPACE_FOR_DIR)
3708 {
3709 p = transstr(buf + off);
3710 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3711 vim_free(p);
3712 }
3713 else
3714 {
3715 vim_strncpy(buf + off, (char_u *)"...",
3716 (size_t)(SPACE_FOR_ARGNR - off));
3717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 STRCAT(buf, ")");
3719 }
3720
Bram Moolenaar2c666692012-09-05 13:30:40 +02003721 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722
3723#if defined(FEAT_CLIENTSERVER)
3724 if (serverName != NULL)
3725 {
3726 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003727 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 }
3729 else
3730#endif
3731 STRCAT(buf, " - VIM");
3732
3733 if (maxlen > 0)
3734 {
3735 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003736 if (vim_strsize(buf) > maxlen)
3737 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 }
3739 }
3740 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02003741 mustset = value_changed(title_str, &lasttitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742
3743 if (p_icon)
3744 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02003745 icon_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 if (*p_iconstring != NUL)
3747 {
3748#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003749 if (stl_syntax & STL_IN_ICON)
3750 {
3751 int use_sandbox = FALSE;
3752 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003753
3754# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003755 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003756# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003757 called_emsg = FALSE;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003758 build_stl_str_hl(curwin, icon_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003759 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003760 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003761 if (called_emsg)
3762 set_string_option_direct((char_u *)"iconstring", -1,
3763 (char_u *)"", OPT_FREE, SID_ERROR);
3764 called_emsg |= save_called_emsg;
3765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 else
3767#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003768 icon_str = p_iconstring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 }
3770 else
3771 {
3772 if (buf_spname(curbuf) != NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003773 p = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 else /* use file name only in icon */
Bram Moolenaar84a93082018-06-16 22:58:15 +02003775 p = gettail(curbuf->b_ffname);
3776 *icon_str = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 /* Truncate name at 100 bytes. */
Bram Moolenaar84a93082018-06-16 22:58:15 +02003778 len = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 if (len > 100)
3780 {
3781 len -= 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 if (has_mbyte)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003783 len += (*mb_tail_off)(p, p + len) + 1;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003784 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02003786 STRCPY(icon_str, p);
3787 trans_characters(icon_str, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 }
3789 }
3790
Bram Moolenaar84a93082018-06-16 22:58:15 +02003791 mustset |= value_changed(icon_str, &lasticon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792
3793 if (mustset)
3794 resettitle();
3795}
3796
3797/*
3798 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3799 * from "str" if it does.
Bram Moolenaar84a93082018-06-16 22:58:15 +02003800 * Return TRUE if resettitle() is to be called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 */
3802 static int
Bram Moolenaar84a93082018-06-16 22:58:15 +02003803value_changed(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804{
3805 if ((str == NULL) != (*last == NULL)
3806 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3807 {
3808 vim_free(*last);
3809 if (str == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003810 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 *last = NULL;
Bram Moolenaar40385db2018-08-07 22:31:44 +02003812 mch_restore_title(
3813 last == &lasttitle ? SAVE_RESTORE_TITLE : SAVE_RESTORE_ICON);
Bram Moolenaar84a93082018-06-16 22:58:15 +02003814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 else
Bram Moolenaar84a93082018-06-16 22:58:15 +02003816 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 *last = vim_strsave(str);
Bram Moolenaar84a93082018-06-16 22:58:15 +02003818 return TRUE;
3819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 }
3821 return FALSE;
3822}
3823
3824/*
3825 * Put current window title back (used after calling a shell)
3826 */
3827 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003828resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829{
3830 mch_settitle(lasttitle, lasticon);
3831}
Bram Moolenaarea408852005-06-25 22:49:46 +00003832
3833# if defined(EXITFREE) || defined(PROTO)
3834 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003835free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00003836{
3837 vim_free(lasttitle);
3838 vim_free(lasticon);
3839}
3840# endif
3841
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842#endif /* FEAT_TITLE */
3843
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003844#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003846 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 * Return length of string in screen cells.
3848 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003849 * Normally works for window "wp", except when working for 'tabline' then it
3850 * is "curwin".
3851 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 * Items are drawn interspersed with the text that surrounds it
3853 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3854 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3855 *
3856 * If maxwidth is not zero, the string will be filled at any middle marker
3857 * or truncated if too long, fillchar is used for all whitespace.
3858 */
3859 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003860build_stl_str_hl(
3861 win_T *wp,
3862 char_u *out, /* buffer to write into != NameBuff */
3863 size_t outlen, /* length of out[] */
3864 char_u *fmt,
3865 int use_sandbox UNUSED, /* "fmt" was set insecurely, use sandbox */
3866 int fillchar,
3867 int maxwidth,
3868 struct stl_hlrec *hltab, /* return: HL attributes (can be NULL) */
3869 struct stl_hlrec *tabtab) /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870{
Bram Moolenaar10772302019-01-20 18:25:54 +01003871 linenr_T lnum;
3872 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 char_u *p;
3874 char_u *s;
3875 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003876 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877#ifdef FEAT_EVAL
Bram Moolenaarba2929b2017-09-08 13:59:21 +02003878 win_T *save_curwin;
3879 buf_T *save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880#endif
3881 int empty_line;
3882 colnr_T virtcol;
3883 long l;
3884 long n;
3885 int prevchar_isflag;
3886 int prevchar_isitem;
3887 int itemisflag;
3888 int fillable;
3889 char_u *str;
3890 long num;
3891 int width;
3892 int itemcnt;
3893 int curitem;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02003894 int group_end_userhl;
3895 int group_start_userhl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 int groupitem[STL_MAX_ITEM];
3897 int groupdepth;
3898 struct stl_item
3899 {
3900 char_u *start;
3901 int minwid;
3902 int maxwid;
3903 enum
3904 {
3905 Normal,
3906 Empty,
3907 Group,
3908 Middle,
3909 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003910 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 Trunc
3912 } type;
3913 } item[STL_MAX_ITEM];
3914 int minwid;
3915 int maxwid;
3916 int zeropad;
3917 char_u base;
3918 char_u opt;
3919#define TMPLEN 70
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02003920 char_u buf_tmp[TMPLEN];
3921 char_u win_tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003922 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003923 struct stl_hlrec *sp;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02003924 int save_must_redraw = must_redraw;
3925 int save_redr_type = curwin->w_redr_type;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003926
3927#ifdef FEAT_EVAL
3928 /*
3929 * When the format starts with "%!" then evaluate it as an expression and
3930 * use the result as the actual format string.
3931 */
3932 if (fmt[0] == '%' && fmt[1] == '!')
3933 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02003934 typval_T tv;
3935
3936 tv.v_type = VAR_NUMBER;
3937 tv.vval.v_number = wp->w_id;
3938 set_var((char_u *)"g:statusline_winid", &tv, FALSE);
3939
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003940 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3941 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003942 usefmt = fmt;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02003943
3944 do_unlet((char_u *)"g:statusline_winid", TRUE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003945 }
3946#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947
3948 if (fillchar == 0)
3949 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003950 /* Can't handle a multi-byte fill character yet. */
3951 else if (mb_char2len(fillchar) > 1)
3952 fillchar = '-';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003953
Bram Moolenaar10772302019-01-20 18:25:54 +01003954 // The cursor in windows other than the current one isn't always
3955 // up-to-date, esp. because of autocommands and timers.
3956 lnum = wp->w_cursor.lnum;
3957 if (lnum > wp->w_buffer->b_ml.ml_line_count)
3958 {
3959 lnum = wp->w_buffer->b_ml.ml_line_count;
3960 wp->w_cursor.lnum = lnum;
3961 }
3962
3963 // Get line & check if empty (cursorpos will show "0-1"). Note that
3964 // p will become invalid when getting another buffer line.
3965 p = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar567199b2013-04-24 16:52:36 +02003966 empty_line = (*p == NUL);
3967
Bram Moolenaar10772302019-01-20 18:25:54 +01003968 // Get the byte value now, in case we need it below. This is more efficient
3969 // than making a copy of the line.
3970 len = STRLEN(p);
3971 if (wp->w_cursor.col > (colnr_T)len)
3972 {
3973 // Line may have changed since checking the cursor column, or the lnum
3974 // was adjusted above.
3975 wp->w_cursor.col = (colnr_T)len;
Bram Moolenaar10772302019-01-20 18:25:54 +01003976 wp->w_cursor.coladd = 0;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003977 byteval = 0;
Bram Moolenaar10772302019-01-20 18:25:54 +01003978 }
Bram Moolenaar567199b2013-04-24 16:52:36 +02003979 else
Bram Moolenaar567199b2013-04-24 16:52:36 +02003980 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981
3982 groupdepth = 0;
3983 p = out;
3984 curitem = 0;
3985 prevchar_isflag = TRUE;
3986 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003987 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003989 if (curitem == STL_MAX_ITEM)
3990 {
3991 /* There are too many items. Add the error code to the statusline
3992 * to give the user a hint about what went wrong. */
3993 if (p + 6 < out + outlen)
3994 {
3995 mch_memmove(p, " E541", (size_t)5);
3996 p += 5;
3997 }
3998 break;
3999 }
4000
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 if (*s != NUL && *s != '%')
4002 prevchar_isflag = prevchar_isitem = FALSE;
4003
4004 /*
4005 * Handle up to the next '%' or the end.
4006 */
4007 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
4008 *p++ = *s++;
4009 if (*s == NUL || p + 1 >= out + outlen)
4010 break;
4011
4012 /*
4013 * Handle one '%' item.
4014 */
4015 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01004016 if (*s == NUL) /* ignore trailing % */
4017 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 if (*s == '%')
4019 {
4020 if (p + 1 >= out + outlen)
4021 break;
4022 *p++ = *s++;
4023 prevchar_isflag = prevchar_isitem = FALSE;
4024 continue;
4025 }
4026 if (*s == STL_MIDDLEMARK)
4027 {
4028 s++;
4029 if (groupdepth > 0)
4030 continue;
4031 item[curitem].type = Middle;
4032 item[curitem++].start = p;
4033 continue;
4034 }
4035 if (*s == STL_TRUNCMARK)
4036 {
4037 s++;
4038 item[curitem].type = Trunc;
4039 item[curitem++].start = p;
4040 continue;
4041 }
4042 if (*s == ')')
4043 {
4044 s++;
4045 if (groupdepth < 1)
4046 continue;
4047 groupdepth--;
4048
4049 t = item[groupitem[groupdepth]].start;
4050 *p = NUL;
4051 l = vim_strsize(t);
4052 if (curitem > groupitem[groupdepth] + 1
4053 && item[groupitem[groupdepth]].minwid == 0)
4054 {
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004055 /* remove group if all items are empty and highlight group
4056 * doesn't change */
4057 group_start_userhl = group_end_userhl = 0;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004058 for (n = groupitem[groupdepth] - 1; n >= 0; n--)
4059 {
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004060 if (item[n].type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004061 {
4062 group_start_userhl = group_end_userhl = item[n].minwid;
4063 break;
4064 }
4065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004067 {
4068 if (item[n].type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 break;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004070 if (item[n].type == Highlight)
4071 group_end_userhl = item[n].minwid;
4072 }
4073 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 {
4075 p = t;
4076 l = 0;
4077 }
4078 }
4079 if (l > item[groupitem[groupdepth]].maxwid)
4080 {
4081 /* truncate, remove n bytes of text at the start */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 if (has_mbyte)
4083 {
4084 /* Find the first character that should be included. */
4085 n = 0;
4086 while (l >= item[groupitem[groupdepth]].maxwid)
4087 {
4088 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004089 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 }
4091 }
4092 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004093 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094
4095 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004096 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 p = p - n + 1;
Bram Moolenaar13505972019-01-24 15:04:48 +01004098
4099 // Fill up space left over by half a double-wide char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 while (++l < item[groupitem[groupdepth]].minwid)
4101 *p++ = fillchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102
4103 /* correct the start of the items for the truncation */
4104 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
4105 {
4106 item[l].start -= n;
4107 if (item[l].start < t)
4108 item[l].start = t;
4109 }
4110 }
4111 else if (abs(item[groupitem[groupdepth]].minwid) > l)
4112 {
4113 /* fill */
4114 n = item[groupitem[groupdepth]].minwid;
4115 if (n < 0)
4116 {
4117 /* fill by appending characters */
4118 n = 0 - n;
4119 while (l++ < n && p + 1 < out + outlen)
4120 *p++ = fillchar;
4121 }
4122 else
4123 {
4124 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004125 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 l = n - l;
4127 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004128 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 p += l;
4130 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
4131 item[n].start += l;
4132 for ( ; l > 0; l--)
4133 *t++ = fillchar;
4134 }
4135 }
4136 continue;
4137 }
4138 minwid = 0;
4139 maxwid = 9999;
4140 zeropad = FALSE;
4141 l = 1;
4142 if (*s == '0')
4143 {
4144 s++;
4145 zeropad = TRUE;
4146 }
4147 if (*s == '-')
4148 {
4149 s++;
4150 l = -1;
4151 }
4152 if (VIM_ISDIGIT(*s))
4153 {
4154 minwid = (int)getdigits(&s);
4155 if (minwid < 0) /* overflow */
4156 minwid = 0;
4157 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004158 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 {
4160 item[curitem].type = Highlight;
4161 item[curitem].start = p;
4162 item[curitem].minwid = minwid > 9 ? 1 : minwid;
4163 s++;
4164 curitem++;
4165 continue;
4166 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004167 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4168 {
4169 if (*s == STL_TABCLOSENR)
4170 {
4171 if (minwid == 0)
4172 {
4173 /* %X ends the close label, go back to the previously
4174 * define tab label nr. */
4175 for (n = curitem - 1; n >= 0; --n)
4176 if (item[n].type == TabPage && item[n].minwid >= 0)
4177 {
4178 minwid = item[n].minwid;
4179 break;
4180 }
4181 }
4182 else
4183 /* close nrs are stored as negative values */
4184 minwid = - minwid;
4185 }
4186 item[curitem].type = TabPage;
4187 item[curitem].start = p;
4188 item[curitem].minwid = minwid;
4189 s++;
4190 curitem++;
4191 continue;
4192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 if (*s == '.')
4194 {
4195 s++;
4196 if (VIM_ISDIGIT(*s))
4197 {
4198 maxwid = (int)getdigits(&s);
4199 if (maxwid <= 0) /* overflow */
4200 maxwid = 50;
4201 }
4202 }
4203 minwid = (minwid > 50 ? 50 : minwid) * l;
4204 if (*s == '(')
4205 {
4206 groupitem[groupdepth++] = curitem;
4207 item[curitem].type = Group;
4208 item[curitem].start = p;
4209 item[curitem].minwid = minwid;
4210 item[curitem].maxwid = maxwid;
4211 s++;
4212 curitem++;
4213 continue;
4214 }
4215 if (vim_strchr(STL_ALL, *s) == NULL)
4216 {
4217 s++;
4218 continue;
4219 }
4220 opt = *s++;
4221
4222 /* OK - now for the real work */
4223 base = 'D';
4224 itemisflag = FALSE;
4225 fillable = TRUE;
4226 num = -1;
4227 str = NULL;
4228 switch (opt)
4229 {
4230 case STL_FILEPATH:
4231 case STL_FULLPATH:
4232 case STL_FILENAME:
4233 fillable = FALSE; /* don't change ' ' to fillchar */
4234 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004235 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 else
4237 {
4238 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004239 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4241 }
4242 trans_characters(NameBuff, MAXPATHL);
4243 if (opt != STL_FILENAME)
4244 str = NameBuff;
4245 else
4246 str = gettail(NameBuff);
4247 break;
4248
4249 case STL_VIM_EXPR: /* '{' */
4250 itemisflag = TRUE;
4251 t = p;
4252 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
4253 *p++ = *s++;
4254 if (*s != '}') /* missing '}' or out of space */
4255 break;
4256 s++;
4257 *p = 0;
4258 p = t;
4259
4260#ifdef FEAT_EVAL
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004261 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp),
4262 "%d", curbuf->b_fnum);
4263 set_internal_string_var((char_u *)"g:actual_curbuf", buf_tmp);
4264 vim_snprintf((char *)win_tmp, sizeof(win_tmp), "%d", curwin->w_id);
4265 set_internal_string_var((char_u *)"g:actual_curwin", win_tmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004267 save_curbuf = curbuf;
4268 save_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 curwin = wp;
4270 curbuf = wp->w_buffer;
4271
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004272 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004274 curwin = save_curwin;
4275 curbuf = save_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00004276 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004277 do_unlet((char_u *)"g:actual_curwin", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278
4279 if (str != NULL && *str != 0)
4280 {
4281 if (*skipdigits(str) == NUL)
4282 {
4283 num = atoi((char *)str);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004284 VIM_CLEAR(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 itemisflag = FALSE;
4286 }
4287 }
4288#endif
4289 break;
4290
4291 case STL_LINE:
4292 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4293 ? 0L : (long)(wp->w_cursor.lnum);
4294 break;
4295
4296 case STL_NUMLINES:
4297 num = wp->w_buffer->b_ml.ml_line_count;
4298 break;
4299
4300 case STL_COLUMN:
4301 num = !(State & INSERT) && empty_line
4302 ? 0 : (int)wp->w_cursor.col + 1;
4303 break;
4304
4305 case STL_VIRTCOL:
4306 case STL_VIRTCOL_ALT:
4307 /* In list mode virtcol needs to be recomputed */
4308 virtcol = wp->w_virtcol;
4309 if (wp->w_p_list && lcs_tab1 == NUL)
4310 {
4311 wp->w_p_list = FALSE;
4312 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
4313 wp->w_p_list = TRUE;
4314 }
4315 ++virtcol;
4316 /* Don't display %V if it's the same as %c. */
4317 if (opt == STL_VIRTCOL_ALT
4318 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
4319 ? 0 : (int)wp->w_cursor.col + 1)))
4320 break;
4321 num = (long)virtcol;
4322 break;
4323
4324 case STL_PERCENTAGE:
4325 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4326 (long)wp->w_buffer->b_ml.ml_line_count);
4327 break;
4328
4329 case STL_ALTPERCENT:
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004330 str = buf_tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004331 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 break;
4333
4334 case STL_ARGLISTSTAT:
4335 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004336 buf_tmp[0] = 0;
4337 if (append_arg_number(wp, buf_tmp, (int)sizeof(buf_tmp), FALSE))
4338 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 break;
4340
4341 case STL_KEYMAP:
4342 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004343 if (get_keymap_str(wp, (char_u *)"<%s>", buf_tmp, TMPLEN))
4344 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 break;
4346 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004347#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004348 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349#else
4350 num = 0;
4351#endif
4352 break;
4353
4354 case STL_BUFNO:
4355 num = wp->w_buffer->b_fnum;
4356 break;
4357
4358 case STL_OFFSET_X:
4359 base = 'X';
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004360 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 case STL_OFFSET:
4362#ifdef FEAT_BYTEOFF
4363 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4364 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4365 0L : l + 1 + (!(State & INSERT) && empty_line ?
4366 0 : (int)wp->w_cursor.col);
4367#endif
4368 break;
4369
4370 case STL_BYTEVAL_X:
4371 base = 'X';
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004372 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004374 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 if (num == NL)
4376 num = 0;
4377 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4378 num = NL;
4379 break;
4380
4381 case STL_ROFLAG:
4382 case STL_ROFLAG_ALT:
4383 itemisflag = TRUE;
4384 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004385 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 break;
4387
4388 case STL_HELPFLAG:
4389 case STL_HELPFLAG_ALT:
4390 itemisflag = TRUE;
4391 if (wp->w_buffer->b_help)
4392 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004393 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 break;
4395
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 case STL_FILETYPE:
4397 if (*wp->w_buffer->b_p_ft != NUL
4398 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4399 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004400 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), "[%s]",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004401 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004402 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 }
4404 break;
4405
4406 case STL_FILETYPE_ALT:
4407 itemisflag = TRUE;
4408 if (*wp->w_buffer->b_p_ft != NUL
4409 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4410 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004411 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), ",%s",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004412 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004413 for (t = buf_tmp; *t != 0; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 *t = TOUPPER_LOC(*t);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004415 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 }
4417 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418
Bram Moolenaar4033c552017-09-16 20:54:51 +02004419#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 case STL_PREVIEWFLAG:
4421 case STL_PREVIEWFLAG_ALT:
4422 itemisflag = TRUE;
4423 if (wp->w_p_pvw)
4424 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4425 : _("[Preview]"));
4426 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004427
4428 case STL_QUICKFIX:
4429 if (bt_quickfix(wp->w_buffer))
4430 str = (char_u *)(wp->w_llist_ref
4431 ? _(msg_loclist)
4432 : _(msg_qflist));
4433 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434#endif
4435
4436 case STL_MODIFIED:
4437 case STL_MODIFIED_ALT:
4438 itemisflag = TRUE;
4439 switch ((opt == STL_MODIFIED_ALT)
4440 + bufIsChanged(wp->w_buffer) * 2
4441 + (!wp->w_buffer->b_p_ma) * 4)
4442 {
4443 case 2: str = (char_u *)"[+]"; break;
4444 case 3: str = (char_u *)",+"; break;
4445 case 4: str = (char_u *)"[-]"; break;
4446 case 5: str = (char_u *)",-"; break;
4447 case 6: str = (char_u *)"[+-]"; break;
4448 case 7: str = (char_u *)",+-"; break;
4449 }
4450 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004451
4452 case STL_HIGHLIGHT:
4453 t = s;
4454 while (*s != '#' && *s != NUL)
4455 ++s;
4456 if (*s == '#')
4457 {
4458 item[curitem].type = Highlight;
4459 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004460 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004461 curitem++;
4462 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004463 if (*s != NUL)
4464 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004465 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 }
4467
4468 item[curitem].start = p;
4469 item[curitem].type = Normal;
4470 if (str != NULL && *str)
4471 {
4472 t = str;
4473 if (itemisflag)
4474 {
4475 if ((t[0] && t[1])
4476 && ((!prevchar_isitem && *t == ',')
4477 || (prevchar_isflag && *t == ' ')))
4478 t++;
4479 prevchar_isflag = TRUE;
4480 }
4481 l = vim_strsize(t);
4482 if (l > 0)
4483 prevchar_isitem = TRUE;
4484 if (l > maxwid)
4485 {
4486 while (l >= maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 if (has_mbyte)
4488 {
4489 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004490 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 }
4492 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 l -= byte2cells(*t++);
4494 if (p + 1 >= out + outlen)
4495 break;
4496 *p++ = '<';
4497 }
4498 if (minwid > 0)
4499 {
4500 for (; l < minwid && p + 1 < out + outlen; l++)
4501 {
4502 /* Don't put a "-" in front of a digit. */
4503 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4504 *p++ = ' ';
4505 else
4506 *p++ = fillchar;
4507 }
4508 minwid = 0;
4509 }
4510 else
4511 minwid *= -1;
4512 while (*t && p + 1 < out + outlen)
4513 {
4514 *p++ = *t++;
4515 /* Change a space by fillchar, unless fillchar is '-' and a
4516 * digit follows. */
4517 if (fillable && p[-1] == ' '
4518 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4519 p[-1] = fillchar;
4520 }
4521 for (; l < minwid && p + 1 < out + outlen; l++)
4522 *p++ = fillchar;
4523 }
4524 else if (num >= 0)
4525 {
4526 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4527 char_u nstr[20];
4528
4529 if (p + 20 >= out + outlen)
4530 break; /* not sufficient space */
4531 prevchar_isitem = TRUE;
4532 t = nstr;
4533 if (opt == STL_VIRTCOL_ALT)
4534 {
4535 *t++ = '-';
4536 minwid--;
4537 }
4538 *t++ = '%';
4539 if (zeropad)
4540 *t++ = '0';
4541 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004542 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 *t = 0;
4544
4545 for (n = num, l = 1; n >= nbase; n /= nbase)
4546 l++;
4547 if (opt == STL_VIRTCOL_ALT)
4548 l++;
4549 if (l > maxwid)
4550 {
4551 l += 2;
4552 n = l - maxwid;
4553 while (l-- > maxwid)
4554 num /= nbase;
4555 *t++ = '>';
4556 *t++ = '%';
4557 *t = t[-3];
4558 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004559 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4560 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 }
4562 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004563 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4564 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 p += STRLEN(p);
4566 }
4567 else
4568 item[curitem].type = Empty;
4569
4570 if (opt == STL_VIM_EXPR)
4571 vim_free(str);
4572
4573 if (num >= 0 || (!itemisflag && str && *str))
4574 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4575 curitem++;
4576 }
4577 *p = NUL;
4578 itemcnt = curitem;
4579
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004580#ifdef FEAT_EVAL
4581 if (usefmt != fmt)
4582 vim_free(usefmt);
4583#endif
4584
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 width = vim_strsize(out);
4586 if (maxwidth > 0 && width > maxwidth)
4587 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004588 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 l = 0;
4590 if (itemcnt == 0)
4591 s = out;
4592 else
4593 {
4594 for ( ; l < itemcnt; l++)
4595 if (item[l].type == Trunc)
4596 {
4597 /* Truncate at %< item. */
4598 s = item[l].start;
4599 break;
4600 }
4601 if (l == itemcnt)
4602 {
4603 /* No %< item, truncate first item. */
4604 s = item[0].start;
4605 l = 0;
4606 }
4607 }
4608
4609 if (width - vim_strsize(s) >= maxwidth)
4610 {
4611 /* Truncation mark is beyond max length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 if (has_mbyte)
4613 {
4614 s = out;
4615 width = 0;
4616 for (;;)
4617 {
4618 width += ptr2cells(s);
4619 if (width >= maxwidth)
4620 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004621 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 }
4623 /* Fill up for half a double-wide character. */
4624 while (++width < maxwidth)
4625 *s++ = fillchar;
4626 }
4627 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 s = out + maxwidth - 1;
4629 for (l = 0; l < itemcnt; l++)
4630 if (item[l].start > s)
4631 break;
4632 itemcnt = l;
4633 *s++ = '>';
4634 *s = 0;
4635 }
4636 else
4637 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 if (has_mbyte)
4639 {
4640 n = 0;
4641 while (width >= maxwidth)
4642 {
4643 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004644 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 }
4646 }
4647 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 n = width - maxwidth + 1;
4649 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004650 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 *s = '<';
4652
4653 /* Fill up for half a double-wide character. */
4654 while (++width < maxwidth)
4655 {
4656 s = s + STRLEN(s);
4657 *s++ = fillchar;
4658 *s = NUL;
4659 }
4660
4661 --n; /* count the '<' */
4662 for (; l < itemcnt; l++)
4663 {
4664 if (item[l].start - n >= s)
4665 item[l].start -= n;
4666 else
4667 item[l].start = s;
4668 }
4669 }
4670 width = maxwidth;
4671 }
4672 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4673 {
4674 /* Apply STL_MIDDLE if any */
4675 for (l = 0; l < itemcnt; l++)
4676 if (item[l].type == Middle)
4677 break;
4678 if (l < itemcnt)
4679 {
4680 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004681 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 for (s = item[l].start; s < p; s++)
4683 *s = fillchar;
4684 for (l++; l < itemcnt; l++)
4685 item[l].start += maxwidth - width;
4686 width = maxwidth;
4687 }
4688 }
4689
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004690 /* Store the info about highlighting. */
4691 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004693 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 for (l = 0; l < itemcnt; l++)
4695 {
4696 if (item[l].type == Highlight)
4697 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004698 sp->start = item[l].start;
4699 sp->userhl = item[l].minwid;
4700 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 }
4702 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004703 sp->start = NULL;
4704 sp->userhl = 0;
4705 }
4706
4707 /* Store the info about tab pages labels. */
4708 if (tabtab != NULL)
4709 {
4710 sp = tabtab;
4711 for (l = 0; l < itemcnt; l++)
4712 {
4713 if (item[l].type == TabPage)
4714 {
4715 sp->start = item[l].start;
4716 sp->userhl = item[l].minwid;
4717 sp++;
4718 }
4719 }
4720 sp->start = NULL;
4721 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 }
4723
Bram Moolenaar65ed1362017-09-30 16:00:14 +02004724 /* When inside update_screen we do not want redrawing a stausline, ruler,
4725 * title, etc. to trigger another redraw, it may cause an endless loop. */
4726 if (updating_screen)
4727 {
4728 must_redraw = save_must_redraw;
4729 curwin->w_redr_type = save_redr_type;
4730 }
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004731
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 return width;
4733}
4734#endif /* FEAT_STL_OPT */
4735
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004736#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4737 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004739 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4740 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 */
4742 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004743get_rel_pos(
4744 win_T *wp,
4745 char_u *buf,
4746 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747{
4748 long above; /* number of lines above window */
4749 long below; /* number of lines below window */
4750
Bram Moolenaar0027c212015-01-07 13:31:52 +01004751 if (buflen < 3) /* need at least 3 chars for writing */
4752 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 above = wp->w_topline - 1;
4754#ifdef FEAT_DIFF
4755 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02004756 if (wp->w_topline == 1 && wp->w_topfill >= 1)
4757 above = 0; /* All buffer lines are displayed and there is an
4758 * indication of filler lines, that can be considered
4759 * seeing all lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760#endif
4761 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4762 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004763 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4764 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004766 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004768 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 ? (int)(above / ((above + below) / 100L))
4770 : (int)(above * 100L / (above + below)));
4771}
4772#endif
4773
4774/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004775 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 * Return TRUE if it was appended.
4777 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004778 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004779append_arg_number(
4780 win_T *wp,
4781 char_u *buf,
4782 int buflen,
4783 int add_file) /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784{
4785 char_u *p;
4786
4787 if (ARGCOUNT <= 1) /* nothing to do */
4788 return FALSE;
4789
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004790 p = buf + STRLEN(buf); /* go to the end of the buffer */
4791 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 return FALSE;
4793 *p++ = ' ';
4794 *p++ = '(';
4795 if (add_file)
4796 {
4797 STRCPY(p, "file ");
4798 p += 5;
4799 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004800 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4801 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4803 return TRUE;
4804}
4805
4806/*
4807 * If fname is not a full path, make it a full path.
4808 * Returns pointer to allocated memory (NULL for failure).
4809 */
4810 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004811fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812{
4813 /*
4814 * Force expanding the path always for Unix, because symbolic links may
4815 * mess up the full path name, even though it starts with a '/'.
4816 * Also expand when there is ".." in the file name, try to remove it,
4817 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004818 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 * For MS-Windows also expand names like "longna~1" to "longname".
4820 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004821#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 return FullName_save(fname, TRUE);
4823#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004824 if (!vim_isAbsName(fname)
4825 || strstr((char *)fname, "..") != NULL
4826 || strstr((char *)fname, "//") != NULL
4827# ifdef BACKSLASH_IN_FILENAME
4828 || strstr((char *)fname, "\\\\") != NULL
4829# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004830# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004832# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 )
4834 return FullName_save(fname, FALSE);
4835
4836 fname = vim_strsave(fname);
4837
Bram Moolenaar9b942202007-10-03 12:31:33 +00004838# ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01004839 if (fname != NULL)
4840 fname_case(fname, 0); /* set correct case for file name */
Bram Moolenaar9b942202007-10-03 12:31:33 +00004841# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842
4843 return fname;
4844#endif
4845}
4846
4847/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02004848 * Make "*ffname" a full file name, set "*sfname" to "*ffname" if not NULL.
4849 * "*ffname" becomes a pointer to allocated memory (or NULL).
4850 * When resolving a link both "*sfname" and "*ffname" will point to the same
4851 * allocated memory.
4852 * The "*ffname" and "*sfname" pointer values on call will not be freed.
4853 * Note that the resulting "*ffname" pointer should be considered not allocaed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004856fname_expand(
4857 buf_T *buf UNUSED,
4858 char_u **ffname,
4859 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02004861 if (*ffname == NULL) // no file name given, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 return;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02004863 if (*sfname == NULL) // no short file name given, use ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 *sfname = *ffname;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02004865 *ffname = fix_fname(*ffname); // expand to full path
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866
4867#ifdef FEAT_SHORTCUT
4868 if (!buf->b_p_bin)
4869 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004870 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02004872 // If the file name is a shortcut file, use the file it links to.
Bram Moolenaardce1e892019-02-10 23:18:53 +01004873 rfname = mch_resolve_path(*ffname, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004874 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 {
4876 vim_free(*ffname);
4877 *ffname = rfname;
4878 *sfname = rfname;
4879 }
4880 }
4881#endif
4882}
4883
4884/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 * Open a window for a number of buffers.
4886 */
4887 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004888ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889{
4890 buf_T *buf;
4891 win_T *wp, *wpnext;
4892 int split_ret = OK;
4893 int p_ea_save;
4894 int open_wins = 0;
4895 int r;
4896 int count; /* Maximum number of windows to open. */
4897 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004898 int had_tab = cmdmod.tab;
4899 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900
4901 if (eap->addr_count == 0) /* make as many windows as possible */
4902 count = 9999;
4903 else
4904 count = eap->line2; /* make as many windows as specified */
4905 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4906 all = FALSE;
4907 else
4908 all = TRUE;
4909
4910 setpcmark();
4911
4912#ifdef FEAT_GUI
4913 need_mouse_correct = TRUE;
4914#endif
4915
4916 /*
4917 * Close superfluous windows (two windows for the same buffer).
4918 * Also close windows that are not full-width.
4919 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004920 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004921 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004922 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004924 tpnext = curtab->tp_next;
4925 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004927 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004928 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004929 || ((cmdmod.split & WSP_VERT)
4930 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004931 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004932 : wp->w_width != Columns)
Bram Moolenaar4033c552017-09-16 20:54:51 +02004933 || (had_tab > 0 && wp != firstwin)) && !ONE_WINDOW
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004934 && !(wp->w_closing || wp->w_buffer->b_locked > 0))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004935 {
4936 win_close(wp, FALSE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004937 wpnext = firstwin; /* just in case an autocommand does
4938 something strange with windows */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02004939 tpnext = first_tabpage; /* start all over... */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004940 open_wins = 0;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004941 }
4942 else
4943 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004945
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004946 /* Without the ":tab" modifier only do the current tab page. */
4947 if (had_tab == 0 || tpnext == NULL)
4948 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004949 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 }
4951
4952 /*
4953 * Go through the buffer list. When a buffer doesn't have a window yet,
4954 * open one. Otherwise move the window to the right position.
4955 * Watch out for autocommands that delete buffers or windows!
4956 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4958 ++autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 win_enter(lastwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4962 {
4963 /* Check if this buffer needs a window */
4964 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4965 continue;
4966
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004967 if (had_tab != 0)
4968 {
4969 /* With the ":tab" modifier don't move the window. */
4970 if (buf->b_nwindows > 0)
4971 wp = lastwin; /* buffer has a window, skip it */
4972 else
4973 wp = NULL;
4974 }
4975 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004976 {
4977 /* Check if this buffer already has a window */
Bram Moolenaar29323592016-07-24 22:04:11 +02004978 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004979 if (wp->w_buffer == buf)
4980 break;
4981 /* If the buffer already has a window, move it */
4982 if (wp != NULL)
4983 win_move_after(wp, curwin);
4984 }
4985
4986 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02004988 bufref_T bufref;
4989
4990 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004991
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 /* Split the window and put the buffer in it */
4993 p_ea_save = p_ea;
4994 p_ea = TRUE; /* use space from all windows */
4995 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4996 ++open_wins;
4997 p_ea = p_ea_save;
4998 if (split_ret == FAIL)
4999 continue;
5000
5001 /* Open the buffer in this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005004 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005006 /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 swap_exists_action = SEA_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 break;
5009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 if (swap_exists_action == SEA_QUIT)
5011 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005012#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005013 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005014
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005015 /* Reset the error/interrupt/exception state here so that
5016 * aborting() returns FALSE when closing a window. */
5017 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005018#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005019
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005020 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 win_close(curwin, TRUE);
5022 --open_wins;
5023 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005024 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005025
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005026#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005027 /* Restore the error/interrupt/exception state if not
5028 * discarded by a new aborting error, interrupt, or uncaught
5029 * exception. */
5030 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 }
5033 else
5034 handle_swap_exists(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 }
5036
5037 ui_breakcheck();
5038 if (got_int)
5039 {
5040 (void)vgetc(); /* only break the file loading, not the rest */
5041 break;
5042 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005043#ifdef FEAT_EVAL
5044 /* Autocommands deleted the buffer or aborted script processing!!! */
5045 if (aborting())
5046 break;
5047#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005048 /* When ":tab" was used open a new tab for a new window repeatedly. */
5049 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5050 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 --autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 win_enter(firstwin, FALSE); /* back to first window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055
5056 /*
5057 * Close superfluous windows.
5058 */
5059 for (wp = lastwin; open_wins > count; )
5060 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005061 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 || autowrite(wp->w_buffer, FALSE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 if (!win_valid(wp))
5064 {
5065 /* BufWrite Autocommands made the window invalid, start over */
5066 wp = lastwin;
5067 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005068 else if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005070 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 --open_wins;
5072 wp = lastwin;
5073 }
5074 else
5075 {
5076 wp = wp->w_prev;
5077 if (wp == NULL)
5078 break;
5079 }
5080 }
5081}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005084static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005085
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086/*
5087 * do_modelines() - process mode lines for the current file
5088 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005089 * "flags" can be:
5090 * OPT_WINONLY only set options local to window
5091 * OPT_NOWIN don't set options local to window
5092 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 * Returns immediately if the "ml" option isn't set.
5094 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005096do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005098 linenr_T lnum;
5099 int nmlines;
5100 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101
5102 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5103 return;
5104
5105 /* Disallow recursive entry here. Can happen when executing a modeline
5106 * triggers an autocommand, which reloads modelines with a ":do". */
5107 if (entered)
5108 return;
5109
5110 ++entered;
5111 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5112 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005113 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 nmlines = 0;
5115
5116 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5117 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005118 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 nmlines = 0;
5120 --entered;
5121}
5122
5123#include "version.h" /* for version number */
5124
5125/*
5126 * chk_modeline() - check a single line for a mode string
5127 * Return FAIL if an error encountered.
5128 */
5129 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005130chk_modeline(
5131 linenr_T lnum,
5132 int flags) /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133{
5134 char_u *s;
5135 char_u *e;
5136 char_u *linecopy; /* local copy of any modeline found */
5137 int prev;
5138 int vers;
5139 int end;
5140 int retval = OK;
5141 char_u *save_sourcing_name;
5142 linenr_T save_sourcing_lnum;
5143#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005144 sctx_T save_current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145#endif
5146
5147 prev = -1;
5148 for (s = ml_get(lnum); *s != NUL; ++s)
5149 {
5150 if (prev == -1 || vim_isspace(prev))
5151 {
5152 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5153 || STRNCMP(s, "vi:", (size_t)3) == 0)
5154 break;
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005155 /* Accept both "vim" and "Vim". */
5156 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 {
5158 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5159 e = s + 4;
5160 else
5161 e = s + 3;
5162 vers = getdigits(&e);
5163 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005164 && (s[0] != 'V'
5165 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 && (s[3] == ':'
5167 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5168 || (VIM_VERSION_100 < vers && s[3] == '<')
5169 || (VIM_VERSION_100 > vers && s[3] == '>')
5170 || (VIM_VERSION_100 == vers && s[3] == '=')))
5171 break;
5172 }
5173 }
5174 prev = *s;
5175 }
5176
5177 if (*s)
5178 {
5179 do /* skip over "ex:", "vi:" or "vim:" */
5180 ++s;
5181 while (s[-1] != ':');
5182
5183 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5184 if (linecopy == NULL)
5185 return FAIL;
5186
5187 save_sourcing_lnum = sourcing_lnum;
5188 save_sourcing_name = sourcing_name;
5189 sourcing_lnum = lnum; /* prepare for emsg() */
5190 sourcing_name = (char_u *)"modelines";
5191
5192 end = FALSE;
5193 while (end == FALSE)
5194 {
5195 s = skipwhite(s);
5196 if (*s == NUL)
5197 break;
5198
5199 /*
5200 * Find end of set command: ':' or end of line.
5201 * Skip over "\:", replacing it with ":".
5202 */
5203 for (e = s; *e != ':' && *e != NUL; ++e)
5204 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005205 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 if (*e == NUL)
5207 end = TRUE;
5208
5209 /*
5210 * If there is a "set" command, require a terminating ':' and
5211 * ignore the stuff after the ':'.
5212 * "vi:set opt opt opt: foo" -- foo not interpreted
5213 * "vi:opt opt opt: foo" -- foo interpreted
5214 * Accept "se" for compatibility with Elvis.
5215 */
5216 if (STRNCMP(s, "set ", (size_t)4) == 0
5217 || STRNCMP(s, "se ", (size_t)3) == 0)
5218 {
5219 if (*e != ':') /* no terminating ':'? */
5220 break;
5221 end = TRUE;
5222 s = vim_strchr(s, ' ') + 1;
5223 }
5224 *e = NUL; /* truncate the set command */
5225
5226 if (*s != NUL) /* skip over an empty "::" */
5227 {
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005228 int secure_save = secure;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005230 save_current_sctx = current_sctx;
5231 current_sctx.sc_sid = SID_MODELINE;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005232 current_sctx.sc_seq = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005233 current_sctx.sc_lnum = 0;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02005234 current_sctx.sc_version = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235#endif
Bram Moolenaar5958f952018-11-20 04:25:21 +01005236 // Make sure no risky things are executed as a side effect.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01005237 secure = 1;
Bram Moolenaar5958f952018-11-20 04:25:21 +01005238
Bram Moolenaara3227e22006-03-08 21:32:40 +00005239 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar5958f952018-11-20 04:25:21 +01005240
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005241 secure = secure_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005243 current_sctx = save_current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244#endif
5245 if (retval == FAIL) /* stop if error found */
5246 break;
5247 }
5248 s = e + 1; /* advance to next part */
5249 }
5250
5251 sourcing_lnum = save_sourcing_lnum;
5252 sourcing_name = save_sourcing_name;
5253
5254 vim_free(linecopy);
5255 }
5256 return retval;
5257}
5258
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005259/*
Bram Moolenaar91335e52018-08-01 17:53:12 +02005260 * Return TRUE if "buf" is a normal buffer, 'buftype' is empty.
5261 */
5262 int
5263bt_normal(buf_T *buf)
5264{
5265 return buf != NULL && buf->b_p_bt[0] == NUL;
5266}
5267
Bram Moolenaar113e1072019-01-20 15:30:40 +01005268#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar91335e52018-08-01 17:53:12 +02005269/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005270 * Return TRUE if "buf" is the quickfix buffer.
5271 */
5272 int
5273bt_quickfix(buf_T *buf)
5274{
5275 return buf != NULL && buf->b_p_bt[0] == 'q';
5276}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005277#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005278
Bram Moolenaar113e1072019-01-20 15:30:40 +01005279#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005280/*
5281 * Return TRUE if "buf" is a terminal buffer.
5282 */
5283 int
5284bt_terminal(buf_T *buf)
5285{
5286 return buf != NULL && buf->b_p_bt[0] == 't';
5287}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005288#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005289
5290/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005291 * Return TRUE if "buf" is a help buffer.
5292 */
5293 int
5294bt_help(buf_T *buf)
5295{
5296 return buf != NULL && buf->b_help;
5297}
5298
5299/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02005300 * Return TRUE if "buf" is a prompt buffer.
5301 */
5302 int
5303bt_prompt(buf_T *buf)
5304{
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005305 return buf != NULL && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'r';
5306}
5307
5308/*
5309 * Return TRUE if "buf" is a buffer for a popup window.
5310 */
5311 int
5312bt_popup(buf_T *buf)
5313{
5314 return buf != NULL && buf->b_p_bt != NULL
5315 && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'o';
Bram Moolenaarf2732452018-06-03 14:47:35 +02005316}
5317
5318/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005319 * Return TRUE if "buf" is a "nofile", "acwrite", "terminal" or "prompt"
5320 * buffer. This means the buffer name is not a file name.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005321 */
5322 int
Bram Moolenaar26910de2019-06-15 19:37:15 +02005323bt_nofilename(buf_T *buf)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005324{
5325 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5326 || buf->b_p_bt[0] == 'a'
Bram Moolenaarf2732452018-06-03 14:47:35 +02005327 || buf->b_p_bt[0] == 't'
5328 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005329}
5330
5331/*
Bram Moolenaar26910de2019-06-15 19:37:15 +02005332 * Return TRUE if "buf" has 'buftype' set to "nofile".
5333 */
5334 int
5335bt_nofile(buf_T *buf)
5336{
5337 return buf != NULL && buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f';
5338}
5339
5340/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005341 * Return TRUE if "buf" is a "nowrite", "nofile", "terminal" or "prompt"
5342 * buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005343 */
5344 int
5345bt_dontwrite(buf_T *buf)
5346{
Bram Moolenaarf2732452018-06-03 14:47:35 +02005347 return buf != NULL && (buf->b_p_bt[0] == 'n'
5348 || buf->b_p_bt[0] == 't'
5349 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005350}
5351
Bram Moolenaar113e1072019-01-20 15:30:40 +01005352#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005353 int
5354bt_dontwrite_msg(buf_T *buf)
5355{
5356 if (bt_dontwrite(buf))
5357 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005358 emsg(_("E382: Cannot write, 'buftype' option is set"));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005359 return TRUE;
5360 }
5361 return FALSE;
5362}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005363#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005364
5365/*
5366 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
5367 * and 'bufhidden'.
5368 */
5369 int
5370buf_hide(buf_T *buf)
5371{
5372 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
5373 switch (buf->b_p_bh[0])
5374 {
5375 case 'u': /* "unload" */
5376 case 'w': /* "wipe" */
5377 case 'd': return FALSE; /* "delete" */
5378 case 'h': return TRUE; /* "hide" */
5379 }
5380 return (p_hid || cmdmod.hide);
5381}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382
5383/*
5384 * Return special buffer name.
5385 * Returns NULL when the buffer has a normal file name.
5386 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005387 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005388buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389{
Bram Moolenaar4033c552017-09-16 20:54:51 +02005390#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005392 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005393 /*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005394 * Differentiate between the quickfix and location list buffers using
5395 * the buffer number stored in the global quickfix stack.
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005396 */
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005397 if (buf->b_fnum == qf_stack_get_bufnr())
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005398 return (char_u *)_(msg_qflist);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005399 else
5400 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02005403
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 /* There is no _file_ when 'buftype' is "nofile", b_sfname
Bram Moolenaar21554412017-07-24 21:44:43 +02005405 * contains the name as specified by the user. */
Bram Moolenaar26910de2019-06-15 19:37:15 +02005406 if (bt_nofilename(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 {
Bram Moolenaar21554412017-07-24 21:44:43 +02005408#ifdef FEAT_TERMINAL
5409 if (buf->b_term != NULL)
5410 return term_get_status_text(buf->b_term);
5411#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02005412 if (buf->b_fname != NULL)
5413 return buf->b_fname;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02005414#ifdef FEAT_JOB_CHANNEL
5415 if (bt_prompt(buf))
5416 return (char_u *)_("[Prompt]");
5417#endif
Bram Moolenaarc6896e22019-05-30 22:32:34 +02005418#ifdef FEAT_TEXT_PROP
5419 if (bt_popup(buf))
5420 return (char_u *)_("[Popup]");
5421#endif
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005422 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 }
Bram Moolenaar21554412017-07-24 21:44:43 +02005424
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005426 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 return NULL;
5428}
5429
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02005430#if defined(FEAT_JOB_CHANNEL) \
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005431 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5432 || defined(PROTO)
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02005433# define SWITCH_TO_WIN
5434
5435/*
5436 * Find a window that contains "buf" and switch to it.
5437 * If there is no such window, use the current window and change "curbuf".
5438 * Caller must initialize save_curbuf to NULL.
5439 * restore_win_for_buf() MUST be called later!
5440 */
5441 void
5442switch_to_win_for_buf(
5443 buf_T *buf,
5444 win_T **save_curwinp,
5445 tabpage_T **save_curtabp,
5446 bufref_T *save_curbuf)
5447{
5448 win_T *wp;
5449 tabpage_T *tp;
5450
5451 if (find_win_for_buf(buf, &wp, &tp) == FAIL)
5452 switch_buffer(save_curbuf, buf);
5453 else if (switch_win(save_curwinp, save_curtabp, wp, tp, TRUE) == FAIL)
5454 {
5455 restore_win(*save_curwinp, *save_curtabp, TRUE);
5456 switch_buffer(save_curbuf, buf);
5457 }
5458}
5459
5460 void
5461restore_win_for_buf(
5462 win_T *save_curwin,
5463 tabpage_T *save_curtab,
5464 bufref_T *save_curbuf)
5465{
5466 if (save_curbuf->br_buf == NULL)
5467 restore_win(save_curwin, save_curtab, TRUE);
5468 else
5469 restore_buffer(save_curbuf);
5470}
5471#endif
5472
Bram Moolenaar4033c552017-09-16 20:54:51 +02005473#if defined(FEAT_QUICKFIX) || defined(SWITCH_TO_WIN) || defined(PROTO)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005474/*
5475 * Find a window for buffer "buf".
5476 * If found OK is returned and "wp" and "tp" are set to the window and tabpage.
5477 * If not found FAIL is returned.
5478 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005479 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005480find_win_for_buf(
5481 buf_T *buf,
5482 win_T **wp,
5483 tabpage_T **tp)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005484{
5485 FOR_ALL_TAB_WINDOWS(*tp, *wp)
5486 if ((*wp)->w_buffer == buf)
5487 goto win_found;
5488 return FAIL;
5489win_found:
5490 return OK;
5491}
5492#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494/*
5495 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5496 */
5497 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005498set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499{
5500 if (on != curbuf->b_p_bl)
5501 {
5502 curbuf->b_p_bl = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 if (on)
5504 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5505 else
5506 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 }
5508}
5509
5510/*
5511 * Read the file for "buf" again and check if the contents changed.
5512 * Return TRUE if it changed or this could not be checked.
5513 */
5514 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005515buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516{
5517 buf_T *newbuf;
5518 int differ = TRUE;
5519 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 exarg_T ea;
5522
5523 /* Allocate a buffer without putting it in the buffer list. */
5524 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5525 if (newbuf == NULL)
5526 return TRUE;
5527
5528 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5529 if (prep_exarg(&ea, buf) == FAIL)
5530 {
5531 wipe_buffer(newbuf, FALSE);
5532 return TRUE;
5533 }
5534
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 /* set curwin/curbuf to buf and save a few things */
5536 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537
Bram Moolenaar4770d092006-01-12 23:22:24 +00005538 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 && readfile(buf->b_ffname, buf->b_fname,
5540 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5541 &ea, READ_NEW | READ_DUMMY) == OK)
5542 {
5543 /* compare the two files line by line */
5544 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5545 {
5546 differ = FALSE;
5547 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5548 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5549 {
5550 differ = TRUE;
5551 break;
5552 }
5553 }
5554 }
5555 vim_free(ea.cmd);
5556
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 /* restore curwin/curbuf and a few other things */
5558 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559
5560 if (curbuf != newbuf) /* safety check */
5561 wipe_buffer(newbuf, FALSE);
5562
5563 return differ;
5564}
5565
5566/*
5567 * Wipe out a buffer and decrement the last buffer number if it was used for
5568 * this buffer. Call this to wipe out a temp buffer that does not contain any
5569 * marks.
5570 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005572wipe_buffer(
5573 buf_T *buf,
5574 int aucmd UNUSED) /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575{
5576 if (buf->b_fnum == top_file_num - 1)
5577 --top_file_num;
5578
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005580 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005581
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005582 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005583
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005585 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586}
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005587
5588#if defined(FEAT_EVAL) || defined(PROTO)
5589/*
5590 * Mark references in functions of buffers.
5591 */
5592 int
5593set_ref_in_buffers(int copyID)
5594{
5595 int abort = FALSE;
5596 buf_T *bp;
5597
5598 FOR_ALL_BUFFERS(bp)
5599 {
5600 listener_T *lnr;
5601 typval_T tv;
5602
5603 for (lnr = bp->b_listener; !abort && lnr != NULL; lnr = lnr->lr_next)
5604 {
5605 if (lnr->lr_callback.cb_partial != NULL)
5606 {
5607 tv.v_type = VAR_PARTIAL;
5608 tv.vval.v_partial = lnr->lr_callback.cb_partial;
5609 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
5610 }
5611 }
5612# ifdef FEAT_JOB_CHANNEL
5613 if (!abort && bp->b_prompt_callback.cb_partial != NULL)
5614 {
5615 tv.v_type = VAR_PARTIAL;
5616 tv.vval.v_partial = bp->b_prompt_callback.cb_partial;
5617 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
5618 }
5619 if (!abort && bp->b_prompt_interrupt.cb_partial != NULL)
5620 {
5621 tv.v_type = VAR_PARTIAL;
5622 tv.vval.v_partial = bp->b_prompt_interrupt.cb_partial;
5623 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
5624 }
5625# endif
5626 if (abort)
5627 break;
5628 }
5629 return abort;
5630}
5631#endif