blob: 3251e37d6a737dbae3812c3b072f0f660358da35 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * buffer.c: functions for dealing with the buffer structure
12 */
13
14/*
15 * The buffer list is a double linked list of all buffers.
16 * Each buffer can be in one of these states:
17 * never loaded: BF_NEVERLOADED is set, only the file name is valid
18 * not loaded: b_ml.ml_mfp == NULL, no memfile allocated
19 * hidden: b_nwindows == 0, loaded but not displayed in a window
20 * normal: loaded and displayed in a window
21 *
22 * Instead of storing file names all over the place, each file name is
23 * stored in the buffer list. It can be referenced by a number.
24 *
25 * The current implementation remembers all file names ever used.
26 */
27
Bram Moolenaar071d4272004-06-13 20:20:40 +000028#include "vim.h"
29
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010030static char_u *buflist_match(regmatch_T *rmp, buf_T *buf, int ignore_case);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010031static char_u *fname_match(regmatch_T *rmp, char_u *name, int ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +020033static buf_T *buflist_findname_stat(char_u *ffname, stat_T *st);
34static int otherfile_buf(buf_T *buf, char_u *ffname, stat_T *stp);
35static int buf_same_ino(buf_T *buf, stat_T *stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000036#else
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010037static int otherfile_buf(buf_T *buf, char_u *ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000038#endif
39#ifdef FEAT_TITLE
Bram Moolenaar84a93082018-06-16 22:58:15 +020040static int value_changed(char_u *str, char_u **last);
Bram Moolenaar071d4272004-06-13 20:20:40 +000041#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010042static int append_arg_number(win_T *wp, char_u *buf, int buflen, int add_file);
43static void free_buffer(buf_T *);
44static void free_buffer_stuff(buf_T *buf, int free_options);
45static void clear_wininfo(buf_T *buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000046
47#ifdef UNIX
48# define dev_T dev_t
49#else
50# define dev_T unsigned
51#endif
52
Bram Moolenaar4033c552017-09-16 20:54:51 +020053#if defined(FEAT_QUICKFIX)
Bram Moolenaar7fd73202010-07-25 16:58:46 +020054static char *msg_loclist = N_("[Location List]");
55static char *msg_qflist = N_("[Quickfix List]");
56#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +010057static char *e_auabort = N_("E855: Autocommands caused command to abort");
Bram Moolenaar7fd73202010-07-25 16:58:46 +020058
Bram Moolenaarb25f9a92016-07-10 18:21:50 +020059/* Number of times free_buffer() was called. */
60static int buf_free_count = 0;
61
Bram Moolenaarc024b462019-06-08 18:07:21 +020062/*
63 * Read data from buffer for retrying.
64 */
Bram Moolenaarf71d7b92016-08-09 22:14:05 +020065 static int
66read_buffer(
67 int read_stdin, /* read file from stdin, otherwise fifo */
68 exarg_T *eap, /* for forced 'ff' and 'fenc' or NULL */
69 int flags) /* extra flags for readfile() */
70{
71 int retval = OK;
72 linenr_T line_count;
73
74 /*
75 * Read from the buffer which the text is already filled in and append at
76 * the end. This makes it possible to retry when 'fileformat' or
77 * 'fileencoding' was guessed wrong.
78 */
79 line_count = curbuf->b_ml.ml_line_count;
80 retval = readfile(
81 read_stdin ? NULL : curbuf->b_ffname,
82 read_stdin ? NULL : curbuf->b_fname,
83 (linenr_T)line_count, (linenr_T)0, (linenr_T)MAXLNUM, eap,
84 flags | READ_BUFFER);
85 if (retval == OK)
86 {
87 /* Delete the binary lines. */
88 while (--line_count >= 0)
89 ml_delete((linenr_T)1, FALSE);
90 }
91 else
92 {
93 /* Delete the converted lines. */
94 while (curbuf->b_ml.ml_line_count > line_count)
95 ml_delete(line_count, FALSE);
96 }
97 /* Put the cursor on the first line. */
98 curwin->w_cursor.lnum = 1;
99 curwin->w_cursor.col = 0;
100
101 if (read_stdin)
102 {
103 /* Set or reset 'modified' before executing autocommands, so that
104 * it can be changed there. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100105 if (!readonlymode && !BUFEMPTY())
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200106 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100107 else if (retval == OK)
Bram Moolenaarc024b462019-06-08 18:07:21 +0200108 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200109
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100110 if (retval == OK)
111 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100112#ifdef FEAT_EVAL
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100113 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100114 curbuf, &retval);
115#else
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100116 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200117#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100118 }
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200119 }
120 return retval;
121}
122
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123/*
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200124 * Ensure buffer "buf" is loaded. Does not trigger the swap-exists action.
125 */
126 void
127buffer_ensure_loaded(buf_T *buf)
128{
129 if (buf->b_ml.ml_mfp == NULL)
130 {
131 aco_save_T aco;
132
133 aucmd_prepbuf(&aco, buf);
134 swap_exists_action = SEA_NONE;
135 open_buffer(FALSE, NULL, 0);
136 aucmd_restbuf(&aco);
137 }
138}
139
140/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200141 * Open current buffer, that is: open the memfile and read the file into
142 * memory.
143 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144 */
145 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100146open_buffer(
147 int read_stdin, /* read file from stdin */
148 exarg_T *eap, /* for forced 'ff' and 'fenc' or NULL */
149 int flags) /* extra flags for readfile() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150{
151 int retval = OK;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200152 bufref_T old_curbuf;
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100153#ifdef FEAT_SYN_HL
154 long old_tw = curbuf->b_p_tw;
155#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200156 int read_fifo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157
158 /*
159 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
160 * When re-entering the same buffer, it should not change, because the
161 * user may have reset the flag by hand.
162 */
163 if (readonlymode && curbuf->b_ffname != NULL
164 && (curbuf->b_flags & BF_NEVERLOADED))
165 curbuf->b_p_ro = TRUE;
166
Bram Moolenaar4770d092006-01-12 23:22:24 +0000167 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168 {
169 /*
170 * There MUST be a memfile, otherwise we can't do anything
171 * If we can't create one for the current buffer, take another buffer
172 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100173 close_buffer(NULL, curbuf, 0, FALSE);
Bram Moolenaar29323592016-07-24 22:04:11 +0200174 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175 if (curbuf->b_ml.ml_mfp != NULL)
176 break;
177 /*
178 * if there is no memfile at all, exit
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000179 * This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 */
181 if (curbuf == NULL)
182 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100183 emsg(_("E82: Cannot allocate any buffer, exiting..."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 getout(2);
185 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100186 emsg(_("E83: Cannot allocate buffer, using other one..."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 enter_buffer(curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100188#ifdef FEAT_SYN_HL
189 if (old_tw != curbuf->b_p_tw)
190 check_colorcolumn(curwin);
191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 return FAIL;
193 }
194
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 /* The autocommands in readfile() may change the buffer, but only AFTER
196 * reading the file. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200197 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 modified_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199
200 /* mark cursor position as being invalid */
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100201 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202
203 if (curbuf->b_ffname != NULL
204#ifdef FEAT_NETBEANS_INTG
205 && netbeansReadFile
206#endif
207 )
208 {
Bram Moolenaar426dd022016-03-15 15:09:29 +0100209 int old_msg_silent = msg_silent;
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200210#ifdef UNIX
211 int save_bin = curbuf->b_p_bin;
212 int perm;
213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214#ifdef FEAT_NETBEANS_INTG
215 int oldFire = netbeansFireChanges;
216
217 netbeansFireChanges = 0;
218#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200219#ifdef UNIX
220 perm = mch_getperm(curbuf->b_ffname);
Bram Moolenaard569bb02018-08-11 13:57:20 +0200221 if (perm >= 0 && (S_ISFIFO(perm)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200222 || S_ISSOCK(perm)
Bram Moolenaarf04507d2016-08-20 15:05:39 +0200223# ifdef OPEN_CHR_FILES
224 || (S_ISCHR(perm) && is_dev_fd_file(curbuf->b_ffname))
225# endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200226 ))
227 read_fifo = TRUE;
228 if (read_fifo)
229 curbuf->b_p_bin = TRUE;
230#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100231 if (shortmess(SHM_FILEINFO))
232 msg_silent = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200234 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200235 flags | READ_NEW | (read_fifo ? READ_FIFO : 0));
236#ifdef UNIX
237 if (read_fifo)
238 {
239 curbuf->b_p_bin = save_bin;
240 if (retval == OK)
241 retval = read_buffer(FALSE, eap, flags);
242 }
243#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100244 msg_silent = old_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245#ifdef FEAT_NETBEANS_INTG
246 netbeansFireChanges = oldFire;
247#endif
248 /* Help buffer is filtered. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +0200249 if (bt_help(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250 fix_help_buffer();
251 }
252 else if (read_stdin)
253 {
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200254 int save_bin = curbuf->b_p_bin;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255
256 /*
257 * First read the text in binary mode into the buffer.
258 * Then read from that same buffer and append at the end. This makes
259 * it possible to retry when 'fileformat' or 'fileencoding' was
260 * guessed wrong.
261 */
262 curbuf->b_p_bin = TRUE;
263 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200264 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
265 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266 curbuf->b_p_bin = save_bin;
267 if (retval == OK)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200268 retval = read_buffer(TRUE, eap, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 }
270
271 /* if first time loading this buffer, init b_chartab[] */
272 if (curbuf->b_flags & BF_NEVERLOADED)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100273 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 (void)buf_init_chartab(curbuf, FALSE);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100275#ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100276 parse_cino(curbuf);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100277#endif
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279
280 /*
281 * Set/reset the Changed flag first, autocmds may change the buffer.
282 * Apply the automatic commands, before processing the modelines.
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200283 * So the modelines have priority over autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284 */
285 /* When reading stdin, the buffer contents always needs writing, so set
286 * the changed flag. Unless in readonly mode: "ls | gview -".
287 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000288 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 || modified_was_set /* ":set modified" used in autocmd */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100290#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000293 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100295 else if (retval == OK && !read_stdin && !read_fifo)
Bram Moolenaarc024b462019-06-08 18:07:21 +0200296 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297 save_file_ff(curbuf); /* keep this fileformat */
298
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100299 /* Set last_changedtick to avoid triggering a TextChanged autocommand right
300 * after it was added. */
301 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
302#ifdef FEAT_INS_EXPAND
303 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
304#endif
305
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 /* require "!" to overwrite the file, because it wasn't read completely */
307#ifdef FEAT_EVAL
308 if (aborting())
309#else
310 if (got_int)
311#endif
312 curbuf->b_flags |= BF_READERR;
313
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000314#ifdef FEAT_FOLDING
315 /* Need to update automatic folding. Do this before the autocommands,
316 * they may use the fold info. */
317 foldUpdateAll(curwin);
318#endif
319
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 /* need to set w_topline, unless some autocommand already did that. */
321 if (!(curwin->w_valid & VALID_TOPLINE))
322 {
323 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100324#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100328#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100330#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332#endif
333
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100334 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 /*
337 * The autocommands may have changed the current buffer. Apply the
338 * modelines to the correct buffer, if it still exists and is loaded.
339 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200340 if (bufref_valid(&old_curbuf) && old_curbuf.br_buf->b_ml.ml_mfp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 {
342 aco_save_T aco;
343
344 /* Go to the buffer that was opened. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200345 aucmd_prepbuf(&aco, old_curbuf.br_buf);
Bram Moolenaara3227e22006-03-08 21:32:40 +0000346 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
348
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100349#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100351 &retval);
352#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355
356 /* restore curwin/curbuf and a few other things */
357 aucmd_restbuf(&aco);
358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 }
360
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 return retval;
362}
363
364/*
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200365 * Store "buf" in "bufref" and set the free count.
366 */
367 void
368set_bufref(bufref_T *bufref, buf_T *buf)
369{
370 bufref->br_buf = buf;
Bram Moolenaarfadacf02017-06-19 20:35:32 +0200371 bufref->br_fnum = buf == NULL ? 0 : buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200372 bufref->br_buf_free_count = buf_free_count;
373}
374
375/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200376 * Return TRUE if "bufref->br_buf" points to the same buffer as when
377 * set_bufref() was called and it is a valid buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200378 * Only goes through the buffer list if buf_free_count changed.
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200379 * Also checks if b_fnum is still the same, a :bwipe followed by :new might get
380 * the same allocated memory, but it's a different buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200381 */
382 int
383bufref_valid(bufref_T *bufref)
384{
385 return bufref->br_buf_free_count == buf_free_count
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200386 ? TRUE : buf_valid(bufref->br_buf)
387 && bufref->br_fnum == bufref->br_buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200388}
389
390/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200392 * This can be slow if there are many buffers, prefer using bufref_valid().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 */
394 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100395buf_valid(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396{
397 buf_T *bp;
398
Bram Moolenaar82404332016-07-10 17:00:38 +0200399 /* Assume that we more often have a recent buffer, start with the last
400 * one. */
401 for (bp = lastbuf; bp != NULL; bp = bp->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402 if (bp == buf)
403 return TRUE;
404 return FALSE;
405}
406
407/*
Bram Moolenaar480778b2016-07-14 22:09:39 +0200408 * A hash table used to quickly lookup a buffer by its number.
409 */
410static hashtab_T buf_hashtab;
411
412 static void
413buf_hashtab_add(buf_T *buf)
414{
415 sprintf((char *)buf->b_key, "%x", buf->b_fnum);
416 if (hash_add(&buf_hashtab, buf->b_key) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100417 emsg(_("E931: Buffer cannot be registered"));
Bram Moolenaar480778b2016-07-14 22:09:39 +0200418}
419
420 static void
421buf_hashtab_remove(buf_T *buf)
422{
423 hashitem_T *hi = hash_find(&buf_hashtab, buf->b_key);
424
425 if (!HASHITEM_EMPTY(hi))
426 hash_remove(&buf_hashtab, hi);
427}
428
Bram Moolenaar94f01952018-09-01 15:30:03 +0200429/*
430 * Return TRUE when buffer "buf" can be unloaded.
431 * Give an error message and return FALSE when the buffer is locked or the
432 * screen is being redrawn and the buffer is in a window.
433 */
434 static int
435can_unload_buffer(buf_T *buf)
436{
437 int can_unload = !buf->b_locked;
438
439 if (can_unload && updating_screen)
440 {
441 win_T *wp;
442
443 FOR_ALL_WINDOWS(wp)
444 if (wp->w_buffer == buf)
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200445 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200446 can_unload = FALSE;
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200447 break;
448 }
Bram Moolenaar94f01952018-09-01 15:30:03 +0200449 }
450 if (!can_unload)
Bram Moolenaardefa0672019-07-21 19:25:37 +0200451 semsg(_("E937: Attempt to delete a buffer that is in use: %s"),
452 buf->b_fname);
Bram Moolenaar94f01952018-09-01 15:30:03 +0200453 return can_unload;
454}
Bram Moolenaara997b452018-04-17 23:24:06 +0200455
Bram Moolenaar480778b2016-07-14 22:09:39 +0200456/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 * Close the link to a buffer.
458 * "action" is used when there is no longer a window for the buffer.
459 * It can be:
460 * 0 buffer becomes hidden
461 * DOBUF_UNLOAD buffer is unloaded
462 * DOBUF_DELETE buffer is unloaded and removed from buffer list
463 * DOBUF_WIPE buffer is unloaded and really deleted
464 * When doing all but the first one on the current buffer, the caller should
465 * get a new buffer very soon!
466 *
467 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100468 *
469 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
470 * cause there to be only one window with this buffer. e.g. when ":quit" is
471 * supposed to close the window but autocommands close all other windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472 */
473 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100474close_buffer(
475 win_T *win, /* if not NULL, set b_last_cursor */
476 buf_T *buf,
477 int action,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200478 int abort_if_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100481 int nwindows;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200482 bufref_T bufref;
Bram Moolenaar3a117e12016-10-30 21:57:52 +0100483 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200484 win_T *the_curwin = curwin;
485 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 int unload_buf = (action != 0);
487 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
488 int wipe_buf = (action == DOBUF_WIPE);
489
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200490 /*
491 * Force unloading or deleting when 'bufhidden' says so.
492 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
493 * "hide" (otherwise we could never free or delete a buffer).
494 */
495 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
496 {
497 del_buf = TRUE;
498 unload_buf = TRUE;
499 }
500 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
501 {
502 del_buf = TRUE;
503 unload_buf = TRUE;
504 wipe_buf = TRUE;
505 }
506 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
507 unload_buf = TRUE;
508
Bram Moolenaar94053a52017-08-01 21:44:33 +0200509#ifdef FEAT_TERMINAL
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200510 if (bt_terminal(buf) && (buf->b_nwindows == 1 || del_buf))
Bram Moolenaar94053a52017-08-01 21:44:33 +0200511 {
512 if (term_job_running(buf->b_term))
513 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200514 if (wipe_buf || unload_buf)
Bram Moolenaara997b452018-04-17 23:24:06 +0200515 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200516 if (!can_unload_buffer(buf))
Bram Moolenaara997b452018-04-17 23:24:06 +0200517 return;
Bram Moolenaar94f01952018-09-01 15:30:03 +0200518
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200519 /* Wiping out or unloading a terminal buffer kills the job. */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200520 free_terminal(buf);
Bram Moolenaara997b452018-04-17 23:24:06 +0200521 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200522 else
523 {
524 /* The job keeps running, hide the buffer. */
525 del_buf = FALSE;
526 unload_buf = FALSE;
527 }
528 }
529 else
530 {
531 /* A terminal buffer is wiped out if the job has finished. */
532 del_buf = TRUE;
533 unload_buf = TRUE;
534 wipe_buf = TRUE;
535 }
536 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200537#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200539 /* Disallow deleting the buffer when it is locked (already being closed or
540 * halfway a command that relies on it). Unloading is allowed. */
Bram Moolenaar94f01952018-09-01 15:30:03 +0200541 if ((del_buf || wipe_buf) && !can_unload_buffer(buf))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200542 return;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200543
Bram Moolenaar4033c552017-09-16 20:54:51 +0200544 /* check no autocommands closed the window */
545 if (win != NULL && win_valid_any_tab(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 {
547 /* Set b_last_cursor when closing the last window for the buffer.
548 * Remember the last cursor position and window options of the buffer.
549 * This used to be only for the current window, but then options like
550 * 'foldmethod' may be lost with a ":only" command. */
551 if (buf->b_nwindows == 1)
552 set_last_cursor(win);
553 buflist_setfpos(buf, win,
554 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
555 win->w_cursor.col, TRUE);
556 }
557
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200558 set_bufref(&bufref, buf);
559
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 /* When the buffer is no longer in a window, trigger BufWinLeave */
561 if (buf->b_nwindows == 1)
562 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200563 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200564 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
565 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200566 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100567 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200568 /* Autocommands deleted the buffer. */
569aucmd_abort:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100570 emsg(_(e_auabort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 return;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100572 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200573 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200574 if (abort_if_last && one_window())
575 /* Autocommands made this the only window. */
576 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577
578 /* When the buffer becomes hidden, but is not unloaded, trigger
579 * BufHidden */
580 if (!unload_buf)
581 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200582 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200583 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
584 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200585 && !bufref_valid(&bufref))
Bram Moolenaar362ce482012-06-06 19:02:45 +0200586 /* Autocommands deleted the buffer. */
587 goto aucmd_abort;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200588 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200589 if (abort_if_last && one_window())
590 /* Autocommands made this the only window. */
591 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100593#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 if (aborting()) /* autocmds may abort script processing */
595 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200598
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200599 /* If the buffer was in curwin and the window has changed, go back to that
600 * window, if it still exists. This avoids that ":edit x" triggering a
601 * "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
602 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
603 {
604 block_autocmds();
605 goto_tabpage_win(the_curtab, the_curwin);
606 unblock_autocmds();
607 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200608
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 nwindows = buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610
611 /* decrease the link count from windows (unless not in any window) */
612 if (buf->b_nwindows > 0)
613 --buf->b_nwindows;
614
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100615#ifdef FEAT_DIFF
616 if (diffopt_hiddenoff() && !unload_buf && buf->b_nwindows == 0)
Bram Moolenaarb7407d32018-02-03 17:36:27 +0100617 diff_buf_delete(buf); /* Clear 'diff' for hidden buffer. */
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100618#endif
619
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 /* Return when a window is displaying the buffer or when it's not
621 * unloaded. */
622 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624
625 /* Always remove the buffer when there is no file name. */
626 if (buf->b_ffname == NULL)
627 del_buf = TRUE;
628
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200629 /* When closing the current buffer stop Visual mode before freeing
630 * anything. */
Bram Moolenaar4930a762016-09-11 14:39:53 +0200631 if (buf == curbuf && VIsual_active
Bram Moolenaar9a27c7f2016-09-09 12:57:09 +0200632#if defined(EXITFREE)
633 && !entered_free_all_mem
634#endif
635 )
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200636 end_visual_mode();
637
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 /*
639 * Free all things allocated for this buffer.
640 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
641 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 /* Remember if we are closing the current buffer. Restore the number of
643 * windows, so that autocommands in buf_freeall() don't get confused. */
644 is_curbuf = (buf == curbuf);
645 buf->b_nwindows = nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200647 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 /* Autocommands may have deleted the buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200650 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100652#ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000653 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100655#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 /*
658 * It's possible that autocommands change curbuf to the one being deleted.
659 * This might cause the previous curbuf to be deleted unexpectedly. But
660 * in some cases it's OK to delete the curbuf, because a new one is
661 * obtained anyway. Therefore only return if curbuf changed to the
662 * deleted buffer.
663 */
664 if (buf == curbuf && !is_curbuf)
665 return;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200666
Bram Moolenaar4033c552017-09-16 20:54:51 +0200667 if (win_valid_any_tab(win) && win->w_buffer == buf)
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200668 win->w_buffer = NULL; /* make sure we don't use the buffer now */
669
670 /* Autocommands may have opened or closed windows for this buffer.
671 * Decrement the count for the close we do here. */
672 if (buf->b_nwindows > 0)
673 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 /*
676 * Remove the buffer from the list.
677 */
678 if (wipe_buf)
679 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +0200680 if (buf->b_sfname != buf->b_ffname)
681 VIM_CLEAR(buf->b_sfname);
682 else
683 buf->b_sfname = NULL;
684 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 if (buf->b_prev == NULL)
686 firstbuf = buf->b_next;
687 else
688 buf->b_prev->b_next = buf->b_next;
689 if (buf->b_next == NULL)
690 lastbuf = buf->b_prev;
691 else
692 buf->b_next->b_prev = buf->b_prev;
693 free_buffer(buf);
694 }
695 else
696 {
697 if (del_buf)
698 {
699 /* Free all internal variables and reset option values, to make
700 * ":bdel" compatible with Vim 5.7. */
701 free_buffer_stuff(buf, TRUE);
702
703 /* Make it look like a new buffer. */
704 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
705
706 /* Init the options when loaded again. */
707 buf->b_p_initialized = FALSE;
708 }
709 buf_clear_file(buf);
710 if (del_buf)
711 buf->b_p_bl = FALSE;
712 }
713}
714
715/*
716 * Make buffer not contain a file.
717 */
718 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100719buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720{
721 buf->b_ml.ml_line_count = 1;
Bram Moolenaarc024b462019-06-08 18:07:21 +0200722 unchanged(buf, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 buf->b_p_eol = TRUE;
725 buf->b_start_eol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000727 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 buf->b_ml.ml_mfp = NULL;
729 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
730#ifdef FEAT_NETBEANS_INTG
731 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732#endif
733}
734
735/*
736 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200737 * the file. Careful: get here with "curwin" NULL when exiting.
738 * flags:
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200739 * BFA_DEL buffer is going to be deleted
740 * BFA_WIPE buffer is going to be wiped out
741 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100744buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200747 bufref_T bufref;
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200748 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaar5a497892016-09-03 16:29:04 +0200749 win_T *the_curwin = curwin;
750 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751
Bram Moolenaar5a497892016-09-03 16:29:04 +0200752 /* Make sure the buffer isn't closed by autocommands. */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200753 ++buf->b_locked;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200754 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200755 if (buf->b_ml.ml_mfp != NULL)
756 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200757 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
758 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200759 && !bufref_valid(&bufref))
760 /* autocommands deleted the buffer */
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200761 return;
762 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200763 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200765 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
766 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200767 && !bufref_valid(&bufref))
768 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 return;
770 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200771 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200773 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
774 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200775 && !bufref_valid(&bufref))
776 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 return;
778 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200779 --buf->b_locked;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200780
Bram Moolenaar5a497892016-09-03 16:29:04 +0200781 /* If the buffer was in curwin and the window has changed, go back to that
782 * window, if it still exists. This avoids that ":edit x" triggering a
783 * "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
784 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
785 {
786 block_autocmds();
787 goto_tabpage_win(the_curtab, the_curwin);
788 unblock_autocmds();
789 }
Bram Moolenaar5a497892016-09-03 16:29:04 +0200790
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100791#ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000792 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795
796 /*
797 * It's possible that autocommands change curbuf to the one being deleted.
798 * This might cause curbuf to be deleted unexpectedly. But in some cases
799 * it's OK to delete the curbuf, because a new one is obtained anyway.
800 * Therefore only return if curbuf changed to the deleted buffer.
801 */
802 if (buf == curbuf && !is_curbuf)
803 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804#ifdef FEAT_DIFF
805 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
806#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200807#ifdef FEAT_SYN_HL
Bram Moolenaar89c71222011-11-30 15:40:56 +0100808 /* Remove any ownsyntax, unless exiting. */
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200809 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaar89c71222011-11-30 15:40:56 +0100810 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200811#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000812
813#ifdef FEAT_FOLDING
814 /* No folds in an empty buffer. */
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000815 {
816 win_T *win;
817 tabpage_T *tp;
818
819 FOR_ALL_TAB_WINDOWS(tp, win)
820 if (win->w_buffer == buf)
821 clearFolding(win);
822 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000823#endif
824
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825#ifdef FEAT_TCL
826 tcl_buffer_free(buf);
827#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 ml_close(buf, TRUE); /* close and delete the memline/memfile */
829 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200830 if ((flags & BFA_KEEP_UNDO) == 0)
831 {
832 u_blockfree(buf); /* free the memory allocated for undo */
833 u_clearall(buf); /* reset all undo information */
834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200836 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100838#ifdef FEAT_TEXT_PROP
839 clear_buf_prop_types(buf);
840#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000841 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842}
843
844/*
845 * Free a buffer structure and the things it contains related to the buffer
846 * itself (not the file, that must have been done already).
847 */
848 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100849free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200851 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200853#ifdef FEAT_EVAL
Bram Moolenaarb2c87502017-10-14 21:15:58 +0200854 /* b:changedtick uses an item in buf_T, remove it now */
855 dictitem_remove(buf->b_vars, (dictitem_T *)&buf->b_ct_di);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200856 unref_var_dict(buf->b_vars);
857#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200858#ifdef FEAT_LUA
859 lua_buffer_free(buf);
860#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000861#ifdef FEAT_MZSCHEME
862 mzscheme_buffer_free(buf);
863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864#ifdef FEAT_PERL
865 perl_buf_free(buf);
866#endif
867#ifdef FEAT_PYTHON
868 python_buffer_free(buf);
869#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200870#ifdef FEAT_PYTHON3
871 python3_buffer_free(buf);
872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873#ifdef FEAT_RUBY
874 ruby_buffer_free(buf);
875#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200876#ifdef FEAT_JOB_CHANNEL
877 channel_buffer_free(buf);
878#endif
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200879#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +0200880 free_terminal(buf);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200881#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +0200882#ifdef FEAT_JOB_CHANNEL
883 vim_free(buf->b_prompt_text);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200884 free_callback(&buf->b_prompt_callback);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200885#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200886
887 buf_hashtab_remove(buf);
888
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200889 aubuflocal_remove(buf);
890
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200891 if (autocmd_busy)
892 {
893 /* Do not free the buffer structure while autocommands are executing,
894 * it's still needed. Free it when autocmd_busy is reset. */
895 buf->b_next = au_pending_free_buf;
896 au_pending_free_buf = buf;
897 }
898 else
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200899 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900}
901
902/*
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100903 * Initializes b:changedtick.
Bram Moolenaar79518e22017-02-17 16:31:35 +0100904 */
905 static void
906init_changedtick(buf_T *buf)
907{
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100908 dictitem_T *di = (dictitem_T *)&buf->b_ct_di;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100909
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100910 di->di_flags = DI_FLAGS_FIX | DI_FLAGS_RO;
911 di->di_tv.v_type = VAR_NUMBER;
912 di->di_tv.v_lock = VAR_FIXED;
913 di->di_tv.vval.v_number = 0;
914
Bram Moolenaar92769c32017-02-25 15:41:37 +0100915#ifdef FEAT_EVAL
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100916 STRCPY(buf->b_ct_di.di_key, "changedtick");
917 (void)dict_add(buf->b_vars, di);
Bram Moolenaar92769c32017-02-25 15:41:37 +0100918#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +0100919}
920
921/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
923 */
924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100925free_buffer_stuff(
926 buf_T *buf,
927 int free_options) /* free options as well */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928{
929 if (free_options)
930 {
931 clear_wininfo(buf); /* including window-local options */
932 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200933#ifdef FEAT_SPELL
934 ga_clear(&buf->b_s.b_langp);
935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 }
937#ifdef FEAT_EVAL
Bram Moolenaar79518e22017-02-17 16:31:35 +0100938 {
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100939 varnumber_T tick = CHANGEDTICK(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +0100940
941 vars_clear(&buf->b_vars->dv_hashtab); /* free all buffer variables */
942 hash_init(&buf->b_vars->dv_hashtab);
943 init_changedtick(buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100944 CHANGEDTICK(buf) = tick;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946#endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200947 uc_clear(&buf->b_ucmds); // clear local user commands
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948#ifdef FEAT_SIGNS
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200949 buf_delete_signs(buf, (char_u *)"*"); // delete any signs
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000951#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200952 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
955 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100956 VIM_CLEAR(buf->b_start_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957}
958
959/*
960 * Free the b_wininfo list for buffer "buf".
961 */
962 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100963clear_wininfo(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964{
965 wininfo_T *wip;
966
967 while (buf->b_wininfo != NULL)
968 {
969 wip = buf->b_wininfo;
970 buf->b_wininfo = wip->wi_next;
971 if (wip->wi_optset)
972 {
973 clear_winopt(&wip->wi_opt);
974#ifdef FEAT_FOLDING
975 deleteFoldRecurse(&wip->wi_folds);
976#endif
977 }
978 vim_free(wip);
979 }
980}
981
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982/*
983 * Go to another buffer. Handles the result of the ATTENTION dialog.
984 */
985 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100986goto_buffer(
987 exarg_T *eap,
988 int start,
989 int dir,
990 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200992 bufref_T old_curbuf;
993
994 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995
996 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
998 start, dir, count, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
1000 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001001#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001002 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001003
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001004 /* Reset the error/interrupt/exception state here so that
1005 * aborting() returns FALSE when closing a window. */
1006 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001007#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001008
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001009 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 win_close(curwin, TRUE);
1011 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001012 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001013
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001014#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001015 /* Restore the error/interrupt/exception state if not discarded by a
1016 * new aborting error, interrupt, or uncaught exception. */
1017 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 }
1020 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001021 handle_swap_exists(&old_curbuf);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001022}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024/*
1025 * Handle the situation of swap_exists_action being set.
1026 * It is allowed for "old_curbuf" to be NULL or invalid.
1027 */
1028 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001029handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030{
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001031#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001032 cleanup_T cs;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001033#endif
1034#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001035 long old_tw = curbuf->b_p_tw;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001036#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001037 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001038
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 if (swap_exists_action == SEA_QUIT)
1040 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001041#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001042 /* Reset the error/interrupt/exception state here so that
1043 * aborting() returns FALSE when closing a buffer. */
1044 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001045#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001046
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 /* User selected Quit at ATTENTION prompt. Go back to previous
1048 * buffer. If that buffer is gone or the same as the current one,
1049 * open a new, empty buffer. */
1050 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001051 swap_exists_did_quit = TRUE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001052 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001053 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1054 || old_curbuf->br_buf == curbuf)
1055 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
1056 else
1057 buf = old_curbuf->br_buf;
1058 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001059 {
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001060 int old_msg_silent = msg_silent;
1061
1062 if (shortmess(SHM_FILEINFO))
1063 msg_silent = 1; // prevent fileinfo message
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001064 enter_buffer(buf);
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001065 // restore msg_silent, so that the command line will be shown
1066 msg_silent = old_msg_silent;
1067
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001068#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001069 if (old_tw != curbuf->b_p_tw)
1070 check_colorcolumn(curwin);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001071#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001074
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001075#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001076 /* Restore the error/interrupt/exception state if not discarded by a
1077 * new aborting error, interrupt, or uncaught exception. */
1078 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 }
1081 else if (swap_exists_action == SEA_RECOVER)
1082 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001083#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001084 /* Reset the error/interrupt/exception state here so that
1085 * aborting() returns FALSE when closing a buffer. */
1086 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001087#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001088
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 /* User selected Recover at ATTENTION prompt. */
1090 msg_scroll = TRUE;
Bram Moolenaar99499b12019-05-23 21:35:48 +02001091 ml_recover(FALSE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001092 msg_puts("\n"); /* don't overwrite the last message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001094 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001095
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001096#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001097 /* Restore the error/interrupt/exception state if not discarded by a
1098 * new aborting error, interrupt, or uncaught exception. */
1099 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 }
1102 swap_exists_action = SEA_NONE;
1103}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105/*
1106 * do_bufdel() - delete or unload buffer(s)
1107 *
1108 * addr_count == 0: ":bdel" - delete current buffer
1109 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1110 * buffer "end_bnr", then any other arguments.
1111 * addr_count == 2: ":N,N bdel" - delete buffers in range
1112 *
1113 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1114 * DOBUF_DEL (":bdel")
1115 *
1116 * Returns error message or NULL
1117 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001118 char *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001119do_bufdel(
1120 int command,
1121 char_u *arg, /* pointer to extra arguments */
1122 int addr_count,
1123 int start_bnr, /* first buffer number in a range */
1124 int end_bnr, /* buffer nr or last buffer nr in a range */
1125 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126{
1127 int do_current = 0; /* delete current buffer? */
1128 int deleted = 0; /* number of buffers deleted */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001129 char *errormsg = NULL; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 int bnr; /* buffer number */
1131 char_u *p;
1132
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 if (addr_count == 0)
1134 {
1135 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1136 }
1137 else
1138 {
1139 if (addr_count == 2)
1140 {
1141 if (*arg) /* both range and argument is not allowed */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001142 return _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 bnr = start_bnr;
1144 }
1145 else /* addr_count == 1 */
1146 bnr = end_bnr;
1147
1148 for ( ;!got_int; ui_breakcheck())
1149 {
1150 /*
1151 * delete the current buffer last, otherwise when the
1152 * current buffer is deleted, the next buffer becomes
1153 * the current one and will be loaded, which may then
1154 * also be deleted, etc.
1155 */
1156 if (bnr == curbuf->b_fnum)
1157 do_current = bnr;
1158 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
1159 forceit) == OK)
1160 ++deleted;
1161
1162 /*
1163 * find next buffer number to delete/unload
1164 */
1165 if (addr_count == 2)
1166 {
1167 if (++bnr > end_bnr)
1168 break;
1169 }
1170 else /* addr_count == 1 */
1171 {
1172 arg = skipwhite(arg);
1173 if (*arg == NUL)
1174 break;
1175 if (!VIM_ISDIGIT(*arg))
1176 {
1177 p = skiptowhite_esc(arg);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01001178 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE,
1179 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 if (bnr < 0) /* failed */
1181 break;
1182 arg = p;
1183 }
1184 else
1185 bnr = getdigits(&arg);
1186 }
1187 }
1188 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1189 FORWARD, do_current, forceit) == OK)
1190 ++deleted;
1191
1192 if (deleted == 0)
1193 {
1194 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001195 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001197 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00001199 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001200 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 }
1202 else if (deleted >= p_report)
1203 {
1204 if (command == DOBUF_UNLOAD)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001205 smsg(NGETTEXT("%d buffer unloaded",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001206 "%d buffers unloaded", deleted), deleted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 else if (command == DOBUF_DEL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001208 smsg(NGETTEXT("%d buffer deleted",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001209 "%d buffers deleted", deleted), deleted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001211 smsg(NGETTEXT("%d buffer wiped out",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001212 "%d buffers wiped out", deleted), deleted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 }
1214 }
1215
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216
1217 return errormsg;
1218}
1219
Bram Moolenaara02471e2014-01-10 16:43:14 +01001220/*
1221 * Make the current buffer empty.
1222 * Used when it is wiped out and it's the last buffer.
1223 */
1224 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001225empty_curbuf(
1226 int close_others,
1227 int forceit,
1228 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001229{
1230 int retval;
1231 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001232 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001233
1234 if (action == DOBUF_UNLOAD)
1235 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001236 emsg(_("E90: Cannot unload last buffer"));
Bram Moolenaara02471e2014-01-10 16:43:14 +01001237 return FAIL;
1238 }
1239
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001240 set_bufref(&bufref, buf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001241 if (close_others)
1242 /* Close any other windows on this buffer, then make it empty. */
Bram Moolenaara02471e2014-01-10 16:43:14 +01001243 close_windows(buf, TRUE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001244
1245 setpcmark();
1246 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1247 forceit ? ECMD_FORCEIT : 0, curwin);
1248
1249 /*
1250 * do_ecmd() may create a new buffer, then we have to delete
1251 * the old one. But do_ecmd() may have done that already, check
1252 * if the buffer still exists.
1253 */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001254 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001255 close_buffer(NULL, buf, action, FALSE);
1256 if (!close_others)
1257 need_fileinfo = FALSE;
1258 return retval;
1259}
Bram Moolenaar94f01952018-09-01 15:30:03 +02001260
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261/*
1262 * Implementation of the commands for the buffer list.
1263 *
1264 * action == DOBUF_GOTO go to specified buffer
1265 * action == DOBUF_SPLIT split window and go to specified buffer
1266 * action == DOBUF_UNLOAD unload specified buffer(s)
1267 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1268 * action == DOBUF_WIPE delete specified buffer(s) really
1269 *
1270 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1271 * start == DOBUF_FIRST go to "count" buffer from first buffer
1272 * start == DOBUF_LAST go to "count" buffer from last buffer
1273 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1274 *
1275 * Return FAIL or OK.
1276 */
1277 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001278do_buffer(
1279 int action,
1280 int start,
1281 int dir, /* FORWARD or BACKWARD */
1282 int count, /* buffer number or number of buffers */
1283 int forceit) /* TRUE for :...! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284{
1285 buf_T *buf;
1286 buf_T *bp;
1287 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1288 || action == DOBUF_WIPE);
1289
1290 switch (start)
1291 {
1292 case DOBUF_FIRST: buf = firstbuf; break;
1293 case DOBUF_LAST: buf = lastbuf; break;
1294 default: buf = curbuf; break;
1295 }
1296 if (start == DOBUF_MOD) /* find next modified buffer */
1297 {
1298 while (count-- > 0)
1299 {
1300 do
1301 {
1302 buf = buf->b_next;
1303 if (buf == NULL)
1304 buf = firstbuf;
1305 }
1306 while (buf != curbuf && !bufIsChanged(buf));
1307 }
1308 if (!bufIsChanged(buf))
1309 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001310 emsg(_("E84: No modified buffer found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 return FAIL;
1312 }
1313 }
1314 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
1315 {
1316 while (buf != NULL && buf->b_fnum != count)
1317 buf = buf->b_next;
1318 }
1319 else
1320 {
1321 bp = NULL;
1322 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1323 {
1324 /* remember the buffer where we start, we come back there when all
1325 * buffers are unlisted. */
1326 if (bp == NULL)
1327 bp = buf;
1328 if (dir == FORWARD)
1329 {
1330 buf = buf->b_next;
1331 if (buf == NULL)
1332 buf = firstbuf;
1333 }
1334 else
1335 {
1336 buf = buf->b_prev;
1337 if (buf == NULL)
1338 buf = lastbuf;
1339 }
1340 /* don't count unlisted buffers */
1341 if (unload || buf->b_p_bl)
1342 {
1343 --count;
1344 bp = NULL; /* use this buffer as new starting point */
1345 }
1346 if (bp == buf)
1347 {
1348 /* back where we started, didn't find anything. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001349 emsg(_("E85: There is no listed buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 return FAIL;
1351 }
1352 }
1353 }
1354
1355 if (buf == NULL) /* could not find it */
1356 {
1357 if (start == DOBUF_FIRST)
1358 {
1359 /* don't warn when deleting */
1360 if (!unload)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001361 semsg(_(e_nobufnr), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 }
1363 else if (dir == FORWARD)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001364 emsg(_("E87: Cannot go beyond last buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001366 emsg(_("E88: Cannot go before first buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 return FAIL;
1368 }
1369
1370#ifdef FEAT_GUI
1371 need_mouse_correct = TRUE;
1372#endif
1373
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 /*
1375 * delete buffer buf from memory and/or the list
1376 */
1377 if (unload)
1378 {
1379 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001380 bufref_T bufref;
1381
Bram Moolenaar94f01952018-09-01 15:30:03 +02001382 if (!can_unload_buffer(buf))
Bram Moolenaara997b452018-04-17 23:24:06 +02001383 return FAIL;
Bram Moolenaara997b452018-04-17 23:24:06 +02001384
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001385 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386
1387 /* When unloading or deleting a buffer that's already unloaded and
1388 * unlisted: fail silently. */
1389 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1390 return FAIL;
1391
1392 if (!forceit && bufIsChanged(buf))
1393 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001394#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 if ((p_confirm || cmdmod.confirm) && p_write)
1396 {
1397 dialog_changed(buf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001398 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 /* Autocommand deleted buffer, oops! It's not changed
1400 * now. */
1401 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001402 /* If it's still changed fail silently, the dialog already
1403 * mentioned why it fails. */
1404 if (bufIsChanged(buf))
1405 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001407 else
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001408#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001410 semsg(_("E89: No write since last change for buffer %d (add ! to override)"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 buf->b_fnum);
1412 return FAIL;
1413 }
1414 }
1415
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001416 /* When closing the current buffer stop Visual mode. */
Bram Moolenaar4930a762016-09-11 14:39:53 +02001417 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001418 end_visual_mode();
1419
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 /*
1421 * If deleting the last (listed) buffer, make it empty.
1422 * The last (listed) buffer cannot be unloaded.
1423 */
Bram Moolenaar29323592016-07-24 22:04:11 +02001424 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 if (bp->b_p_bl && bp != buf)
1426 break;
1427 if (bp == NULL && buf == curbuf)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001428 return empty_curbuf(TRUE, forceit, action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 /*
1431 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001432 * (unless it's the only window). Repeat this so long as we end up in
1433 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001435 while (buf == curbuf
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001436 && !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
Bram Moolenaar459ca562016-11-10 18:16:33 +01001437 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001438 {
1439 if (win_close(curwin, FALSE) == FAIL)
1440 break;
1441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442
1443 /*
1444 * If the buffer to be deleted is not the current one, delete it here.
1445 */
1446 if (buf != curbuf)
1447 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001448 close_windows(buf, FALSE);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001449 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows <= 0)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001450 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 return OK;
1452 }
1453
1454 /*
1455 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001456 * There should be another, otherwise it would have been handled
1457 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001458 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 * Then prefer the buffer we most recently visited.
1460 * Else try to find one that is loaded, after the current buffer,
1461 * then before the current buffer.
1462 * Finally use any buffer.
1463 */
1464 buf = NULL; /* selected buffer */
1465 bp = NULL; /* used when no loaded buffer found */
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001466 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1467 buf = au_new_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468#ifdef FEAT_JUMPLIST
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001469 else if (curwin->w_jumplistlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 {
1471 int jumpidx;
1472
1473 jumpidx = curwin->w_jumplistidx - 1;
1474 if (jumpidx < 0)
1475 jumpidx = curwin->w_jumplistlen - 1;
1476
1477 forward = jumpidx;
1478 while (jumpidx != curwin->w_jumplistidx)
1479 {
1480 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1481 if (buf != NULL)
1482 {
1483 if (buf == curbuf || !buf->b_p_bl)
1484 buf = NULL; /* skip current and unlisted bufs */
1485 else if (buf->b_ml.ml_mfp == NULL)
1486 {
1487 /* skip unloaded buf, but may keep it for later */
1488 if (bp == NULL)
1489 bp = buf;
1490 buf = NULL;
1491 }
1492 }
1493 if (buf != NULL) /* found a valid buffer: stop searching */
1494 break;
1495 /* advance to older entry in jump list */
1496 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1497 break;
1498 if (--jumpidx < 0)
1499 jumpidx = curwin->w_jumplistlen - 1;
1500 if (jumpidx == forward) /* List exhausted for sure */
1501 break;
1502 }
1503 }
1504#endif
1505
1506 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1507 {
1508 forward = TRUE;
1509 buf = curbuf->b_next;
1510 for (;;)
1511 {
1512 if (buf == NULL)
1513 {
1514 if (!forward) /* tried both directions */
1515 break;
1516 buf = curbuf->b_prev;
1517 forward = FALSE;
1518 continue;
1519 }
1520 /* in non-help buffer, try to skip help buffers, and vv */
1521 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1522 {
1523 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1524 break;
1525 if (bp == NULL) /* remember unloaded buf for later */
1526 bp = buf;
1527 }
1528 if (forward)
1529 buf = buf->b_next;
1530 else
1531 buf = buf->b_prev;
1532 }
1533 }
1534 if (buf == NULL) /* No loaded buffer, use unloaded one */
1535 buf = bp;
1536 if (buf == NULL) /* No loaded buffer, find listed one */
1537 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001538 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 if (buf->b_p_bl && buf != curbuf)
1540 break;
1541 }
1542 if (buf == NULL) /* Still no buffer, just take one */
1543 {
1544 if (curbuf->b_next != NULL)
1545 buf = curbuf->b_next;
1546 else
1547 buf = curbuf->b_prev;
1548 }
1549 }
1550
Bram Moolenaara02471e2014-01-10 16:43:14 +01001551 if (buf == NULL)
1552 {
1553 /* Autocommands must have wiped out all other buffers. Only option
1554 * now is to make the current buffer empty. */
1555 return empty_curbuf(FALSE, forceit, action);
1556 }
1557
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 /*
1559 * make buf current buffer
1560 */
1561 if (action == DOBUF_SPLIT) /* split window first */
1562 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001563 /* If 'switchbuf' contains "useopen": jump to first window containing
1564 * "buf" if one exists */
1565 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001566 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001567 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001568 * page containing "buf" if one exists */
1569 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 return OK;
1571 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 return FAIL;
1573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574
1575 /* go to current buffer - nothing to do */
1576 if (buf == curbuf)
1577 return OK;
1578
1579 /*
1580 * Check if the current buffer may be abandoned.
1581 */
1582 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1583 {
1584#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1585 if ((p_confirm || cmdmod.confirm) && p_write)
1586 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001587 bufref_T bufref;
1588
1589 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 dialog_changed(curbuf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001591 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 /* Autocommand deleted buffer, oops! */
1593 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 }
1595 if (bufIsChanged(curbuf))
1596#endif
1597 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001598 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 return FAIL;
1600 }
1601 }
1602
1603 /* Go to the other buffer. */
1604 set_curbuf(buf, action);
1605
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001607 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001609#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 if (aborting()) /* autocmds may abort script processing */
1611 return FAIL;
1612#endif
1613
1614 return OK;
1615}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616
1617/*
1618 * Set current buffer to "buf". Executes autocommands and closes current
1619 * buffer. "action" tells how to close the current buffer:
1620 * DOBUF_GOTO free or hide it
1621 * DOBUF_SPLIT nothing
1622 * DOBUF_UNLOAD unload it
1623 * DOBUF_DEL delete it
1624 * DOBUF_WIPE wipe it out
1625 */
1626 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001627set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628{
1629 buf_T *prevbuf;
1630 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1631 || action == DOBUF_WIPE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001632#ifdef FEAT_SYN_HL
1633 long old_tw = curbuf->b_p_tw;
1634#endif
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001635 bufref_T newbufref;
1636 bufref_T prevbufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637
1638 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001639 if (!cmdmod.keepalt)
1640 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001641 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 /* Don't restart Select mode after switching to another buffer. */
1644 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001646 /* close_windows() or apply_autocmds() may change curbuf and wipe out "buf"
1647 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 prevbuf = curbuf;
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001649 set_bufref(&prevbufref, prevbuf);
1650 set_bufref(&newbufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001652 /* Autocommands may delete the curren buffer and/or the buffer we wan to go
1653 * to. In those cases don't close the buffer. */
Bram Moolenaar82404332016-07-10 17:00:38 +02001654 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001655 || (bufref_valid(&prevbufref)
1656 && bufref_valid(&newbufref)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001657#ifdef FEAT_EVAL
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001658 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001660 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001662#ifdef FEAT_SYN_HL
1663 if (prevbuf == curwin->w_buffer)
1664 reset_synblock(curwin);
1665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001667 close_windows(prevbuf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001668#if defined(FEAT_EVAL)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001669 if (bufref_valid(&prevbufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670#else
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001671 if (bufref_valid(&prevbufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001673 {
Bram Moolenaare25865a2012-07-06 16:22:02 +02001674 win_T *previouswin = curwin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001675 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001676 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1678 unload ? action : (action == DOBUF_GOTO
Bram Moolenaareb44a682017-08-03 22:44:55 +02001679 && !buf_hide(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001680 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaare25865a2012-07-06 16:22:02 +02001681 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaar9e931222012-06-20 11:55:01 +02001682 /* autocommands changed curwin, Grr! */
Bram Moolenaare25865a2012-07-06 16:22:02 +02001683 curwin = previouswin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 }
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001686 /* An autocommand may have deleted "buf", already entered it (e.g., when
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001687 * it did ":bunload") or aborted the script processing.
Bram Moolenaar9e931222012-06-20 11:55:01 +02001688 * If curwin->w_buffer is null, enter_buffer() will make it valid again */
1689 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001690#ifdef FEAT_EVAL
Bram Moolenaar4033c552017-09-16 20:54:51 +02001691 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001693 ) || curwin->w_buffer == NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001694 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001696#ifdef FEAT_SYN_HL
1697 if (old_tw != curbuf->b_p_tw)
1698 check_colorcolumn(curwin);
1699#endif
1700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701}
1702
1703/*
1704 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001705 * Old curbuf must have been abandoned already! This also means "curbuf" may
1706 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 */
1708 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001709enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710{
1711 /* Copy buffer and window local option values. Not for a help buffer. */
1712 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1713 if (!buf->b_help)
1714 get_winopts(buf);
1715#ifdef FEAT_FOLDING
1716 else
1717 /* Remove all folds in the window. */
1718 clearFolding(curwin);
1719 foldUpdateAll(curwin); /* update folds (later). */
1720#endif
1721
1722 /* Get the buffer in the current window. */
1723 curwin->w_buffer = buf;
1724 curbuf = buf;
1725 ++curbuf->b_nwindows;
1726
1727#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001728 if (curwin->w_p_diff)
1729 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730#endif
1731
Bram Moolenaara971b822011-09-14 14:43:25 +02001732#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001733 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001734#endif
1735
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 /* Cursor on first line by default. */
1737 curwin->w_cursor.lnum = 1;
1738 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001741 curwin->w_topline_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001743 /* mark cursor position as being invalid */
1744 curwin->w_valid = 0;
1745
Bram Moolenaar9cea87c2018-09-21 16:59:45 +02001746 buflist_setfpos(curbuf, curwin, curbuf->b_last_cursor.lnum,
1747 curbuf->b_last_cursor.col, TRUE);
1748
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 /* Make sure the buffer is loaded. */
1750 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001751 {
1752 /* If there is no filetype, allow for detecting one. Esp. useful for
1753 * ":ball" used in a autocommand. If there already is a filetype we
1754 * might prefer to keep it. */
1755 if (*curbuf->b_p_ft == NUL)
1756 did_filetype = FALSE;
1757
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001758 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 else
1761 {
Bram Moolenaareda65222019-05-16 20:29:44 +02001762 if (!msg_silent && !shortmess(SHM_FILEINFO))
1763 need_fileinfo = TRUE; // display file info after redraw
1764
1765 // check if file changed
1766 (void)buf_check_timestamp(curbuf, FALSE);
1767
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001769#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1773 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 }
1775
1776 /* If autocommands did not change the cursor position, restore cursor lnum
1777 * and possibly cursor col. */
1778 if (curwin->w_cursor.lnum == 1 && inindent(0))
1779 buflist_getfpos();
1780
1781 check_arg_idx(curwin); /* check for valid arg_idx */
1782#ifdef FEAT_TITLE
1783 maketitle();
1784#endif
Bram Moolenaard4153d42008-11-15 15:06:17 +00001785 /* when autocmds didn't change it */
1786 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1788
Bram Moolenaar009b2592004-10-24 19:18:58 +00001789#ifdef FEAT_NETBEANS_INTG
1790 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001791 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001792#endif
1793
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001794 /* Change directories when the 'acd' option is set. */
Bram Moolenaar6f470022018-04-10 18:47:20 +02001795 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796
1797#ifdef FEAT_KEYMAP
1798 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001799 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001801#ifdef FEAT_SPELL
1802 /* May need to set the spell language. Can only do this after the buffer
1803 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001804 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1805 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001806#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001807#ifdef FEAT_VIMINFO
1808 curbuf->b_last_used = vim_time();
1809#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001810
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 redraw_later(NOT_VALID);
1812}
1813
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001814#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1815/*
1816 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001817 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001818 */
1819 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001820do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001821{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001822 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001823 && curbuf->b_ffname != NULL
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001824 && vim_chdirfile(curbuf->b_ffname, "auto") == OK)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001825 shorten_fnames(TRUE);
1826}
1827#endif
1828
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001829 void
1830no_write_message(void)
1831{
1832#ifdef FEAT_TERMINAL
1833 if (term_job_running(curbuf->b_term))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001834 emsg(_("E948: Job still running (add ! to end the job)"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001835 else
1836#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001837 emsg(_("E37: No write since last change (add ! to override)"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001838}
1839
1840 void
Bram Moolenaar7a760922018-02-19 23:10:02 +01001841no_write_message_nobang(buf_T *buf UNUSED)
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001842{
1843#ifdef FEAT_TERMINAL
Bram Moolenaar7a760922018-02-19 23:10:02 +01001844 if (term_job_running(buf->b_term))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001845 emsg(_("E948: Job still running"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001846 else
1847#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001848 emsg(_("E37: No write since last change"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001849}
1850
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851/*
1852 * functions for dealing with the buffer list
1853 */
1854
Bram Moolenaar480778b2016-07-14 22:09:39 +02001855static int top_file_num = 1; /* highest file number */
1856
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857/*
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001858 * Return TRUE if the current buffer is empty, unnamed, unmodified and used in
1859 * only one window. That means it can be re-used.
1860 */
1861 int
1862curbuf_reusable(void)
1863{
1864 return (curbuf != NULL
1865 && curbuf->b_ffname == NULL
1866 && curbuf->b_nwindows <= 1
1867 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY())
Bram Moolenaard85c3962019-04-07 14:19:14 +02001868#if defined(FEAT_QUICKFIX)
Bram Moolenaar39803d82019-04-07 12:04:51 +02001869 && !bt_quickfix(curbuf)
Bram Moolenaard85c3962019-04-07 14:19:14 +02001870#endif
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001871 && !curbufIsChanged());
1872}
1873
1874/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 * Add a file name to the buffer list. Return a pointer to the buffer.
1876 * If the same file name already exists return a pointer to that buffer.
1877 * If it does not exist, or if fname == NULL, a new entry is created.
1878 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1879 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1880 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001881 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001882 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
1883 * if the buffer already exists.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 * This is the ONLY way to create a new buffer.
1885 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001887buflist_new(
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001888 char_u *ffname_arg, // full path of fname or relative
1889 char_u *sfname_arg, // short fname or NULL
1890 linenr_T lnum, // preferred cursor line
1891 int flags) // BLN_ defines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001893 char_u *ffname = ffname_arg;
1894 char_u *sfname = sfname_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 buf_T *buf;
1896#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001897 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898#endif
1899
Bram Moolenaar480778b2016-07-14 22:09:39 +02001900 if (top_file_num == 1)
1901 hash_init(&buf_hashtab);
1902
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001903 fname_expand(curbuf, &ffname, &sfname); // will allocate ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904
1905 /*
1906 * If file name already exists in the list, update the entry.
1907 */
1908#ifdef UNIX
1909 /* On Unix we can use inode numbers when the file exists. Works better
1910 * for hard links. */
1911 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1912 st.st_dev = (dev_T)-1;
1913#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001914 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915#ifdef UNIX
1916 buflist_findname_stat(ffname, &st)
1917#else
1918 buflist_findname(ffname)
1919#endif
1920 ) != NULL)
1921 {
1922 vim_free(ffname);
1923 if (lnum != 0)
1924 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02001925
1926 if ((flags & BLN_NOOPT) == 0)
1927 /* copy the options now, if 'cpo' doesn't have 's' and not done
1928 * already */
1929 buf_copy_options(buf, 0);
1930
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1932 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001933 bufref_T bufref;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001934
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 buf->b_p_bl = TRUE;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001936 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001938 {
Bram Moolenaar82404332016-07-10 17:00:38 +02001939 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001940 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001941 return NULL;
1942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 }
1944 return buf;
1945 }
1946
1947 /*
1948 * If the current buffer has no name and no contents, use the current
1949 * buffer. Otherwise: Need to allocate a new buffer structure.
1950 *
1951 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001952 * (A spell file buffer is allocated in spell.c, but that's not a normal
1953 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 */
1955 buf = NULL;
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001956 if ((flags & BLN_CURBUF) && curbuf_reusable())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 {
1958 buf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 /* It's like this buffer is deleted. Watch out for autocommands that
1960 * change curbuf! If that happens, allocate a new buffer anyway. */
1961 if (curbuf->b_p_bl)
1962 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1963 if (buf == curbuf)
1964 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001965#ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001966 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 if (buf == curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 {
1971 /* Make sure 'bufhidden' and 'buftype' are empty */
1972 clear_string_option(&buf->b_p_bh);
1973 clear_string_option(&buf->b_p_bt);
1974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 }
1976 if (buf != curbuf || curbuf == NULL)
1977 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001978 buf = ALLOC_CLEAR_ONE(buf_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 if (buf == NULL)
1980 {
1981 vim_free(ffname);
1982 return NULL;
1983 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02001984#ifdef FEAT_EVAL
1985 /* init b: variables */
1986 buf->b_vars = dict_alloc();
1987 if (buf->b_vars == NULL)
1988 {
1989 vim_free(ffname);
1990 vim_free(buf);
1991 return NULL;
1992 }
1993 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
1994#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01001995 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 }
1997
1998 if (ffname != NULL)
1999 {
2000 buf->b_ffname = ffname;
2001 buf->b_sfname = vim_strsave(sfname);
2002 }
2003
2004 clear_wininfo(buf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002005 buf->b_wininfo = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006
2007 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
2008 || buf->b_wininfo == NULL)
2009 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02002010 if (buf->b_sfname != buf->b_ffname)
2011 VIM_CLEAR(buf->b_sfname);
2012 else
2013 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01002014 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 if (buf != curbuf)
2016 free_buffer(buf);
2017 return NULL;
2018 }
2019
2020 if (buf == curbuf)
2021 {
2022 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002023 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 if (buf != curbuf) /* autocommands deleted the buffer! */
2025 return NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002026#if defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002027 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 return NULL;
2029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002031
2032 /* Init the options. */
2033 buf->b_p_initialized = FALSE;
2034 buf_copy_options(buf, BCO_ENTER);
2035
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036#ifdef FEAT_KEYMAP
2037 /* need to reload lmaps and set b:keymap_name */
2038 curbuf->b_kmap_state |= KEYMAP_INIT;
2039#endif
2040 }
2041 else
2042 {
2043 /*
2044 * put new buffer at the end of the buffer list
2045 */
2046 buf->b_next = NULL;
2047 if (firstbuf == NULL) /* buffer list is empty */
2048 {
2049 buf->b_prev = NULL;
2050 firstbuf = buf;
2051 }
2052 else /* append new buffer at end of list */
2053 {
2054 lastbuf->b_next = buf;
2055 buf->b_prev = lastbuf;
2056 }
2057 lastbuf = buf;
2058
2059 buf->b_fnum = top_file_num++;
2060 if (top_file_num < 0) /* wrap around (may cause duplicates) */
2061 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002062 emsg(_("W14: Warning: List of file names overflow"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 if (emsg_silent == 0)
2064 {
2065 out_flush();
2066 ui_delay(3000L, TRUE); /* make sure it is noticed */
2067 }
2068 top_file_num = 1;
2069 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002070 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071
2072 /*
2073 * Always copy the options from the current buffer.
2074 */
2075 buf_copy_options(buf, BCO_ALWAYS);
2076 }
2077
2078 buf->b_wininfo->wi_fpos.lnum = lnum;
2079 buf->b_wininfo->wi_win = curwin;
2080
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002081#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002082 hash_init(&buf->b_s.b_keywtab);
2083 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084#endif
2085
2086 buf->b_fname = buf->b_sfname;
2087#ifdef UNIX
2088 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002089 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 else
2091 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002092 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 buf->b_dev = st.st_dev;
2094 buf->b_ino = st.st_ino;
2095 }
2096#endif
2097 buf->b_u_synced = TRUE;
2098 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002099 if (flags & BLN_DUMMY)
2100 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 buf_clear_file(buf);
2102 clrallmarks(buf); /* clear marks */
2103 fmarks_check_names(buf); /* check file marks for this file */
2104 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 if (!(flags & BLN_DUMMY))
2106 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002107 bufref_T bufref;
2108
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01002109 /* Tricky: these autocommands may change the buffer list. They could
2110 * also split the window with re-using the one empty buffer. This may
2111 * result in unexpectedly losing the empty buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002112 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002113 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002114 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002115 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002117 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002118 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002119 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002120 return NULL;
2121 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002122#ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002123 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127
2128 return buf;
2129}
2130
2131/*
2132 * Free the memory for the options of a buffer.
2133 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2134 * 'fileencoding'.
2135 */
2136 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002137free_buf_options(
2138 buf_T *buf,
2139 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140{
2141 if (free_p_ff)
2142 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 clear_string_option(&buf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 clear_string_option(&buf->b_p_bh);
2146 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 }
2148#ifdef FEAT_FIND_ID
2149 clear_string_option(&buf->b_p_def);
2150 clear_string_option(&buf->b_p_inc);
2151# ifdef FEAT_EVAL
2152 clear_string_option(&buf->b_p_inex);
2153# endif
2154#endif
2155#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
2156 clear_string_option(&buf->b_p_inde);
2157 clear_string_option(&buf->b_p_indk);
2158#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002159#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2160 clear_string_option(&buf->b_p_bexpr);
2161#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002162#if defined(FEAT_CRYPT)
2163 clear_string_option(&buf->b_p_cm);
2164#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002165 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002166#if defined(FEAT_EVAL)
2167 clear_string_option(&buf->b_p_fex);
2168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169#ifdef FEAT_CRYPT
2170 clear_string_option(&buf->b_p_key);
2171#endif
2172 clear_string_option(&buf->b_p_kp);
2173 clear_string_option(&buf->b_p_mps);
2174 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002175 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 clear_string_option(&buf->b_p_isk);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002177#ifdef FEAT_VARTABS
2178 clear_string_option(&buf->b_p_vsts);
Bram Moolenaarbdace832019-03-02 10:13:42 +01002179 vim_free(buf->b_p_vsts_nopaste);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002180 buf->b_p_vsts_nopaste = NULL;
Bram Moolenaarbdace832019-03-02 10:13:42 +01002181 vim_free(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002182 buf->b_p_vsts_array = NULL;
2183 clear_string_option(&buf->b_p_vts);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002184 VIM_CLEAR(buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002185#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186#ifdef FEAT_KEYMAP
2187 clear_string_option(&buf->b_p_keymap);
Bram Moolenaar50138322018-01-28 17:05:16 +01002188 keymap_clear(&buf->b_kmap_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 ga_clear(&buf->b_kmap_ga);
2190#endif
2191#ifdef FEAT_COMMENTS
2192 clear_string_option(&buf->b_p_com);
2193#endif
2194#ifdef FEAT_FOLDING
2195 clear_string_option(&buf->b_p_cms);
2196#endif
2197 clear_string_option(&buf->b_p_nf);
2198#ifdef FEAT_SYN_HL
2199 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002200 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002201#endif
2202#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002203 clear_string_option(&buf->b_s.b_p_spc);
2204 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002205 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002206 buf->b_s.b_cap_prog = NULL;
2207 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208#endif
2209#ifdef FEAT_SEARCHPATH
2210 clear_string_option(&buf->b_p_sua);
2211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 clear_string_option(&buf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213#ifdef FEAT_CINDENT
2214 clear_string_option(&buf->b_p_cink);
2215 clear_string_option(&buf->b_p_cino);
2216#endif
2217#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
2218 clear_string_option(&buf->b_p_cinw);
2219#endif
2220#ifdef FEAT_INS_EXPAND
2221 clear_string_option(&buf->b_p_cpt);
2222#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002223#ifdef FEAT_COMPL_FUNC
2224 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002225 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227#ifdef FEAT_QUICKFIX
2228 clear_string_option(&buf->b_p_gp);
2229 clear_string_option(&buf->b_p_mp);
2230 clear_string_option(&buf->b_p_efm);
2231#endif
2232 clear_string_option(&buf->b_p_ep);
2233 clear_string_option(&buf->b_p_path);
2234 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002235 clear_string_option(&buf->b_p_tc);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002236#ifdef FEAT_EVAL
2237 clear_string_option(&buf->b_p_tfu);
2238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239#ifdef FEAT_INS_EXPAND
2240 clear_string_option(&buf->b_p_dict);
2241 clear_string_option(&buf->b_p_tsr);
2242#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002243#ifdef FEAT_TEXTOBJ
2244 clear_string_option(&buf->b_p_qe);
2245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002247 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002248#ifdef FEAT_LISP
2249 clear_string_option(&buf->b_p_lw);
2250#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002251 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002252 clear_string_option(&buf->b_p_menc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253}
2254
2255/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002256 * Get alternate file "n".
2257 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2258 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 * if (options & GETF_SETMARK) call setpcmark()
2260 * if (options & GETF_ALT) we are jumping to an alternate file.
2261 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2262 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002263 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 */
2265 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002266buflist_getfile(
2267 int n,
2268 linenr_T lnum,
2269 int options,
2270 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271{
2272 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 pos_T *fpos;
2275 colnr_T col;
2276
2277 buf = buflist_findnr(n);
2278 if (buf == NULL)
2279 {
2280 if ((options & GETF_ALT) && n == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002281 emsg(_(e_noalt));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 else
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01002283 semsg(_("E92: Buffer %d not found"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 return FAIL;
2285 }
2286
2287 /* if alternate file is the current buffer, nothing to do */
2288 if (buf == curbuf)
2289 return OK;
2290
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002291 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002292 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002293 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002295 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002296 if (curbuf_locked())
2297 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298
2299 /* altfpos may be changed by getfile(), get it now */
2300 if (lnum == 0)
2301 {
2302 fpos = buflist_findfpos(buf);
2303 lnum = fpos->lnum;
2304 col = fpos->col;
2305 }
2306 else
2307 col = 0;
2308
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 if (options & GETF_SWITCH)
2310 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002311 /* If 'switchbuf' contains "useopen": jump to first window containing
2312 * "buf" if one exists */
2313 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002315
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002316 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00002317 * page containing "buf" if one exists */
2318 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002319 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002320
2321 /* If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2322 * current buffer isn't empty: open new tab or window */
2323 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002324 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002326 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002327 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002328 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2329 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002331 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 }
2333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334
2335 ++RedrawingDisabled;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002336 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2337 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 {
2339 --RedrawingDisabled;
2340
2341 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
2342 if (!p_sol && col != 0)
2343 {
2344 curwin->w_cursor.col = col;
2345 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 curwin->w_set_curswant = TRUE;
2348 }
2349 return OK;
2350 }
2351 --RedrawingDisabled;
2352 return FAIL;
2353}
2354
2355/*
2356 * go to the last know line number for the current buffer
2357 */
2358 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002359buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360{
2361 pos_T *fpos;
2362
2363 fpos = buflist_findfpos(curbuf);
2364
2365 curwin->w_cursor.lnum = fpos->lnum;
2366 check_cursor_lnum();
2367
2368 if (p_sol)
2369 curwin->w_cursor.col = 0;
2370 else
2371 {
2372 curwin->w_cursor.col = fpos->col;
2373 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 curwin->w_set_curswant = TRUE;
2376 }
2377}
2378
Bram Moolenaar81695252004-12-29 20:58:21 +00002379#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2380/*
2381 * Find file in buffer list by name (it has to be for the current window).
2382 * Returns NULL if not found.
2383 */
2384 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002385buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002386{
2387 char_u *ffname;
2388 buf_T *buf = NULL;
2389
2390 /* First make the name into a full path name */
2391 ffname = FullName_save(fname,
2392#ifdef UNIX
2393 TRUE /* force expansion, get rid of symbolic links */
2394#else
2395 FALSE
2396#endif
2397 );
2398 if (ffname != NULL)
2399 {
2400 buf = buflist_findname(ffname);
2401 vim_free(ffname);
2402 }
2403 return buf;
2404}
2405#endif
2406
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407/*
2408 * Find file in buffer list by name (it has to be for the current window).
2409 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002410 * Skips dummy buffers.
2411 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 */
2413 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002414buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415{
2416#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002417 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418
2419 if (mch_stat((char *)ffname, &st) < 0)
2420 st.st_dev = (dev_T)-1;
2421 return buflist_findname_stat(ffname, &st);
2422}
2423
2424/*
2425 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2426 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002427 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 */
2429 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002430buflist_findname_stat(
2431 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002432 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433{
2434#endif
2435 buf_T *buf;
2436
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002437 /* Start at the last buffer, expect to find a match sooner. */
2438 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar81695252004-12-29 20:58:21 +00002439 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440#ifdef UNIX
2441 , stp
2442#endif
2443 ))
2444 return buf;
2445 return NULL;
2446}
2447
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448/*
2449 * Find file in buffer list by a regexp pattern.
2450 * Return fnum of the found buffer.
2451 * Return < 0 for error.
2452 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002454buflist_findpat(
2455 char_u *pattern,
2456 char_u *pattern_end, /* pointer to first char after pattern */
2457 int unlisted, /* find unlisted buffers */
2458 int diffmode UNUSED, /* find diff-mode buffers only */
2459 int curtab_only) /* find buffers in current tab only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460{
2461 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 int match = -1;
2463 int find_listed;
2464 char_u *pat;
2465 char_u *patend;
2466 int attempt;
2467 char_u *p;
2468 int toggledollar;
2469
2470 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2471 {
2472 if (*pattern == '%')
2473 match = curbuf->b_fnum;
2474 else
2475 match = curwin->w_alt_fnum;
2476#ifdef FEAT_DIFF
2477 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2478 match = -1;
2479#endif
2480 }
2481
2482 /*
2483 * Try four ways of matching a listed buffer:
2484 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002485 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 * attempt == 2: with '$' at end (only match at end)
2487 * attempt == 3: with '^' at start and '$' at end (only full match)
2488 * Repeat this for finding an unlisted buffer if there was no matching
2489 * listed buffer.
2490 */
2491 else
2492 {
2493 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2494 if (pat == NULL)
2495 return -1;
2496 patend = pat + STRLEN(pat) - 1;
2497 toggledollar = (patend > pat && *patend == '$');
2498
2499 /* First try finding a listed buffer. If not found and "unlisted"
2500 * is TRUE, try finding an unlisted buffer. */
2501 find_listed = TRUE;
2502 for (;;)
2503 {
2504 for (attempt = 0; attempt <= 3; ++attempt)
2505 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002506 regmatch_T regmatch;
2507
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 /* may add '^' and '$' */
2509 if (toggledollar)
2510 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2511 p = pat;
2512 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2513 ++p;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002514 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2515 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 {
2517 vim_free(pat);
2518 return -1;
2519 }
2520
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002521 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 if (buf->b_p_bl == find_listed
2523#ifdef FEAT_DIFF
2524 && (!diffmode || diff_mode_buf(buf))
2525#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002526 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002528 if (curtab_only)
2529 {
2530 /* Ignore the match if the buffer is not open in
2531 * the current tab. */
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002532 win_T *wp;
2533
Bram Moolenaar29323592016-07-24 22:04:11 +02002534 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002535 if (wp->w_buffer == buf)
2536 break;
2537 if (wp == NULL)
2538 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 if (match >= 0) /* already found a match */
2541 {
2542 match = -2;
2543 break;
2544 }
2545 match = buf->b_fnum; /* remember first match */
2546 }
2547
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002548 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 if (match >= 0) /* found one match */
2550 break;
2551 }
2552
2553 /* Only search for unlisted buffers if there was no match with
2554 * a listed buffer. */
2555 if (!unlisted || !find_listed || match != -1)
2556 break;
2557 find_listed = FALSE;
2558 }
2559
2560 vim_free(pat);
2561 }
2562
2563 if (match == -2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002564 semsg(_("E93: More than one match for %s"), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 else if (match < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002566 semsg(_("E94: No matching buffer for %s"), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 return match;
2568}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569
2570#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2571
2572/*
2573 * Find all buffer names that match.
2574 * For command line expansion of ":buf" and ":sbuf".
2575 * Return OK if matches found, FAIL otherwise.
2576 */
2577 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002578ExpandBufnames(
2579 char_u *pat,
2580 int *num_file,
2581 char_u ***file,
2582 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583{
2584 int count = 0;
2585 buf_T *buf;
2586 int round;
2587 char_u *p;
2588 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002589 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590
2591 *num_file = 0; /* return values in case of FAIL */
2592 *file = NULL;
2593
Bram Moolenaar05159a02005-02-26 23:04:13 +00002594 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2595 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002597 patc = alloc(STRLEN(pat) + 11);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002598 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002600 STRCPY(patc, "\\(^\\|[\\/]\\)");
2601 STRCPY(patc + 11, pat + 1);
2602 }
2603 else
2604 patc = pat;
2605
2606 /*
2607 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002608 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002609 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002610 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002611 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002612 regmatch_T regmatch;
2613
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002614 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002615 break; /* there was no anchor, no need to try again */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002616 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2617 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002618 {
2619 if (patc != pat)
2620 vim_free(patc);
2621 return FAIL;
2622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623
2624 /*
2625 * round == 1: Count the matches.
2626 * round == 2: Build the array to keep the matches.
2627 */
2628 for (round = 1; round <= 2; ++round)
2629 {
2630 count = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002631 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 {
2633 if (!buf->b_p_bl) /* skip unlisted buffers */
2634 continue;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002635 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 if (p != NULL)
2637 {
2638 if (round == 1)
2639 ++count;
2640 else
2641 {
2642 if (options & WILD_HOME_REPLACE)
2643 p = home_replace_save(buf, p);
2644 else
2645 p = vim_strsave(p);
2646 (*file)[count++] = p;
2647 }
2648 }
2649 }
2650 if (count == 0) /* no match found, break here */
2651 break;
2652 if (round == 1)
2653 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002654 *file = ALLOC_MULT(char_u *, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 if (*file == NULL)
2656 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002657 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002658 if (patc != pat)
2659 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 return FAIL;
2661 }
2662 }
2663 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002664 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 if (count) /* match(es) found, break here */
2666 break;
2667 }
2668
Bram Moolenaar05159a02005-02-26 23:04:13 +00002669 if (patc != pat)
2670 vim_free(patc);
2671
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 *num_file = count;
2673 return (count == 0 ? FAIL : OK);
2674}
2675
2676#endif /* FEAT_CMDL_COMPL */
2677
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678/*
2679 * Check for a match on the file name for buffer "buf" with regprog "prog".
2680 */
2681 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002682buflist_match(
2683 regmatch_T *rmp,
2684 buf_T *buf,
2685 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686{
2687 char_u *match;
2688
2689 /* First try the short file name, then the long file name. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002690 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002692 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693
2694 return match;
2695}
2696
2697/*
2698 * Try matching the regexp in "prog" with file name "name".
2699 * Return "name" when there is a match, NULL when not.
2700 */
2701 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002702fname_match(
2703 regmatch_T *rmp,
2704 char_u *name,
2705 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706{
2707 char_u *match = NULL;
2708 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709
2710 if (name != NULL)
2711 {
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002712 /* Ignore case when 'fileignorecase' or the argument is set. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002713 rmp->rm_ic = p_fic || ignore_case;
2714 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 match = name;
2716 else
2717 {
2718 /* Replace $(HOME) with '~' and try matching again. */
2719 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002720 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 match = name;
2722 vim_free(p);
2723 }
2724 }
2725
2726 return match;
2727}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728
2729/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02002730 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 */
2732 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002733buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734{
Bram Moolenaar480778b2016-07-14 22:09:39 +02002735 char_u key[VIM_SIZEOF_INT * 2 + 1];
2736 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737
2738 if (nr == 0)
2739 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02002740 sprintf((char *)key, "%x", nr);
2741 hi = hash_find(&buf_hashtab, key);
2742
2743 if (!HASHITEM_EMPTY(hi))
2744 return (buf_T *)(hi->hi_key
2745 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 return NULL;
2747}
2748
2749/*
2750 * Get name of file 'n' in the buffer list.
2751 * When the file has no name an empty string is returned.
2752 * home_replace() is used to shorten the file name (used for marks).
2753 * Returns a pointer to allocated memory, of NULL when failed.
2754 */
2755 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002756buflist_nr2name(
2757 int n,
2758 int fullname,
2759 int helptail) /* for help buffers return tail only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760{
2761 buf_T *buf;
2762
2763 buf = buflist_findnr(n);
2764 if (buf == NULL)
2765 return NULL;
2766 return home_replace_save(helptail ? buf : NULL,
2767 fullname ? buf->b_ffname : buf->b_fname);
2768}
2769
2770/*
2771 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2772 * When "copy_options" is TRUE save the local window option values.
2773 * When "lnum" is 0 only do the options.
2774 */
Bram Moolenaardefa0672019-07-21 19:25:37 +02002775 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002776buflist_setfpos(
2777 buf_T *buf,
2778 win_T *win,
2779 linenr_T lnum,
2780 colnr_T col,
2781 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782{
2783 wininfo_T *wip;
2784
2785 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2786 if (wip->wi_win == win)
2787 break;
2788 if (wip == NULL)
2789 {
2790 /* allocate a new entry */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002791 wip = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 if (wip == NULL)
2793 return;
2794 wip->wi_win = win;
2795 if (lnum == 0) /* set lnum even when it's 0 */
2796 lnum = 1;
2797 }
2798 else
2799 {
2800 /* remove the entry from the list */
2801 if (wip->wi_prev)
2802 wip->wi_prev->wi_next = wip->wi_next;
2803 else
2804 buf->b_wininfo = wip->wi_next;
2805 if (wip->wi_next)
2806 wip->wi_next->wi_prev = wip->wi_prev;
2807 if (copy_options && wip->wi_optset)
2808 {
2809 clear_winopt(&wip->wi_opt);
2810#ifdef FEAT_FOLDING
2811 deleteFoldRecurse(&wip->wi_folds);
2812#endif
2813 }
2814 }
2815 if (lnum != 0)
2816 {
2817 wip->wi_fpos.lnum = lnum;
2818 wip->wi_fpos.col = col;
2819 }
2820 if (copy_options)
2821 {
2822 /* Save the window-specific option values. */
2823 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2824#ifdef FEAT_FOLDING
2825 wip->wi_fold_manual = win->w_fold_manual;
2826 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2827#endif
2828 wip->wi_optset = TRUE;
2829 }
2830
2831 /* insert the entry in front of the list */
2832 wip->wi_next = buf->b_wininfo;
2833 buf->b_wininfo = wip;
2834 wip->wi_prev = NULL;
2835 if (wip->wi_next)
2836 wip->wi_next->wi_prev = wip;
2837
2838 return;
2839}
2840
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002841#ifdef FEAT_DIFF
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002842/*
2843 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2844 * page. That's because a diff is local to a tab page.
2845 */
2846 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002847wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002848{
2849 win_T *wp;
2850
2851 if (wip->wi_opt.wo_diff)
2852 {
Bram Moolenaar29323592016-07-24 22:04:11 +02002853 FOR_ALL_WINDOWS(wp)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002854 /* return FALSE when it's a window in the current tab page, thus
2855 * the buffer was in diff mode here */
2856 if (wip->wi_win == wp)
2857 return FALSE;
2858 return TRUE;
2859 }
2860 return FALSE;
2861}
2862#endif
2863
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864/*
2865 * Find info for the current window in buffer "buf".
2866 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002867 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2868 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 * Returns NULL when there isn't any info.
2870 */
2871 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002872find_wininfo(
2873 buf_T *buf,
2874 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875{
2876 wininfo_T *wip;
2877
2878 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002879 if (wip->wi_win == curwin
2880#ifdef FEAT_DIFF
2881 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2882#endif
2883 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002885
2886 /* If no wininfo for curwin, use the first in the list (that doesn't have
2887 * 'diff' set and is in another tab page). */
2888 if (wip == NULL)
2889 {
2890#ifdef FEAT_DIFF
2891 if (skip_diff_buffer)
2892 {
2893 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2894 if (!wininfo_other_tab_diff(wip))
2895 break;
2896 }
2897 else
2898#endif
2899 wip = buf->b_wininfo;
2900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 return wip;
2902}
2903
2904/*
2905 * Reset the local window options to the values last used in this window.
2906 * If the buffer wasn't used in this window before, use the values from
2907 * the most recently used window. If the values were never set, use the
2908 * global values for the window.
2909 */
2910 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002911get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912{
2913 wininfo_T *wip;
2914
2915 clear_winopt(&curwin->w_onebuf_opt);
2916#ifdef FEAT_FOLDING
2917 clearFolding(curwin);
2918#endif
2919
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002920 wip = find_wininfo(buf, TRUE);
Bram Moolenaar25782a72018-05-13 18:05:33 +02002921 if (wip != NULL && wip->wi_win != NULL
2922 && wip->wi_win != curwin && wip->wi_win->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 {
Bram Moolenaar25782a72018-05-13 18:05:33 +02002924 /* The buffer is currently displayed in the window: use the actual
2925 * option values instead of the saved (possibly outdated) values. */
2926 win_T *wp = wip->wi_win;
2927
2928 copy_winopt(&wp->w_onebuf_opt, &curwin->w_onebuf_opt);
2929#ifdef FEAT_FOLDING
2930 curwin->w_fold_manual = wp->w_fold_manual;
2931 curwin->w_foldinvalid = TRUE;
2932 cloneFoldGrowArray(&wp->w_folds, &curwin->w_folds);
2933#endif
2934 }
2935 else if (wip != NULL && wip->wi_optset)
2936 {
2937 /* the buffer was displayed in the current window earlier */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2939#ifdef FEAT_FOLDING
2940 curwin->w_fold_manual = wip->wi_fold_manual;
2941 curwin->w_foldinvalid = TRUE;
2942 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2943#endif
2944 }
2945 else
2946 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2947
2948#ifdef FEAT_FOLDING
2949 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2950 if (p_fdls >= 0)
2951 curwin->w_p_fdl = p_fdls;
2952#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002953#ifdef FEAT_SYN_HL
2954 check_colorcolumn(curwin);
2955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956}
2957
2958/*
2959 * Find the position (lnum and col) for the buffer 'buf' for the current
2960 * window.
2961 * Returns a pointer to no_position if no position is found.
2962 */
2963 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002964buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965{
2966 wininfo_T *wip;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002967 static pos_T no_position = {1, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002969 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 if (wip != NULL)
2971 return &(wip->wi_fpos);
2972 else
2973 return &no_position;
2974}
2975
2976/*
2977 * Find the lnum for the buffer 'buf' for the current window.
2978 */
2979 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01002980buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981{
2982 return buflist_findfpos(buf)->lnum;
2983}
2984
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002986 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002989buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990{
2991 buf_T *buf;
2992 int len;
2993 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02002994 int ro_char;
2995 int changed_char;
Bram Moolenaar0751f512018-03-29 16:37:16 +02002996#ifdef FEAT_TERMINAL
2997 int job_running;
2998 int job_none_open;
2999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000
3001 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
3002 {
Bram Moolenaar0751f512018-03-29 16:37:16 +02003003#ifdef FEAT_TERMINAL
3004 job_running = term_job_running(buf->b_term);
3005 job_none_open = job_running && term_none_open(buf->b_term);
3006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 /* skip unlisted buffers, unless ! was used */
Bram Moolenaard51cb702015-07-21 15:03:06 +02003008 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
3009 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
3010 || (vim_strchr(eap->arg, '+')
3011 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
3012 || (vim_strchr(eap->arg, 'a')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003013 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
Bram Moolenaard51cb702015-07-21 15:03:06 +02003014 || (vim_strchr(eap->arg, 'h')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003015 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3016#ifdef FEAT_TERMINAL
3017 || (vim_strchr(eap->arg, 'R')
3018 && (!job_running || (job_running && job_none_open)))
3019 || (vim_strchr(eap->arg, '?')
3020 && (!job_running || (job_running && !job_none_open)))
3021 || (vim_strchr(eap->arg, 'F')
3022 && (job_running || buf->b_term == NULL))
3023#endif
Bram Moolenaard51cb702015-07-21 15:03:06 +02003024 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3025 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3026 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3027 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3028 || (vim_strchr(eap->arg, '#')
3029 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003032 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 else
3034 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003035 if (message_filtered(NameBuff))
3036 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003038 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3039 : (bufIsChanged(buf) ? '+' : ' ');
3040#ifdef FEAT_TERMINAL
3041 if (term_job_running(buf->b_term))
3042 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02003043 if (term_none_open(buf->b_term))
3044 ro_char = '?';
3045 else
3046 ro_char = 'R';
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003047 changed_char = ' '; /* bufIsChanged() returns TRUE to avoid
3048 * closing, but it's not actually changed. */
3049 }
3050 else if (buf->b_term != NULL)
3051 ro_char = 'F';
3052 else
3053#endif
3054 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3055
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003056 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00003057 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 buf->b_fnum,
3059 buf->b_p_bl ? ' ' : 'u',
3060 buf == curbuf ? '%' :
3061 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3062 buf->b_ml.ml_mfp == NULL ? ' ' :
3063 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003064 ro_char,
3065 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003066 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003067 if (len > IOSIZE - 20)
3068 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069
3070 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 i = 40 - vim_strsize(IObuff);
3072 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 IObuff[len++] = ' ';
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003074 while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003075 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3076 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003077 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 msg_outtrans(IObuff);
3079 out_flush(); /* output one line at a time */
3080 ui_breakcheck();
3081 }
3082}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083
3084/*
3085 * Get file name and line number for file 'fnum'.
3086 * Used by DoOneCmd() for translating '%' and '#'.
3087 * Used by insert_reg() and cmdline_paste() for '#' register.
3088 * Return FAIL if not found, OK for success.
3089 */
3090 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003091buflist_name_nr(
3092 int fnum,
3093 char_u **fname,
3094 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095{
3096 buf_T *buf;
3097
3098 buf = buflist_findnr(fnum);
3099 if (buf == NULL || buf->b_fname == NULL)
3100 return FAIL;
3101
3102 *fname = buf->b_fname;
3103 *lnum = buflist_findlnum(buf);
3104
3105 return OK;
3106}
3107
3108/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003109 * Set the file name for "buf"' to "ffname_arg", short file name to
3110 * "sfname_arg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 * The file name with the full path is also remembered, for when :cd is used.
3112 * Returns FAIL for failure (file name already in use by other buffer)
3113 * OK otherwise.
3114 */
3115 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003116setfname(
3117 buf_T *buf,
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003118 char_u *ffname_arg,
3119 char_u *sfname_arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +01003120 int message) /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003122 char_u *ffname = ffname_arg;
3123 char_u *sfname = sfname_arg;
Bram Moolenaar81695252004-12-29 20:58:21 +00003124 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003126 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127#endif
3128
3129 if (ffname == NULL || *ffname == NUL)
3130 {
3131 /* Removing the name. */
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003132 if (buf->b_sfname != buf->b_ffname)
3133 VIM_CLEAR(buf->b_sfname);
3134 else
3135 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003136 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137#ifdef UNIX
3138 st.st_dev = (dev_T)-1;
3139#endif
3140 }
3141 else
3142 {
3143 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
3144 if (ffname == NULL) /* out of memory */
3145 return FAIL;
3146
3147 /*
3148 * if the file name is already used in another buffer:
3149 * - if the buffer is loaded, fail
3150 * - if the buffer is not loaded, delete it from the list
3151 */
3152#ifdef UNIX
3153 if (mch_stat((char *)ffname, &st) < 0)
3154 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003155#endif
3156 if (!(buf->b_flags & BF_DUMMY))
3157#ifdef UNIX
3158 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003160 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161#endif
3162 if (obuf != NULL && obuf != buf)
3163 {
3164 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
3165 {
3166 if (message)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003167 emsg(_("E95: Buffer with this name already exists"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 vim_free(ffname);
3169 return FAIL;
3170 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01003171 /* delete from the list */
3172 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 }
3174 sfname = vim_strsave(sfname);
3175 if (ffname == NULL || sfname == NULL)
3176 {
3177 vim_free(sfname);
3178 vim_free(ffname);
3179 return FAIL;
3180 }
3181#ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003182 fname_case(sfname, 0); /* set correct case for short file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183#endif
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003184 if (buf->b_sfname != buf->b_ffname)
3185 vim_free(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 vim_free(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 buf->b_ffname = ffname;
3188 buf->b_sfname = sfname;
3189 }
3190 buf->b_fname = buf->b_sfname;
3191#ifdef UNIX
3192 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003193 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 else
3195 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003196 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 buf->b_dev = st.st_dev;
3198 buf->b_ino = st.st_ino;
3199 }
3200#endif
3201
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203
3204 buf_name_changed(buf);
3205 return OK;
3206}
3207
3208/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003209 * Crude way of changing the name of a buffer. Use with care!
3210 * The name should be relative to the current directory.
3211 */
3212 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003213buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003214{
3215 buf_T *buf;
3216
3217 buf = buflist_findnr(fnum);
3218 if (buf != NULL)
3219 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003220 if (buf->b_sfname != buf->b_ffname)
3221 vim_free(buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003222 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003223 buf->b_ffname = vim_strsave(name);
3224 buf->b_sfname = NULL;
3225 /* Allocate ffname and expand into full path. Also resolves .lnk
3226 * files on Win32. */
3227 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003228 buf->b_fname = buf->b_sfname;
3229 }
3230}
3231
3232/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 * Take care of what needs to be done when the name of buffer "buf" has
3234 * changed.
3235 */
3236 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003237buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238{
3239 /*
3240 * If the file name changed, also change the name of the swapfile
3241 */
3242 if (buf->b_ml.ml_mfp != NULL)
3243 ml_setname(buf);
3244
3245 if (curwin->w_buffer == buf)
3246 check_arg_idx(curwin); /* check file name for arg list */
3247#ifdef FEAT_TITLE
3248 maketitle(); /* set window title */
3249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 status_redraw_all(); /* status lines need to be redrawn */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 fmarks_check_names(buf); /* check named file marks */
3252 ml_timestamp(buf); /* reset timestamp */
3253}
3254
3255/*
3256 * set alternate file name for current window
3257 *
3258 * Used by do_one_cmd(), do_write() and do_ecmd().
3259 * Return the buffer.
3260 */
3261 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003262setaltfname(
3263 char_u *ffname,
3264 char_u *sfname,
3265 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266{
3267 buf_T *buf;
3268
3269 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
3270 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003271 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 curwin->w_alt_fnum = buf->b_fnum;
3273 return buf;
3274}
3275
3276/*
3277 * Get alternate file name for current window.
3278 * Return NULL if there isn't any, and give error message if requested.
3279 */
3280 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003281getaltfname(
3282 int errmsg) /* give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283{
3284 char_u *fname;
3285 linenr_T dummy;
3286
3287 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3288 {
3289 if (errmsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003290 emsg(_(e_noalt));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 return NULL;
3292 }
3293 return fname;
3294}
3295
3296/*
3297 * Add a file name to the buflist and return its number.
3298 * Uses same flags as buflist_new(), except BLN_DUMMY.
3299 *
3300 * used by qf_init(), main() and doarglist()
3301 */
3302 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003303buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304{
3305 buf_T *buf;
3306
3307 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3308 if (buf != NULL)
3309 return buf->b_fnum;
3310 return 0;
3311}
3312
3313#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3314/*
3315 * Adjust slashes in file names. Called after 'shellslash' was set.
3316 */
3317 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003318buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319{
3320 buf_T *bp;
3321
Bram Moolenaar29323592016-07-24 22:04:11 +02003322 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 {
3324 if (bp->b_ffname != NULL)
3325 slash_adjust(bp->b_ffname);
3326 if (bp->b_sfname != NULL)
3327 slash_adjust(bp->b_sfname);
3328 }
3329}
3330#endif
3331
3332/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003333 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 * Also save the local window option values.
3335 */
3336 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003337buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003339 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340}
3341
3342/*
3343 * Return TRUE if 'ffname' is not the same file as current file.
3344 * Fname must have a full path (expanded by mch_FullName()).
3345 */
3346 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003347otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348{
3349 return otherfile_buf(curbuf, ffname
3350#ifdef UNIX
3351 , NULL
3352#endif
3353 );
3354}
3355
3356 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003357otherfile_buf(
3358 buf_T *buf,
3359 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003361 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003363 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364{
3365 /* no name is different */
3366 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3367 return TRUE;
3368 if (fnamecmp(ffname, buf->b_ffname) == 0)
3369 return FALSE;
3370#ifdef UNIX
3371 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003372 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373
Bram Moolenaar8767f522016-07-01 17:17:39 +02003374 /* If no stat_T given, get it now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 if (stp == NULL)
3376 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003377 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 st.st_dev = (dev_T)-1;
3379 stp = &st;
3380 }
3381 /* Use dev/ino to check if the files are the same, even when the names
3382 * are different (possible with links). Still need to compare the
3383 * name above, for when the file doesn't exist yet.
3384 * Problem: The dev/ino changes when a file is deleted (and created
3385 * again) and remains the same when renamed/moved. We don't want to
3386 * mch_stat() each buffer each time, that would be too slow. Get the
3387 * dev/ino again when they appear to match, but not when they appear
3388 * to be different: Could skip a buffer when it's actually the same
3389 * file. */
3390 if (buf_same_ino(buf, stp))
3391 {
3392 buf_setino(buf);
3393 if (buf_same_ino(buf, stp))
3394 return FALSE;
3395 }
3396 }
3397#endif
3398 return TRUE;
3399}
3400
3401#if defined(UNIX) || defined(PROTO)
3402/*
3403 * Set inode and device number for a buffer.
3404 * Must always be called when b_fname is changed!.
3405 */
3406 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003407buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003409 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410
3411 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3412 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003413 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 buf->b_dev = st.st_dev;
3415 buf->b_ino = st.st_ino;
3416 }
3417 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003418 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419}
3420
3421/*
3422 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3423 */
3424 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003425buf_same_ino(
3426 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003427 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003429 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 && stp->st_dev == buf->b_dev
3431 && stp->st_ino == buf->b_ino);
3432}
3433#endif
3434
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003435/*
3436 * Print info about the current buffer.
3437 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003439fileinfo(
3440 int fullname, /* when non-zero print full path */
3441 int shorthelp,
3442 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443{
3444 char_u *name;
3445 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003446 char *p;
3447 char *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003448 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003450 buffer = alloc(IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 if (buffer == NULL)
3452 return;
3453
3454 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3455 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003456 vim_snprintf(buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 p = buffer + STRLEN(buffer);
3458 }
3459 else
3460 p = buffer;
3461
3462 *p++ = '"';
3463 if (buf_spname(curbuf) != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003464 vim_strncpy((char_u *)p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 else
3466 {
3467 if (!fullname && curbuf->b_fname != NULL)
3468 name = curbuf->b_fname;
3469 else
3470 name = curbuf->b_ffname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003471 home_replace(shorthelp ? curbuf : NULL, name, (char_u *)p,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 (int)(IOSIZE - (p - buffer)), TRUE);
3473 }
3474
Bram Moolenaar32526b32019-01-19 17:43:09 +01003475 vim_snprintf_add(buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 curbufIsChanged() ? (shortmess(SHM_MOD)
3477 ? " [+]" : _(" [Modified]")) : " ",
3478 (curbuf->b_flags & BF_NOTEDITED)
3479#ifdef FEAT_QUICKFIX
3480 && !bt_dontwrite(curbuf)
3481#endif
3482 ? _("[Not edited]") : "",
3483 (curbuf->b_flags & BF_NEW)
3484#ifdef FEAT_QUICKFIX
3485 && !bt_dontwrite(curbuf)
3486#endif
3487 ? _("[New file]") : "",
3488 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003489 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 : _("[readonly]")) : "",
3491 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3492 || curbuf->b_p_ro) ?
3493 " " : "");
3494 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3495 * causes an overflow, thus for large numbers divide instead. */
3496 if (curwin->w_cursor.lnum > 1000000L)
3497 n = (int)(((long)curwin->w_cursor.lnum) /
3498 ((long)curbuf->b_ml.ml_line_count / 100L));
3499 else
3500 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3501 (long)curbuf->b_ml.ml_line_count);
3502 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003503 vim_snprintf_add(buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504#ifdef FEAT_CMDL_INFO
3505 else if (p_ru)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 /* Current line and column are already on the screen -- webb */
Bram Moolenaar32526b32019-01-19 17:43:09 +01003507 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003508 NGETTEXT("%ld line --%d%%--", "%ld lines --%d%%--",
3509 curbuf->b_ml.ml_line_count),
3510 (long)curbuf->b_ml.ml_line_count, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511#endif
3512 else
3513 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003514 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 _("line %ld of %ld --%d%%-- col "),
3516 (long)curwin->w_cursor.lnum,
3517 (long)curbuf->b_ml.ml_line_count,
3518 n);
3519 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003520 len = STRLEN(buffer);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003521 col_print((char_u *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3523 }
3524
Bram Moolenaar32526b32019-01-19 17:43:09 +01003525 (void)append_arg_number(curwin, (char_u *)buffer, IOSIZE,
3526 !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527
3528 if (dont_truncate)
3529 {
3530 /* Temporarily set msg_scroll to avoid the message being truncated.
3531 * First call msg_start() to get the message in the right place. */
3532 msg_start();
3533 n = msg_scroll;
3534 msg_scroll = TRUE;
3535 msg(buffer);
3536 msg_scroll = n;
3537 }
3538 else
3539 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003540 p = (char *)msg_trunc_attr(buffer, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 /* Need to repeat the message after redrawing when:
3543 * - When restart_edit is set (otherwise there will be a delay
3544 * before redrawing).
3545 * - When the screen was scrolled but there is no wait-return
3546 * prompt. */
Bram Moolenaar32526b32019-01-19 17:43:09 +01003547 set_keep_msg((char_u *)p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 }
3549
3550 vim_free(buffer);
3551}
3552
3553 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003554col_print(
3555 char_u *buf,
3556 size_t buflen,
3557 int col,
3558 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559{
3560 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003561 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003563 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564}
3565
3566#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567static char_u *lasttitle = NULL;
3568static char_u *lasticon = NULL;
3569
Bram Moolenaar84a93082018-06-16 22:58:15 +02003570/*
3571 * Put the file name in the title bar and icon of the window.
3572 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003574maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575{
3576 char_u *p;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003577 char_u *title_str = NULL;
3578 char_u *icon_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 int maxlen = 0;
3580 int len;
3581 int mustset;
3582 char_u buf[IOSIZE];
3583 int off;
3584
3585 if (!redrawing())
3586 {
3587 /* Postpone updating the title when 'lazyredraw' is set. */
3588 need_maketitle = TRUE;
3589 return;
3590 }
3591
3592 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003593 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003594 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595
3596 if (p_title)
3597 {
3598 if (p_titlelen > 0)
3599 {
3600 maxlen = p_titlelen * Columns / 100;
3601 if (maxlen < 10)
3602 maxlen = 10;
3603 }
3604
Bram Moolenaar84a93082018-06-16 22:58:15 +02003605 title_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 if (*p_titlestring != NUL)
3607 {
3608#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003609 if (stl_syntax & STL_IN_TITLE)
3610 {
3611 int use_sandbox = FALSE;
3612 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003613
3614# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003615 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003616# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003617 called_emsg = FALSE;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003618 build_stl_str_hl(curwin, title_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003619 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003620 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003621 if (called_emsg)
3622 set_string_option_direct((char_u *)"titlestring", -1,
3623 (char_u *)"", OPT_FREE, SID_ERROR);
3624 called_emsg |= save_called_emsg;
3625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 else
3627#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003628 title_str = p_titlestring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 }
3630 else
3631 {
3632 /* format: "fname + (path) (1 of 2) - VIM" */
3633
Bram Moolenaar2c666692012-09-05 13:30:40 +02003634#define SPACE_FOR_FNAME (IOSIZE - 100)
3635#define SPACE_FOR_DIR (IOSIZE - 20)
3636#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003638 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar21554412017-07-24 21:44:43 +02003639#ifdef FEAT_TERMINAL
3640 else if (curbuf->b_term != NULL)
3641 {
3642 vim_strncpy(buf, term_get_status_text(curbuf->b_term),
3643 SPACE_FOR_FNAME);
3644 }
3645#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 else
3647 {
3648 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003649 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 }
3652
Bram Moolenaar21554412017-07-24 21:44:43 +02003653#ifdef FEAT_TERMINAL
3654 if (curbuf->b_term == NULL)
3655#endif
3656 switch (bufIsChanged(curbuf)
3657 + (curbuf->b_p_ro * 2)
3658 + (!curbuf->b_p_ma * 4))
3659 {
3660 case 1: STRCAT(buf, " +"); break;
3661 case 2: STRCAT(buf, " ="); break;
3662 case 3: STRCAT(buf, " =+"); break;
3663 case 4:
3664 case 6: STRCAT(buf, " -"); break;
3665 case 5:
3666 case 7: STRCAT(buf, " -+"); break;
3667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668
Bram Moolenaar21554412017-07-24 21:44:43 +02003669 if (curbuf->b_fname != NULL
3670#ifdef FEAT_TERMINAL
3671 && curbuf->b_term == NULL
3672#endif
3673 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 {
3675 /* Get path of file, replace home dir with ~ */
3676 off = (int)STRLEN(buf);
3677 buf[off++] = ' ';
3678 buf[off++] = '(';
3679 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003680 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681#ifdef BACKSLASH_IN_FILENAME
3682 /* avoid "c:/name" to be reduced to "c" */
3683 if (isalpha(buf[off]) && buf[off + 1] == ':')
3684 off += 2;
3685#endif
3686 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003687 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 if (p == buf + off)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003689 {
Bram Moolenaar21554412017-07-24 21:44:43 +02003690 /* must be a help buffer */
3691 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003692 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003696
Bram Moolenaar2c666692012-09-05 13:30:40 +02003697 /* Translate unprintable chars and concatenate. Keep some
3698 * room for the server name. When there is no room (very long
3699 * file name) use (...). */
3700 if (off < SPACE_FOR_DIR)
3701 {
3702 p = transstr(buf + off);
3703 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3704 vim_free(p);
3705 }
3706 else
3707 {
3708 vim_strncpy(buf + off, (char_u *)"...",
3709 (size_t)(SPACE_FOR_ARGNR - off));
3710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 STRCAT(buf, ")");
3712 }
3713
Bram Moolenaar2c666692012-09-05 13:30:40 +02003714 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715
3716#if defined(FEAT_CLIENTSERVER)
3717 if (serverName != NULL)
3718 {
3719 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003720 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 }
3722 else
3723#endif
3724 STRCAT(buf, " - VIM");
3725
3726 if (maxlen > 0)
3727 {
3728 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003729 if (vim_strsize(buf) > maxlen)
3730 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 }
3732 }
3733 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02003734 mustset = value_changed(title_str, &lasttitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735
3736 if (p_icon)
3737 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02003738 icon_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 if (*p_iconstring != NUL)
3740 {
3741#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003742 if (stl_syntax & STL_IN_ICON)
3743 {
3744 int use_sandbox = FALSE;
3745 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003746
3747# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003748 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003749# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003750 called_emsg = FALSE;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003751 build_stl_str_hl(curwin, icon_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003752 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003753 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003754 if (called_emsg)
3755 set_string_option_direct((char_u *)"iconstring", -1,
3756 (char_u *)"", OPT_FREE, SID_ERROR);
3757 called_emsg |= save_called_emsg;
3758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 else
3760#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003761 icon_str = p_iconstring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 }
3763 else
3764 {
3765 if (buf_spname(curbuf) != NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003766 p = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 else /* use file name only in icon */
Bram Moolenaar84a93082018-06-16 22:58:15 +02003768 p = gettail(curbuf->b_ffname);
3769 *icon_str = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 /* Truncate name at 100 bytes. */
Bram Moolenaar84a93082018-06-16 22:58:15 +02003771 len = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 if (len > 100)
3773 {
3774 len -= 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 if (has_mbyte)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003776 len += (*mb_tail_off)(p, p + len) + 1;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003777 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02003779 STRCPY(icon_str, p);
3780 trans_characters(icon_str, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 }
3782 }
3783
Bram Moolenaar84a93082018-06-16 22:58:15 +02003784 mustset |= value_changed(icon_str, &lasticon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785
3786 if (mustset)
3787 resettitle();
3788}
3789
3790/*
3791 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3792 * from "str" if it does.
Bram Moolenaar84a93082018-06-16 22:58:15 +02003793 * Return TRUE if resettitle() is to be called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 */
3795 static int
Bram Moolenaar84a93082018-06-16 22:58:15 +02003796value_changed(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797{
3798 if ((str == NULL) != (*last == NULL)
3799 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3800 {
3801 vim_free(*last);
3802 if (str == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003803 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 *last = NULL;
Bram Moolenaar40385db2018-08-07 22:31:44 +02003805 mch_restore_title(
3806 last == &lasttitle ? SAVE_RESTORE_TITLE : SAVE_RESTORE_ICON);
Bram Moolenaar84a93082018-06-16 22:58:15 +02003807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 else
Bram Moolenaar84a93082018-06-16 22:58:15 +02003809 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 *last = vim_strsave(str);
Bram Moolenaar84a93082018-06-16 22:58:15 +02003811 return TRUE;
3812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 }
3814 return FALSE;
3815}
3816
3817/*
3818 * Put current window title back (used after calling a shell)
3819 */
3820 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003821resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822{
3823 mch_settitle(lasttitle, lasticon);
3824}
Bram Moolenaarea408852005-06-25 22:49:46 +00003825
3826# if defined(EXITFREE) || defined(PROTO)
3827 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003828free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00003829{
3830 vim_free(lasttitle);
3831 vim_free(lasticon);
3832}
3833# endif
3834
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835#endif /* FEAT_TITLE */
3836
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003837#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003839 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 * Return length of string in screen cells.
3841 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003842 * Normally works for window "wp", except when working for 'tabline' then it
3843 * is "curwin".
3844 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 * Items are drawn interspersed with the text that surrounds it
3846 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3847 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3848 *
3849 * If maxwidth is not zero, the string will be filled at any middle marker
3850 * or truncated if too long, fillchar is used for all whitespace.
3851 */
3852 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003853build_stl_str_hl(
3854 win_T *wp,
3855 char_u *out, /* buffer to write into != NameBuff */
3856 size_t outlen, /* length of out[] */
3857 char_u *fmt,
3858 int use_sandbox UNUSED, /* "fmt" was set insecurely, use sandbox */
3859 int fillchar,
3860 int maxwidth,
3861 struct stl_hlrec *hltab, /* return: HL attributes (can be NULL) */
3862 struct stl_hlrec *tabtab) /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863{
Bram Moolenaar10772302019-01-20 18:25:54 +01003864 linenr_T lnum;
3865 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 char_u *p;
3867 char_u *s;
3868 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003869 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870#ifdef FEAT_EVAL
Bram Moolenaarba2929b2017-09-08 13:59:21 +02003871 win_T *save_curwin;
3872 buf_T *save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873#endif
3874 int empty_line;
3875 colnr_T virtcol;
3876 long l;
3877 long n;
3878 int prevchar_isflag;
3879 int prevchar_isitem;
3880 int itemisflag;
3881 int fillable;
3882 char_u *str;
3883 long num;
3884 int width;
3885 int itemcnt;
3886 int curitem;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02003887 int group_end_userhl;
3888 int group_start_userhl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 int groupitem[STL_MAX_ITEM];
3890 int groupdepth;
3891 struct stl_item
3892 {
3893 char_u *start;
3894 int minwid;
3895 int maxwid;
3896 enum
3897 {
3898 Normal,
3899 Empty,
3900 Group,
3901 Middle,
3902 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003903 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 Trunc
3905 } type;
3906 } item[STL_MAX_ITEM];
3907 int minwid;
3908 int maxwid;
3909 int zeropad;
3910 char_u base;
3911 char_u opt;
3912#define TMPLEN 70
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02003913 char_u buf_tmp[TMPLEN];
3914 char_u win_tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003915 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003916 struct stl_hlrec *sp;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02003917 int save_must_redraw = must_redraw;
3918 int save_redr_type = curwin->w_redr_type;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003919
3920#ifdef FEAT_EVAL
3921 /*
3922 * When the format starts with "%!" then evaluate it as an expression and
3923 * use the result as the actual format string.
3924 */
3925 if (fmt[0] == '%' && fmt[1] == '!')
3926 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02003927 typval_T tv;
3928
3929 tv.v_type = VAR_NUMBER;
3930 tv.vval.v_number = wp->w_id;
3931 set_var((char_u *)"g:statusline_winid", &tv, FALSE);
3932
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003933 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3934 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003935 usefmt = fmt;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02003936
3937 do_unlet((char_u *)"g:statusline_winid", TRUE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003938 }
3939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940
3941 if (fillchar == 0)
3942 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003943 /* Can't handle a multi-byte fill character yet. */
3944 else if (mb_char2len(fillchar) > 1)
3945 fillchar = '-';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003946
Bram Moolenaar10772302019-01-20 18:25:54 +01003947 // The cursor in windows other than the current one isn't always
3948 // up-to-date, esp. because of autocommands and timers.
3949 lnum = wp->w_cursor.lnum;
3950 if (lnum > wp->w_buffer->b_ml.ml_line_count)
3951 {
3952 lnum = wp->w_buffer->b_ml.ml_line_count;
3953 wp->w_cursor.lnum = lnum;
3954 }
3955
3956 // Get line & check if empty (cursorpos will show "0-1"). Note that
3957 // p will become invalid when getting another buffer line.
3958 p = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar567199b2013-04-24 16:52:36 +02003959 empty_line = (*p == NUL);
3960
Bram Moolenaar10772302019-01-20 18:25:54 +01003961 // Get the byte value now, in case we need it below. This is more efficient
3962 // than making a copy of the line.
3963 len = STRLEN(p);
3964 if (wp->w_cursor.col > (colnr_T)len)
3965 {
3966 // Line may have changed since checking the cursor column, or the lnum
3967 // was adjusted above.
3968 wp->w_cursor.col = (colnr_T)len;
Bram Moolenaar10772302019-01-20 18:25:54 +01003969 wp->w_cursor.coladd = 0;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003970 byteval = 0;
Bram Moolenaar10772302019-01-20 18:25:54 +01003971 }
Bram Moolenaar567199b2013-04-24 16:52:36 +02003972 else
Bram Moolenaar567199b2013-04-24 16:52:36 +02003973 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974
3975 groupdepth = 0;
3976 p = out;
3977 curitem = 0;
3978 prevchar_isflag = TRUE;
3979 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003980 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003982 if (curitem == STL_MAX_ITEM)
3983 {
3984 /* There are too many items. Add the error code to the statusline
3985 * to give the user a hint about what went wrong. */
3986 if (p + 6 < out + outlen)
3987 {
3988 mch_memmove(p, " E541", (size_t)5);
3989 p += 5;
3990 }
3991 break;
3992 }
3993
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 if (*s != NUL && *s != '%')
3995 prevchar_isflag = prevchar_isitem = FALSE;
3996
3997 /*
3998 * Handle up to the next '%' or the end.
3999 */
4000 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
4001 *p++ = *s++;
4002 if (*s == NUL || p + 1 >= out + outlen)
4003 break;
4004
4005 /*
4006 * Handle one '%' item.
4007 */
4008 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01004009 if (*s == NUL) /* ignore trailing % */
4010 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 if (*s == '%')
4012 {
4013 if (p + 1 >= out + outlen)
4014 break;
4015 *p++ = *s++;
4016 prevchar_isflag = prevchar_isitem = FALSE;
4017 continue;
4018 }
4019 if (*s == STL_MIDDLEMARK)
4020 {
4021 s++;
4022 if (groupdepth > 0)
4023 continue;
4024 item[curitem].type = Middle;
4025 item[curitem++].start = p;
4026 continue;
4027 }
4028 if (*s == STL_TRUNCMARK)
4029 {
4030 s++;
4031 item[curitem].type = Trunc;
4032 item[curitem++].start = p;
4033 continue;
4034 }
4035 if (*s == ')')
4036 {
4037 s++;
4038 if (groupdepth < 1)
4039 continue;
4040 groupdepth--;
4041
4042 t = item[groupitem[groupdepth]].start;
4043 *p = NUL;
4044 l = vim_strsize(t);
4045 if (curitem > groupitem[groupdepth] + 1
4046 && item[groupitem[groupdepth]].minwid == 0)
4047 {
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004048 /* remove group if all items are empty and highlight group
4049 * doesn't change */
4050 group_start_userhl = group_end_userhl = 0;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004051 for (n = groupitem[groupdepth] - 1; n >= 0; n--)
4052 {
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004053 if (item[n].type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004054 {
4055 group_start_userhl = group_end_userhl = item[n].minwid;
4056 break;
4057 }
4058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004060 {
4061 if (item[n].type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 break;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004063 if (item[n].type == Highlight)
4064 group_end_userhl = item[n].minwid;
4065 }
4066 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 {
4068 p = t;
4069 l = 0;
4070 }
4071 }
4072 if (l > item[groupitem[groupdepth]].maxwid)
4073 {
4074 /* truncate, remove n bytes of text at the start */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 if (has_mbyte)
4076 {
4077 /* Find the first character that should be included. */
4078 n = 0;
4079 while (l >= item[groupitem[groupdepth]].maxwid)
4080 {
4081 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004082 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 }
4084 }
4085 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004086 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087
4088 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004089 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 p = p - n + 1;
Bram Moolenaar13505972019-01-24 15:04:48 +01004091
4092 // Fill up space left over by half a double-wide char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 while (++l < item[groupitem[groupdepth]].minwid)
4094 *p++ = fillchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095
4096 /* correct the start of the items for the truncation */
4097 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
4098 {
4099 item[l].start -= n;
4100 if (item[l].start < t)
4101 item[l].start = t;
4102 }
4103 }
4104 else if (abs(item[groupitem[groupdepth]].minwid) > l)
4105 {
4106 /* fill */
4107 n = item[groupitem[groupdepth]].minwid;
4108 if (n < 0)
4109 {
4110 /* fill by appending characters */
4111 n = 0 - n;
4112 while (l++ < n && p + 1 < out + outlen)
4113 *p++ = fillchar;
4114 }
4115 else
4116 {
4117 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004118 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 l = n - l;
4120 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004121 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 p += l;
4123 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
4124 item[n].start += l;
4125 for ( ; l > 0; l--)
4126 *t++ = fillchar;
4127 }
4128 }
4129 continue;
4130 }
4131 minwid = 0;
4132 maxwid = 9999;
4133 zeropad = FALSE;
4134 l = 1;
4135 if (*s == '0')
4136 {
4137 s++;
4138 zeropad = TRUE;
4139 }
4140 if (*s == '-')
4141 {
4142 s++;
4143 l = -1;
4144 }
4145 if (VIM_ISDIGIT(*s))
4146 {
4147 minwid = (int)getdigits(&s);
4148 if (minwid < 0) /* overflow */
4149 minwid = 0;
4150 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004151 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 {
4153 item[curitem].type = Highlight;
4154 item[curitem].start = p;
4155 item[curitem].minwid = minwid > 9 ? 1 : minwid;
4156 s++;
4157 curitem++;
4158 continue;
4159 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004160 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4161 {
4162 if (*s == STL_TABCLOSENR)
4163 {
4164 if (minwid == 0)
4165 {
4166 /* %X ends the close label, go back to the previously
4167 * define tab label nr. */
4168 for (n = curitem - 1; n >= 0; --n)
4169 if (item[n].type == TabPage && item[n].minwid >= 0)
4170 {
4171 minwid = item[n].minwid;
4172 break;
4173 }
4174 }
4175 else
4176 /* close nrs are stored as negative values */
4177 minwid = - minwid;
4178 }
4179 item[curitem].type = TabPage;
4180 item[curitem].start = p;
4181 item[curitem].minwid = minwid;
4182 s++;
4183 curitem++;
4184 continue;
4185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 if (*s == '.')
4187 {
4188 s++;
4189 if (VIM_ISDIGIT(*s))
4190 {
4191 maxwid = (int)getdigits(&s);
4192 if (maxwid <= 0) /* overflow */
4193 maxwid = 50;
4194 }
4195 }
4196 minwid = (minwid > 50 ? 50 : minwid) * l;
4197 if (*s == '(')
4198 {
4199 groupitem[groupdepth++] = curitem;
4200 item[curitem].type = Group;
4201 item[curitem].start = p;
4202 item[curitem].minwid = minwid;
4203 item[curitem].maxwid = maxwid;
4204 s++;
4205 curitem++;
4206 continue;
4207 }
4208 if (vim_strchr(STL_ALL, *s) == NULL)
4209 {
4210 s++;
4211 continue;
4212 }
4213 opt = *s++;
4214
4215 /* OK - now for the real work */
4216 base = 'D';
4217 itemisflag = FALSE;
4218 fillable = TRUE;
4219 num = -1;
4220 str = NULL;
4221 switch (opt)
4222 {
4223 case STL_FILEPATH:
4224 case STL_FULLPATH:
4225 case STL_FILENAME:
4226 fillable = FALSE; /* don't change ' ' to fillchar */
4227 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004228 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 else
4230 {
4231 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004232 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4234 }
4235 trans_characters(NameBuff, MAXPATHL);
4236 if (opt != STL_FILENAME)
4237 str = NameBuff;
4238 else
4239 str = gettail(NameBuff);
4240 break;
4241
4242 case STL_VIM_EXPR: /* '{' */
4243 itemisflag = TRUE;
4244 t = p;
4245 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
4246 *p++ = *s++;
4247 if (*s != '}') /* missing '}' or out of space */
4248 break;
4249 s++;
4250 *p = 0;
4251 p = t;
4252
4253#ifdef FEAT_EVAL
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004254 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp),
4255 "%d", curbuf->b_fnum);
4256 set_internal_string_var((char_u *)"g:actual_curbuf", buf_tmp);
4257 vim_snprintf((char *)win_tmp, sizeof(win_tmp), "%d", curwin->w_id);
4258 set_internal_string_var((char_u *)"g:actual_curwin", win_tmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004260 save_curbuf = curbuf;
4261 save_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 curwin = wp;
4263 curbuf = wp->w_buffer;
4264
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004265 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004267 curwin = save_curwin;
4268 curbuf = save_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00004269 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004270 do_unlet((char_u *)"g:actual_curwin", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271
4272 if (str != NULL && *str != 0)
4273 {
4274 if (*skipdigits(str) == NUL)
4275 {
4276 num = atoi((char *)str);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004277 VIM_CLEAR(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 itemisflag = FALSE;
4279 }
4280 }
4281#endif
4282 break;
4283
4284 case STL_LINE:
4285 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4286 ? 0L : (long)(wp->w_cursor.lnum);
4287 break;
4288
4289 case STL_NUMLINES:
4290 num = wp->w_buffer->b_ml.ml_line_count;
4291 break;
4292
4293 case STL_COLUMN:
4294 num = !(State & INSERT) && empty_line
4295 ? 0 : (int)wp->w_cursor.col + 1;
4296 break;
4297
4298 case STL_VIRTCOL:
4299 case STL_VIRTCOL_ALT:
4300 /* In list mode virtcol needs to be recomputed */
4301 virtcol = wp->w_virtcol;
4302 if (wp->w_p_list && lcs_tab1 == NUL)
4303 {
4304 wp->w_p_list = FALSE;
4305 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
4306 wp->w_p_list = TRUE;
4307 }
4308 ++virtcol;
4309 /* Don't display %V if it's the same as %c. */
4310 if (opt == STL_VIRTCOL_ALT
4311 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
4312 ? 0 : (int)wp->w_cursor.col + 1)))
4313 break;
4314 num = (long)virtcol;
4315 break;
4316
4317 case STL_PERCENTAGE:
4318 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4319 (long)wp->w_buffer->b_ml.ml_line_count);
4320 break;
4321
4322 case STL_ALTPERCENT:
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004323 str = buf_tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004324 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 break;
4326
4327 case STL_ARGLISTSTAT:
4328 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004329 buf_tmp[0] = 0;
4330 if (append_arg_number(wp, buf_tmp, (int)sizeof(buf_tmp), FALSE))
4331 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 break;
4333
4334 case STL_KEYMAP:
4335 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004336 if (get_keymap_str(wp, (char_u *)"<%s>", buf_tmp, TMPLEN))
4337 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 break;
4339 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004340#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004341 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342#else
4343 num = 0;
4344#endif
4345 break;
4346
4347 case STL_BUFNO:
4348 num = wp->w_buffer->b_fnum;
4349 break;
4350
4351 case STL_OFFSET_X:
4352 base = 'X';
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004353 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 case STL_OFFSET:
4355#ifdef FEAT_BYTEOFF
4356 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4357 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4358 0L : l + 1 + (!(State & INSERT) && empty_line ?
4359 0 : (int)wp->w_cursor.col);
4360#endif
4361 break;
4362
4363 case STL_BYTEVAL_X:
4364 base = 'X';
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004365 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004367 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 if (num == NL)
4369 num = 0;
4370 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4371 num = NL;
4372 break;
4373
4374 case STL_ROFLAG:
4375 case STL_ROFLAG_ALT:
4376 itemisflag = TRUE;
4377 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004378 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 break;
4380
4381 case STL_HELPFLAG:
4382 case STL_HELPFLAG_ALT:
4383 itemisflag = TRUE;
4384 if (wp->w_buffer->b_help)
4385 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004386 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 break;
4388
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 case STL_FILETYPE:
4390 if (*wp->w_buffer->b_p_ft != NUL
4391 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4392 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004393 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), "[%s]",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004394 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004395 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 }
4397 break;
4398
4399 case STL_FILETYPE_ALT:
4400 itemisflag = TRUE;
4401 if (*wp->w_buffer->b_p_ft != NUL
4402 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4403 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004404 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), ",%s",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004405 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004406 for (t = buf_tmp; *t != 0; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 *t = TOUPPER_LOC(*t);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004408 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 }
4410 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411
Bram Moolenaar4033c552017-09-16 20:54:51 +02004412#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 case STL_PREVIEWFLAG:
4414 case STL_PREVIEWFLAG_ALT:
4415 itemisflag = TRUE;
4416 if (wp->w_p_pvw)
4417 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4418 : _("[Preview]"));
4419 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004420
4421 case STL_QUICKFIX:
4422 if (bt_quickfix(wp->w_buffer))
4423 str = (char_u *)(wp->w_llist_ref
4424 ? _(msg_loclist)
4425 : _(msg_qflist));
4426 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427#endif
4428
4429 case STL_MODIFIED:
4430 case STL_MODIFIED_ALT:
4431 itemisflag = TRUE;
4432 switch ((opt == STL_MODIFIED_ALT)
4433 + bufIsChanged(wp->w_buffer) * 2
4434 + (!wp->w_buffer->b_p_ma) * 4)
4435 {
4436 case 2: str = (char_u *)"[+]"; break;
4437 case 3: str = (char_u *)",+"; break;
4438 case 4: str = (char_u *)"[-]"; break;
4439 case 5: str = (char_u *)",-"; break;
4440 case 6: str = (char_u *)"[+-]"; break;
4441 case 7: str = (char_u *)",+-"; break;
4442 }
4443 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004444
4445 case STL_HIGHLIGHT:
4446 t = s;
4447 while (*s != '#' && *s != NUL)
4448 ++s;
4449 if (*s == '#')
4450 {
4451 item[curitem].type = Highlight;
4452 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004453 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004454 curitem++;
4455 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004456 if (*s != NUL)
4457 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004458 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 }
4460
4461 item[curitem].start = p;
4462 item[curitem].type = Normal;
4463 if (str != NULL && *str)
4464 {
4465 t = str;
4466 if (itemisflag)
4467 {
4468 if ((t[0] && t[1])
4469 && ((!prevchar_isitem && *t == ',')
4470 || (prevchar_isflag && *t == ' ')))
4471 t++;
4472 prevchar_isflag = TRUE;
4473 }
4474 l = vim_strsize(t);
4475 if (l > 0)
4476 prevchar_isitem = TRUE;
4477 if (l > maxwid)
4478 {
4479 while (l >= maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 if (has_mbyte)
4481 {
4482 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004483 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 }
4485 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 l -= byte2cells(*t++);
4487 if (p + 1 >= out + outlen)
4488 break;
4489 *p++ = '<';
4490 }
4491 if (minwid > 0)
4492 {
4493 for (; l < minwid && p + 1 < out + outlen; l++)
4494 {
4495 /* Don't put a "-" in front of a digit. */
4496 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4497 *p++ = ' ';
4498 else
4499 *p++ = fillchar;
4500 }
4501 minwid = 0;
4502 }
4503 else
4504 minwid *= -1;
4505 while (*t && p + 1 < out + outlen)
4506 {
4507 *p++ = *t++;
4508 /* Change a space by fillchar, unless fillchar is '-' and a
4509 * digit follows. */
4510 if (fillable && p[-1] == ' '
4511 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4512 p[-1] = fillchar;
4513 }
4514 for (; l < minwid && p + 1 < out + outlen; l++)
4515 *p++ = fillchar;
4516 }
4517 else if (num >= 0)
4518 {
4519 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4520 char_u nstr[20];
4521
4522 if (p + 20 >= out + outlen)
4523 break; /* not sufficient space */
4524 prevchar_isitem = TRUE;
4525 t = nstr;
4526 if (opt == STL_VIRTCOL_ALT)
4527 {
4528 *t++ = '-';
4529 minwid--;
4530 }
4531 *t++ = '%';
4532 if (zeropad)
4533 *t++ = '0';
4534 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004535 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 *t = 0;
4537
4538 for (n = num, l = 1; n >= nbase; n /= nbase)
4539 l++;
4540 if (opt == STL_VIRTCOL_ALT)
4541 l++;
4542 if (l > maxwid)
4543 {
4544 l += 2;
4545 n = l - maxwid;
4546 while (l-- > maxwid)
4547 num /= nbase;
4548 *t++ = '>';
4549 *t++ = '%';
4550 *t = t[-3];
4551 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004552 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4553 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 }
4555 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004556 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4557 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 p += STRLEN(p);
4559 }
4560 else
4561 item[curitem].type = Empty;
4562
4563 if (opt == STL_VIM_EXPR)
4564 vim_free(str);
4565
4566 if (num >= 0 || (!itemisflag && str && *str))
4567 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4568 curitem++;
4569 }
4570 *p = NUL;
4571 itemcnt = curitem;
4572
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004573#ifdef FEAT_EVAL
4574 if (usefmt != fmt)
4575 vim_free(usefmt);
4576#endif
4577
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 width = vim_strsize(out);
4579 if (maxwidth > 0 && width > maxwidth)
4580 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004581 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 l = 0;
4583 if (itemcnt == 0)
4584 s = out;
4585 else
4586 {
4587 for ( ; l < itemcnt; l++)
4588 if (item[l].type == Trunc)
4589 {
4590 /* Truncate at %< item. */
4591 s = item[l].start;
4592 break;
4593 }
4594 if (l == itemcnt)
4595 {
4596 /* No %< item, truncate first item. */
4597 s = item[0].start;
4598 l = 0;
4599 }
4600 }
4601
4602 if (width - vim_strsize(s) >= maxwidth)
4603 {
4604 /* Truncation mark is beyond max length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 if (has_mbyte)
4606 {
4607 s = out;
4608 width = 0;
4609 for (;;)
4610 {
4611 width += ptr2cells(s);
4612 if (width >= maxwidth)
4613 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004614 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 }
4616 /* Fill up for half a double-wide character. */
4617 while (++width < maxwidth)
4618 *s++ = fillchar;
4619 }
4620 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 s = out + maxwidth - 1;
4622 for (l = 0; l < itemcnt; l++)
4623 if (item[l].start > s)
4624 break;
4625 itemcnt = l;
4626 *s++ = '>';
4627 *s = 0;
4628 }
4629 else
4630 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 if (has_mbyte)
4632 {
4633 n = 0;
4634 while (width >= maxwidth)
4635 {
4636 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004637 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 }
4639 }
4640 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 n = width - maxwidth + 1;
4642 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004643 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 *s = '<';
4645
4646 /* Fill up for half a double-wide character. */
4647 while (++width < maxwidth)
4648 {
4649 s = s + STRLEN(s);
4650 *s++ = fillchar;
4651 *s = NUL;
4652 }
4653
4654 --n; /* count the '<' */
4655 for (; l < itemcnt; l++)
4656 {
4657 if (item[l].start - n >= s)
4658 item[l].start -= n;
4659 else
4660 item[l].start = s;
4661 }
4662 }
4663 width = maxwidth;
4664 }
4665 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4666 {
4667 /* Apply STL_MIDDLE if any */
4668 for (l = 0; l < itemcnt; l++)
4669 if (item[l].type == Middle)
4670 break;
4671 if (l < itemcnt)
4672 {
4673 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004674 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 for (s = item[l].start; s < p; s++)
4676 *s = fillchar;
4677 for (l++; l < itemcnt; l++)
4678 item[l].start += maxwidth - width;
4679 width = maxwidth;
4680 }
4681 }
4682
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004683 /* Store the info about highlighting. */
4684 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004686 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 for (l = 0; l < itemcnt; l++)
4688 {
4689 if (item[l].type == Highlight)
4690 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004691 sp->start = item[l].start;
4692 sp->userhl = item[l].minwid;
4693 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 }
4695 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004696 sp->start = NULL;
4697 sp->userhl = 0;
4698 }
4699
4700 /* Store the info about tab pages labels. */
4701 if (tabtab != NULL)
4702 {
4703 sp = tabtab;
4704 for (l = 0; l < itemcnt; l++)
4705 {
4706 if (item[l].type == TabPage)
4707 {
4708 sp->start = item[l].start;
4709 sp->userhl = item[l].minwid;
4710 sp++;
4711 }
4712 }
4713 sp->start = NULL;
4714 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 }
4716
Bram Moolenaar65ed1362017-09-30 16:00:14 +02004717 /* When inside update_screen we do not want redrawing a stausline, ruler,
4718 * title, etc. to trigger another redraw, it may cause an endless loop. */
4719 if (updating_screen)
4720 {
4721 must_redraw = save_must_redraw;
4722 curwin->w_redr_type = save_redr_type;
4723 }
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004724
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 return width;
4726}
4727#endif /* FEAT_STL_OPT */
4728
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004729#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4730 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004732 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4733 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 */
4735 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004736get_rel_pos(
4737 win_T *wp,
4738 char_u *buf,
4739 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740{
4741 long above; /* number of lines above window */
4742 long below; /* number of lines below window */
4743
Bram Moolenaar0027c212015-01-07 13:31:52 +01004744 if (buflen < 3) /* need at least 3 chars for writing */
4745 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 above = wp->w_topline - 1;
4747#ifdef FEAT_DIFF
4748 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02004749 if (wp->w_topline == 1 && wp->w_topfill >= 1)
4750 above = 0; /* All buffer lines are displayed and there is an
4751 * indication of filler lines, that can be considered
4752 * seeing all lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753#endif
4754 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4755 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004756 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4757 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004759 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004761 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 ? (int)(above / ((above + below) / 100L))
4763 : (int)(above * 100L / (above + below)));
4764}
4765#endif
4766
4767/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004768 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 * Return TRUE if it was appended.
4770 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004771 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004772append_arg_number(
4773 win_T *wp,
4774 char_u *buf,
4775 int buflen,
4776 int add_file) /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777{
4778 char_u *p;
4779
4780 if (ARGCOUNT <= 1) /* nothing to do */
4781 return FALSE;
4782
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004783 p = buf + STRLEN(buf); /* go to the end of the buffer */
4784 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 return FALSE;
4786 *p++ = ' ';
4787 *p++ = '(';
4788 if (add_file)
4789 {
4790 STRCPY(p, "file ");
4791 p += 5;
4792 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004793 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4794 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4796 return TRUE;
4797}
4798
4799/*
4800 * If fname is not a full path, make it a full path.
4801 * Returns pointer to allocated memory (NULL for failure).
4802 */
4803 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004804fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805{
4806 /*
4807 * Force expanding the path always for Unix, because symbolic links may
4808 * mess up the full path name, even though it starts with a '/'.
4809 * Also expand when there is ".." in the file name, try to remove it,
4810 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004811 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 * For MS-Windows also expand names like "longna~1" to "longname".
4813 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004814#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 return FullName_save(fname, TRUE);
4816#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004817 if (!vim_isAbsName(fname)
4818 || strstr((char *)fname, "..") != NULL
4819 || strstr((char *)fname, "//") != NULL
4820# ifdef BACKSLASH_IN_FILENAME
4821 || strstr((char *)fname, "\\\\") != NULL
4822# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004823# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004825# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 )
4827 return FullName_save(fname, FALSE);
4828
4829 fname = vim_strsave(fname);
4830
Bram Moolenaar9b942202007-10-03 12:31:33 +00004831# ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01004832 if (fname != NULL)
4833 fname_case(fname, 0); /* set correct case for file name */
Bram Moolenaar9b942202007-10-03 12:31:33 +00004834# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835
4836 return fname;
4837#endif
4838}
4839
4840/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02004841 * Make "*ffname" a full file name, set "*sfname" to "*ffname" if not NULL.
4842 * "*ffname" becomes a pointer to allocated memory (or NULL).
4843 * When resolving a link both "*sfname" and "*ffname" will point to the same
4844 * allocated memory.
4845 * The "*ffname" and "*sfname" pointer values on call will not be freed.
4846 * Note that the resulting "*ffname" pointer should be considered not allocaed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004849fname_expand(
4850 buf_T *buf UNUSED,
4851 char_u **ffname,
4852 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02004854 if (*ffname == NULL) // no file name given, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 return;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02004856 if (*sfname == NULL) // no short file name given, use ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 *sfname = *ffname;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02004858 *ffname = fix_fname(*ffname); // expand to full path
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859
4860#ifdef FEAT_SHORTCUT
4861 if (!buf->b_p_bin)
4862 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004863 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02004865 // If the file name is a shortcut file, use the file it links to.
Bram Moolenaardce1e892019-02-10 23:18:53 +01004866 rfname = mch_resolve_path(*ffname, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004867 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 {
4869 vim_free(*ffname);
4870 *ffname = rfname;
4871 *sfname = rfname;
4872 }
4873 }
4874#endif
4875}
4876
4877/*
4878 * Get the file name for an argument list entry.
4879 */
4880 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004881alist_name(aentry_T *aep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882{
4883 buf_T *bp;
4884
4885 /* Use the name from the associated buffer if it exists. */
4886 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004887 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 return aep->ae_fname;
4889 return bp->b_fname;
4890}
4891
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892/*
4893 * do_arg_all(): Open up to 'count' windows, one for each argument.
4894 */
4895 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004896do_arg_all(
4897 int count,
4898 int forceit, /* hide buffers in current windows */
4899 int keep_tabs) /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900{
4901 int i;
4902 win_T *wp, *wpnext;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004903 char_u *opened; /* Array of weight for which args are open:
4904 * 0: not opened
4905 * 1: opened in other tab
4906 * 2: opened in curtab
4907 * 3: opened in curtab and curwin
4908 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004909 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 int use_firstwin = FALSE; /* use first window for arglist */
4911 int split_ret = OK;
4912 int p_ea_save;
4913 alist_T *alist; /* argument list to be used */
4914 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004915 tabpage_T *tpnext;
4916 int had_tab = cmdmod.tab;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004917 win_T *old_curwin, *last_curwin;
4918 tabpage_T *old_curtab, *last_curtab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004919 win_T *new_curwin = NULL;
4920 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921
4922 if (ARGCOUNT <= 0)
4923 {
4924 /* Don't give an error message. We don't want it when the ":all"
4925 * command is in the .vimrc. */
4926 return;
4927 }
4928 setpcmark();
4929
4930 opened_len = ARGCOUNT;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004931 opened = alloc_clear(opened_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 if (opened == NULL)
4933 return;
4934
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004935 /* Autocommands may do anything to the argument list. Make sure it's not
4936 * freed while we are working here by "locking" it. We still have to
4937 * watch out for its size to be changed. */
4938 alist = curwin->w_alist;
4939 ++alist->al_refcount;
4940
4941 old_curwin = curwin;
4942 old_curtab = curtab;
4943
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004944# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004946# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947
4948 /*
4949 * Try closing all windows that are not in the argument list.
4950 * Also close windows that are not full width;
4951 * When 'hidden' or "forceit" set the buffer becomes hidden.
4952 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004953 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004955 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004956 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004957 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004959 tpnext = curtab->tp_next;
4960 for (wp = firstwin; wp != NULL; wp = wpnext)
4961 {
4962 wpnext = wp->w_next;
4963 buf = wp->w_buffer;
4964 if (buf->b_ffname == NULL
Bram Moolenaar5a030a52016-12-01 17:48:29 +01004965 || (!keep_tabs && (buf->b_nwindows > 1
4966 || wp->w_width != Columns)))
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004967 i = opened_len;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004968 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004970 /* check if the buffer in this window is in the arglist */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004971 for (i = 0; i < opened_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004973 if (i < alist->al_ga.ga_len
4974 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
4975 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
Bram Moolenaar99499b12019-05-23 21:35:48 +02004976 buf->b_ffname, TRUE, TRUE) & FPC_SAME))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004978 int weight = 1;
4979
4980 if (old_curtab == curtab)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004981 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004982 ++weight;
4983 if (old_curwin == wp)
4984 ++weight;
4985 }
4986
4987 if (weight > (int)opened[i])
4988 {
4989 opened[i] = (char_u)weight;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004990 if (i == 0)
4991 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004992 if (new_curwin != NULL)
4993 new_curwin->w_arg_idx = opened_len;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004994 new_curwin = wp;
4995 new_curtab = curtab;
4996 }
4997 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004998 else if (keep_tabs)
4999 i = opened_len;
5000
5001 if (wp->w_alist != alist)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005002 {
5003 /* Use the current argument list for all windows
5004 * containing a file from it. */
5005 alist_unlink(wp->w_alist);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005006 wp->w_alist = alist;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005007 ++wp->w_alist->al_refcount;
5008 }
5009 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 }
5012 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005013 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005015 if (i == opened_len && !keep_tabs)/* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005017 if (buf_hide(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005018 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005020 /* If the buffer was changed, and we would like to hide it,
5021 * try autowriting. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02005022 if (!buf_hide(buf) && buf->b_nwindows <= 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005023 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005025 bufref_T bufref;
5026
5027 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005028
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005029 (void)autowrite(buf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005030
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005031 /* check if autocommands removed the window */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005032 if (!win_valid(wp) || !bufref_valid(&bufref))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005033 {
5034 wpnext = firstwin; /* start all over... */
5035 continue;
5036 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005037 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005038 /* don't close last window */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005039 if (ONE_WINDOW
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005040 && (first_tabpage->tp_next == NULL || !had_tab))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005041 use_firstwin = TRUE;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005042 else
5043 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005044 win_close(wp, !buf_hide(buf) && !bufIsChanged(buf));
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005045
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005046 /* check if autocommands removed the next window */
5047 if (!win_valid(wpnext))
5048 wpnext = firstwin; /* start all over... */
Bram Moolenaar4033c552017-09-16 20:54:51 +02005049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 }
5052 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005053
5054 /* Without the ":tab" modifier only do the current tab page. */
5055 if (had_tab == 0 || tpnext == NULL)
5056 break;
5057
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005058 /* check if autocommands removed the next tab page */
5059 if (!valid_tabpage(tpnext))
5060 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005061
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005062 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 }
5064
5065 /*
5066 * Open a window for files in the argument list that don't have one.
5067 * ARGCOUNT may change while doing this, because of autocommands.
5068 */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005069 if (count > opened_len || count <= 0)
5070 count = opened_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 /* Don't execute Win/Buf Enter/Leave autocommands here. */
5073 ++autocmd_no_enter;
5074 ++autocmd_no_leave;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005075 last_curwin = curwin;
5076 last_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005078 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
5079 * leaving an empty tab page when executed locally. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005080 if (keep_tabs && BUFEMPTY() && curbuf->b_nwindows == 1
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005081 && curbuf->b_ffname == NULL && !curbuf->b_changed)
5082 use_firstwin = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005083
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005084 for (i = 0; i < count && i < opened_len && !got_int; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 {
5086 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
5087 arg_had_last = TRUE;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005088 if (opened[i] > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 {
5090 /* Move the already present window to below the current window */
5091 if (curwin->w_arg_idx != i)
5092 {
5093 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
5094 {
5095 if (wpnext->w_arg_idx == i)
5096 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005097 if (keep_tabs)
5098 {
5099 new_curwin = wpnext;
5100 new_curtab = curtab;
5101 }
Bram Moolenaar1417c762019-07-27 17:31:36 +02005102 else if (wpnext->w_frame->fr_parent
5103 != curwin->w_frame->fr_parent)
5104 {
5105 emsg(_("E249: window layout changed unexpectedly"));
5106 i = count;
5107 break;
5108 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005109 else
5110 win_move_after(wpnext, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 break;
5112 }
5113 }
5114 }
5115 }
5116 else if (split_ret == OK)
5117 {
5118 if (!use_firstwin) /* split current window */
5119 {
5120 p_ea_save = p_ea;
5121 p_ea = TRUE; /* use space from all windows */
5122 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5123 p_ea = p_ea_save;
5124 if (split_ret == FAIL)
5125 continue;
5126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 else /* first window: do autocmd for leaving this buffer */
5128 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129
5130 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005131 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 */
5133 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005134 if (i == 0)
5135 {
5136 new_curwin = curwin;
5137 new_curtab = curtab;
5138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
5140 ECMD_ONE,
Bram Moolenaareb44a682017-08-03 22:44:55 +02005141 ((buf_hide(curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00005143 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 if (use_firstwin)
5145 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 use_firstwin = FALSE;
5147 }
5148 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005149
5150 /* When ":tab" was used open a new tab for a new window repeatedly. */
5151 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5152 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 }
5154
5155 /* Remove the "lock" on the argument list. */
5156 alist_unlink(alist);
5157
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 --autocmd_no_enter;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005159
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005160 /* restore last referenced tabpage's curwin */
5161 if (last_curtab != new_curtab)
5162 {
5163 if (valid_tabpage(last_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005164 goto_tabpage_tp(last_curtab, TRUE, TRUE);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005165 if (win_valid(last_curwin))
5166 win_enter(last_curwin, FALSE);
5167 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005168 /* to window with first arg */
5169 if (valid_tabpage(new_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005170 goto_tabpage_tp(new_curtab, TRUE, TRUE);
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005171 if (win_valid(new_curwin))
5172 win_enter(new_curwin, FALSE);
5173
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 vim_free(opened);
5176}
5177
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178/*
5179 * Open a window for a number of buffers.
5180 */
5181 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005182ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183{
5184 buf_T *buf;
5185 win_T *wp, *wpnext;
5186 int split_ret = OK;
5187 int p_ea_save;
5188 int open_wins = 0;
5189 int r;
5190 int count; /* Maximum number of windows to open. */
5191 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005192 int had_tab = cmdmod.tab;
5193 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194
5195 if (eap->addr_count == 0) /* make as many windows as possible */
5196 count = 9999;
5197 else
5198 count = eap->line2; /* make as many windows as specified */
5199 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5200 all = FALSE;
5201 else
5202 all = TRUE;
5203
5204 setpcmark();
5205
5206#ifdef FEAT_GUI
5207 need_mouse_correct = TRUE;
5208#endif
5209
5210 /*
5211 * Close superfluous windows (two windows for the same buffer).
5212 * Also close windows that are not full-width.
5213 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005214 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005215 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005216 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005218 tpnext = curtab->tp_next;
5219 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005221 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005222 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005223 || ((cmdmod.split & WSP_VERT)
5224 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005225 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005226 : wp->w_width != Columns)
Bram Moolenaar4033c552017-09-16 20:54:51 +02005227 || (had_tab > 0 && wp != firstwin)) && !ONE_WINDOW
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005228 && !(wp->w_closing || wp->w_buffer->b_locked > 0))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005229 {
5230 win_close(wp, FALSE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005231 wpnext = firstwin; /* just in case an autocommand does
5232 something strange with windows */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005233 tpnext = first_tabpage; /* start all over... */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005234 open_wins = 0;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005235 }
5236 else
5237 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005239
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005240 /* Without the ":tab" modifier only do the current tab page. */
5241 if (had_tab == 0 || tpnext == NULL)
5242 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005243 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 }
5245
5246 /*
5247 * Go through the buffer list. When a buffer doesn't have a window yet,
5248 * open one. Otherwise move the window to the right position.
5249 * Watch out for autocommands that delete buffers or windows!
5250 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 /* Don't execute Win/Buf Enter/Leave autocommands here. */
5252 ++autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 win_enter(lastwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5256 {
5257 /* Check if this buffer needs a window */
5258 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5259 continue;
5260
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005261 if (had_tab != 0)
5262 {
5263 /* With the ":tab" modifier don't move the window. */
5264 if (buf->b_nwindows > 0)
5265 wp = lastwin; /* buffer has a window, skip it */
5266 else
5267 wp = NULL;
5268 }
5269 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005270 {
5271 /* Check if this buffer already has a window */
Bram Moolenaar29323592016-07-24 22:04:11 +02005272 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005273 if (wp->w_buffer == buf)
5274 break;
5275 /* If the buffer already has a window, move it */
5276 if (wp != NULL)
5277 win_move_after(wp, curwin);
5278 }
5279
5280 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005282 bufref_T bufref;
5283
5284 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005285
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 /* Split the window and put the buffer in it */
5287 p_ea_save = p_ea;
5288 p_ea = TRUE; /* use space from all windows */
5289 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5290 ++open_wins;
5291 p_ea = p_ea_save;
5292 if (split_ret == FAIL)
5293 continue;
5294
5295 /* Open the buffer in this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005298 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005300 /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 swap_exists_action = SEA_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 break;
5303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 if (swap_exists_action == SEA_QUIT)
5305 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005306#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005307 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005308
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005309 /* Reset the error/interrupt/exception state here so that
5310 * aborting() returns FALSE when closing a window. */
5311 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005312#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005313
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005314 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 win_close(curwin, TRUE);
5316 --open_wins;
5317 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005318 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005319
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005320#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005321 /* Restore the error/interrupt/exception state if not
5322 * discarded by a new aborting error, interrupt, or uncaught
5323 * exception. */
5324 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005325#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 }
5327 else
5328 handle_swap_exists(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 }
5330
5331 ui_breakcheck();
5332 if (got_int)
5333 {
5334 (void)vgetc(); /* only break the file loading, not the rest */
5335 break;
5336 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005337#ifdef FEAT_EVAL
5338 /* Autocommands deleted the buffer or aborted script processing!!! */
5339 if (aborting())
5340 break;
5341#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005342 /* When ":tab" was used open a new tab for a new window repeatedly. */
5343 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5344 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 --autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 win_enter(firstwin, FALSE); /* back to first window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349
5350 /*
5351 * Close superfluous windows.
5352 */
5353 for (wp = lastwin; open_wins > count; )
5354 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005355 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 || autowrite(wp->w_buffer, FALSE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 if (!win_valid(wp))
5358 {
5359 /* BufWrite Autocommands made the window invalid, start over */
5360 wp = lastwin;
5361 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005362 else if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005364 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 --open_wins;
5366 wp = lastwin;
5367 }
5368 else
5369 {
5370 wp = wp->w_prev;
5371 if (wp == NULL)
5372 break;
5373 }
5374 }
5375}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005378static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005379
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380/*
5381 * do_modelines() - process mode lines for the current file
5382 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005383 * "flags" can be:
5384 * OPT_WINONLY only set options local to window
5385 * OPT_NOWIN don't set options local to window
5386 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 * Returns immediately if the "ml" option isn't set.
5388 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005390do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005392 linenr_T lnum;
5393 int nmlines;
5394 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395
5396 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5397 return;
5398
5399 /* Disallow recursive entry here. Can happen when executing a modeline
5400 * triggers an autocommand, which reloads modelines with a ":do". */
5401 if (entered)
5402 return;
5403
5404 ++entered;
5405 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5406 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005407 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 nmlines = 0;
5409
5410 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5411 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005412 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 nmlines = 0;
5414 --entered;
5415}
5416
5417#include "version.h" /* for version number */
5418
5419/*
5420 * chk_modeline() - check a single line for a mode string
5421 * Return FAIL if an error encountered.
5422 */
5423 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005424chk_modeline(
5425 linenr_T lnum,
5426 int flags) /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427{
5428 char_u *s;
5429 char_u *e;
5430 char_u *linecopy; /* local copy of any modeline found */
5431 int prev;
5432 int vers;
5433 int end;
5434 int retval = OK;
5435 char_u *save_sourcing_name;
5436 linenr_T save_sourcing_lnum;
5437#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005438 sctx_T save_current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439#endif
5440
5441 prev = -1;
5442 for (s = ml_get(lnum); *s != NUL; ++s)
5443 {
5444 if (prev == -1 || vim_isspace(prev))
5445 {
5446 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5447 || STRNCMP(s, "vi:", (size_t)3) == 0)
5448 break;
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005449 /* Accept both "vim" and "Vim". */
5450 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 {
5452 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5453 e = s + 4;
5454 else
5455 e = s + 3;
5456 vers = getdigits(&e);
5457 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005458 && (s[0] != 'V'
5459 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 && (s[3] == ':'
5461 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5462 || (VIM_VERSION_100 < vers && s[3] == '<')
5463 || (VIM_VERSION_100 > vers && s[3] == '>')
5464 || (VIM_VERSION_100 == vers && s[3] == '=')))
5465 break;
5466 }
5467 }
5468 prev = *s;
5469 }
5470
5471 if (*s)
5472 {
5473 do /* skip over "ex:", "vi:" or "vim:" */
5474 ++s;
5475 while (s[-1] != ':');
5476
5477 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5478 if (linecopy == NULL)
5479 return FAIL;
5480
5481 save_sourcing_lnum = sourcing_lnum;
5482 save_sourcing_name = sourcing_name;
5483 sourcing_lnum = lnum; /* prepare for emsg() */
5484 sourcing_name = (char_u *)"modelines";
5485
5486 end = FALSE;
5487 while (end == FALSE)
5488 {
5489 s = skipwhite(s);
5490 if (*s == NUL)
5491 break;
5492
5493 /*
5494 * Find end of set command: ':' or end of line.
5495 * Skip over "\:", replacing it with ":".
5496 */
5497 for (e = s; *e != ':' && *e != NUL; ++e)
5498 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005499 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 if (*e == NUL)
5501 end = TRUE;
5502
5503 /*
5504 * If there is a "set" command, require a terminating ':' and
5505 * ignore the stuff after the ':'.
5506 * "vi:set opt opt opt: foo" -- foo not interpreted
5507 * "vi:opt opt opt: foo" -- foo interpreted
5508 * Accept "se" for compatibility with Elvis.
5509 */
5510 if (STRNCMP(s, "set ", (size_t)4) == 0
5511 || STRNCMP(s, "se ", (size_t)3) == 0)
5512 {
5513 if (*e != ':') /* no terminating ':'? */
5514 break;
5515 end = TRUE;
5516 s = vim_strchr(s, ' ') + 1;
5517 }
5518 *e = NUL; /* truncate the set command */
5519
5520 if (*s != NUL) /* skip over an empty "::" */
5521 {
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005522 int secure_save = secure;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005524 save_current_sctx = current_sctx;
5525 current_sctx.sc_sid = SID_MODELINE;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005526 current_sctx.sc_seq = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005527 current_sctx.sc_lnum = 0;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02005528 current_sctx.sc_version = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529#endif
Bram Moolenaar5958f952018-11-20 04:25:21 +01005530 // Make sure no risky things are executed as a side effect.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01005531 secure = 1;
Bram Moolenaar5958f952018-11-20 04:25:21 +01005532
Bram Moolenaara3227e22006-03-08 21:32:40 +00005533 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar5958f952018-11-20 04:25:21 +01005534
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005535 secure = secure_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005537 current_sctx = save_current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538#endif
5539 if (retval == FAIL) /* stop if error found */
5540 break;
5541 }
5542 s = e + 1; /* advance to next part */
5543 }
5544
5545 sourcing_lnum = save_sourcing_lnum;
5546 sourcing_name = save_sourcing_name;
5547
5548 vim_free(linecopy);
5549 }
5550 return retval;
5551}
5552
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005553/*
Bram Moolenaar91335e52018-08-01 17:53:12 +02005554 * Return TRUE if "buf" is a normal buffer, 'buftype' is empty.
5555 */
5556 int
5557bt_normal(buf_T *buf)
5558{
5559 return buf != NULL && buf->b_p_bt[0] == NUL;
5560}
5561
Bram Moolenaar113e1072019-01-20 15:30:40 +01005562#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar91335e52018-08-01 17:53:12 +02005563/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005564 * Return TRUE if "buf" is the quickfix buffer.
5565 */
5566 int
5567bt_quickfix(buf_T *buf)
5568{
5569 return buf != NULL && buf->b_p_bt[0] == 'q';
5570}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005571#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005572
Bram Moolenaar113e1072019-01-20 15:30:40 +01005573#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005574/*
5575 * Return TRUE if "buf" is a terminal buffer.
5576 */
5577 int
5578bt_terminal(buf_T *buf)
5579{
5580 return buf != NULL && buf->b_p_bt[0] == 't';
5581}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005582#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005583
5584/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005585 * Return TRUE if "buf" is a help buffer.
5586 */
5587 int
5588bt_help(buf_T *buf)
5589{
5590 return buf != NULL && buf->b_help;
5591}
5592
5593/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02005594 * Return TRUE if "buf" is a prompt buffer.
5595 */
5596 int
5597bt_prompt(buf_T *buf)
5598{
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005599 return buf != NULL && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'r';
5600}
5601
5602/*
5603 * Return TRUE if "buf" is a buffer for a popup window.
5604 */
5605 int
5606bt_popup(buf_T *buf)
5607{
5608 return buf != NULL && buf->b_p_bt != NULL
5609 && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'o';
Bram Moolenaarf2732452018-06-03 14:47:35 +02005610}
5611
5612/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005613 * Return TRUE if "buf" is a "nofile", "acwrite", "terminal" or "prompt"
5614 * buffer. This means the buffer name is not a file name.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005615 */
5616 int
Bram Moolenaar26910de2019-06-15 19:37:15 +02005617bt_nofilename(buf_T *buf)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005618{
5619 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5620 || buf->b_p_bt[0] == 'a'
Bram Moolenaarf2732452018-06-03 14:47:35 +02005621 || buf->b_p_bt[0] == 't'
5622 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005623}
5624
5625/*
Bram Moolenaar26910de2019-06-15 19:37:15 +02005626 * Return TRUE if "buf" has 'buftype' set to "nofile".
5627 */
5628 int
5629bt_nofile(buf_T *buf)
5630{
5631 return buf != NULL && buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f';
5632}
5633
5634/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005635 * Return TRUE if "buf" is a "nowrite", "nofile", "terminal" or "prompt"
5636 * buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005637 */
5638 int
5639bt_dontwrite(buf_T *buf)
5640{
Bram Moolenaarf2732452018-06-03 14:47:35 +02005641 return buf != NULL && (buf->b_p_bt[0] == 'n'
5642 || buf->b_p_bt[0] == 't'
5643 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005644}
5645
Bram Moolenaar113e1072019-01-20 15:30:40 +01005646#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005647 int
5648bt_dontwrite_msg(buf_T *buf)
5649{
5650 if (bt_dontwrite(buf))
5651 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005652 emsg(_("E382: Cannot write, 'buftype' option is set"));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005653 return TRUE;
5654 }
5655 return FALSE;
5656}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005657#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005658
5659/*
5660 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
5661 * and 'bufhidden'.
5662 */
5663 int
5664buf_hide(buf_T *buf)
5665{
5666 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
5667 switch (buf->b_p_bh[0])
5668 {
5669 case 'u': /* "unload" */
5670 case 'w': /* "wipe" */
5671 case 'd': return FALSE; /* "delete" */
5672 case 'h': return TRUE; /* "hide" */
5673 }
5674 return (p_hid || cmdmod.hide);
5675}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676
5677/*
5678 * Return special buffer name.
5679 * Returns NULL when the buffer has a normal file name.
5680 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005681 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005682buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683{
Bram Moolenaar4033c552017-09-16 20:54:51 +02005684#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005686 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005687 /*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005688 * Differentiate between the quickfix and location list buffers using
5689 * the buffer number stored in the global quickfix stack.
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005690 */
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005691 if (buf->b_fnum == qf_stack_get_bufnr())
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005692 return (char_u *)_(msg_qflist);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005693 else
5694 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02005697
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 /* There is no _file_ when 'buftype' is "nofile", b_sfname
Bram Moolenaar21554412017-07-24 21:44:43 +02005699 * contains the name as specified by the user. */
Bram Moolenaar26910de2019-06-15 19:37:15 +02005700 if (bt_nofilename(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 {
Bram Moolenaar21554412017-07-24 21:44:43 +02005702#ifdef FEAT_TERMINAL
5703 if (buf->b_term != NULL)
5704 return term_get_status_text(buf->b_term);
5705#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02005706 if (buf->b_fname != NULL)
5707 return buf->b_fname;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02005708#ifdef FEAT_JOB_CHANNEL
5709 if (bt_prompt(buf))
5710 return (char_u *)_("[Prompt]");
5711#endif
Bram Moolenaarc6896e22019-05-30 22:32:34 +02005712#ifdef FEAT_TEXT_PROP
5713 if (bt_popup(buf))
5714 return (char_u *)_("[Popup]");
5715#endif
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005716 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 }
Bram Moolenaar21554412017-07-24 21:44:43 +02005718
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005720 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 return NULL;
5722}
5723
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02005724#if defined(FEAT_JOB_CHANNEL) \
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005725 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5726 || defined(PROTO)
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02005727# define SWITCH_TO_WIN
5728
5729/*
5730 * Find a window that contains "buf" and switch to it.
5731 * If there is no such window, use the current window and change "curbuf".
5732 * Caller must initialize save_curbuf to NULL.
5733 * restore_win_for_buf() MUST be called later!
5734 */
5735 void
5736switch_to_win_for_buf(
5737 buf_T *buf,
5738 win_T **save_curwinp,
5739 tabpage_T **save_curtabp,
5740 bufref_T *save_curbuf)
5741{
5742 win_T *wp;
5743 tabpage_T *tp;
5744
5745 if (find_win_for_buf(buf, &wp, &tp) == FAIL)
5746 switch_buffer(save_curbuf, buf);
5747 else if (switch_win(save_curwinp, save_curtabp, wp, tp, TRUE) == FAIL)
5748 {
5749 restore_win(*save_curwinp, *save_curtabp, TRUE);
5750 switch_buffer(save_curbuf, buf);
5751 }
5752}
5753
5754 void
5755restore_win_for_buf(
5756 win_T *save_curwin,
5757 tabpage_T *save_curtab,
5758 bufref_T *save_curbuf)
5759{
5760 if (save_curbuf->br_buf == NULL)
5761 restore_win(save_curwin, save_curtab, TRUE);
5762 else
5763 restore_buffer(save_curbuf);
5764}
5765#endif
5766
Bram Moolenaar4033c552017-09-16 20:54:51 +02005767#if defined(FEAT_QUICKFIX) || defined(SWITCH_TO_WIN) || defined(PROTO)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005768/*
5769 * Find a window for buffer "buf".
5770 * If found OK is returned and "wp" and "tp" are set to the window and tabpage.
5771 * If not found FAIL is returned.
5772 */
5773 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005774find_win_for_buf(
5775 buf_T *buf,
5776 win_T **wp,
5777 tabpage_T **tp)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005778{
5779 FOR_ALL_TAB_WINDOWS(*tp, *wp)
5780 if ((*wp)->w_buffer == buf)
5781 goto win_found;
5782 return FAIL;
5783win_found:
5784 return OK;
5785}
5786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788/*
5789 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5790 */
5791 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005792set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793{
5794 if (on != curbuf->b_p_bl)
5795 {
5796 curbuf->b_p_bl = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 if (on)
5798 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5799 else
5800 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 }
5802}
5803
5804/*
5805 * Read the file for "buf" again and check if the contents changed.
5806 * Return TRUE if it changed or this could not be checked.
5807 */
5808 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005809buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810{
5811 buf_T *newbuf;
5812 int differ = TRUE;
5813 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 exarg_T ea;
5816
5817 /* Allocate a buffer without putting it in the buffer list. */
5818 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5819 if (newbuf == NULL)
5820 return TRUE;
5821
5822 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5823 if (prep_exarg(&ea, buf) == FAIL)
5824 {
5825 wipe_buffer(newbuf, FALSE);
5826 return TRUE;
5827 }
5828
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 /* set curwin/curbuf to buf and save a few things */
5830 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831
Bram Moolenaar4770d092006-01-12 23:22:24 +00005832 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 && readfile(buf->b_ffname, buf->b_fname,
5834 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5835 &ea, READ_NEW | READ_DUMMY) == OK)
5836 {
5837 /* compare the two files line by line */
5838 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5839 {
5840 differ = FALSE;
5841 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5842 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5843 {
5844 differ = TRUE;
5845 break;
5846 }
5847 }
5848 }
5849 vim_free(ea.cmd);
5850
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 /* restore curwin/curbuf and a few other things */
5852 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853
5854 if (curbuf != newbuf) /* safety check */
5855 wipe_buffer(newbuf, FALSE);
5856
5857 return differ;
5858}
5859
5860/*
5861 * Wipe out a buffer and decrement the last buffer number if it was used for
5862 * this buffer. Call this to wipe out a temp buffer that does not contain any
5863 * marks.
5864 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005866wipe_buffer(
5867 buf_T *buf,
5868 int aucmd UNUSED) /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869{
5870 if (buf->b_fnum == top_file_num - 1)
5871 --top_file_num;
5872
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005874 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005875
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005876 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005877
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005879 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880}
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005881
5882#if defined(FEAT_EVAL) || defined(PROTO)
5883/*
5884 * Mark references in functions of buffers.
5885 */
5886 int
5887set_ref_in_buffers(int copyID)
5888{
5889 int abort = FALSE;
5890 buf_T *bp;
5891
5892 FOR_ALL_BUFFERS(bp)
5893 {
5894 listener_T *lnr;
5895 typval_T tv;
5896
5897 for (lnr = bp->b_listener; !abort && lnr != NULL; lnr = lnr->lr_next)
5898 {
5899 if (lnr->lr_callback.cb_partial != NULL)
5900 {
5901 tv.v_type = VAR_PARTIAL;
5902 tv.vval.v_partial = lnr->lr_callback.cb_partial;
5903 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
5904 }
5905 }
5906# ifdef FEAT_JOB_CHANNEL
5907 if (!abort && bp->b_prompt_callback.cb_partial != NULL)
5908 {
5909 tv.v_type = VAR_PARTIAL;
5910 tv.vval.v_partial = bp->b_prompt_callback.cb_partial;
5911 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
5912 }
5913 if (!abort && bp->b_prompt_interrupt.cb_partial != NULL)
5914 {
5915 tv.v_type = VAR_PARTIAL;
5916 tv.vval.v_partial = bp->b_prompt_interrupt.cb_partial;
5917 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
5918 }
5919# endif
5920 if (abort)
5921 break;
5922 }
5923 return abort;
5924}
5925#endif