blob: e6ae09d255588b2182c565128f167c6ce336c92e [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 Moolenaarf28dbce2016-01-29 22:03:47 +010032static void buflist_setfpos(buf_T *buf, win_T *win, linenr_T lnum, colnr_T col, int copy_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +000033#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +020034static buf_T *buflist_findname_stat(char_u *ffname, stat_T *st);
35static int otherfile_buf(buf_T *buf, char_u *ffname, stat_T *stp);
36static int buf_same_ino(buf_T *buf, stat_T *stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037#else
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010038static int otherfile_buf(buf_T *buf, char_u *ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000039#endif
40#ifdef FEAT_TITLE
Bram Moolenaar84a93082018-06-16 22:58:15 +020041static int value_changed(char_u *str, char_u **last);
Bram Moolenaar071d4272004-06-13 20:20:40 +000042#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010043static int append_arg_number(win_T *wp, char_u *buf, int buflen, int add_file);
44static void free_buffer(buf_T *);
45static void free_buffer_stuff(buf_T *buf, int free_options);
46static void clear_wininfo(buf_T *buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000047
48#ifdef UNIX
49# define dev_T dev_t
50#else
51# define dev_T unsigned
52#endif
53
Bram Moolenaar4033c552017-09-16 20:54:51 +020054#if defined(FEAT_QUICKFIX)
Bram Moolenaar7fd73202010-07-25 16:58:46 +020055static char *msg_loclist = N_("[Location List]");
56static char *msg_qflist = N_("[Quickfix List]");
57#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +010058static char *e_auabort = N_("E855: Autocommands caused command to abort");
Bram Moolenaar7fd73202010-07-25 16:58:46 +020059
Bram Moolenaarb25f9a92016-07-10 18:21:50 +020060/* Number of times free_buffer() was called. */
61static int buf_free_count = 0;
62
Bram Moolenaarc024b462019-06-08 18:07:21 +020063/*
64 * Read data from buffer for retrying.
65 */
Bram Moolenaarf71d7b92016-08-09 22:14:05 +020066 static int
67read_buffer(
68 int read_stdin, /* read file from stdin, otherwise fifo */
69 exarg_T *eap, /* for forced 'ff' and 'fenc' or NULL */
70 int flags) /* extra flags for readfile() */
71{
72 int retval = OK;
73 linenr_T line_count;
74
75 /*
76 * Read from the buffer which the text is already filled in and append at
77 * the end. This makes it possible to retry when 'fileformat' or
78 * 'fileencoding' was guessed wrong.
79 */
80 line_count = curbuf->b_ml.ml_line_count;
81 retval = readfile(
82 read_stdin ? NULL : curbuf->b_ffname,
83 read_stdin ? NULL : curbuf->b_fname,
84 (linenr_T)line_count, (linenr_T)0, (linenr_T)MAXLNUM, eap,
85 flags | READ_BUFFER);
86 if (retval == OK)
87 {
88 /* Delete the binary lines. */
89 while (--line_count >= 0)
90 ml_delete((linenr_T)1, FALSE);
91 }
92 else
93 {
94 /* Delete the converted lines. */
95 while (curbuf->b_ml.ml_line_count > line_count)
96 ml_delete(line_count, FALSE);
97 }
98 /* Put the cursor on the first line. */
99 curwin->w_cursor.lnum = 1;
100 curwin->w_cursor.col = 0;
101
102 if (read_stdin)
103 {
104 /* Set or reset 'modified' before executing autocommands, so that
105 * it can be changed there. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100106 if (!readonlymode && !BUFEMPTY())
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200107 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100108 else if (retval == OK)
Bram Moolenaarc024b462019-06-08 18:07:21 +0200109 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200110
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100111 if (retval == OK)
112 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100113#ifdef FEAT_EVAL
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100114 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100115 curbuf, &retval);
116#else
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100117 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200118#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100119 }
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200120 }
121 return retval;
122}
123
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200125 * Open current buffer, that is: open the memfile and read the file into
126 * memory.
127 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128 */
129 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100130open_buffer(
131 int read_stdin, /* read file from stdin */
132 exarg_T *eap, /* for forced 'ff' and 'fenc' or NULL */
133 int flags) /* extra flags for readfile() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134{
135 int retval = OK;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200136 bufref_T old_curbuf;
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100137#ifdef FEAT_SYN_HL
138 long old_tw = curbuf->b_p_tw;
139#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200140 int read_fifo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141
142 /*
143 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
144 * When re-entering the same buffer, it should not change, because the
145 * user may have reset the flag by hand.
146 */
147 if (readonlymode && curbuf->b_ffname != NULL
148 && (curbuf->b_flags & BF_NEVERLOADED))
149 curbuf->b_p_ro = TRUE;
150
Bram Moolenaar4770d092006-01-12 23:22:24 +0000151 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152 {
153 /*
154 * There MUST be a memfile, otherwise we can't do anything
155 * If we can't create one for the current buffer, take another buffer
156 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100157 close_buffer(NULL, curbuf, 0, FALSE);
Bram Moolenaar29323592016-07-24 22:04:11 +0200158 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159 if (curbuf->b_ml.ml_mfp != NULL)
160 break;
161 /*
162 * if there is no memfile at all, exit
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000163 * This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 */
165 if (curbuf == NULL)
166 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100167 emsg(_("E82: Cannot allocate any buffer, exiting..."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168 getout(2);
169 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100170 emsg(_("E83: Cannot allocate buffer, using other one..."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 enter_buffer(curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100172#ifdef FEAT_SYN_HL
173 if (old_tw != curbuf->b_p_tw)
174 check_colorcolumn(curwin);
175#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 return FAIL;
177 }
178
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 /* The autocommands in readfile() may change the buffer, but only AFTER
180 * reading the file. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200181 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 modified_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183
184 /* mark cursor position as being invalid */
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100185 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186
187 if (curbuf->b_ffname != NULL
188#ifdef FEAT_NETBEANS_INTG
189 && netbeansReadFile
190#endif
191 )
192 {
Bram Moolenaar426dd022016-03-15 15:09:29 +0100193 int old_msg_silent = msg_silent;
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200194#ifdef UNIX
195 int save_bin = curbuf->b_p_bin;
196 int perm;
197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198#ifdef FEAT_NETBEANS_INTG
199 int oldFire = netbeansFireChanges;
200
201 netbeansFireChanges = 0;
202#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200203#ifdef UNIX
204 perm = mch_getperm(curbuf->b_ffname);
Bram Moolenaard569bb02018-08-11 13:57:20 +0200205 if (perm >= 0 && (S_ISFIFO(perm)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200206 || S_ISSOCK(perm)
Bram Moolenaarf04507d2016-08-20 15:05:39 +0200207# ifdef OPEN_CHR_FILES
208 || (S_ISCHR(perm) && is_dev_fd_file(curbuf->b_ffname))
209# endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200210 ))
211 read_fifo = TRUE;
212 if (read_fifo)
213 curbuf->b_p_bin = TRUE;
214#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100215 if (shortmess(SHM_FILEINFO))
216 msg_silent = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200218 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200219 flags | READ_NEW | (read_fifo ? READ_FIFO : 0));
220#ifdef UNIX
221 if (read_fifo)
222 {
223 curbuf->b_p_bin = save_bin;
224 if (retval == OK)
225 retval = read_buffer(FALSE, eap, flags);
226 }
227#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100228 msg_silent = old_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229#ifdef FEAT_NETBEANS_INTG
230 netbeansFireChanges = oldFire;
231#endif
232 /* Help buffer is filtered. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +0200233 if (bt_help(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234 fix_help_buffer();
235 }
236 else if (read_stdin)
237 {
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200238 int save_bin = curbuf->b_p_bin;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239
240 /*
241 * First read the text in binary mode into the buffer.
242 * Then read from that same buffer and append at the end. This makes
243 * it possible to retry when 'fileformat' or 'fileencoding' was
244 * guessed wrong.
245 */
246 curbuf->b_p_bin = TRUE;
247 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200248 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
249 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250 curbuf->b_p_bin = save_bin;
251 if (retval == OK)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200252 retval = read_buffer(TRUE, eap, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253 }
254
255 /* if first time loading this buffer, init b_chartab[] */
256 if (curbuf->b_flags & BF_NEVERLOADED)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100257 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258 (void)buf_init_chartab(curbuf, FALSE);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100259#ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100260 parse_cino(curbuf);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100261#endif
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263
264 /*
265 * Set/reset the Changed flag first, autocmds may change the buffer.
266 * Apply the automatic commands, before processing the modelines.
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200267 * So the modelines have priority over autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268 */
269 /* When reading stdin, the buffer contents always needs writing, so set
270 * the changed flag. Unless in readonly mode: "ls | gview -".
271 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000272 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 || modified_was_set /* ":set modified" used in autocmd */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100274#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000277 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100279 else if (retval == OK && !read_stdin && !read_fifo)
Bram Moolenaarc024b462019-06-08 18:07:21 +0200280 unchanged(curbuf, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281 save_file_ff(curbuf); /* keep this fileformat */
282
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100283 /* Set last_changedtick to avoid triggering a TextChanged autocommand right
284 * after it was added. */
285 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
286#ifdef FEAT_INS_EXPAND
287 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
288#endif
289
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290 /* require "!" to overwrite the file, because it wasn't read completely */
291#ifdef FEAT_EVAL
292 if (aborting())
293#else
294 if (got_int)
295#endif
296 curbuf->b_flags |= BF_READERR;
297
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000298#ifdef FEAT_FOLDING
299 /* Need to update automatic folding. Do this before the autocommands,
300 * they may use the fold info. */
301 foldUpdateAll(curwin);
302#endif
303
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304 /* need to set w_topline, unless some autocommand already did that. */
305 if (!(curwin->w_valid & VALID_TOPLINE))
306 {
307 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100308#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100310#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100312#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100314#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316#endif
317
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100318 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 /*
321 * The autocommands may have changed the current buffer. Apply the
322 * modelines to the correct buffer, if it still exists and is loaded.
323 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200324 if (bufref_valid(&old_curbuf) && old_curbuf.br_buf->b_ml.ml_mfp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 {
326 aco_save_T aco;
327
328 /* Go to the buffer that was opened. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200329 aucmd_prepbuf(&aco, old_curbuf.br_buf);
Bram Moolenaara3227e22006-03-08 21:32:40 +0000330 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
332
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100333#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100335 &retval);
336#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339
340 /* restore curwin/curbuf and a few other things */
341 aucmd_restbuf(&aco);
342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 }
344
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 return retval;
346}
347
348/*
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200349 * Store "buf" in "bufref" and set the free count.
350 */
351 void
352set_bufref(bufref_T *bufref, buf_T *buf)
353{
354 bufref->br_buf = buf;
Bram Moolenaarfadacf02017-06-19 20:35:32 +0200355 bufref->br_fnum = buf == NULL ? 0 : buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200356 bufref->br_buf_free_count = buf_free_count;
357}
358
359/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200360 * Return TRUE if "bufref->br_buf" points to the same buffer as when
361 * set_bufref() was called and it is a valid buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200362 * Only goes through the buffer list if buf_free_count changed.
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200363 * Also checks if b_fnum is still the same, a :bwipe followed by :new might get
364 * the same allocated memory, but it's a different buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200365 */
366 int
367bufref_valid(bufref_T *bufref)
368{
369 return bufref->br_buf_free_count == buf_free_count
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200370 ? TRUE : buf_valid(bufref->br_buf)
371 && bufref->br_fnum == bufref->br_buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200372}
373
374/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200376 * This can be slow if there are many buffers, prefer using bufref_valid().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 */
378 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100379buf_valid(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380{
381 buf_T *bp;
382
Bram Moolenaar82404332016-07-10 17:00:38 +0200383 /* Assume that we more often have a recent buffer, start with the last
384 * one. */
385 for (bp = lastbuf; bp != NULL; bp = bp->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 if (bp == buf)
387 return TRUE;
388 return FALSE;
389}
390
391/*
Bram Moolenaar480778b2016-07-14 22:09:39 +0200392 * A hash table used to quickly lookup a buffer by its number.
393 */
394static hashtab_T buf_hashtab;
395
396 static void
397buf_hashtab_add(buf_T *buf)
398{
399 sprintf((char *)buf->b_key, "%x", buf->b_fnum);
400 if (hash_add(&buf_hashtab, buf->b_key) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100401 emsg(_("E931: Buffer cannot be registered"));
Bram Moolenaar480778b2016-07-14 22:09:39 +0200402}
403
404 static void
405buf_hashtab_remove(buf_T *buf)
406{
407 hashitem_T *hi = hash_find(&buf_hashtab, buf->b_key);
408
409 if (!HASHITEM_EMPTY(hi))
410 hash_remove(&buf_hashtab, hi);
411}
412
Bram Moolenaar94f01952018-09-01 15:30:03 +0200413/*
414 * Return TRUE when buffer "buf" can be unloaded.
415 * Give an error message and return FALSE when the buffer is locked or the
416 * screen is being redrawn and the buffer is in a window.
417 */
418 static int
419can_unload_buffer(buf_T *buf)
420{
421 int can_unload = !buf->b_locked;
422
423 if (can_unload && updating_screen)
424 {
425 win_T *wp;
426
427 FOR_ALL_WINDOWS(wp)
428 if (wp->w_buffer == buf)
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200429 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200430 can_unload = FALSE;
Bram Moolenaar9cea87c2018-09-21 16:59:45 +0200431 break;
432 }
Bram Moolenaar94f01952018-09-01 15:30:03 +0200433 }
434 if (!can_unload)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100435 emsg(_("E937: Attempt to delete a buffer that is in use"));
Bram Moolenaar94f01952018-09-01 15:30:03 +0200436 return can_unload;
437}
Bram Moolenaara997b452018-04-17 23:24:06 +0200438
Bram Moolenaar480778b2016-07-14 22:09:39 +0200439/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 * Close the link to a buffer.
441 * "action" is used when there is no longer a window for the buffer.
442 * It can be:
443 * 0 buffer becomes hidden
444 * DOBUF_UNLOAD buffer is unloaded
445 * DOBUF_DELETE buffer is unloaded and removed from buffer list
446 * DOBUF_WIPE buffer is unloaded and really deleted
447 * When doing all but the first one on the current buffer, the caller should
448 * get a new buffer very soon!
449 *
450 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100451 *
452 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
453 * cause there to be only one window with this buffer. e.g. when ":quit" is
454 * supposed to close the window but autocommands close all other windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 */
456 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100457close_buffer(
458 win_T *win, /* if not NULL, set b_last_cursor */
459 buf_T *buf,
460 int action,
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200461 int abort_if_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100464 int nwindows;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200465 bufref_T bufref;
Bram Moolenaar3a117e12016-10-30 21:57:52 +0100466 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200467 win_T *the_curwin = curwin;
468 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 int unload_buf = (action != 0);
470 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
471 int wipe_buf = (action == DOBUF_WIPE);
472
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200473 /*
474 * Force unloading or deleting when 'bufhidden' says so.
475 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
476 * "hide" (otherwise we could never free or delete a buffer).
477 */
478 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
479 {
480 del_buf = TRUE;
481 unload_buf = TRUE;
482 }
483 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
484 {
485 del_buf = TRUE;
486 unload_buf = TRUE;
487 wipe_buf = TRUE;
488 }
489 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
490 unload_buf = TRUE;
491
Bram Moolenaar94053a52017-08-01 21:44:33 +0200492#ifdef FEAT_TERMINAL
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200493 if (bt_terminal(buf) && (buf->b_nwindows == 1 || del_buf))
Bram Moolenaar94053a52017-08-01 21:44:33 +0200494 {
495 if (term_job_running(buf->b_term))
496 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200497 if (wipe_buf || unload_buf)
Bram Moolenaara997b452018-04-17 23:24:06 +0200498 {
Bram Moolenaar94f01952018-09-01 15:30:03 +0200499 if (!can_unload_buffer(buf))
Bram Moolenaara997b452018-04-17 23:24:06 +0200500 return;
Bram Moolenaar94f01952018-09-01 15:30:03 +0200501
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200502 /* Wiping out or unloading a terminal buffer kills the job. */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200503 free_terminal(buf);
Bram Moolenaara997b452018-04-17 23:24:06 +0200504 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200505 else
506 {
507 /* The job keeps running, hide the buffer. */
508 del_buf = FALSE;
509 unload_buf = FALSE;
510 }
511 }
512 else
513 {
514 /* A terminal buffer is wiped out if the job has finished. */
515 del_buf = TRUE;
516 unload_buf = TRUE;
517 wipe_buf = TRUE;
518 }
519 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200520#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200522 /* Disallow deleting the buffer when it is locked (already being closed or
523 * halfway a command that relies on it). Unloading is allowed. */
Bram Moolenaar94f01952018-09-01 15:30:03 +0200524 if ((del_buf || wipe_buf) && !can_unload_buffer(buf))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200525 return;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200526
Bram Moolenaar4033c552017-09-16 20:54:51 +0200527 /* check no autocommands closed the window */
528 if (win != NULL && win_valid_any_tab(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 {
530 /* Set b_last_cursor when closing the last window for the buffer.
531 * Remember the last cursor position and window options of the buffer.
532 * This used to be only for the current window, but then options like
533 * 'foldmethod' may be lost with a ":only" command. */
534 if (buf->b_nwindows == 1)
535 set_last_cursor(win);
536 buflist_setfpos(buf, win,
537 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
538 win->w_cursor.col, TRUE);
539 }
540
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200541 set_bufref(&bufref, buf);
542
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 /* When the buffer is no longer in a window, trigger BufWinLeave */
544 if (buf->b_nwindows == 1)
545 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200546 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200547 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
548 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200549 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100550 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200551 /* Autocommands deleted the buffer. */
552aucmd_abort:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100553 emsg(_(e_auabort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 return;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100555 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200556 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200557 if (abort_if_last && one_window())
558 /* Autocommands made this the only window. */
559 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560
561 /* When the buffer becomes hidden, but is not unloaded, trigger
562 * BufHidden */
563 if (!unload_buf)
564 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200565 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200566 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
567 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200568 && !bufref_valid(&bufref))
Bram Moolenaar362ce482012-06-06 19:02:45 +0200569 /* Autocommands deleted the buffer. */
570 goto aucmd_abort;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200571 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200572 if (abort_if_last && one_window())
573 /* Autocommands made this the only window. */
574 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100576#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 if (aborting()) /* autocmds may abort script processing */
578 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100579#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200581
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200582 /* If the buffer was in curwin and the window has changed, go back to that
583 * window, if it still exists. This avoids that ":edit x" triggering a
584 * "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
585 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
586 {
587 block_autocmds();
588 goto_tabpage_win(the_curtab, the_curwin);
589 unblock_autocmds();
590 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200591
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 nwindows = buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593
594 /* decrease the link count from windows (unless not in any window) */
595 if (buf->b_nwindows > 0)
596 --buf->b_nwindows;
597
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100598#ifdef FEAT_DIFF
599 if (diffopt_hiddenoff() && !unload_buf && buf->b_nwindows == 0)
Bram Moolenaarb7407d32018-02-03 17:36:27 +0100600 diff_buf_delete(buf); /* Clear 'diff' for hidden buffer. */
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100601#endif
602
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 /* Return when a window is displaying the buffer or when it's not
604 * unloaded. */
605 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607
608 /* Always remove the buffer when there is no file name. */
609 if (buf->b_ffname == NULL)
610 del_buf = TRUE;
611
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200612 /* When closing the current buffer stop Visual mode before freeing
613 * anything. */
Bram Moolenaar4930a762016-09-11 14:39:53 +0200614 if (buf == curbuf && VIsual_active
Bram Moolenaar9a27c7f2016-09-09 12:57:09 +0200615#if defined(EXITFREE)
616 && !entered_free_all_mem
617#endif
618 )
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200619 end_visual_mode();
620
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 /*
622 * Free all things allocated for this buffer.
623 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
624 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 /* Remember if we are closing the current buffer. Restore the number of
626 * windows, so that autocommands in buf_freeall() don't get confused. */
627 is_curbuf = (buf == curbuf);
628 buf->b_nwindows = nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200630 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 /* Autocommands may have deleted the buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200633 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100635#ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000636 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100638#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 /*
641 * It's possible that autocommands change curbuf to the one being deleted.
642 * This might cause the previous curbuf to be deleted unexpectedly. But
643 * in some cases it's OK to delete the curbuf, because a new one is
644 * obtained anyway. Therefore only return if curbuf changed to the
645 * deleted buffer.
646 */
647 if (buf == curbuf && !is_curbuf)
648 return;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200649
Bram Moolenaar4033c552017-09-16 20:54:51 +0200650 if (win_valid_any_tab(win) && win->w_buffer == buf)
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200651 win->w_buffer = NULL; /* make sure we don't use the buffer now */
652
653 /* Autocommands may have opened or closed windows for this buffer.
654 * Decrement the count for the close we do here. */
655 if (buf->b_nwindows > 0)
656 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 /*
659 * Remove the buffer from the list.
660 */
661 if (wipe_buf)
662 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +0200663 if (buf->b_sfname != buf->b_ffname)
664 VIM_CLEAR(buf->b_sfname);
665 else
666 buf->b_sfname = NULL;
667 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 if (buf->b_prev == NULL)
669 firstbuf = buf->b_next;
670 else
671 buf->b_prev->b_next = buf->b_next;
672 if (buf->b_next == NULL)
673 lastbuf = buf->b_prev;
674 else
675 buf->b_next->b_prev = buf->b_prev;
676 free_buffer(buf);
677 }
678 else
679 {
680 if (del_buf)
681 {
682 /* Free all internal variables and reset option values, to make
683 * ":bdel" compatible with Vim 5.7. */
684 free_buffer_stuff(buf, TRUE);
685
686 /* Make it look like a new buffer. */
687 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
688
689 /* Init the options when loaded again. */
690 buf->b_p_initialized = FALSE;
691 }
692 buf_clear_file(buf);
693 if (del_buf)
694 buf->b_p_bl = FALSE;
695 }
696}
697
698/*
699 * Make buffer not contain a file.
700 */
701 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100702buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703{
704 buf->b_ml.ml_line_count = 1;
Bram Moolenaarc024b462019-06-08 18:07:21 +0200705 unchanged(buf, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 buf->b_p_eol = TRUE;
708 buf->b_start_eol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000710 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 buf->b_ml.ml_mfp = NULL;
712 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
713#ifdef FEAT_NETBEANS_INTG
714 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715#endif
716}
717
718/*
719 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200720 * the file. Careful: get here with "curwin" NULL when exiting.
721 * flags:
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200722 * BFA_DEL buffer is going to be deleted
723 * BFA_WIPE buffer is going to be wiped out
724 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100727buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200730 bufref_T bufref;
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200731 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaar5a497892016-09-03 16:29:04 +0200732 win_T *the_curwin = curwin;
733 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734
Bram Moolenaar5a497892016-09-03 16:29:04 +0200735 /* Make sure the buffer isn't closed by autocommands. */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200736 ++buf->b_locked;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200737 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200738 if (buf->b_ml.ml_mfp != NULL)
739 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200740 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
741 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200742 && !bufref_valid(&bufref))
743 /* autocommands deleted the buffer */
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200744 return;
745 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200746 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200748 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
749 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200750 && !bufref_valid(&bufref))
751 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 return;
753 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200754 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200756 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
757 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200758 && !bufref_valid(&bufref))
759 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 return;
761 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200762 --buf->b_locked;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200763
Bram Moolenaar5a497892016-09-03 16:29:04 +0200764 /* If the buffer was in curwin and the window has changed, go back to that
765 * window, if it still exists. This avoids that ":edit x" triggering a
766 * "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
767 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
768 {
769 block_autocmds();
770 goto_tabpage_win(the_curtab, the_curwin);
771 unblock_autocmds();
772 }
Bram Moolenaar5a497892016-09-03 16:29:04 +0200773
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100774#ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000775 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778
779 /*
780 * It's possible that autocommands change curbuf to the one being deleted.
781 * This might cause curbuf to be deleted unexpectedly. But in some cases
782 * it's OK to delete the curbuf, because a new one is obtained anyway.
783 * Therefore only return if curbuf changed to the deleted buffer.
784 */
785 if (buf == curbuf && !is_curbuf)
786 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787#ifdef FEAT_DIFF
788 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
789#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200790#ifdef FEAT_SYN_HL
Bram Moolenaar89c71222011-11-30 15:40:56 +0100791 /* Remove any ownsyntax, unless exiting. */
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200792 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaar89c71222011-11-30 15:40:56 +0100793 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200794#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000795
796#ifdef FEAT_FOLDING
797 /* No folds in an empty buffer. */
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000798 {
799 win_T *win;
800 tabpage_T *tp;
801
802 FOR_ALL_TAB_WINDOWS(tp, win)
803 if (win->w_buffer == buf)
804 clearFolding(win);
805 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000806#endif
807
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808#ifdef FEAT_TCL
809 tcl_buffer_free(buf);
810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 ml_close(buf, TRUE); /* close and delete the memline/memfile */
812 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200813 if ((flags & BFA_KEEP_UNDO) == 0)
814 {
815 u_blockfree(buf); /* free the memory allocated for undo */
816 u_clearall(buf); /* reset all undo information */
817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200819 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100821#ifdef FEAT_TEXT_PROP
822 clear_buf_prop_types(buf);
823#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000824 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825}
826
827/*
828 * Free a buffer structure and the things it contains related to the buffer
829 * itself (not the file, that must have been done already).
830 */
831 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100832free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200834 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200836#ifdef FEAT_EVAL
Bram Moolenaarb2c87502017-10-14 21:15:58 +0200837 /* b:changedtick uses an item in buf_T, remove it now */
838 dictitem_remove(buf->b_vars, (dictitem_T *)&buf->b_ct_di);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200839 unref_var_dict(buf->b_vars);
840#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200841#ifdef FEAT_LUA
842 lua_buffer_free(buf);
843#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000844#ifdef FEAT_MZSCHEME
845 mzscheme_buffer_free(buf);
846#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847#ifdef FEAT_PERL
848 perl_buf_free(buf);
849#endif
850#ifdef FEAT_PYTHON
851 python_buffer_free(buf);
852#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200853#ifdef FEAT_PYTHON3
854 python3_buffer_free(buf);
855#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856#ifdef FEAT_RUBY
857 ruby_buffer_free(buf);
858#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200859#ifdef FEAT_JOB_CHANNEL
860 channel_buffer_free(buf);
861#endif
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200862#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +0200863 free_terminal(buf);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200864#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +0200865#ifdef FEAT_JOB_CHANNEL
866 vim_free(buf->b_prompt_text);
Bram Moolenaar3a97bb32019-06-01 13:28:35 +0200867 free_callback(&buf->b_prompt_callback);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200868#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200869
870 buf_hashtab_remove(buf);
871
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200872 aubuflocal_remove(buf);
873
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200874 if (autocmd_busy)
875 {
876 /* Do not free the buffer structure while autocommands are executing,
877 * it's still needed. Free it when autocmd_busy is reset. */
878 buf->b_next = au_pending_free_buf;
879 au_pending_free_buf = buf;
880 }
881 else
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200882 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883}
884
885/*
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100886 * Initializes b:changedtick.
Bram Moolenaar79518e22017-02-17 16:31:35 +0100887 */
888 static void
889init_changedtick(buf_T *buf)
890{
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100891 dictitem_T *di = (dictitem_T *)&buf->b_ct_di;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100892
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100893 di->di_flags = DI_FLAGS_FIX | DI_FLAGS_RO;
894 di->di_tv.v_type = VAR_NUMBER;
895 di->di_tv.v_lock = VAR_FIXED;
896 di->di_tv.vval.v_number = 0;
897
Bram Moolenaar92769c32017-02-25 15:41:37 +0100898#ifdef FEAT_EVAL
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100899 STRCPY(buf->b_ct_di.di_key, "changedtick");
900 (void)dict_add(buf->b_vars, di);
Bram Moolenaar92769c32017-02-25 15:41:37 +0100901#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +0100902}
903
904/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
906 */
907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100908free_buffer_stuff(
909 buf_T *buf,
910 int free_options) /* free options as well */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911{
912 if (free_options)
913 {
914 clear_wininfo(buf); /* including window-local options */
915 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200916#ifdef FEAT_SPELL
917 ga_clear(&buf->b_s.b_langp);
918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 }
920#ifdef FEAT_EVAL
Bram Moolenaar79518e22017-02-17 16:31:35 +0100921 {
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100922 varnumber_T tick = CHANGEDTICK(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +0100923
924 vars_clear(&buf->b_vars->dv_hashtab); /* free all buffer variables */
925 hash_init(&buf->b_vars->dv_hashtab);
926 init_changedtick(buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100927 CHANGEDTICK(buf) = tick;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929#endif
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200930 uc_clear(&buf->b_ucmds); // clear local user commands
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931#ifdef FEAT_SIGNS
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200932 buf_delete_signs(buf, (char_u *)"*"); // delete any signs
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000934#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200935 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937#ifdef FEAT_LOCALMAP
938 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
939 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
940#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +0100941 VIM_CLEAR(buf->b_start_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942}
943
944/*
945 * Free the b_wininfo list for buffer "buf".
946 */
947 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100948clear_wininfo(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949{
950 wininfo_T *wip;
951
952 while (buf->b_wininfo != NULL)
953 {
954 wip = buf->b_wininfo;
955 buf->b_wininfo = wip->wi_next;
956 if (wip->wi_optset)
957 {
958 clear_winopt(&wip->wi_opt);
959#ifdef FEAT_FOLDING
960 deleteFoldRecurse(&wip->wi_folds);
961#endif
962 }
963 vim_free(wip);
964 }
965}
966
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967/*
968 * Go to another buffer. Handles the result of the ATTENTION dialog.
969 */
970 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100971goto_buffer(
972 exarg_T *eap,
973 int start,
974 int dir,
975 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200977 bufref_T old_curbuf;
978
979 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980
981 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
983 start, dir, count, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
985 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +0200986#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000987 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000988
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000989 /* Reset the error/interrupt/exception state here so that
990 * aborting() returns FALSE when closing a window. */
991 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +0200992#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000993
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000994 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 win_close(curwin, TRUE);
996 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000997 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000998
Bram Moolenaar67cf86b2019-04-28 22:25:38 +0200999#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001000 /* Restore the error/interrupt/exception state if not discarded by a
1001 * new aborting error, interrupt, or uncaught exception. */
1002 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 }
1005 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001006 handle_swap_exists(&old_curbuf);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001007}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009/*
1010 * Handle the situation of swap_exists_action being set.
1011 * It is allowed for "old_curbuf" to be NULL or invalid.
1012 */
1013 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001014handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015{
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001016#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001017 cleanup_T cs;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001018#endif
1019#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001020 long old_tw = curbuf->b_p_tw;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001021#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001022 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001023
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 if (swap_exists_action == SEA_QUIT)
1025 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001026#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001027 /* Reset the error/interrupt/exception state here so that
1028 * aborting() returns FALSE when closing a buffer. */
1029 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001030#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001031
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 /* User selected Quit at ATTENTION prompt. Go back to previous
1033 * buffer. If that buffer is gone or the same as the current one,
1034 * open a new, empty buffer. */
1035 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001036 swap_exists_did_quit = TRUE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001037 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001038 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1039 || old_curbuf->br_buf == curbuf)
1040 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
1041 else
1042 buf = old_curbuf->br_buf;
1043 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001044 {
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001045 int old_msg_silent = msg_silent;
1046
1047 if (shortmess(SHM_FILEINFO))
1048 msg_silent = 1; // prevent fileinfo message
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001049 enter_buffer(buf);
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001050 // restore msg_silent, so that the command line will be shown
1051 msg_silent = old_msg_silent;
1052
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001053#ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001054 if (old_tw != curbuf->b_p_tw)
1055 check_colorcolumn(curwin);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001056#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001059
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001060#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001061 /* Restore the error/interrupt/exception state if not discarded by a
1062 * new aborting error, interrupt, or uncaught exception. */
1063 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001064#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 }
1066 else if (swap_exists_action == SEA_RECOVER)
1067 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001068#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001069 /* Reset the error/interrupt/exception state here so that
1070 * aborting() returns FALSE when closing a buffer. */
1071 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001072#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001073
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 /* User selected Recover at ATTENTION prompt. */
1075 msg_scroll = TRUE;
Bram Moolenaar99499b12019-05-23 21:35:48 +02001076 ml_recover(FALSE);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001077 msg_puts("\n"); /* don't overwrite the last message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001079 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001080
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001081#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001082 /* Restore the error/interrupt/exception state if not discarded by a
1083 * new aborting error, interrupt, or uncaught exception. */
1084 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02001085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 }
1087 swap_exists_action = SEA_NONE;
1088}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090/*
1091 * do_bufdel() - delete or unload buffer(s)
1092 *
1093 * addr_count == 0: ":bdel" - delete current buffer
1094 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1095 * buffer "end_bnr", then any other arguments.
1096 * addr_count == 2: ":N,N bdel" - delete buffers in range
1097 *
1098 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1099 * DOBUF_DEL (":bdel")
1100 *
1101 * Returns error message or NULL
1102 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001103 char *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001104do_bufdel(
1105 int command,
1106 char_u *arg, /* pointer to extra arguments */
1107 int addr_count,
1108 int start_bnr, /* first buffer number in a range */
1109 int end_bnr, /* buffer nr or last buffer nr in a range */
1110 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111{
1112 int do_current = 0; /* delete current buffer? */
1113 int deleted = 0; /* number of buffers deleted */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001114 char *errormsg = NULL; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 int bnr; /* buffer number */
1116 char_u *p;
1117
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 if (addr_count == 0)
1119 {
1120 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1121 }
1122 else
1123 {
1124 if (addr_count == 2)
1125 {
1126 if (*arg) /* both range and argument is not allowed */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001127 return _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 bnr = start_bnr;
1129 }
1130 else /* addr_count == 1 */
1131 bnr = end_bnr;
1132
1133 for ( ;!got_int; ui_breakcheck())
1134 {
1135 /*
1136 * delete the current buffer last, otherwise when the
1137 * current buffer is deleted, the next buffer becomes
1138 * the current one and will be loaded, which may then
1139 * also be deleted, etc.
1140 */
1141 if (bnr == curbuf->b_fnum)
1142 do_current = bnr;
1143 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
1144 forceit) == OK)
1145 ++deleted;
1146
1147 /*
1148 * find next buffer number to delete/unload
1149 */
1150 if (addr_count == 2)
1151 {
1152 if (++bnr > end_bnr)
1153 break;
1154 }
1155 else /* addr_count == 1 */
1156 {
1157 arg = skipwhite(arg);
1158 if (*arg == NUL)
1159 break;
1160 if (!VIM_ISDIGIT(*arg))
1161 {
1162 p = skiptowhite_esc(arg);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01001163 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE,
1164 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 if (bnr < 0) /* failed */
1166 break;
1167 arg = p;
1168 }
1169 else
1170 bnr = getdigits(&arg);
1171 }
1172 }
1173 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1174 FORWARD, do_current, forceit) == OK)
1175 ++deleted;
1176
1177 if (deleted == 0)
1178 {
1179 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001180 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001182 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00001184 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001185 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 }
1187 else if (deleted >= p_report)
1188 {
1189 if (command == DOBUF_UNLOAD)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001190 smsg(NGETTEXT("%d buffer unloaded",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001191 "%d buffers unloaded", deleted), deleted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 else if (command == DOBUF_DEL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001193 smsg(NGETTEXT("%d buffer deleted",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001194 "%d buffers deleted", deleted), deleted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001196 smsg(NGETTEXT("%d buffer wiped out",
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001197 "%d buffers wiped out", deleted), deleted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 }
1199 }
1200
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201
1202 return errormsg;
1203}
1204
Bram Moolenaara02471e2014-01-10 16:43:14 +01001205/*
1206 * Make the current buffer empty.
1207 * Used when it is wiped out and it's the last buffer.
1208 */
1209 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001210empty_curbuf(
1211 int close_others,
1212 int forceit,
1213 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001214{
1215 int retval;
1216 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001217 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001218
1219 if (action == DOBUF_UNLOAD)
1220 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001221 emsg(_("E90: Cannot unload last buffer"));
Bram Moolenaara02471e2014-01-10 16:43:14 +01001222 return FAIL;
1223 }
1224
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001225 set_bufref(&bufref, buf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001226 if (close_others)
1227 /* Close any other windows on this buffer, then make it empty. */
Bram Moolenaara02471e2014-01-10 16:43:14 +01001228 close_windows(buf, TRUE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001229
1230 setpcmark();
1231 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1232 forceit ? ECMD_FORCEIT : 0, curwin);
1233
1234 /*
1235 * do_ecmd() may create a new buffer, then we have to delete
1236 * the old one. But do_ecmd() may have done that already, check
1237 * if the buffer still exists.
1238 */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001239 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001240 close_buffer(NULL, buf, action, FALSE);
1241 if (!close_others)
1242 need_fileinfo = FALSE;
1243 return retval;
1244}
Bram Moolenaar94f01952018-09-01 15:30:03 +02001245
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246/*
1247 * Implementation of the commands for the buffer list.
1248 *
1249 * action == DOBUF_GOTO go to specified buffer
1250 * action == DOBUF_SPLIT split window and go to specified buffer
1251 * action == DOBUF_UNLOAD unload specified buffer(s)
1252 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1253 * action == DOBUF_WIPE delete specified buffer(s) really
1254 *
1255 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1256 * start == DOBUF_FIRST go to "count" buffer from first buffer
1257 * start == DOBUF_LAST go to "count" buffer from last buffer
1258 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1259 *
1260 * Return FAIL or OK.
1261 */
1262 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001263do_buffer(
1264 int action,
1265 int start,
1266 int dir, /* FORWARD or BACKWARD */
1267 int count, /* buffer number or number of buffers */
1268 int forceit) /* TRUE for :...! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269{
1270 buf_T *buf;
1271 buf_T *bp;
1272 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1273 || action == DOBUF_WIPE);
1274
1275 switch (start)
1276 {
1277 case DOBUF_FIRST: buf = firstbuf; break;
1278 case DOBUF_LAST: buf = lastbuf; break;
1279 default: buf = curbuf; break;
1280 }
1281 if (start == DOBUF_MOD) /* find next modified buffer */
1282 {
1283 while (count-- > 0)
1284 {
1285 do
1286 {
1287 buf = buf->b_next;
1288 if (buf == NULL)
1289 buf = firstbuf;
1290 }
1291 while (buf != curbuf && !bufIsChanged(buf));
1292 }
1293 if (!bufIsChanged(buf))
1294 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001295 emsg(_("E84: No modified buffer found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 return FAIL;
1297 }
1298 }
1299 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
1300 {
1301 while (buf != NULL && buf->b_fnum != count)
1302 buf = buf->b_next;
1303 }
1304 else
1305 {
1306 bp = NULL;
1307 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1308 {
1309 /* remember the buffer where we start, we come back there when all
1310 * buffers are unlisted. */
1311 if (bp == NULL)
1312 bp = buf;
1313 if (dir == FORWARD)
1314 {
1315 buf = buf->b_next;
1316 if (buf == NULL)
1317 buf = firstbuf;
1318 }
1319 else
1320 {
1321 buf = buf->b_prev;
1322 if (buf == NULL)
1323 buf = lastbuf;
1324 }
1325 /* don't count unlisted buffers */
1326 if (unload || buf->b_p_bl)
1327 {
1328 --count;
1329 bp = NULL; /* use this buffer as new starting point */
1330 }
1331 if (bp == buf)
1332 {
1333 /* back where we started, didn't find anything. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001334 emsg(_("E85: There is no listed buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 return FAIL;
1336 }
1337 }
1338 }
1339
1340 if (buf == NULL) /* could not find it */
1341 {
1342 if (start == DOBUF_FIRST)
1343 {
1344 /* don't warn when deleting */
1345 if (!unload)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001346 semsg(_(e_nobufnr), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 }
1348 else if (dir == FORWARD)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001349 emsg(_("E87: Cannot go beyond last buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001351 emsg(_("E88: Cannot go before first buffer"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 return FAIL;
1353 }
1354
1355#ifdef FEAT_GUI
1356 need_mouse_correct = TRUE;
1357#endif
1358
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 /*
1360 * delete buffer buf from memory and/or the list
1361 */
1362 if (unload)
1363 {
1364 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001365 bufref_T bufref;
1366
Bram Moolenaar94f01952018-09-01 15:30:03 +02001367 if (!can_unload_buffer(buf))
Bram Moolenaara997b452018-04-17 23:24:06 +02001368 return FAIL;
Bram Moolenaara997b452018-04-17 23:24:06 +02001369
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001370 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371
1372 /* When unloading or deleting a buffer that's already unloaded and
1373 * unlisted: fail silently. */
1374 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1375 return FAIL;
1376
1377 if (!forceit && bufIsChanged(buf))
1378 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001379#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 if ((p_confirm || cmdmod.confirm) && p_write)
1381 {
1382 dialog_changed(buf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001383 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 /* Autocommand deleted buffer, oops! It's not changed
1385 * now. */
1386 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001387 /* If it's still changed fail silently, the dialog already
1388 * mentioned why it fails. */
1389 if (bufIsChanged(buf))
1390 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001392 else
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001393#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001395 semsg(_("E89: No write since last change for buffer %d (add ! to override)"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 buf->b_fnum);
1397 return FAIL;
1398 }
1399 }
1400
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001401 /* When closing the current buffer stop Visual mode. */
Bram Moolenaar4930a762016-09-11 14:39:53 +02001402 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001403 end_visual_mode();
1404
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 /*
1406 * If deleting the last (listed) buffer, make it empty.
1407 * The last (listed) buffer cannot be unloaded.
1408 */
Bram Moolenaar29323592016-07-24 22:04:11 +02001409 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 if (bp->b_p_bl && bp != buf)
1411 break;
1412 if (bp == NULL && buf == curbuf)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001413 return empty_curbuf(TRUE, forceit, action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 /*
1416 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001417 * (unless it's the only window). Repeat this so long as we end up in
1418 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001420 while (buf == curbuf
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001421 && !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
Bram Moolenaar459ca562016-11-10 18:16:33 +01001422 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001423 {
1424 if (win_close(curwin, FALSE) == FAIL)
1425 break;
1426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427
1428 /*
1429 * If the buffer to be deleted is not the current one, delete it here.
1430 */
1431 if (buf != curbuf)
1432 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001433 close_windows(buf, FALSE);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001434 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows <= 0)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001435 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 return OK;
1437 }
1438
1439 /*
1440 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001441 * There should be another, otherwise it would have been handled
1442 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001443 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 * Then prefer the buffer we most recently visited.
1445 * Else try to find one that is loaded, after the current buffer,
1446 * then before the current buffer.
1447 * Finally use any buffer.
1448 */
1449 buf = NULL; /* selected buffer */
1450 bp = NULL; /* used when no loaded buffer found */
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001451 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1452 buf = au_new_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453#ifdef FEAT_JUMPLIST
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001454 else if (curwin->w_jumplistlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 {
1456 int jumpidx;
1457
1458 jumpidx = curwin->w_jumplistidx - 1;
1459 if (jumpidx < 0)
1460 jumpidx = curwin->w_jumplistlen - 1;
1461
1462 forward = jumpidx;
1463 while (jumpidx != curwin->w_jumplistidx)
1464 {
1465 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1466 if (buf != NULL)
1467 {
1468 if (buf == curbuf || !buf->b_p_bl)
1469 buf = NULL; /* skip current and unlisted bufs */
1470 else if (buf->b_ml.ml_mfp == NULL)
1471 {
1472 /* skip unloaded buf, but may keep it for later */
1473 if (bp == NULL)
1474 bp = buf;
1475 buf = NULL;
1476 }
1477 }
1478 if (buf != NULL) /* found a valid buffer: stop searching */
1479 break;
1480 /* advance to older entry in jump list */
1481 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1482 break;
1483 if (--jumpidx < 0)
1484 jumpidx = curwin->w_jumplistlen - 1;
1485 if (jumpidx == forward) /* List exhausted for sure */
1486 break;
1487 }
1488 }
1489#endif
1490
1491 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1492 {
1493 forward = TRUE;
1494 buf = curbuf->b_next;
1495 for (;;)
1496 {
1497 if (buf == NULL)
1498 {
1499 if (!forward) /* tried both directions */
1500 break;
1501 buf = curbuf->b_prev;
1502 forward = FALSE;
1503 continue;
1504 }
1505 /* in non-help buffer, try to skip help buffers, and vv */
1506 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1507 {
1508 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1509 break;
1510 if (bp == NULL) /* remember unloaded buf for later */
1511 bp = buf;
1512 }
1513 if (forward)
1514 buf = buf->b_next;
1515 else
1516 buf = buf->b_prev;
1517 }
1518 }
1519 if (buf == NULL) /* No loaded buffer, use unloaded one */
1520 buf = bp;
1521 if (buf == NULL) /* No loaded buffer, find listed one */
1522 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001523 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 if (buf->b_p_bl && buf != curbuf)
1525 break;
1526 }
1527 if (buf == NULL) /* Still no buffer, just take one */
1528 {
1529 if (curbuf->b_next != NULL)
1530 buf = curbuf->b_next;
1531 else
1532 buf = curbuf->b_prev;
1533 }
1534 }
1535
Bram Moolenaara02471e2014-01-10 16:43:14 +01001536 if (buf == NULL)
1537 {
1538 /* Autocommands must have wiped out all other buffers. Only option
1539 * now is to make the current buffer empty. */
1540 return empty_curbuf(FALSE, forceit, action);
1541 }
1542
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 /*
1544 * make buf current buffer
1545 */
1546 if (action == DOBUF_SPLIT) /* split window first */
1547 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001548 /* If 'switchbuf' contains "useopen": jump to first window containing
1549 * "buf" if one exists */
1550 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001551 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001552 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001553 * page containing "buf" if one exists */
1554 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 return OK;
1556 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 return FAIL;
1558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559
1560 /* go to current buffer - nothing to do */
1561 if (buf == curbuf)
1562 return OK;
1563
1564 /*
1565 * Check if the current buffer may be abandoned.
1566 */
1567 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1568 {
1569#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1570 if ((p_confirm || cmdmod.confirm) && p_write)
1571 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001572 bufref_T bufref;
1573
1574 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 dialog_changed(curbuf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001576 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 /* Autocommand deleted buffer, oops! */
1578 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 }
1580 if (bufIsChanged(curbuf))
1581#endif
1582 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001583 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 return FAIL;
1585 }
1586 }
1587
1588 /* Go to the other buffer. */
1589 set_curbuf(buf, action);
1590
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001592 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001594#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 if (aborting()) /* autocmds may abort script processing */
1596 return FAIL;
1597#endif
1598
1599 return OK;
1600}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601
1602/*
1603 * Set current buffer to "buf". Executes autocommands and closes current
1604 * buffer. "action" tells how to close the current buffer:
1605 * DOBUF_GOTO free or hide it
1606 * DOBUF_SPLIT nothing
1607 * DOBUF_UNLOAD unload it
1608 * DOBUF_DEL delete it
1609 * DOBUF_WIPE wipe it out
1610 */
1611 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001612set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613{
1614 buf_T *prevbuf;
1615 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1616 || action == DOBUF_WIPE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001617#ifdef FEAT_SYN_HL
1618 long old_tw = curbuf->b_p_tw;
1619#endif
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001620 bufref_T newbufref;
1621 bufref_T prevbufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622
1623 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001624 if (!cmdmod.keepalt)
1625 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001626 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 /* Don't restart Select mode after switching to another buffer. */
1629 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001631 /* close_windows() or apply_autocmds() may change curbuf and wipe out "buf"
1632 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 prevbuf = curbuf;
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001634 set_bufref(&prevbufref, prevbuf);
1635 set_bufref(&newbufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001637 /* Autocommands may delete the curren buffer and/or the buffer we wan to go
1638 * to. In those cases don't close the buffer. */
Bram Moolenaar82404332016-07-10 17:00:38 +02001639 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001640 || (bufref_valid(&prevbufref)
1641 && bufref_valid(&newbufref)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001642#ifdef FEAT_EVAL
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001643 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001645 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001647#ifdef FEAT_SYN_HL
1648 if (prevbuf == curwin->w_buffer)
1649 reset_synblock(curwin);
1650#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001652 close_windows(prevbuf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001653#if defined(FEAT_EVAL)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001654 if (bufref_valid(&prevbufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655#else
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001656 if (bufref_valid(&prevbufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001658 {
Bram Moolenaare25865a2012-07-06 16:22:02 +02001659 win_T *previouswin = curwin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001660 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001661 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1663 unload ? action : (action == DOBUF_GOTO
Bram Moolenaareb44a682017-08-03 22:44:55 +02001664 && !buf_hide(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001665 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaare25865a2012-07-06 16:22:02 +02001666 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaar9e931222012-06-20 11:55:01 +02001667 /* autocommands changed curwin, Grr! */
Bram Moolenaare25865a2012-07-06 16:22:02 +02001668 curwin = previouswin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 }
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001671 /* An autocommand may have deleted "buf", already entered it (e.g., when
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001672 * it did ":bunload") or aborted the script processing.
Bram Moolenaar9e931222012-06-20 11:55:01 +02001673 * If curwin->w_buffer is null, enter_buffer() will make it valid again */
1674 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001675#ifdef FEAT_EVAL
Bram Moolenaar4033c552017-09-16 20:54:51 +02001676 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001678 ) || curwin->w_buffer == NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001679 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001681#ifdef FEAT_SYN_HL
1682 if (old_tw != curbuf->b_p_tw)
1683 check_colorcolumn(curwin);
1684#endif
1685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686}
1687
1688/*
1689 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001690 * Old curbuf must have been abandoned already! This also means "curbuf" may
1691 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 */
1693 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001694enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695{
1696 /* Copy buffer and window local option values. Not for a help buffer. */
1697 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1698 if (!buf->b_help)
1699 get_winopts(buf);
1700#ifdef FEAT_FOLDING
1701 else
1702 /* Remove all folds in the window. */
1703 clearFolding(curwin);
1704 foldUpdateAll(curwin); /* update folds (later). */
1705#endif
1706
1707 /* Get the buffer in the current window. */
1708 curwin->w_buffer = buf;
1709 curbuf = buf;
1710 ++curbuf->b_nwindows;
1711
1712#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001713 if (curwin->w_p_diff)
1714 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715#endif
1716
Bram Moolenaara971b822011-09-14 14:43:25 +02001717#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001718 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001719#endif
1720
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 /* Cursor on first line by default. */
1722 curwin->w_cursor.lnum = 1;
1723 curwin->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001726 curwin->w_topline_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001728 /* mark cursor position as being invalid */
1729 curwin->w_valid = 0;
1730
Bram Moolenaar9cea87c2018-09-21 16:59:45 +02001731 buflist_setfpos(curbuf, curwin, curbuf->b_last_cursor.lnum,
1732 curbuf->b_last_cursor.col, TRUE);
1733
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 /* Make sure the buffer is loaded. */
1735 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001736 {
1737 /* If there is no filetype, allow for detecting one. Esp. useful for
1738 * ":ball" used in a autocommand. If there already is a filetype we
1739 * might prefer to keep it. */
1740 if (*curbuf->b_p_ft == NUL)
1741 did_filetype = FALSE;
1742
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001743 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 else
1746 {
Bram Moolenaareda65222019-05-16 20:29:44 +02001747 if (!msg_silent && !shortmess(SHM_FILEINFO))
1748 need_fileinfo = TRUE; // display file info after redraw
1749
1750 // check if file changed
1751 (void)buf_check_timestamp(curbuf, FALSE);
1752
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001754#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001756#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1758 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 }
1760
1761 /* If autocommands did not change the cursor position, restore cursor lnum
1762 * and possibly cursor col. */
1763 if (curwin->w_cursor.lnum == 1 && inindent(0))
1764 buflist_getfpos();
1765
1766 check_arg_idx(curwin); /* check for valid arg_idx */
1767#ifdef FEAT_TITLE
1768 maketitle();
1769#endif
Bram Moolenaard4153d42008-11-15 15:06:17 +00001770 /* when autocmds didn't change it */
1771 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1773
Bram Moolenaar009b2592004-10-24 19:18:58 +00001774#ifdef FEAT_NETBEANS_INTG
1775 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001776 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001777#endif
1778
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001779 /* Change directories when the 'acd' option is set. */
Bram Moolenaar6f470022018-04-10 18:47:20 +02001780 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781
1782#ifdef FEAT_KEYMAP
1783 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001784 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001786#ifdef FEAT_SPELL
1787 /* May need to set the spell language. Can only do this after the buffer
1788 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001789 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1790 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001791#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001792#ifdef FEAT_VIMINFO
1793 curbuf->b_last_used = vim_time();
1794#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001795
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 redraw_later(NOT_VALID);
1797}
1798
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001799#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1800/*
1801 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001802 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001803 */
1804 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001805do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001806{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001807 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001808 && curbuf->b_ffname != NULL
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001809 && vim_chdirfile(curbuf->b_ffname, "auto") == OK)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001810 shorten_fnames(TRUE);
1811}
1812#endif
1813
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001814 void
1815no_write_message(void)
1816{
1817#ifdef FEAT_TERMINAL
1818 if (term_job_running(curbuf->b_term))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001819 emsg(_("E948: Job still running (add ! to end the job)"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001820 else
1821#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001822 emsg(_("E37: No write since last change (add ! to override)"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001823}
1824
1825 void
Bram Moolenaar7a760922018-02-19 23:10:02 +01001826no_write_message_nobang(buf_T *buf UNUSED)
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001827{
1828#ifdef FEAT_TERMINAL
Bram Moolenaar7a760922018-02-19 23:10:02 +01001829 if (term_job_running(buf->b_term))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001830 emsg(_("E948: Job still running"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001831 else
1832#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001833 emsg(_("E37: No write since last change"));
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001834}
1835
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836/*
1837 * functions for dealing with the buffer list
1838 */
1839
Bram Moolenaar480778b2016-07-14 22:09:39 +02001840static int top_file_num = 1; /* highest file number */
1841
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842/*
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001843 * Return TRUE if the current buffer is empty, unnamed, unmodified and used in
1844 * only one window. That means it can be re-used.
1845 */
1846 int
1847curbuf_reusable(void)
1848{
1849 return (curbuf != NULL
1850 && curbuf->b_ffname == NULL
1851 && curbuf->b_nwindows <= 1
1852 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY())
Bram Moolenaard85c3962019-04-07 14:19:14 +02001853#if defined(FEAT_QUICKFIX)
Bram Moolenaar39803d82019-04-07 12:04:51 +02001854 && !bt_quickfix(curbuf)
Bram Moolenaard85c3962019-04-07 14:19:14 +02001855#endif
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001856 && !curbufIsChanged());
1857}
1858
1859/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 * Add a file name to the buffer list. Return a pointer to the buffer.
1861 * If the same file name already exists return a pointer to that buffer.
1862 * If it does not exist, or if fname == NULL, a new entry is created.
1863 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1864 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1865 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001866 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001867 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
1868 * if the buffer already exists.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 * This is the ONLY way to create a new buffer.
1870 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001872buflist_new(
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001873 char_u *ffname_arg, // full path of fname or relative
1874 char_u *sfname_arg, // short fname or NULL
1875 linenr_T lnum, // preferred cursor line
1876 int flags) // BLN_ defines
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001878 char_u *ffname = ffname_arg;
1879 char_u *sfname = sfname_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 buf_T *buf;
1881#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001882 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883#endif
1884
Bram Moolenaar480778b2016-07-14 22:09:39 +02001885 if (top_file_num == 1)
1886 hash_init(&buf_hashtab);
1887
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001888 fname_expand(curbuf, &ffname, &sfname); // will allocate ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889
1890 /*
1891 * If file name already exists in the list, update the entry.
1892 */
1893#ifdef UNIX
1894 /* On Unix we can use inode numbers when the file exists. Works better
1895 * for hard links. */
1896 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1897 st.st_dev = (dev_T)-1;
1898#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001899 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900#ifdef UNIX
1901 buflist_findname_stat(ffname, &st)
1902#else
1903 buflist_findname(ffname)
1904#endif
1905 ) != NULL)
1906 {
1907 vim_free(ffname);
1908 if (lnum != 0)
1909 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02001910
1911 if ((flags & BLN_NOOPT) == 0)
1912 /* copy the options now, if 'cpo' doesn't have 's' and not done
1913 * already */
1914 buf_copy_options(buf, 0);
1915
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1917 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001918 bufref_T bufref;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001919
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 buf->b_p_bl = TRUE;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001921 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001923 {
Bram Moolenaar82404332016-07-10 17:00:38 +02001924 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001925 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001926 return NULL;
1927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 }
1929 return buf;
1930 }
1931
1932 /*
1933 * If the current buffer has no name and no contents, use the current
1934 * buffer. Otherwise: Need to allocate a new buffer structure.
1935 *
1936 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001937 * (A spell file buffer is allocated in spell.c, but that's not a normal
1938 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 */
1940 buf = NULL;
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001941 if ((flags & BLN_CURBUF) && curbuf_reusable())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 {
1943 buf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 /* It's like this buffer is deleted. Watch out for autocommands that
1945 * change curbuf! If that happens, allocate a new buffer anyway. */
1946 if (curbuf->b_p_bl)
1947 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1948 if (buf == curbuf)
1949 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001950#ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001951 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 if (buf == curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 {
1956 /* Make sure 'bufhidden' and 'buftype' are empty */
1957 clear_string_option(&buf->b_p_bh);
1958 clear_string_option(&buf->b_p_bt);
1959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 }
1961 if (buf != curbuf || curbuf == NULL)
1962 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001963 buf = ALLOC_CLEAR_ONE(buf_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 if (buf == NULL)
1965 {
1966 vim_free(ffname);
1967 return NULL;
1968 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02001969#ifdef FEAT_EVAL
1970 /* init b: variables */
1971 buf->b_vars = dict_alloc();
1972 if (buf->b_vars == NULL)
1973 {
1974 vim_free(ffname);
1975 vim_free(buf);
1976 return NULL;
1977 }
1978 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
1979#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01001980 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 }
1982
1983 if (ffname != NULL)
1984 {
1985 buf->b_ffname = ffname;
1986 buf->b_sfname = vim_strsave(sfname);
1987 }
1988
1989 clear_wininfo(buf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001990 buf->b_wininfo = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991
1992 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1993 || buf->b_wininfo == NULL)
1994 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02001995 if (buf->b_sfname != buf->b_ffname)
1996 VIM_CLEAR(buf->b_sfname);
1997 else
1998 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01001999 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 if (buf != curbuf)
2001 free_buffer(buf);
2002 return NULL;
2003 }
2004
2005 if (buf == curbuf)
2006 {
2007 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002008 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 if (buf != curbuf) /* autocommands deleted the buffer! */
2010 return NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002011#if defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002012 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 return NULL;
2014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002016
2017 /* Init the options. */
2018 buf->b_p_initialized = FALSE;
2019 buf_copy_options(buf, BCO_ENTER);
2020
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021#ifdef FEAT_KEYMAP
2022 /* need to reload lmaps and set b:keymap_name */
2023 curbuf->b_kmap_state |= KEYMAP_INIT;
2024#endif
2025 }
2026 else
2027 {
2028 /*
2029 * put new buffer at the end of the buffer list
2030 */
2031 buf->b_next = NULL;
2032 if (firstbuf == NULL) /* buffer list is empty */
2033 {
2034 buf->b_prev = NULL;
2035 firstbuf = buf;
2036 }
2037 else /* append new buffer at end of list */
2038 {
2039 lastbuf->b_next = buf;
2040 buf->b_prev = lastbuf;
2041 }
2042 lastbuf = buf;
2043
2044 buf->b_fnum = top_file_num++;
2045 if (top_file_num < 0) /* wrap around (may cause duplicates) */
2046 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002047 emsg(_("W14: Warning: List of file names overflow"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 if (emsg_silent == 0)
2049 {
2050 out_flush();
2051 ui_delay(3000L, TRUE); /* make sure it is noticed */
2052 }
2053 top_file_num = 1;
2054 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002055 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056
2057 /*
2058 * Always copy the options from the current buffer.
2059 */
2060 buf_copy_options(buf, BCO_ALWAYS);
2061 }
2062
2063 buf->b_wininfo->wi_fpos.lnum = lnum;
2064 buf->b_wininfo->wi_win = curwin;
2065
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002066#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002067 hash_init(&buf->b_s.b_keywtab);
2068 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069#endif
2070
2071 buf->b_fname = buf->b_sfname;
2072#ifdef UNIX
2073 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002074 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 else
2076 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002077 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 buf->b_dev = st.st_dev;
2079 buf->b_ino = st.st_ino;
2080 }
2081#endif
2082 buf->b_u_synced = TRUE;
2083 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002084 if (flags & BLN_DUMMY)
2085 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 buf_clear_file(buf);
2087 clrallmarks(buf); /* clear marks */
2088 fmarks_check_names(buf); /* check file marks for this file */
2089 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 if (!(flags & BLN_DUMMY))
2091 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002092 bufref_T bufref;
2093
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01002094 /* Tricky: these autocommands may change the buffer list. They could
2095 * also split the window with re-using the one empty buffer. This may
2096 * result in unexpectedly losing the empty buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002097 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002098 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002099 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002100 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002102 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002103 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002104 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002105 return NULL;
2106 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002107#ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002108 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112
2113 return buf;
2114}
2115
2116/*
2117 * Free the memory for the options of a buffer.
2118 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2119 * 'fileencoding'.
2120 */
2121 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002122free_buf_options(
2123 buf_T *buf,
2124 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125{
2126 if (free_p_ff)
2127 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 clear_string_option(&buf->b_p_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 clear_string_option(&buf->b_p_bh);
2131 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 }
2133#ifdef FEAT_FIND_ID
2134 clear_string_option(&buf->b_p_def);
2135 clear_string_option(&buf->b_p_inc);
2136# ifdef FEAT_EVAL
2137 clear_string_option(&buf->b_p_inex);
2138# endif
2139#endif
2140#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
2141 clear_string_option(&buf->b_p_inde);
2142 clear_string_option(&buf->b_p_indk);
2143#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002144#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2145 clear_string_option(&buf->b_p_bexpr);
2146#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002147#if defined(FEAT_CRYPT)
2148 clear_string_option(&buf->b_p_cm);
2149#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002150 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002151#if defined(FEAT_EVAL)
2152 clear_string_option(&buf->b_p_fex);
2153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154#ifdef FEAT_CRYPT
2155 clear_string_option(&buf->b_p_key);
2156#endif
2157 clear_string_option(&buf->b_p_kp);
2158 clear_string_option(&buf->b_p_mps);
2159 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002160 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 clear_string_option(&buf->b_p_isk);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002162#ifdef FEAT_VARTABS
2163 clear_string_option(&buf->b_p_vsts);
Bram Moolenaarbdace832019-03-02 10:13:42 +01002164 vim_free(buf->b_p_vsts_nopaste);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002165 buf->b_p_vsts_nopaste = NULL;
Bram Moolenaarbdace832019-03-02 10:13:42 +01002166 vim_free(buf->b_p_vsts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002167 buf->b_p_vsts_array = NULL;
2168 clear_string_option(&buf->b_p_vts);
Bram Moolenaar55c77cf2019-02-16 19:05:11 +01002169 VIM_CLEAR(buf->b_p_vts_array);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002170#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171#ifdef FEAT_KEYMAP
2172 clear_string_option(&buf->b_p_keymap);
Bram Moolenaar50138322018-01-28 17:05:16 +01002173 keymap_clear(&buf->b_kmap_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 ga_clear(&buf->b_kmap_ga);
2175#endif
2176#ifdef FEAT_COMMENTS
2177 clear_string_option(&buf->b_p_com);
2178#endif
2179#ifdef FEAT_FOLDING
2180 clear_string_option(&buf->b_p_cms);
2181#endif
2182 clear_string_option(&buf->b_p_nf);
2183#ifdef FEAT_SYN_HL
2184 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002185 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002186#endif
2187#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002188 clear_string_option(&buf->b_s.b_p_spc);
2189 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002190 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002191 buf->b_s.b_cap_prog = NULL;
2192 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193#endif
2194#ifdef FEAT_SEARCHPATH
2195 clear_string_option(&buf->b_p_sua);
2196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 clear_string_option(&buf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198#ifdef FEAT_CINDENT
2199 clear_string_option(&buf->b_p_cink);
2200 clear_string_option(&buf->b_p_cino);
2201#endif
2202#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
2203 clear_string_option(&buf->b_p_cinw);
2204#endif
2205#ifdef FEAT_INS_EXPAND
2206 clear_string_option(&buf->b_p_cpt);
2207#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002208#ifdef FEAT_COMPL_FUNC
2209 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002210 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212#ifdef FEAT_QUICKFIX
2213 clear_string_option(&buf->b_p_gp);
2214 clear_string_option(&buf->b_p_mp);
2215 clear_string_option(&buf->b_p_efm);
2216#endif
2217 clear_string_option(&buf->b_p_ep);
2218 clear_string_option(&buf->b_p_path);
2219 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002220 clear_string_option(&buf->b_p_tc);
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02002221#ifdef FEAT_EVAL
2222 clear_string_option(&buf->b_p_tfu);
2223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224#ifdef FEAT_INS_EXPAND
2225 clear_string_option(&buf->b_p_dict);
2226 clear_string_option(&buf->b_p_tsr);
2227#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002228#ifdef FEAT_TEXTOBJ
2229 clear_string_option(&buf->b_p_qe);
2230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002232 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002233#ifdef FEAT_LISP
2234 clear_string_option(&buf->b_p_lw);
2235#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002236 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002237 clear_string_option(&buf->b_p_menc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238}
2239
2240/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002241 * Get alternate file "n".
2242 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2243 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 * if (options & GETF_SETMARK) call setpcmark()
2245 * if (options & GETF_ALT) we are jumping to an alternate file.
2246 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2247 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002248 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 */
2250 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002251buflist_getfile(
2252 int n,
2253 linenr_T lnum,
2254 int options,
2255 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256{
2257 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 pos_T *fpos;
2260 colnr_T col;
2261
2262 buf = buflist_findnr(n);
2263 if (buf == NULL)
2264 {
2265 if ((options & GETF_ALT) && n == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002266 emsg(_(e_noalt));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 else
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01002268 semsg(_("E92: Buffer %d not found"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 return FAIL;
2270 }
2271
2272 /* if alternate file is the current buffer, nothing to do */
2273 if (buf == curbuf)
2274 return OK;
2275
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002276 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002277 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002278 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002280 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002281 if (curbuf_locked())
2282 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283
2284 /* altfpos may be changed by getfile(), get it now */
2285 if (lnum == 0)
2286 {
2287 fpos = buflist_findfpos(buf);
2288 lnum = fpos->lnum;
2289 col = fpos->col;
2290 }
2291 else
2292 col = 0;
2293
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 if (options & GETF_SWITCH)
2295 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002296 /* If 'switchbuf' contains "useopen": jump to first window containing
2297 * "buf" if one exists */
2298 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002300
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002301 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00002302 * page containing "buf" if one exists */
2303 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002304 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002305
2306 /* If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2307 * current buffer isn't empty: open new tab or window */
2308 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002309 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002311 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002312 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002313 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2314 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002316 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 }
2318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319
2320 ++RedrawingDisabled;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002321 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2322 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 {
2324 --RedrawingDisabled;
2325
2326 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
2327 if (!p_sol && col != 0)
2328 {
2329 curwin->w_cursor.col = col;
2330 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 curwin->w_set_curswant = TRUE;
2333 }
2334 return OK;
2335 }
2336 --RedrawingDisabled;
2337 return FAIL;
2338}
2339
2340/*
2341 * go to the last know line number for the current buffer
2342 */
2343 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002344buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345{
2346 pos_T *fpos;
2347
2348 fpos = buflist_findfpos(curbuf);
2349
2350 curwin->w_cursor.lnum = fpos->lnum;
2351 check_cursor_lnum();
2352
2353 if (p_sol)
2354 curwin->w_cursor.col = 0;
2355 else
2356 {
2357 curwin->w_cursor.col = fpos->col;
2358 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 curwin->w_set_curswant = TRUE;
2361 }
2362}
2363
Bram Moolenaar81695252004-12-29 20:58:21 +00002364#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2365/*
2366 * Find file in buffer list by name (it has to be for the current window).
2367 * Returns NULL if not found.
2368 */
2369 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002370buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002371{
2372 char_u *ffname;
2373 buf_T *buf = NULL;
2374
2375 /* First make the name into a full path name */
2376 ffname = FullName_save(fname,
2377#ifdef UNIX
2378 TRUE /* force expansion, get rid of symbolic links */
2379#else
2380 FALSE
2381#endif
2382 );
2383 if (ffname != NULL)
2384 {
2385 buf = buflist_findname(ffname);
2386 vim_free(ffname);
2387 }
2388 return buf;
2389}
2390#endif
2391
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392/*
2393 * Find file in buffer list by name (it has to be for the current window).
2394 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002395 * Skips dummy buffers.
2396 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 */
2398 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002399buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400{
2401#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002402 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403
2404 if (mch_stat((char *)ffname, &st) < 0)
2405 st.st_dev = (dev_T)-1;
2406 return buflist_findname_stat(ffname, &st);
2407}
2408
2409/*
2410 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2411 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002412 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 */
2414 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002415buflist_findname_stat(
2416 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002417 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418{
2419#endif
2420 buf_T *buf;
2421
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002422 /* Start at the last buffer, expect to find a match sooner. */
2423 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar81695252004-12-29 20:58:21 +00002424 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425#ifdef UNIX
2426 , stp
2427#endif
2428 ))
2429 return buf;
2430 return NULL;
2431}
2432
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433/*
2434 * Find file in buffer list by a regexp pattern.
2435 * Return fnum of the found buffer.
2436 * Return < 0 for error.
2437 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002439buflist_findpat(
2440 char_u *pattern,
2441 char_u *pattern_end, /* pointer to first char after pattern */
2442 int unlisted, /* find unlisted buffers */
2443 int diffmode UNUSED, /* find diff-mode buffers only */
2444 int curtab_only) /* find buffers in current tab only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445{
2446 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 int match = -1;
2448 int find_listed;
2449 char_u *pat;
2450 char_u *patend;
2451 int attempt;
2452 char_u *p;
2453 int toggledollar;
2454
2455 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2456 {
2457 if (*pattern == '%')
2458 match = curbuf->b_fnum;
2459 else
2460 match = curwin->w_alt_fnum;
2461#ifdef FEAT_DIFF
2462 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2463 match = -1;
2464#endif
2465 }
2466
2467 /*
2468 * Try four ways of matching a listed buffer:
2469 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002470 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 * attempt == 2: with '$' at end (only match at end)
2472 * attempt == 3: with '^' at start and '$' at end (only full match)
2473 * Repeat this for finding an unlisted buffer if there was no matching
2474 * listed buffer.
2475 */
2476 else
2477 {
2478 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2479 if (pat == NULL)
2480 return -1;
2481 patend = pat + STRLEN(pat) - 1;
2482 toggledollar = (patend > pat && *patend == '$');
2483
2484 /* First try finding a listed buffer. If not found and "unlisted"
2485 * is TRUE, try finding an unlisted buffer. */
2486 find_listed = TRUE;
2487 for (;;)
2488 {
2489 for (attempt = 0; attempt <= 3; ++attempt)
2490 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002491 regmatch_T regmatch;
2492
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 /* may add '^' and '$' */
2494 if (toggledollar)
2495 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2496 p = pat;
2497 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2498 ++p;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002499 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2500 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 {
2502 vim_free(pat);
2503 return -1;
2504 }
2505
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002506 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 if (buf->b_p_bl == find_listed
2508#ifdef FEAT_DIFF
2509 && (!diffmode || diff_mode_buf(buf))
2510#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002511 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002513 if (curtab_only)
2514 {
2515 /* Ignore the match if the buffer is not open in
2516 * the current tab. */
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002517 win_T *wp;
2518
Bram Moolenaar29323592016-07-24 22:04:11 +02002519 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002520 if (wp->w_buffer == buf)
2521 break;
2522 if (wp == NULL)
2523 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 if (match >= 0) /* already found a match */
2526 {
2527 match = -2;
2528 break;
2529 }
2530 match = buf->b_fnum; /* remember first match */
2531 }
2532
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002533 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 if (match >= 0) /* found one match */
2535 break;
2536 }
2537
2538 /* Only search for unlisted buffers if there was no match with
2539 * a listed buffer. */
2540 if (!unlisted || !find_listed || match != -1)
2541 break;
2542 find_listed = FALSE;
2543 }
2544
2545 vim_free(pat);
2546 }
2547
2548 if (match == -2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002549 semsg(_("E93: More than one match for %s"), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 else if (match < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002551 semsg(_("E94: No matching buffer for %s"), pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 return match;
2553}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554
2555#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2556
2557/*
2558 * Find all buffer names that match.
2559 * For command line expansion of ":buf" and ":sbuf".
2560 * Return OK if matches found, FAIL otherwise.
2561 */
2562 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002563ExpandBufnames(
2564 char_u *pat,
2565 int *num_file,
2566 char_u ***file,
2567 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568{
2569 int count = 0;
2570 buf_T *buf;
2571 int round;
2572 char_u *p;
2573 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002574 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575
2576 *num_file = 0; /* return values in case of FAIL */
2577 *file = NULL;
2578
Bram Moolenaar05159a02005-02-26 23:04:13 +00002579 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2580 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002582 patc = alloc(STRLEN(pat) + 11);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002583 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002585 STRCPY(patc, "\\(^\\|[\\/]\\)");
2586 STRCPY(patc + 11, pat + 1);
2587 }
2588 else
2589 patc = pat;
2590
2591 /*
2592 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002593 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002594 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002595 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002596 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002597 regmatch_T regmatch;
2598
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002599 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002600 break; /* there was no anchor, no need to try again */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002601 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2602 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002603 {
2604 if (patc != pat)
2605 vim_free(patc);
2606 return FAIL;
2607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608
2609 /*
2610 * round == 1: Count the matches.
2611 * round == 2: Build the array to keep the matches.
2612 */
2613 for (round = 1; round <= 2; ++round)
2614 {
2615 count = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002616 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 {
2618 if (!buf->b_p_bl) /* skip unlisted buffers */
2619 continue;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002620 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 if (p != NULL)
2622 {
2623 if (round == 1)
2624 ++count;
2625 else
2626 {
2627 if (options & WILD_HOME_REPLACE)
2628 p = home_replace_save(buf, p);
2629 else
2630 p = vim_strsave(p);
2631 (*file)[count++] = p;
2632 }
2633 }
2634 }
2635 if (count == 0) /* no match found, break here */
2636 break;
2637 if (round == 1)
2638 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002639 *file = ALLOC_MULT(char_u *, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 if (*file == NULL)
2641 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002642 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002643 if (patc != pat)
2644 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 return FAIL;
2646 }
2647 }
2648 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002649 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 if (count) /* match(es) found, break here */
2651 break;
2652 }
2653
Bram Moolenaar05159a02005-02-26 23:04:13 +00002654 if (patc != pat)
2655 vim_free(patc);
2656
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 *num_file = count;
2658 return (count == 0 ? FAIL : OK);
2659}
2660
2661#endif /* FEAT_CMDL_COMPL */
2662
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663/*
2664 * Check for a match on the file name for buffer "buf" with regprog "prog".
2665 */
2666 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002667buflist_match(
2668 regmatch_T *rmp,
2669 buf_T *buf,
2670 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671{
2672 char_u *match;
2673
2674 /* First try the short file name, then the long file name. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002675 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002677 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678
2679 return match;
2680}
2681
2682/*
2683 * Try matching the regexp in "prog" with file name "name".
2684 * Return "name" when there is a match, NULL when not.
2685 */
2686 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002687fname_match(
2688 regmatch_T *rmp,
2689 char_u *name,
2690 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691{
2692 char_u *match = NULL;
2693 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694
2695 if (name != NULL)
2696 {
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002697 /* Ignore case when 'fileignorecase' or the argument is set. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002698 rmp->rm_ic = p_fic || ignore_case;
2699 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 match = name;
2701 else
2702 {
2703 /* Replace $(HOME) with '~' and try matching again. */
2704 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002705 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 match = name;
2707 vim_free(p);
2708 }
2709 }
2710
2711 return match;
2712}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713
2714/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02002715 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 */
2717 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002718buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719{
Bram Moolenaar480778b2016-07-14 22:09:39 +02002720 char_u key[VIM_SIZEOF_INT * 2 + 1];
2721 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722
2723 if (nr == 0)
2724 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02002725 sprintf((char *)key, "%x", nr);
2726 hi = hash_find(&buf_hashtab, key);
2727
2728 if (!HASHITEM_EMPTY(hi))
2729 return (buf_T *)(hi->hi_key
2730 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 return NULL;
2732}
2733
2734/*
2735 * Get name of file 'n' in the buffer list.
2736 * When the file has no name an empty string is returned.
2737 * home_replace() is used to shorten the file name (used for marks).
2738 * Returns a pointer to allocated memory, of NULL when failed.
2739 */
2740 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002741buflist_nr2name(
2742 int n,
2743 int fullname,
2744 int helptail) /* for help buffers return tail only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745{
2746 buf_T *buf;
2747
2748 buf = buflist_findnr(n);
2749 if (buf == NULL)
2750 return NULL;
2751 return home_replace_save(helptail ? buf : NULL,
2752 fullname ? buf->b_ffname : buf->b_fname);
2753}
2754
2755/*
2756 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2757 * When "copy_options" is TRUE save the local window option values.
2758 * When "lnum" is 0 only do the options.
2759 */
2760 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002761buflist_setfpos(
2762 buf_T *buf,
2763 win_T *win,
2764 linenr_T lnum,
2765 colnr_T col,
2766 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767{
2768 wininfo_T *wip;
2769
2770 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2771 if (wip->wi_win == win)
2772 break;
2773 if (wip == NULL)
2774 {
2775 /* allocate a new entry */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002776 wip = ALLOC_CLEAR_ONE(wininfo_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 if (wip == NULL)
2778 return;
2779 wip->wi_win = win;
2780 if (lnum == 0) /* set lnum even when it's 0 */
2781 lnum = 1;
2782 }
2783 else
2784 {
2785 /* remove the entry from the list */
2786 if (wip->wi_prev)
2787 wip->wi_prev->wi_next = wip->wi_next;
2788 else
2789 buf->b_wininfo = wip->wi_next;
2790 if (wip->wi_next)
2791 wip->wi_next->wi_prev = wip->wi_prev;
2792 if (copy_options && wip->wi_optset)
2793 {
2794 clear_winopt(&wip->wi_opt);
2795#ifdef FEAT_FOLDING
2796 deleteFoldRecurse(&wip->wi_folds);
2797#endif
2798 }
2799 }
2800 if (lnum != 0)
2801 {
2802 wip->wi_fpos.lnum = lnum;
2803 wip->wi_fpos.col = col;
2804 }
2805 if (copy_options)
2806 {
2807 /* Save the window-specific option values. */
2808 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2809#ifdef FEAT_FOLDING
2810 wip->wi_fold_manual = win->w_fold_manual;
2811 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2812#endif
2813 wip->wi_optset = TRUE;
2814 }
2815
2816 /* insert the entry in front of the list */
2817 wip->wi_next = buf->b_wininfo;
2818 buf->b_wininfo = wip;
2819 wip->wi_prev = NULL;
2820 if (wip->wi_next)
2821 wip->wi_next->wi_prev = wip;
2822
2823 return;
2824}
2825
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002826#ifdef FEAT_DIFF
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002827/*
2828 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2829 * page. That's because a diff is local to a tab page.
2830 */
2831 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002832wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002833{
2834 win_T *wp;
2835
2836 if (wip->wi_opt.wo_diff)
2837 {
Bram Moolenaar29323592016-07-24 22:04:11 +02002838 FOR_ALL_WINDOWS(wp)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002839 /* return FALSE when it's a window in the current tab page, thus
2840 * the buffer was in diff mode here */
2841 if (wip->wi_win == wp)
2842 return FALSE;
2843 return TRUE;
2844 }
2845 return FALSE;
2846}
2847#endif
2848
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849/*
2850 * Find info for the current window in buffer "buf".
2851 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002852 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2853 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 * Returns NULL when there isn't any info.
2855 */
2856 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002857find_wininfo(
2858 buf_T *buf,
2859 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860{
2861 wininfo_T *wip;
2862
2863 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002864 if (wip->wi_win == curwin
2865#ifdef FEAT_DIFF
2866 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2867#endif
2868 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002870
2871 /* If no wininfo for curwin, use the first in the list (that doesn't have
2872 * 'diff' set and is in another tab page). */
2873 if (wip == NULL)
2874 {
2875#ifdef FEAT_DIFF
2876 if (skip_diff_buffer)
2877 {
2878 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2879 if (!wininfo_other_tab_diff(wip))
2880 break;
2881 }
2882 else
2883#endif
2884 wip = buf->b_wininfo;
2885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 return wip;
2887}
2888
2889/*
2890 * Reset the local window options to the values last used in this window.
2891 * If the buffer wasn't used in this window before, use the values from
2892 * the most recently used window. If the values were never set, use the
2893 * global values for the window.
2894 */
2895 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002896get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897{
2898 wininfo_T *wip;
2899
2900 clear_winopt(&curwin->w_onebuf_opt);
2901#ifdef FEAT_FOLDING
2902 clearFolding(curwin);
2903#endif
2904
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002905 wip = find_wininfo(buf, TRUE);
Bram Moolenaar25782a72018-05-13 18:05:33 +02002906 if (wip != NULL && wip->wi_win != NULL
2907 && wip->wi_win != curwin && wip->wi_win->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 {
Bram Moolenaar25782a72018-05-13 18:05:33 +02002909 /* The buffer is currently displayed in the window: use the actual
2910 * option values instead of the saved (possibly outdated) values. */
2911 win_T *wp = wip->wi_win;
2912
2913 copy_winopt(&wp->w_onebuf_opt, &curwin->w_onebuf_opt);
2914#ifdef FEAT_FOLDING
2915 curwin->w_fold_manual = wp->w_fold_manual;
2916 curwin->w_foldinvalid = TRUE;
2917 cloneFoldGrowArray(&wp->w_folds, &curwin->w_folds);
2918#endif
2919 }
2920 else if (wip != NULL && wip->wi_optset)
2921 {
2922 /* the buffer was displayed in the current window earlier */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2924#ifdef FEAT_FOLDING
2925 curwin->w_fold_manual = wip->wi_fold_manual;
2926 curwin->w_foldinvalid = TRUE;
2927 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2928#endif
2929 }
2930 else
2931 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2932
2933#ifdef FEAT_FOLDING
2934 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2935 if (p_fdls >= 0)
2936 curwin->w_p_fdl = p_fdls;
2937#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002938#ifdef FEAT_SYN_HL
2939 check_colorcolumn(curwin);
2940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941}
2942
2943/*
2944 * Find the position (lnum and col) for the buffer 'buf' for the current
2945 * window.
2946 * Returns a pointer to no_position if no position is found.
2947 */
2948 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002949buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950{
2951 wininfo_T *wip;
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002952 static pos_T no_position = {1, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002954 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 if (wip != NULL)
2956 return &(wip->wi_fpos);
2957 else
2958 return &no_position;
2959}
2960
2961/*
2962 * Find the lnum for the buffer 'buf' for the current window.
2963 */
2964 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01002965buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966{
2967 return buflist_findfpos(buf)->lnum;
2968}
2969
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002971 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002974buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975{
2976 buf_T *buf;
2977 int len;
2978 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02002979 int ro_char;
2980 int changed_char;
Bram Moolenaar0751f512018-03-29 16:37:16 +02002981#ifdef FEAT_TERMINAL
2982 int job_running;
2983 int job_none_open;
2984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985
2986 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2987 {
Bram Moolenaar0751f512018-03-29 16:37:16 +02002988#ifdef FEAT_TERMINAL
2989 job_running = term_job_running(buf->b_term);
2990 job_none_open = job_running && term_none_open(buf->b_term);
2991#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 /* skip unlisted buffers, unless ! was used */
Bram Moolenaard51cb702015-07-21 15:03:06 +02002993 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
2994 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
2995 || (vim_strchr(eap->arg, '+')
2996 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
2997 || (vim_strchr(eap->arg, 'a')
Bram Moolenaar0751f512018-03-29 16:37:16 +02002998 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
Bram Moolenaard51cb702015-07-21 15:03:06 +02002999 || (vim_strchr(eap->arg, 'h')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003000 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3001#ifdef FEAT_TERMINAL
3002 || (vim_strchr(eap->arg, 'R')
3003 && (!job_running || (job_running && job_none_open)))
3004 || (vim_strchr(eap->arg, '?')
3005 && (!job_running || (job_running && !job_none_open)))
3006 || (vim_strchr(eap->arg, 'F')
3007 && (job_running || buf->b_term == NULL))
3008#endif
Bram Moolenaard51cb702015-07-21 15:03:06 +02003009 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3010 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3011 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3012 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3013 || (vim_strchr(eap->arg, '#')
3014 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003017 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 else
3019 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003020 if (message_filtered(NameBuff))
3021 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003023 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3024 : (bufIsChanged(buf) ? '+' : ' ');
3025#ifdef FEAT_TERMINAL
3026 if (term_job_running(buf->b_term))
3027 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02003028 if (term_none_open(buf->b_term))
3029 ro_char = '?';
3030 else
3031 ro_char = 'R';
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003032 changed_char = ' '; /* bufIsChanged() returns TRUE to avoid
3033 * closing, but it's not actually changed. */
3034 }
3035 else if (buf->b_term != NULL)
3036 ro_char = 'F';
3037 else
3038#endif
3039 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3040
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003041 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00003042 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 buf->b_fnum,
3044 buf->b_p_bl ? ' ' : 'u',
3045 buf == curbuf ? '%' :
3046 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3047 buf->b_ml.ml_mfp == NULL ? ' ' :
3048 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003049 ro_char,
3050 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003051 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003052 if (len > IOSIZE - 20)
3053 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054
3055 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 i = 40 - vim_strsize(IObuff);
3057 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 IObuff[len++] = ' ';
Bram Moolenaarabab0b02019-03-30 18:47:01 +01003059 while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003060 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3061 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003062 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 msg_outtrans(IObuff);
3064 out_flush(); /* output one line at a time */
3065 ui_breakcheck();
3066 }
3067}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068
3069/*
3070 * Get file name and line number for file 'fnum'.
3071 * Used by DoOneCmd() for translating '%' and '#'.
3072 * Used by insert_reg() and cmdline_paste() for '#' register.
3073 * Return FAIL if not found, OK for success.
3074 */
3075 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003076buflist_name_nr(
3077 int fnum,
3078 char_u **fname,
3079 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080{
3081 buf_T *buf;
3082
3083 buf = buflist_findnr(fnum);
3084 if (buf == NULL || buf->b_fname == NULL)
3085 return FAIL;
3086
3087 *fname = buf->b_fname;
3088 *lnum = buflist_findlnum(buf);
3089
3090 return OK;
3091}
3092
3093/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003094 * Set the file name for "buf"' to "ffname_arg", short file name to
3095 * "sfname_arg".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 * The file name with the full path is also remembered, for when :cd is used.
3097 * Returns FAIL for failure (file name already in use by other buffer)
3098 * OK otherwise.
3099 */
3100 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003101setfname(
3102 buf_T *buf,
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003103 char_u *ffname_arg,
3104 char_u *sfname_arg,
Bram Moolenaar7454a062016-01-30 15:14:10 +01003105 int message) /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003107 char_u *ffname = ffname_arg;
3108 char_u *sfname = sfname_arg;
Bram Moolenaar81695252004-12-29 20:58:21 +00003109 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003111 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112#endif
3113
3114 if (ffname == NULL || *ffname == NUL)
3115 {
3116 /* Removing the name. */
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003117 if (buf->b_sfname != buf->b_ffname)
3118 VIM_CLEAR(buf->b_sfname);
3119 else
3120 buf->b_sfname = NULL;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003121 VIM_CLEAR(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122#ifdef UNIX
3123 st.st_dev = (dev_T)-1;
3124#endif
3125 }
3126 else
3127 {
3128 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
3129 if (ffname == NULL) /* out of memory */
3130 return FAIL;
3131
3132 /*
3133 * if the file name is already used in another buffer:
3134 * - if the buffer is loaded, fail
3135 * - if the buffer is not loaded, delete it from the list
3136 */
3137#ifdef UNIX
3138 if (mch_stat((char *)ffname, &st) < 0)
3139 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003140#endif
3141 if (!(buf->b_flags & BF_DUMMY))
3142#ifdef UNIX
3143 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003145 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146#endif
3147 if (obuf != NULL && obuf != buf)
3148 {
3149 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
3150 {
3151 if (message)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003152 emsg(_("E95: Buffer with this name already exists"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 vim_free(ffname);
3154 return FAIL;
3155 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01003156 /* delete from the list */
3157 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 }
3159 sfname = vim_strsave(sfname);
3160 if (ffname == NULL || sfname == NULL)
3161 {
3162 vim_free(sfname);
3163 vim_free(ffname);
3164 return FAIL;
3165 }
3166#ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01003167 fname_case(sfname, 0); /* set correct case for short file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168#endif
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003169 if (buf->b_sfname != buf->b_ffname)
3170 vim_free(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 vim_free(buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 buf->b_ffname = ffname;
3173 buf->b_sfname = sfname;
3174 }
3175 buf->b_fname = buf->b_sfname;
3176#ifdef UNIX
3177 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003178 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 else
3180 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003181 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 buf->b_dev = st.st_dev;
3183 buf->b_ino = st.st_ino;
3184 }
3185#endif
3186
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188
3189 buf_name_changed(buf);
3190 return OK;
3191}
3192
3193/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003194 * Crude way of changing the name of a buffer. Use with care!
3195 * The name should be relative to the current directory.
3196 */
3197 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003198buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003199{
3200 buf_T *buf;
3201
3202 buf = buflist_findnr(fnum);
3203 if (buf != NULL)
3204 {
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02003205 if (buf->b_sfname != buf->b_ffname)
3206 vim_free(buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003207 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003208 buf->b_ffname = vim_strsave(name);
3209 buf->b_sfname = NULL;
3210 /* Allocate ffname and expand into full path. Also resolves .lnk
3211 * files on Win32. */
3212 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003213 buf->b_fname = buf->b_sfname;
3214 }
3215}
3216
3217/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 * Take care of what needs to be done when the name of buffer "buf" has
3219 * changed.
3220 */
3221 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003222buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223{
3224 /*
3225 * If the file name changed, also change the name of the swapfile
3226 */
3227 if (buf->b_ml.ml_mfp != NULL)
3228 ml_setname(buf);
3229
3230 if (curwin->w_buffer == buf)
3231 check_arg_idx(curwin); /* check file name for arg list */
3232#ifdef FEAT_TITLE
3233 maketitle(); /* set window title */
3234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 status_redraw_all(); /* status lines need to be redrawn */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 fmarks_check_names(buf); /* check named file marks */
3237 ml_timestamp(buf); /* reset timestamp */
3238}
3239
3240/*
3241 * set alternate file name for current window
3242 *
3243 * Used by do_one_cmd(), do_write() and do_ecmd().
3244 * Return the buffer.
3245 */
3246 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003247setaltfname(
3248 char_u *ffname,
3249 char_u *sfname,
3250 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251{
3252 buf_T *buf;
3253
3254 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
3255 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003256 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 curwin->w_alt_fnum = buf->b_fnum;
3258 return buf;
3259}
3260
3261/*
3262 * Get alternate file name for current window.
3263 * Return NULL if there isn't any, and give error message if requested.
3264 */
3265 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003266getaltfname(
3267 int errmsg) /* give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268{
3269 char_u *fname;
3270 linenr_T dummy;
3271
3272 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3273 {
3274 if (errmsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003275 emsg(_(e_noalt));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 return NULL;
3277 }
3278 return fname;
3279}
3280
3281/*
3282 * Add a file name to the buflist and return its number.
3283 * Uses same flags as buflist_new(), except BLN_DUMMY.
3284 *
3285 * used by qf_init(), main() and doarglist()
3286 */
3287 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003288buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289{
3290 buf_T *buf;
3291
3292 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3293 if (buf != NULL)
3294 return buf->b_fnum;
3295 return 0;
3296}
3297
3298#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3299/*
3300 * Adjust slashes in file names. Called after 'shellslash' was set.
3301 */
3302 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003303buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304{
3305 buf_T *bp;
3306
Bram Moolenaar29323592016-07-24 22:04:11 +02003307 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 {
3309 if (bp->b_ffname != NULL)
3310 slash_adjust(bp->b_ffname);
3311 if (bp->b_sfname != NULL)
3312 slash_adjust(bp->b_sfname);
3313 }
3314}
3315#endif
3316
3317/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003318 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 * Also save the local window option values.
3320 */
3321 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003322buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003324 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325}
3326
3327/*
3328 * Return TRUE if 'ffname' is not the same file as current file.
3329 * Fname must have a full path (expanded by mch_FullName()).
3330 */
3331 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003332otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333{
3334 return otherfile_buf(curbuf, ffname
3335#ifdef UNIX
3336 , NULL
3337#endif
3338 );
3339}
3340
3341 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003342otherfile_buf(
3343 buf_T *buf,
3344 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003346 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003348 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349{
3350 /* no name is different */
3351 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3352 return TRUE;
3353 if (fnamecmp(ffname, buf->b_ffname) == 0)
3354 return FALSE;
3355#ifdef UNIX
3356 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003357 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358
Bram Moolenaar8767f522016-07-01 17:17:39 +02003359 /* If no stat_T given, get it now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 if (stp == NULL)
3361 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003362 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 st.st_dev = (dev_T)-1;
3364 stp = &st;
3365 }
3366 /* Use dev/ino to check if the files are the same, even when the names
3367 * are different (possible with links). Still need to compare the
3368 * name above, for when the file doesn't exist yet.
3369 * Problem: The dev/ino changes when a file is deleted (and created
3370 * again) and remains the same when renamed/moved. We don't want to
3371 * mch_stat() each buffer each time, that would be too slow. Get the
3372 * dev/ino again when they appear to match, but not when they appear
3373 * to be different: Could skip a buffer when it's actually the same
3374 * file. */
3375 if (buf_same_ino(buf, stp))
3376 {
3377 buf_setino(buf);
3378 if (buf_same_ino(buf, stp))
3379 return FALSE;
3380 }
3381 }
3382#endif
3383 return TRUE;
3384}
3385
3386#if defined(UNIX) || defined(PROTO)
3387/*
3388 * Set inode and device number for a buffer.
3389 * Must always be called when b_fname is changed!.
3390 */
3391 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003392buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003394 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395
3396 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3397 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003398 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 buf->b_dev = st.st_dev;
3400 buf->b_ino = st.st_ino;
3401 }
3402 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003403 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404}
3405
3406/*
3407 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3408 */
3409 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003410buf_same_ino(
3411 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003412 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003414 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 && stp->st_dev == buf->b_dev
3416 && stp->st_ino == buf->b_ino);
3417}
3418#endif
3419
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003420/*
3421 * Print info about the current buffer.
3422 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003424fileinfo(
3425 int fullname, /* when non-zero print full path */
3426 int shorthelp,
3427 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428{
3429 char_u *name;
3430 int n;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003431 char *p;
3432 char *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003433 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003435 buffer = alloc(IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 if (buffer == NULL)
3437 return;
3438
3439 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3440 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003441 vim_snprintf(buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 p = buffer + STRLEN(buffer);
3443 }
3444 else
3445 p = buffer;
3446
3447 *p++ = '"';
3448 if (buf_spname(curbuf) != NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003449 vim_strncpy((char_u *)p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 else
3451 {
3452 if (!fullname && curbuf->b_fname != NULL)
3453 name = curbuf->b_fname;
3454 else
3455 name = curbuf->b_ffname;
Bram Moolenaar32526b32019-01-19 17:43:09 +01003456 home_replace(shorthelp ? curbuf : NULL, name, (char_u *)p,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 (int)(IOSIZE - (p - buffer)), TRUE);
3458 }
3459
Bram Moolenaar32526b32019-01-19 17:43:09 +01003460 vim_snprintf_add(buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 curbufIsChanged() ? (shortmess(SHM_MOD)
3462 ? " [+]" : _(" [Modified]")) : " ",
3463 (curbuf->b_flags & BF_NOTEDITED)
3464#ifdef FEAT_QUICKFIX
3465 && !bt_dontwrite(curbuf)
3466#endif
3467 ? _("[Not edited]") : "",
3468 (curbuf->b_flags & BF_NEW)
3469#ifdef FEAT_QUICKFIX
3470 && !bt_dontwrite(curbuf)
3471#endif
3472 ? _("[New file]") : "",
3473 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003474 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 : _("[readonly]")) : "",
3476 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3477 || curbuf->b_p_ro) ?
3478 " " : "");
3479 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3480 * causes an overflow, thus for large numbers divide instead. */
3481 if (curwin->w_cursor.lnum > 1000000L)
3482 n = (int)(((long)curwin->w_cursor.lnum) /
3483 ((long)curbuf->b_ml.ml_line_count / 100L));
3484 else
3485 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3486 (long)curbuf->b_ml.ml_line_count);
3487 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003488 vim_snprintf_add(buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489#ifdef FEAT_CMDL_INFO
3490 else if (p_ru)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 /* Current line and column are already on the screen -- webb */
Bram Moolenaar32526b32019-01-19 17:43:09 +01003492 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003493 NGETTEXT("%ld line --%d%%--", "%ld lines --%d%%--",
3494 curbuf->b_ml.ml_line_count),
3495 (long)curbuf->b_ml.ml_line_count, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496#endif
3497 else
3498 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003499 vim_snprintf_add(buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 _("line %ld of %ld --%d%%-- col "),
3501 (long)curwin->w_cursor.lnum,
3502 (long)curbuf->b_ml.ml_line_count,
3503 n);
3504 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003505 len = STRLEN(buffer);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003506 col_print((char_u *)buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3508 }
3509
Bram Moolenaar32526b32019-01-19 17:43:09 +01003510 (void)append_arg_number(curwin, (char_u *)buffer, IOSIZE,
3511 !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512
3513 if (dont_truncate)
3514 {
3515 /* Temporarily set msg_scroll to avoid the message being truncated.
3516 * First call msg_start() to get the message in the right place. */
3517 msg_start();
3518 n = msg_scroll;
3519 msg_scroll = TRUE;
3520 msg(buffer);
3521 msg_scroll = n;
3522 }
3523 else
3524 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003525 p = (char *)msg_trunc_attr(buffer, FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 /* Need to repeat the message after redrawing when:
3528 * - When restart_edit is set (otherwise there will be a delay
3529 * before redrawing).
3530 * - When the screen was scrolled but there is no wait-return
3531 * prompt. */
Bram Moolenaar32526b32019-01-19 17:43:09 +01003532 set_keep_msg((char_u *)p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 }
3534
3535 vim_free(buffer);
3536}
3537
3538 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003539col_print(
3540 char_u *buf,
3541 size_t buflen,
3542 int col,
3543 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544{
3545 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003546 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003548 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549}
3550
3551#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552static char_u *lasttitle = NULL;
3553static char_u *lasticon = NULL;
3554
Bram Moolenaar84a93082018-06-16 22:58:15 +02003555/*
3556 * Put the file name in the title bar and icon of the window.
3557 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003559maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560{
3561 char_u *p;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003562 char_u *title_str = NULL;
3563 char_u *icon_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 int maxlen = 0;
3565 int len;
3566 int mustset;
3567 char_u buf[IOSIZE];
3568 int off;
3569
3570 if (!redrawing())
3571 {
3572 /* Postpone updating the title when 'lazyredraw' is set. */
3573 need_maketitle = TRUE;
3574 return;
3575 }
3576
3577 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003578 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003579 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580
3581 if (p_title)
3582 {
3583 if (p_titlelen > 0)
3584 {
3585 maxlen = p_titlelen * Columns / 100;
3586 if (maxlen < 10)
3587 maxlen = 10;
3588 }
3589
Bram Moolenaar84a93082018-06-16 22:58:15 +02003590 title_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 if (*p_titlestring != NUL)
3592 {
3593#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003594 if (stl_syntax & STL_IN_TITLE)
3595 {
3596 int use_sandbox = FALSE;
3597 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003598
3599# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003600 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003601# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003602 called_emsg = FALSE;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003603 build_stl_str_hl(curwin, title_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003604 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003605 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003606 if (called_emsg)
3607 set_string_option_direct((char_u *)"titlestring", -1,
3608 (char_u *)"", OPT_FREE, SID_ERROR);
3609 called_emsg |= save_called_emsg;
3610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 else
3612#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003613 title_str = p_titlestring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 }
3615 else
3616 {
3617 /* format: "fname + (path) (1 of 2) - VIM" */
3618
Bram Moolenaar2c666692012-09-05 13:30:40 +02003619#define SPACE_FOR_FNAME (IOSIZE - 100)
3620#define SPACE_FOR_DIR (IOSIZE - 20)
3621#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003623 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar21554412017-07-24 21:44:43 +02003624#ifdef FEAT_TERMINAL
3625 else if (curbuf->b_term != NULL)
3626 {
3627 vim_strncpy(buf, term_get_status_text(curbuf->b_term),
3628 SPACE_FOR_FNAME);
3629 }
3630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 else
3632 {
3633 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003634 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 }
3637
Bram Moolenaar21554412017-07-24 21:44:43 +02003638#ifdef FEAT_TERMINAL
3639 if (curbuf->b_term == NULL)
3640#endif
3641 switch (bufIsChanged(curbuf)
3642 + (curbuf->b_p_ro * 2)
3643 + (!curbuf->b_p_ma * 4))
3644 {
3645 case 1: STRCAT(buf, " +"); break;
3646 case 2: STRCAT(buf, " ="); break;
3647 case 3: STRCAT(buf, " =+"); break;
3648 case 4:
3649 case 6: STRCAT(buf, " -"); break;
3650 case 5:
3651 case 7: STRCAT(buf, " -+"); break;
3652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653
Bram Moolenaar21554412017-07-24 21:44:43 +02003654 if (curbuf->b_fname != NULL
3655#ifdef FEAT_TERMINAL
3656 && curbuf->b_term == NULL
3657#endif
3658 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 {
3660 /* Get path of file, replace home dir with ~ */
3661 off = (int)STRLEN(buf);
3662 buf[off++] = ' ';
3663 buf[off++] = '(';
3664 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003665 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666#ifdef BACKSLASH_IN_FILENAME
3667 /* avoid "c:/name" to be reduced to "c" */
3668 if (isalpha(buf[off]) && buf[off + 1] == ':')
3669 off += 2;
3670#endif
3671 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003672 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 if (p == buf + off)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003674 {
Bram Moolenaar21554412017-07-24 21:44:43 +02003675 /* must be a help buffer */
3676 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003677 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003681
Bram Moolenaar2c666692012-09-05 13:30:40 +02003682 /* Translate unprintable chars and concatenate. Keep some
3683 * room for the server name. When there is no room (very long
3684 * file name) use (...). */
3685 if (off < SPACE_FOR_DIR)
3686 {
3687 p = transstr(buf + off);
3688 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3689 vim_free(p);
3690 }
3691 else
3692 {
3693 vim_strncpy(buf + off, (char_u *)"...",
3694 (size_t)(SPACE_FOR_ARGNR - off));
3695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 STRCAT(buf, ")");
3697 }
3698
Bram Moolenaar2c666692012-09-05 13:30:40 +02003699 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700
3701#if defined(FEAT_CLIENTSERVER)
3702 if (serverName != NULL)
3703 {
3704 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003705 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 }
3707 else
3708#endif
3709 STRCAT(buf, " - VIM");
3710
3711 if (maxlen > 0)
3712 {
3713 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003714 if (vim_strsize(buf) > maxlen)
3715 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 }
3717 }
3718 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02003719 mustset = value_changed(title_str, &lasttitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720
3721 if (p_icon)
3722 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02003723 icon_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 if (*p_iconstring != NUL)
3725 {
3726#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003727 if (stl_syntax & STL_IN_ICON)
3728 {
3729 int use_sandbox = FALSE;
3730 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003731
3732# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003733 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003734# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003735 called_emsg = FALSE;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003736 build_stl_str_hl(curwin, icon_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003737 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003738 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003739 if (called_emsg)
3740 set_string_option_direct((char_u *)"iconstring", -1,
3741 (char_u *)"", OPT_FREE, SID_ERROR);
3742 called_emsg |= save_called_emsg;
3743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 else
3745#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003746 icon_str = p_iconstring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 }
3748 else
3749 {
3750 if (buf_spname(curbuf) != NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003751 p = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 else /* use file name only in icon */
Bram Moolenaar84a93082018-06-16 22:58:15 +02003753 p = gettail(curbuf->b_ffname);
3754 *icon_str = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 /* Truncate name at 100 bytes. */
Bram Moolenaar84a93082018-06-16 22:58:15 +02003756 len = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 if (len > 100)
3758 {
3759 len -= 100;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 if (has_mbyte)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003761 len += (*mb_tail_off)(p, p + len) + 1;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003762 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02003764 STRCPY(icon_str, p);
3765 trans_characters(icon_str, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 }
3767 }
3768
Bram Moolenaar84a93082018-06-16 22:58:15 +02003769 mustset |= value_changed(icon_str, &lasticon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770
3771 if (mustset)
3772 resettitle();
3773}
3774
3775/*
3776 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3777 * from "str" if it does.
Bram Moolenaar84a93082018-06-16 22:58:15 +02003778 * Return TRUE if resettitle() is to be called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 */
3780 static int
Bram Moolenaar84a93082018-06-16 22:58:15 +02003781value_changed(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782{
3783 if ((str == NULL) != (*last == NULL)
3784 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3785 {
3786 vim_free(*last);
3787 if (str == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003788 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 *last = NULL;
Bram Moolenaar40385db2018-08-07 22:31:44 +02003790 mch_restore_title(
3791 last == &lasttitle ? SAVE_RESTORE_TITLE : SAVE_RESTORE_ICON);
Bram Moolenaar84a93082018-06-16 22:58:15 +02003792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 else
Bram Moolenaar84a93082018-06-16 22:58:15 +02003794 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 *last = vim_strsave(str);
Bram Moolenaar84a93082018-06-16 22:58:15 +02003796 return TRUE;
3797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 }
3799 return FALSE;
3800}
3801
3802/*
3803 * Put current window title back (used after calling a shell)
3804 */
3805 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003806resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807{
3808 mch_settitle(lasttitle, lasticon);
3809}
Bram Moolenaarea408852005-06-25 22:49:46 +00003810
3811# if defined(EXITFREE) || defined(PROTO)
3812 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003813free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00003814{
3815 vim_free(lasttitle);
3816 vim_free(lasticon);
3817}
3818# endif
3819
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820#endif /* FEAT_TITLE */
3821
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003822#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003824 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 * Return length of string in screen cells.
3826 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003827 * Normally works for window "wp", except when working for 'tabline' then it
3828 * is "curwin".
3829 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 * Items are drawn interspersed with the text that surrounds it
3831 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3832 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3833 *
3834 * If maxwidth is not zero, the string will be filled at any middle marker
3835 * or truncated if too long, fillchar is used for all whitespace.
3836 */
3837 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003838build_stl_str_hl(
3839 win_T *wp,
3840 char_u *out, /* buffer to write into != NameBuff */
3841 size_t outlen, /* length of out[] */
3842 char_u *fmt,
3843 int use_sandbox UNUSED, /* "fmt" was set insecurely, use sandbox */
3844 int fillchar,
3845 int maxwidth,
3846 struct stl_hlrec *hltab, /* return: HL attributes (can be NULL) */
3847 struct stl_hlrec *tabtab) /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848{
Bram Moolenaar10772302019-01-20 18:25:54 +01003849 linenr_T lnum;
3850 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 char_u *p;
3852 char_u *s;
3853 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003854 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855#ifdef FEAT_EVAL
Bram Moolenaarba2929b2017-09-08 13:59:21 +02003856 win_T *save_curwin;
3857 buf_T *save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858#endif
3859 int empty_line;
3860 colnr_T virtcol;
3861 long l;
3862 long n;
3863 int prevchar_isflag;
3864 int prevchar_isitem;
3865 int itemisflag;
3866 int fillable;
3867 char_u *str;
3868 long num;
3869 int width;
3870 int itemcnt;
3871 int curitem;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02003872 int group_end_userhl;
3873 int group_start_userhl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 int groupitem[STL_MAX_ITEM];
3875 int groupdepth;
3876 struct stl_item
3877 {
3878 char_u *start;
3879 int minwid;
3880 int maxwid;
3881 enum
3882 {
3883 Normal,
3884 Empty,
3885 Group,
3886 Middle,
3887 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003888 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 Trunc
3890 } type;
3891 } item[STL_MAX_ITEM];
3892 int minwid;
3893 int maxwid;
3894 int zeropad;
3895 char_u base;
3896 char_u opt;
3897#define TMPLEN 70
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02003898 char_u buf_tmp[TMPLEN];
3899 char_u win_tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003900 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003901 struct stl_hlrec *sp;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02003902 int save_must_redraw = must_redraw;
3903 int save_redr_type = curwin->w_redr_type;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003904
3905#ifdef FEAT_EVAL
3906 /*
3907 * When the format starts with "%!" then evaluate it as an expression and
3908 * use the result as the actual format string.
3909 */
3910 if (fmt[0] == '%' && fmt[1] == '!')
3911 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02003912 typval_T tv;
3913
3914 tv.v_type = VAR_NUMBER;
3915 tv.vval.v_number = wp->w_id;
3916 set_var((char_u *)"g:statusline_winid", &tv, FALSE);
3917
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003918 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3919 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003920 usefmt = fmt;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02003921
3922 do_unlet((char_u *)"g:statusline_winid", TRUE);
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003923 }
3924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925
3926 if (fillchar == 0)
3927 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003928 /* Can't handle a multi-byte fill character yet. */
3929 else if (mb_char2len(fillchar) > 1)
3930 fillchar = '-';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003931
Bram Moolenaar10772302019-01-20 18:25:54 +01003932 // The cursor in windows other than the current one isn't always
3933 // up-to-date, esp. because of autocommands and timers.
3934 lnum = wp->w_cursor.lnum;
3935 if (lnum > wp->w_buffer->b_ml.ml_line_count)
3936 {
3937 lnum = wp->w_buffer->b_ml.ml_line_count;
3938 wp->w_cursor.lnum = lnum;
3939 }
3940
3941 // Get line & check if empty (cursorpos will show "0-1"). Note that
3942 // p will become invalid when getting another buffer line.
3943 p = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar567199b2013-04-24 16:52:36 +02003944 empty_line = (*p == NUL);
3945
Bram Moolenaar10772302019-01-20 18:25:54 +01003946 // Get the byte value now, in case we need it below. This is more efficient
3947 // than making a copy of the line.
3948 len = STRLEN(p);
3949 if (wp->w_cursor.col > (colnr_T)len)
3950 {
3951 // Line may have changed since checking the cursor column, or the lnum
3952 // was adjusted above.
3953 wp->w_cursor.col = (colnr_T)len;
Bram Moolenaar10772302019-01-20 18:25:54 +01003954 wp->w_cursor.coladd = 0;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003955 byteval = 0;
Bram Moolenaar10772302019-01-20 18:25:54 +01003956 }
Bram Moolenaar567199b2013-04-24 16:52:36 +02003957 else
Bram Moolenaar567199b2013-04-24 16:52:36 +02003958 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959
3960 groupdepth = 0;
3961 p = out;
3962 curitem = 0;
3963 prevchar_isflag = TRUE;
3964 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003965 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003967 if (curitem == STL_MAX_ITEM)
3968 {
3969 /* There are too many items. Add the error code to the statusline
3970 * to give the user a hint about what went wrong. */
3971 if (p + 6 < out + outlen)
3972 {
3973 mch_memmove(p, " E541", (size_t)5);
3974 p += 5;
3975 }
3976 break;
3977 }
3978
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 if (*s != NUL && *s != '%')
3980 prevchar_isflag = prevchar_isitem = FALSE;
3981
3982 /*
3983 * Handle up to the next '%' or the end.
3984 */
3985 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3986 *p++ = *s++;
3987 if (*s == NUL || p + 1 >= out + outlen)
3988 break;
3989
3990 /*
3991 * Handle one '%' item.
3992 */
3993 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003994 if (*s == NUL) /* ignore trailing % */
3995 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 if (*s == '%')
3997 {
3998 if (p + 1 >= out + outlen)
3999 break;
4000 *p++ = *s++;
4001 prevchar_isflag = prevchar_isitem = FALSE;
4002 continue;
4003 }
4004 if (*s == STL_MIDDLEMARK)
4005 {
4006 s++;
4007 if (groupdepth > 0)
4008 continue;
4009 item[curitem].type = Middle;
4010 item[curitem++].start = p;
4011 continue;
4012 }
4013 if (*s == STL_TRUNCMARK)
4014 {
4015 s++;
4016 item[curitem].type = Trunc;
4017 item[curitem++].start = p;
4018 continue;
4019 }
4020 if (*s == ')')
4021 {
4022 s++;
4023 if (groupdepth < 1)
4024 continue;
4025 groupdepth--;
4026
4027 t = item[groupitem[groupdepth]].start;
4028 *p = NUL;
4029 l = vim_strsize(t);
4030 if (curitem > groupitem[groupdepth] + 1
4031 && item[groupitem[groupdepth]].minwid == 0)
4032 {
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004033 /* remove group if all items are empty and highlight group
4034 * doesn't change */
4035 group_start_userhl = group_end_userhl = 0;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004036 for (n = groupitem[groupdepth] - 1; n >= 0; n--)
4037 {
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004038 if (item[n].type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004039 {
4040 group_start_userhl = group_end_userhl = item[n].minwid;
4041 break;
4042 }
4043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004045 {
4046 if (item[n].type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 break;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004048 if (item[n].type == Highlight)
4049 group_end_userhl = item[n].minwid;
4050 }
4051 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 {
4053 p = t;
4054 l = 0;
4055 }
4056 }
4057 if (l > item[groupitem[groupdepth]].maxwid)
4058 {
4059 /* truncate, remove n bytes of text at the start */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 if (has_mbyte)
4061 {
4062 /* Find the first character that should be included. */
4063 n = 0;
4064 while (l >= item[groupitem[groupdepth]].maxwid)
4065 {
4066 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004067 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 }
4069 }
4070 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004071 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072
4073 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004074 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 p = p - n + 1;
Bram Moolenaar13505972019-01-24 15:04:48 +01004076
4077 // Fill up space left over by half a double-wide char.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 while (++l < item[groupitem[groupdepth]].minwid)
4079 *p++ = fillchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080
4081 /* correct the start of the items for the truncation */
4082 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
4083 {
4084 item[l].start -= n;
4085 if (item[l].start < t)
4086 item[l].start = t;
4087 }
4088 }
4089 else if (abs(item[groupitem[groupdepth]].minwid) > l)
4090 {
4091 /* fill */
4092 n = item[groupitem[groupdepth]].minwid;
4093 if (n < 0)
4094 {
4095 /* fill by appending characters */
4096 n = 0 - n;
4097 while (l++ < n && p + 1 < out + outlen)
4098 *p++ = fillchar;
4099 }
4100 else
4101 {
4102 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004103 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 l = n - l;
4105 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004106 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 p += l;
4108 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
4109 item[n].start += l;
4110 for ( ; l > 0; l--)
4111 *t++ = fillchar;
4112 }
4113 }
4114 continue;
4115 }
4116 minwid = 0;
4117 maxwid = 9999;
4118 zeropad = FALSE;
4119 l = 1;
4120 if (*s == '0')
4121 {
4122 s++;
4123 zeropad = TRUE;
4124 }
4125 if (*s == '-')
4126 {
4127 s++;
4128 l = -1;
4129 }
4130 if (VIM_ISDIGIT(*s))
4131 {
4132 minwid = (int)getdigits(&s);
4133 if (minwid < 0) /* overflow */
4134 minwid = 0;
4135 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004136 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 {
4138 item[curitem].type = Highlight;
4139 item[curitem].start = p;
4140 item[curitem].minwid = minwid > 9 ? 1 : minwid;
4141 s++;
4142 curitem++;
4143 continue;
4144 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004145 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4146 {
4147 if (*s == STL_TABCLOSENR)
4148 {
4149 if (minwid == 0)
4150 {
4151 /* %X ends the close label, go back to the previously
4152 * define tab label nr. */
4153 for (n = curitem - 1; n >= 0; --n)
4154 if (item[n].type == TabPage && item[n].minwid >= 0)
4155 {
4156 minwid = item[n].minwid;
4157 break;
4158 }
4159 }
4160 else
4161 /* close nrs are stored as negative values */
4162 minwid = - minwid;
4163 }
4164 item[curitem].type = TabPage;
4165 item[curitem].start = p;
4166 item[curitem].minwid = minwid;
4167 s++;
4168 curitem++;
4169 continue;
4170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 if (*s == '.')
4172 {
4173 s++;
4174 if (VIM_ISDIGIT(*s))
4175 {
4176 maxwid = (int)getdigits(&s);
4177 if (maxwid <= 0) /* overflow */
4178 maxwid = 50;
4179 }
4180 }
4181 minwid = (minwid > 50 ? 50 : minwid) * l;
4182 if (*s == '(')
4183 {
4184 groupitem[groupdepth++] = curitem;
4185 item[curitem].type = Group;
4186 item[curitem].start = p;
4187 item[curitem].minwid = minwid;
4188 item[curitem].maxwid = maxwid;
4189 s++;
4190 curitem++;
4191 continue;
4192 }
4193 if (vim_strchr(STL_ALL, *s) == NULL)
4194 {
4195 s++;
4196 continue;
4197 }
4198 opt = *s++;
4199
4200 /* OK - now for the real work */
4201 base = 'D';
4202 itemisflag = FALSE;
4203 fillable = TRUE;
4204 num = -1;
4205 str = NULL;
4206 switch (opt)
4207 {
4208 case STL_FILEPATH:
4209 case STL_FULLPATH:
4210 case STL_FILENAME:
4211 fillable = FALSE; /* don't change ' ' to fillchar */
4212 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004213 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 else
4215 {
4216 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004217 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4219 }
4220 trans_characters(NameBuff, MAXPATHL);
4221 if (opt != STL_FILENAME)
4222 str = NameBuff;
4223 else
4224 str = gettail(NameBuff);
4225 break;
4226
4227 case STL_VIM_EXPR: /* '{' */
4228 itemisflag = TRUE;
4229 t = p;
4230 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
4231 *p++ = *s++;
4232 if (*s != '}') /* missing '}' or out of space */
4233 break;
4234 s++;
4235 *p = 0;
4236 p = t;
4237
4238#ifdef FEAT_EVAL
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004239 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp),
4240 "%d", curbuf->b_fnum);
4241 set_internal_string_var((char_u *)"g:actual_curbuf", buf_tmp);
4242 vim_snprintf((char *)win_tmp, sizeof(win_tmp), "%d", curwin->w_id);
4243 set_internal_string_var((char_u *)"g:actual_curwin", win_tmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004245 save_curbuf = curbuf;
4246 save_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 curwin = wp;
4248 curbuf = wp->w_buffer;
4249
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004250 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004252 curwin = save_curwin;
4253 curbuf = save_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00004254 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004255 do_unlet((char_u *)"g:actual_curwin", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256
4257 if (str != NULL && *str != 0)
4258 {
4259 if (*skipdigits(str) == NUL)
4260 {
4261 num = atoi((char *)str);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004262 VIM_CLEAR(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 itemisflag = FALSE;
4264 }
4265 }
4266#endif
4267 break;
4268
4269 case STL_LINE:
4270 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4271 ? 0L : (long)(wp->w_cursor.lnum);
4272 break;
4273
4274 case STL_NUMLINES:
4275 num = wp->w_buffer->b_ml.ml_line_count;
4276 break;
4277
4278 case STL_COLUMN:
4279 num = !(State & INSERT) && empty_line
4280 ? 0 : (int)wp->w_cursor.col + 1;
4281 break;
4282
4283 case STL_VIRTCOL:
4284 case STL_VIRTCOL_ALT:
4285 /* In list mode virtcol needs to be recomputed */
4286 virtcol = wp->w_virtcol;
4287 if (wp->w_p_list && lcs_tab1 == NUL)
4288 {
4289 wp->w_p_list = FALSE;
4290 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
4291 wp->w_p_list = TRUE;
4292 }
4293 ++virtcol;
4294 /* Don't display %V if it's the same as %c. */
4295 if (opt == STL_VIRTCOL_ALT
4296 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
4297 ? 0 : (int)wp->w_cursor.col + 1)))
4298 break;
4299 num = (long)virtcol;
4300 break;
4301
4302 case STL_PERCENTAGE:
4303 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4304 (long)wp->w_buffer->b_ml.ml_line_count);
4305 break;
4306
4307 case STL_ALTPERCENT:
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004308 str = buf_tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004309 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 break;
4311
4312 case STL_ARGLISTSTAT:
4313 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004314 buf_tmp[0] = 0;
4315 if (append_arg_number(wp, buf_tmp, (int)sizeof(buf_tmp), FALSE))
4316 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 break;
4318
4319 case STL_KEYMAP:
4320 fillable = FALSE;
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004321 if (get_keymap_str(wp, (char_u *)"<%s>", buf_tmp, TMPLEN))
4322 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 break;
4324 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004325#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004326 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327#else
4328 num = 0;
4329#endif
4330 break;
4331
4332 case STL_BUFNO:
4333 num = wp->w_buffer->b_fnum;
4334 break;
4335
4336 case STL_OFFSET_X:
4337 base = 'X';
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004338 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 case STL_OFFSET:
4340#ifdef FEAT_BYTEOFF
4341 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4342 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4343 0L : l + 1 + (!(State & INSERT) && empty_line ?
4344 0 : (int)wp->w_cursor.col);
4345#endif
4346 break;
4347
4348 case STL_BYTEVAL_X:
4349 base = 'X';
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004350 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004352 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 if (num == NL)
4354 num = 0;
4355 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4356 num = NL;
4357 break;
4358
4359 case STL_ROFLAG:
4360 case STL_ROFLAG_ALT:
4361 itemisflag = TRUE;
4362 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004363 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 break;
4365
4366 case STL_HELPFLAG:
4367 case STL_HELPFLAG_ALT:
4368 itemisflag = TRUE;
4369 if (wp->w_buffer->b_help)
4370 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004371 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 break;
4373
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 case STL_FILETYPE:
4375 if (*wp->w_buffer->b_p_ft != NUL
4376 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4377 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004378 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), "[%s]",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004379 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004380 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 }
4382 break;
4383
4384 case STL_FILETYPE_ALT:
4385 itemisflag = TRUE;
4386 if (*wp->w_buffer->b_p_ft != NUL
4387 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4388 {
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004389 vim_snprintf((char *)buf_tmp, sizeof(buf_tmp), ",%s",
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004390 wp->w_buffer->b_p_ft);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004391 for (t = buf_tmp; *t != 0; t++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 *t = TOUPPER_LOC(*t);
Bram Moolenaar1c6fd1e2019-05-23 22:11:59 +02004393 str = buf_tmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 }
4395 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396
Bram Moolenaar4033c552017-09-16 20:54:51 +02004397#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 case STL_PREVIEWFLAG:
4399 case STL_PREVIEWFLAG_ALT:
4400 itemisflag = TRUE;
4401 if (wp->w_p_pvw)
4402 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4403 : _("[Preview]"));
4404 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004405
4406 case STL_QUICKFIX:
4407 if (bt_quickfix(wp->w_buffer))
4408 str = (char_u *)(wp->w_llist_ref
4409 ? _(msg_loclist)
4410 : _(msg_qflist));
4411 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412#endif
4413
4414 case STL_MODIFIED:
4415 case STL_MODIFIED_ALT:
4416 itemisflag = TRUE;
4417 switch ((opt == STL_MODIFIED_ALT)
4418 + bufIsChanged(wp->w_buffer) * 2
4419 + (!wp->w_buffer->b_p_ma) * 4)
4420 {
4421 case 2: str = (char_u *)"[+]"; break;
4422 case 3: str = (char_u *)",+"; break;
4423 case 4: str = (char_u *)"[-]"; break;
4424 case 5: str = (char_u *)",-"; break;
4425 case 6: str = (char_u *)"[+-]"; break;
4426 case 7: str = (char_u *)",+-"; break;
4427 }
4428 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004429
4430 case STL_HIGHLIGHT:
4431 t = s;
4432 while (*s != '#' && *s != NUL)
4433 ++s;
4434 if (*s == '#')
4435 {
4436 item[curitem].type = Highlight;
4437 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004438 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004439 curitem++;
4440 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004441 if (*s != NUL)
4442 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004443 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 }
4445
4446 item[curitem].start = p;
4447 item[curitem].type = Normal;
4448 if (str != NULL && *str)
4449 {
4450 t = str;
4451 if (itemisflag)
4452 {
4453 if ((t[0] && t[1])
4454 && ((!prevchar_isitem && *t == ',')
4455 || (prevchar_isflag && *t == ' ')))
4456 t++;
4457 prevchar_isflag = TRUE;
4458 }
4459 l = vim_strsize(t);
4460 if (l > 0)
4461 prevchar_isitem = TRUE;
4462 if (l > maxwid)
4463 {
4464 while (l >= maxwid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 if (has_mbyte)
4466 {
4467 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004468 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 }
4470 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 l -= byte2cells(*t++);
4472 if (p + 1 >= out + outlen)
4473 break;
4474 *p++ = '<';
4475 }
4476 if (minwid > 0)
4477 {
4478 for (; l < minwid && p + 1 < out + outlen; l++)
4479 {
4480 /* Don't put a "-" in front of a digit. */
4481 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4482 *p++ = ' ';
4483 else
4484 *p++ = fillchar;
4485 }
4486 minwid = 0;
4487 }
4488 else
4489 minwid *= -1;
4490 while (*t && p + 1 < out + outlen)
4491 {
4492 *p++ = *t++;
4493 /* Change a space by fillchar, unless fillchar is '-' and a
4494 * digit follows. */
4495 if (fillable && p[-1] == ' '
4496 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4497 p[-1] = fillchar;
4498 }
4499 for (; l < minwid && p + 1 < out + outlen; l++)
4500 *p++ = fillchar;
4501 }
4502 else if (num >= 0)
4503 {
4504 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4505 char_u nstr[20];
4506
4507 if (p + 20 >= out + outlen)
4508 break; /* not sufficient space */
4509 prevchar_isitem = TRUE;
4510 t = nstr;
4511 if (opt == STL_VIRTCOL_ALT)
4512 {
4513 *t++ = '-';
4514 minwid--;
4515 }
4516 *t++ = '%';
4517 if (zeropad)
4518 *t++ = '0';
4519 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004520 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 *t = 0;
4522
4523 for (n = num, l = 1; n >= nbase; n /= nbase)
4524 l++;
4525 if (opt == STL_VIRTCOL_ALT)
4526 l++;
4527 if (l > maxwid)
4528 {
4529 l += 2;
4530 n = l - maxwid;
4531 while (l-- > maxwid)
4532 num /= nbase;
4533 *t++ = '>';
4534 *t++ = '%';
4535 *t = t[-3];
4536 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004537 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4538 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 }
4540 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004541 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4542 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 p += STRLEN(p);
4544 }
4545 else
4546 item[curitem].type = Empty;
4547
4548 if (opt == STL_VIM_EXPR)
4549 vim_free(str);
4550
4551 if (num >= 0 || (!itemisflag && str && *str))
4552 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4553 curitem++;
4554 }
4555 *p = NUL;
4556 itemcnt = curitem;
4557
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004558#ifdef FEAT_EVAL
4559 if (usefmt != fmt)
4560 vim_free(usefmt);
4561#endif
4562
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 width = vim_strsize(out);
4564 if (maxwidth > 0 && width > maxwidth)
4565 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004566 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 l = 0;
4568 if (itemcnt == 0)
4569 s = out;
4570 else
4571 {
4572 for ( ; l < itemcnt; l++)
4573 if (item[l].type == Trunc)
4574 {
4575 /* Truncate at %< item. */
4576 s = item[l].start;
4577 break;
4578 }
4579 if (l == itemcnt)
4580 {
4581 /* No %< item, truncate first item. */
4582 s = item[0].start;
4583 l = 0;
4584 }
4585 }
4586
4587 if (width - vim_strsize(s) >= maxwidth)
4588 {
4589 /* Truncation mark is beyond max length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 if (has_mbyte)
4591 {
4592 s = out;
4593 width = 0;
4594 for (;;)
4595 {
4596 width += ptr2cells(s);
4597 if (width >= maxwidth)
4598 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004599 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 }
4601 /* Fill up for half a double-wide character. */
4602 while (++width < maxwidth)
4603 *s++ = fillchar;
4604 }
4605 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 s = out + maxwidth - 1;
4607 for (l = 0; l < itemcnt; l++)
4608 if (item[l].start > s)
4609 break;
4610 itemcnt = l;
4611 *s++ = '>';
4612 *s = 0;
4613 }
4614 else
4615 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 if (has_mbyte)
4617 {
4618 n = 0;
4619 while (width >= maxwidth)
4620 {
4621 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004622 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 }
4624 }
4625 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 n = width - maxwidth + 1;
4627 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004628 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 *s = '<';
4630
4631 /* Fill up for half a double-wide character. */
4632 while (++width < maxwidth)
4633 {
4634 s = s + STRLEN(s);
4635 *s++ = fillchar;
4636 *s = NUL;
4637 }
4638
4639 --n; /* count the '<' */
4640 for (; l < itemcnt; l++)
4641 {
4642 if (item[l].start - n >= s)
4643 item[l].start -= n;
4644 else
4645 item[l].start = s;
4646 }
4647 }
4648 width = maxwidth;
4649 }
4650 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4651 {
4652 /* Apply STL_MIDDLE if any */
4653 for (l = 0; l < itemcnt; l++)
4654 if (item[l].type == Middle)
4655 break;
4656 if (l < itemcnt)
4657 {
4658 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004659 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 for (s = item[l].start; s < p; s++)
4661 *s = fillchar;
4662 for (l++; l < itemcnt; l++)
4663 item[l].start += maxwidth - width;
4664 width = maxwidth;
4665 }
4666 }
4667
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004668 /* Store the info about highlighting. */
4669 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004671 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 for (l = 0; l < itemcnt; l++)
4673 {
4674 if (item[l].type == Highlight)
4675 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004676 sp->start = item[l].start;
4677 sp->userhl = item[l].minwid;
4678 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 }
4680 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004681 sp->start = NULL;
4682 sp->userhl = 0;
4683 }
4684
4685 /* Store the info about tab pages labels. */
4686 if (tabtab != NULL)
4687 {
4688 sp = tabtab;
4689 for (l = 0; l < itemcnt; l++)
4690 {
4691 if (item[l].type == TabPage)
4692 {
4693 sp->start = item[l].start;
4694 sp->userhl = item[l].minwid;
4695 sp++;
4696 }
4697 }
4698 sp->start = NULL;
4699 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 }
4701
Bram Moolenaar65ed1362017-09-30 16:00:14 +02004702 /* When inside update_screen we do not want redrawing a stausline, ruler,
4703 * title, etc. to trigger another redraw, it may cause an endless loop. */
4704 if (updating_screen)
4705 {
4706 must_redraw = save_must_redraw;
4707 curwin->w_redr_type = save_redr_type;
4708 }
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004709
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 return width;
4711}
4712#endif /* FEAT_STL_OPT */
4713
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004714#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4715 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004717 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4718 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 */
4720 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004721get_rel_pos(
4722 win_T *wp,
4723 char_u *buf,
4724 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725{
4726 long above; /* number of lines above window */
4727 long below; /* number of lines below window */
4728
Bram Moolenaar0027c212015-01-07 13:31:52 +01004729 if (buflen < 3) /* need at least 3 chars for writing */
4730 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 above = wp->w_topline - 1;
4732#ifdef FEAT_DIFF
4733 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02004734 if (wp->w_topline == 1 && wp->w_topfill >= 1)
4735 above = 0; /* All buffer lines are displayed and there is an
4736 * indication of filler lines, that can be considered
4737 * seeing all lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738#endif
4739 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4740 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004741 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4742 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004744 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004746 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 ? (int)(above / ((above + below) / 100L))
4748 : (int)(above * 100L / (above + below)));
4749}
4750#endif
4751
4752/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004753 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 * Return TRUE if it was appended.
4755 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004756 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004757append_arg_number(
4758 win_T *wp,
4759 char_u *buf,
4760 int buflen,
4761 int add_file) /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762{
4763 char_u *p;
4764
4765 if (ARGCOUNT <= 1) /* nothing to do */
4766 return FALSE;
4767
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004768 p = buf + STRLEN(buf); /* go to the end of the buffer */
4769 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 return FALSE;
4771 *p++ = ' ';
4772 *p++ = '(';
4773 if (add_file)
4774 {
4775 STRCPY(p, "file ");
4776 p += 5;
4777 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004778 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4779 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4781 return TRUE;
4782}
4783
4784/*
4785 * If fname is not a full path, make it a full path.
4786 * Returns pointer to allocated memory (NULL for failure).
4787 */
4788 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004789fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790{
4791 /*
4792 * Force expanding the path always for Unix, because symbolic links may
4793 * mess up the full path name, even though it starts with a '/'.
4794 * Also expand when there is ".." in the file name, try to remove it,
4795 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004796 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 * For MS-Windows also expand names like "longna~1" to "longname".
4798 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004799#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 return FullName_save(fname, TRUE);
4801#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004802 if (!vim_isAbsName(fname)
4803 || strstr((char *)fname, "..") != NULL
4804 || strstr((char *)fname, "//") != NULL
4805# ifdef BACKSLASH_IN_FILENAME
4806 || strstr((char *)fname, "\\\\") != NULL
4807# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004808# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004810# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 )
4812 return FullName_save(fname, FALSE);
4813
4814 fname = vim_strsave(fname);
4815
Bram Moolenaar9b942202007-10-03 12:31:33 +00004816# ifdef USE_FNAME_CASE
Bram Moolenaar00f148d2019-02-12 22:37:27 +01004817 if (fname != NULL)
4818 fname_case(fname, 0); /* set correct case for file name */
Bram Moolenaar9b942202007-10-03 12:31:33 +00004819# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820
4821 return fname;
4822#endif
4823}
4824
4825/*
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02004826 * Make "*ffname" a full file name, set "*sfname" to "*ffname" if not NULL.
4827 * "*ffname" becomes a pointer to allocated memory (or NULL).
4828 * When resolving a link both "*sfname" and "*ffname" will point to the same
4829 * allocated memory.
4830 * The "*ffname" and "*sfname" pointer values on call will not be freed.
4831 * Note that the resulting "*ffname" pointer should be considered not allocaed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004834fname_expand(
4835 buf_T *buf UNUSED,
4836 char_u **ffname,
4837 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838{
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02004839 if (*ffname == NULL) // no file name given, nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 return;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02004841 if (*sfname == NULL) // no short file name given, use ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 *sfname = *ffname;
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02004843 *ffname = fix_fname(*ffname); // expand to full path
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844
4845#ifdef FEAT_SHORTCUT
4846 if (!buf->b_p_bin)
4847 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004848 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849
Bram Moolenaar3d6014f2018-10-11 19:27:47 +02004850 // If the file name is a shortcut file, use the file it links to.
Bram Moolenaardce1e892019-02-10 23:18:53 +01004851 rfname = mch_resolve_path(*ffname, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004852 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 {
4854 vim_free(*ffname);
4855 *ffname = rfname;
4856 *sfname = rfname;
4857 }
4858 }
4859#endif
4860}
4861
4862/*
4863 * Get the file name for an argument list entry.
4864 */
4865 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004866alist_name(aentry_T *aep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867{
4868 buf_T *bp;
4869
4870 /* Use the name from the associated buffer if it exists. */
4871 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004872 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 return aep->ae_fname;
4874 return bp->b_fname;
4875}
4876
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877/*
4878 * do_arg_all(): Open up to 'count' windows, one for each argument.
4879 */
4880 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004881do_arg_all(
4882 int count,
4883 int forceit, /* hide buffers in current windows */
4884 int keep_tabs) /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885{
4886 int i;
4887 win_T *wp, *wpnext;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004888 char_u *opened; /* Array of weight for which args are open:
4889 * 0: not opened
4890 * 1: opened in other tab
4891 * 2: opened in curtab
4892 * 3: opened in curtab and curwin
4893 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004894 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 int use_firstwin = FALSE; /* use first window for arglist */
4896 int split_ret = OK;
4897 int p_ea_save;
4898 alist_T *alist; /* argument list to be used */
4899 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004900 tabpage_T *tpnext;
4901 int had_tab = cmdmod.tab;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004902 win_T *old_curwin, *last_curwin;
4903 tabpage_T *old_curtab, *last_curtab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004904 win_T *new_curwin = NULL;
4905 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906
4907 if (ARGCOUNT <= 0)
4908 {
4909 /* Don't give an error message. We don't want it when the ":all"
4910 * command is in the .vimrc. */
4911 return;
4912 }
4913 setpcmark();
4914
4915 opened_len = ARGCOUNT;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004916 opened = alloc_clear(opened_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 if (opened == NULL)
4918 return;
4919
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004920 /* Autocommands may do anything to the argument list. Make sure it's not
4921 * freed while we are working here by "locking" it. We still have to
4922 * watch out for its size to be changed. */
4923 alist = curwin->w_alist;
4924 ++alist->al_refcount;
4925
4926 old_curwin = curwin;
4927 old_curtab = curtab;
4928
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004929# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004931# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932
4933 /*
4934 * Try closing all windows that are not in the argument list.
4935 * Also close windows that are not full width;
4936 * When 'hidden' or "forceit" set the buffer becomes hidden.
4937 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004938 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004940 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004941 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004942 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004944 tpnext = curtab->tp_next;
4945 for (wp = firstwin; wp != NULL; wp = wpnext)
4946 {
4947 wpnext = wp->w_next;
4948 buf = wp->w_buffer;
4949 if (buf->b_ffname == NULL
Bram Moolenaar5a030a52016-12-01 17:48:29 +01004950 || (!keep_tabs && (buf->b_nwindows > 1
4951 || wp->w_width != Columns)))
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004952 i = opened_len;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004953 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004955 /* check if the buffer in this window is in the arglist */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004956 for (i = 0; i < opened_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004958 if (i < alist->al_ga.ga_len
4959 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
4960 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
Bram Moolenaar99499b12019-05-23 21:35:48 +02004961 buf->b_ffname, TRUE, TRUE) & FPC_SAME))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004963 int weight = 1;
4964
4965 if (old_curtab == curtab)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004966 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004967 ++weight;
4968 if (old_curwin == wp)
4969 ++weight;
4970 }
4971
4972 if (weight > (int)opened[i])
4973 {
4974 opened[i] = (char_u)weight;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004975 if (i == 0)
4976 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004977 if (new_curwin != NULL)
4978 new_curwin->w_arg_idx = opened_len;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004979 new_curwin = wp;
4980 new_curtab = curtab;
4981 }
4982 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004983 else if (keep_tabs)
4984 i = opened_len;
4985
4986 if (wp->w_alist != alist)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004987 {
4988 /* Use the current argument list for all windows
4989 * containing a file from it. */
4990 alist_unlink(wp->w_alist);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004991 wp->w_alist = alist;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004992 ++wp->w_alist->al_refcount;
4993 }
4994 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 }
4997 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004998 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005000 if (i == opened_len && !keep_tabs)/* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005002 if (buf_hide(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005003 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005005 /* If the buffer was changed, and we would like to hide it,
5006 * try autowriting. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02005007 if (!buf_hide(buf) && buf->b_nwindows <= 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005008 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005010 bufref_T bufref;
5011
5012 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005013
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005014 (void)autowrite(buf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005015
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005016 /* check if autocommands removed the window */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005017 if (!win_valid(wp) || !bufref_valid(&bufref))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005018 {
5019 wpnext = firstwin; /* start all over... */
5020 continue;
5021 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005022 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005023 /* don't close last window */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005024 if (ONE_WINDOW
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005025 && (first_tabpage->tp_next == NULL || !had_tab))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005026 use_firstwin = TRUE;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005027 else
5028 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005029 win_close(wp, !buf_hide(buf) && !bufIsChanged(buf));
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005030
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005031 /* check if autocommands removed the next window */
5032 if (!win_valid(wpnext))
5033 wpnext = firstwin; /* start all over... */
Bram Moolenaar4033c552017-09-16 20:54:51 +02005034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 }
5037 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005038
5039 /* Without the ":tab" modifier only do the current tab page. */
5040 if (had_tab == 0 || tpnext == NULL)
5041 break;
5042
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005043 /* check if autocommands removed the next tab page */
5044 if (!valid_tabpage(tpnext))
5045 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005046
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005047 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 }
5049
5050 /*
5051 * Open a window for files in the argument list that don't have one.
5052 * ARGCOUNT may change while doing this, because of autocommands.
5053 */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005054 if (count > opened_len || count <= 0)
5055 count = opened_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 /* Don't execute Win/Buf Enter/Leave autocommands here. */
5058 ++autocmd_no_enter;
5059 ++autocmd_no_leave;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005060 last_curwin = curwin;
5061 last_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005063 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
5064 * leaving an empty tab page when executed locally. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005065 if (keep_tabs && BUFEMPTY() && curbuf->b_nwindows == 1
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005066 && curbuf->b_ffname == NULL && !curbuf->b_changed)
5067 use_firstwin = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005068
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005069 for (i = 0; i < count && i < opened_len && !got_int; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 {
5071 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
5072 arg_had_last = TRUE;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005073 if (opened[i] > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 {
5075 /* Move the already present window to below the current window */
5076 if (curwin->w_arg_idx != i)
5077 {
5078 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
5079 {
5080 if (wpnext->w_arg_idx == i)
5081 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005082 if (keep_tabs)
5083 {
5084 new_curwin = wpnext;
5085 new_curtab = curtab;
5086 }
5087 else
5088 win_move_after(wpnext, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 break;
5090 }
5091 }
5092 }
5093 }
5094 else if (split_ret == OK)
5095 {
5096 if (!use_firstwin) /* split current window */
5097 {
5098 p_ea_save = p_ea;
5099 p_ea = TRUE; /* use space from all windows */
5100 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5101 p_ea = p_ea_save;
5102 if (split_ret == FAIL)
5103 continue;
5104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 else /* first window: do autocmd for leaving this buffer */
5106 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107
5108 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005109 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 */
5111 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005112 if (i == 0)
5113 {
5114 new_curwin = curwin;
5115 new_curtab = curtab;
5116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
5118 ECMD_ONE,
Bram Moolenaareb44a682017-08-03 22:44:55 +02005119 ((buf_hide(curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00005121 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 if (use_firstwin)
5123 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 use_firstwin = FALSE;
5125 }
5126 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005127
5128 /* When ":tab" was used open a new tab for a new window repeatedly. */
5129 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5130 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 }
5132
5133 /* Remove the "lock" on the argument list. */
5134 alist_unlink(alist);
5135
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 --autocmd_no_enter;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005137
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005138 /* restore last referenced tabpage's curwin */
5139 if (last_curtab != new_curtab)
5140 {
5141 if (valid_tabpage(last_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005142 goto_tabpage_tp(last_curtab, TRUE, TRUE);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005143 if (win_valid(last_curwin))
5144 win_enter(last_curwin, FALSE);
5145 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005146 /* to window with first arg */
5147 if (valid_tabpage(new_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005148 goto_tabpage_tp(new_curtab, TRUE, TRUE);
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005149 if (win_valid(new_curwin))
5150 win_enter(new_curwin, FALSE);
5151
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 vim_free(opened);
5154}
5155
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156/*
5157 * Open a window for a number of buffers.
5158 */
5159 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005160ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161{
5162 buf_T *buf;
5163 win_T *wp, *wpnext;
5164 int split_ret = OK;
5165 int p_ea_save;
5166 int open_wins = 0;
5167 int r;
5168 int count; /* Maximum number of windows to open. */
5169 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005170 int had_tab = cmdmod.tab;
5171 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172
5173 if (eap->addr_count == 0) /* make as many windows as possible */
5174 count = 9999;
5175 else
5176 count = eap->line2; /* make as many windows as specified */
5177 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5178 all = FALSE;
5179 else
5180 all = TRUE;
5181
5182 setpcmark();
5183
5184#ifdef FEAT_GUI
5185 need_mouse_correct = TRUE;
5186#endif
5187
5188 /*
5189 * Close superfluous windows (two windows for the same buffer).
5190 * Also close windows that are not full-width.
5191 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005192 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005193 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005194 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005196 tpnext = curtab->tp_next;
5197 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005199 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005200 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005201 || ((cmdmod.split & WSP_VERT)
5202 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005203 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005204 : wp->w_width != Columns)
Bram Moolenaar4033c552017-09-16 20:54:51 +02005205 || (had_tab > 0 && wp != firstwin)) && !ONE_WINDOW
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005206 && !(wp->w_closing || wp->w_buffer->b_locked > 0))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005207 {
5208 win_close(wp, FALSE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005209 wpnext = firstwin; /* just in case an autocommand does
5210 something strange with windows */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005211 tpnext = first_tabpage; /* start all over... */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005212 open_wins = 0;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005213 }
5214 else
5215 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005217
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005218 /* Without the ":tab" modifier only do the current tab page. */
5219 if (had_tab == 0 || tpnext == NULL)
5220 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005221 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 }
5223
5224 /*
5225 * Go through the buffer list. When a buffer doesn't have a window yet,
5226 * open one. Otherwise move the window to the right position.
5227 * Watch out for autocommands that delete buffers or windows!
5228 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 /* Don't execute Win/Buf Enter/Leave autocommands here. */
5230 ++autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 win_enter(lastwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5234 {
5235 /* Check if this buffer needs a window */
5236 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5237 continue;
5238
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005239 if (had_tab != 0)
5240 {
5241 /* With the ":tab" modifier don't move the window. */
5242 if (buf->b_nwindows > 0)
5243 wp = lastwin; /* buffer has a window, skip it */
5244 else
5245 wp = NULL;
5246 }
5247 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005248 {
5249 /* Check if this buffer already has a window */
Bram Moolenaar29323592016-07-24 22:04:11 +02005250 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005251 if (wp->w_buffer == buf)
5252 break;
5253 /* If the buffer already has a window, move it */
5254 if (wp != NULL)
5255 win_move_after(wp, curwin);
5256 }
5257
5258 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005260 bufref_T bufref;
5261
5262 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005263
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 /* Split the window and put the buffer in it */
5265 p_ea_save = p_ea;
5266 p_ea = TRUE; /* use space from all windows */
5267 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5268 ++open_wins;
5269 p_ea = p_ea_save;
5270 if (split_ret == FAIL)
5271 continue;
5272
5273 /* Open the buffer in this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 swap_exists_action = SEA_DIALOG;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005276 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005278 /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 swap_exists_action = SEA_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 break;
5281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 if (swap_exists_action == SEA_QUIT)
5283 {
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005284#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005285 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005286
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005287 /* Reset the error/interrupt/exception state here so that
5288 * aborting() returns FALSE when closing a window. */
5289 enter_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005290#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005291
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005292 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 win_close(curwin, TRUE);
5294 --open_wins;
5295 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005296 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005297
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005298#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005299 /* Restore the error/interrupt/exception state if not
5300 * discarded by a new aborting error, interrupt, or uncaught
5301 * exception. */
5302 leave_cleanup(&cs);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02005303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 }
5305 else
5306 handle_swap_exists(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 }
5308
5309 ui_breakcheck();
5310 if (got_int)
5311 {
5312 (void)vgetc(); /* only break the file loading, not the rest */
5313 break;
5314 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005315#ifdef FEAT_EVAL
5316 /* Autocommands deleted the buffer or aborted script processing!!! */
5317 if (aborting())
5318 break;
5319#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005320 /* When ":tab" was used open a new tab for a new window repeatedly. */
5321 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5322 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 --autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 win_enter(firstwin, FALSE); /* back to first window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327
5328 /*
5329 * Close superfluous windows.
5330 */
5331 for (wp = lastwin; open_wins > count; )
5332 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005333 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 || autowrite(wp->w_buffer, FALSE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 if (!win_valid(wp))
5336 {
5337 /* BufWrite Autocommands made the window invalid, start over */
5338 wp = lastwin;
5339 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005340 else if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005342 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 --open_wins;
5344 wp = lastwin;
5345 }
5346 else
5347 {
5348 wp = wp->w_prev;
5349 if (wp == NULL)
5350 break;
5351 }
5352 }
5353}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005356static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005357
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358/*
5359 * do_modelines() - process mode lines for the current file
5360 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005361 * "flags" can be:
5362 * OPT_WINONLY only set options local to window
5363 * OPT_NOWIN don't set options local to window
5364 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 * Returns immediately if the "ml" option isn't set.
5366 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005368do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005370 linenr_T lnum;
5371 int nmlines;
5372 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373
5374 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5375 return;
5376
5377 /* Disallow recursive entry here. Can happen when executing a modeline
5378 * triggers an autocommand, which reloads modelines with a ":do". */
5379 if (entered)
5380 return;
5381
5382 ++entered;
5383 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5384 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005385 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 nmlines = 0;
5387
5388 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5389 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005390 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 nmlines = 0;
5392 --entered;
5393}
5394
5395#include "version.h" /* for version number */
5396
5397/*
5398 * chk_modeline() - check a single line for a mode string
5399 * Return FAIL if an error encountered.
5400 */
5401 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005402chk_modeline(
5403 linenr_T lnum,
5404 int flags) /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405{
5406 char_u *s;
5407 char_u *e;
5408 char_u *linecopy; /* local copy of any modeline found */
5409 int prev;
5410 int vers;
5411 int end;
5412 int retval = OK;
5413 char_u *save_sourcing_name;
5414 linenr_T save_sourcing_lnum;
5415#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005416 sctx_T save_current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417#endif
5418
5419 prev = -1;
5420 for (s = ml_get(lnum); *s != NUL; ++s)
5421 {
5422 if (prev == -1 || vim_isspace(prev))
5423 {
5424 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5425 || STRNCMP(s, "vi:", (size_t)3) == 0)
5426 break;
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005427 /* Accept both "vim" and "Vim". */
5428 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 {
5430 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5431 e = s + 4;
5432 else
5433 e = s + 3;
5434 vers = getdigits(&e);
5435 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005436 && (s[0] != 'V'
5437 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 && (s[3] == ':'
5439 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5440 || (VIM_VERSION_100 < vers && s[3] == '<')
5441 || (VIM_VERSION_100 > vers && s[3] == '>')
5442 || (VIM_VERSION_100 == vers && s[3] == '=')))
5443 break;
5444 }
5445 }
5446 prev = *s;
5447 }
5448
5449 if (*s)
5450 {
5451 do /* skip over "ex:", "vi:" or "vim:" */
5452 ++s;
5453 while (s[-1] != ':');
5454
5455 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5456 if (linecopy == NULL)
5457 return FAIL;
5458
5459 save_sourcing_lnum = sourcing_lnum;
5460 save_sourcing_name = sourcing_name;
5461 sourcing_lnum = lnum; /* prepare for emsg() */
5462 sourcing_name = (char_u *)"modelines";
5463
5464 end = FALSE;
5465 while (end == FALSE)
5466 {
5467 s = skipwhite(s);
5468 if (*s == NUL)
5469 break;
5470
5471 /*
5472 * Find end of set command: ':' or end of line.
5473 * Skip over "\:", replacing it with ":".
5474 */
5475 for (e = s; *e != ':' && *e != NUL; ++e)
5476 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005477 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 if (*e == NUL)
5479 end = TRUE;
5480
5481 /*
5482 * If there is a "set" command, require a terminating ':' and
5483 * ignore the stuff after the ':'.
5484 * "vi:set opt opt opt: foo" -- foo not interpreted
5485 * "vi:opt opt opt: foo" -- foo interpreted
5486 * Accept "se" for compatibility with Elvis.
5487 */
5488 if (STRNCMP(s, "set ", (size_t)4) == 0
5489 || STRNCMP(s, "se ", (size_t)3) == 0)
5490 {
5491 if (*e != ':') /* no terminating ':'? */
5492 break;
5493 end = TRUE;
5494 s = vim_strchr(s, ' ') + 1;
5495 }
5496 *e = NUL; /* truncate the set command */
5497
5498 if (*s != NUL) /* skip over an empty "::" */
5499 {
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005500 int secure_save = secure;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005502 save_current_sctx = current_sctx;
5503 current_sctx.sc_sid = SID_MODELINE;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005504 current_sctx.sc_seq = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005505 current_sctx.sc_lnum = 0;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02005506 current_sctx.sc_version = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507#endif
Bram Moolenaar5958f952018-11-20 04:25:21 +01005508 // Make sure no risky things are executed as a side effect.
Bram Moolenaar82b033e2019-03-24 14:02:04 +01005509 secure = 1;
Bram Moolenaar5958f952018-11-20 04:25:21 +01005510
Bram Moolenaara3227e22006-03-08 21:32:40 +00005511 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar5958f952018-11-20 04:25:21 +01005512
Bram Moolenaar48f377a2018-12-21 13:03:28 +01005513 secure = secure_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005515 current_sctx = save_current_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516#endif
5517 if (retval == FAIL) /* stop if error found */
5518 break;
5519 }
5520 s = e + 1; /* advance to next part */
5521 }
5522
5523 sourcing_lnum = save_sourcing_lnum;
5524 sourcing_name = save_sourcing_name;
5525
5526 vim_free(linecopy);
5527 }
5528 return retval;
5529}
5530
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005531#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005533read_viminfo_bufferlist(
5534 vir_T *virp,
5535 int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536{
5537 char_u *tab;
5538 linenr_T lnum;
5539 colnr_T col;
5540 buf_T *buf;
5541 char_u *sfname;
5542 char_u *xline;
5543
5544 /* Handle long line and escaped characters. */
5545 xline = viminfo_readstring(virp, 1, FALSE);
5546
5547 /* don't read in if there are files on the command-line or if writing: */
5548 if (xline != NULL && !writing && ARGCOUNT == 0
5549 && find_viminfo_parameter('%') != NULL)
5550 {
5551 /* Format is: <fname> Tab <lnum> Tab <col>.
5552 * Watch out for a Tab in the file name, work from the end. */
5553 lnum = 0;
5554 col = 0;
5555 tab = vim_strrchr(xline, '\t');
5556 if (tab != NULL)
5557 {
5558 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005559 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 tab = vim_strrchr(xline, '\t');
5561 if (tab != NULL)
5562 {
5563 *tab++ = '\0';
5564 lnum = atol((char *)tab);
5565 }
5566 }
5567
5568 /* Expand "~/" in the file name at "line + 1" to a full path.
5569 * Then try shortening it by comparing with the current directory */
5570 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005571 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572
5573 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5574 if (buf != NULL) /* just in case... */
5575 {
5576 buf->b_last_cursor.lnum = lnum;
5577 buf->b_last_cursor.col = col;
5578 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5579 }
5580 }
5581 vim_free(xline);
5582
5583 return viminfo_readline(virp);
5584}
5585
5586 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005587write_viminfo_bufferlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588{
5589 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005591 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005593 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594
5595 if (find_viminfo_parameter('%') == NULL)
5596 return;
5597
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005598 /* Without a number -1 is returned: do all buffers. */
5599 max_buffers = get_viminfo_parameter('%');
5600
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005602#define LINE_BUF_LEN (MAXPATHL + 40)
5603 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 if (line == NULL)
5605 return;
5606
Bram Moolenaarf740b292006-02-16 22:11:02 +00005607 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 set_last_cursor(win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005610 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar29323592016-07-24 22:04:11 +02005611 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 {
5613 if (buf->b_fname == NULL
5614 || !buf->b_p_bl
5615#ifdef FEAT_QUICKFIX
5616 || bt_quickfix(buf)
5617#endif
Bram Moolenaare6278052017-08-13 18:11:17 +02005618#ifdef FEAT_TERMINAL
5619 || bt_terminal(buf)
5620#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 || removable(buf->b_ffname))
5622 continue;
5623
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005624 if (max_buffers-- == 0)
5625 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 putc('%', fp);
5627 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005628 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 (long)buf->b_last_cursor.lnum,
5630 buf->b_last_cursor.col);
5631 viminfo_writestring(fp, line);
5632 }
5633 vim_free(line);
5634}
5635#endif
5636
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005637/*
Bram Moolenaar91335e52018-08-01 17:53:12 +02005638 * Return TRUE if "buf" is a normal buffer, 'buftype' is empty.
5639 */
5640 int
5641bt_normal(buf_T *buf)
5642{
5643 return buf != NULL && buf->b_p_bt[0] == NUL;
5644}
5645
Bram Moolenaar113e1072019-01-20 15:30:40 +01005646#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar91335e52018-08-01 17:53:12 +02005647/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005648 * Return TRUE if "buf" is the quickfix buffer.
5649 */
5650 int
5651bt_quickfix(buf_T *buf)
5652{
5653 return buf != NULL && buf->b_p_bt[0] == 'q';
5654}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005655#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005656
Bram Moolenaar113e1072019-01-20 15:30:40 +01005657#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005658/*
5659 * Return TRUE if "buf" is a terminal buffer.
5660 */
5661 int
5662bt_terminal(buf_T *buf)
5663{
5664 return buf != NULL && buf->b_p_bt[0] == 't';
5665}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005666#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005667
5668/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005669 * Return TRUE if "buf" is a help buffer.
5670 */
5671 int
5672bt_help(buf_T *buf)
5673{
5674 return buf != NULL && buf->b_help;
5675}
5676
5677/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02005678 * Return TRUE if "buf" is a prompt buffer.
5679 */
5680 int
5681bt_prompt(buf_T *buf)
5682{
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005683 return buf != NULL && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'r';
5684}
5685
5686/*
5687 * Return TRUE if "buf" is a buffer for a popup window.
5688 */
5689 int
5690bt_popup(buf_T *buf)
5691{
5692 return buf != NULL && buf->b_p_bt != NULL
5693 && buf->b_p_bt[0] == 'p' && buf->b_p_bt[1] == 'o';
Bram Moolenaarf2732452018-06-03 14:47:35 +02005694}
5695
5696/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005697 * Return TRUE if "buf" is a "nofile", "acwrite", "terminal" or "prompt"
5698 * buffer. This means the buffer name is not a file name.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005699 */
5700 int
5701bt_nofile(buf_T *buf)
5702{
5703 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5704 || buf->b_p_bt[0] == 'a'
Bram Moolenaarf2732452018-06-03 14:47:35 +02005705 || buf->b_p_bt[0] == 't'
5706 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005707}
5708
5709/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005710 * Return TRUE if "buf" is a "nowrite", "nofile", "terminal" or "prompt"
5711 * buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005712 */
5713 int
5714bt_dontwrite(buf_T *buf)
5715{
Bram Moolenaarf2732452018-06-03 14:47:35 +02005716 return buf != NULL && (buf->b_p_bt[0] == 'n'
5717 || buf->b_p_bt[0] == 't'
5718 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005719}
5720
Bram Moolenaar113e1072019-01-20 15:30:40 +01005721#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005722 int
5723bt_dontwrite_msg(buf_T *buf)
5724{
5725 if (bt_dontwrite(buf))
5726 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005727 emsg(_("E382: Cannot write, 'buftype' option is set"));
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005728 return TRUE;
5729 }
5730 return FALSE;
5731}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005732#endif
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005733
5734/*
5735 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
5736 * and 'bufhidden'.
5737 */
5738 int
5739buf_hide(buf_T *buf)
5740{
5741 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
5742 switch (buf->b_p_bh[0])
5743 {
5744 case 'u': /* "unload" */
5745 case 'w': /* "wipe" */
5746 case 'd': return FALSE; /* "delete" */
5747 case 'h': return TRUE; /* "hide" */
5748 }
5749 return (p_hid || cmdmod.hide);
5750}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751
5752/*
5753 * Return special buffer name.
5754 * Returns NULL when the buffer has a normal file name.
5755 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005756 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005757buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758{
Bram Moolenaar4033c552017-09-16 20:54:51 +02005759#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005761 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005762 /*
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005763 * Differentiate between the quickfix and location list buffers using
5764 * the buffer number stored in the global quickfix stack.
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005765 */
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005766 if (buf->b_fnum == qf_stack_get_bufnr())
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005767 return (char_u *)_(msg_qflist);
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005768 else
5769 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02005772
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 /* There is no _file_ when 'buftype' is "nofile", b_sfname
Bram Moolenaar21554412017-07-24 21:44:43 +02005774 * contains the name as specified by the user. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 if (bt_nofile(buf))
5776 {
Bram Moolenaar21554412017-07-24 21:44:43 +02005777#ifdef FEAT_TERMINAL
5778 if (buf->b_term != NULL)
5779 return term_get_status_text(buf->b_term);
5780#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02005781 if (buf->b_fname != NULL)
5782 return buf->b_fname;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02005783#ifdef FEAT_JOB_CHANNEL
5784 if (bt_prompt(buf))
5785 return (char_u *)_("[Prompt]");
5786#endif
Bram Moolenaarc6896e22019-05-30 22:32:34 +02005787#ifdef FEAT_TEXT_PROP
5788 if (bt_popup(buf))
5789 return (char_u *)_("[Popup]");
5790#endif
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005791 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 }
Bram Moolenaar21554412017-07-24 21:44:43 +02005793
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005795 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 return NULL;
5797}
5798
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02005799#if defined(FEAT_JOB_CHANNEL) \
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005800 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5801 || defined(PROTO)
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02005802# define SWITCH_TO_WIN
5803
5804/*
5805 * Find a window that contains "buf" and switch to it.
5806 * If there is no such window, use the current window and change "curbuf".
5807 * Caller must initialize save_curbuf to NULL.
5808 * restore_win_for_buf() MUST be called later!
5809 */
5810 void
5811switch_to_win_for_buf(
5812 buf_T *buf,
5813 win_T **save_curwinp,
5814 tabpage_T **save_curtabp,
5815 bufref_T *save_curbuf)
5816{
5817 win_T *wp;
5818 tabpage_T *tp;
5819
5820 if (find_win_for_buf(buf, &wp, &tp) == FAIL)
5821 switch_buffer(save_curbuf, buf);
5822 else if (switch_win(save_curwinp, save_curtabp, wp, tp, TRUE) == FAIL)
5823 {
5824 restore_win(*save_curwinp, *save_curtabp, TRUE);
5825 switch_buffer(save_curbuf, buf);
5826 }
5827}
5828
5829 void
5830restore_win_for_buf(
5831 win_T *save_curwin,
5832 tabpage_T *save_curtab,
5833 bufref_T *save_curbuf)
5834{
5835 if (save_curbuf->br_buf == NULL)
5836 restore_win(save_curwin, save_curtab, TRUE);
5837 else
5838 restore_buffer(save_curbuf);
5839}
5840#endif
5841
Bram Moolenaar4033c552017-09-16 20:54:51 +02005842#if defined(FEAT_QUICKFIX) || defined(SWITCH_TO_WIN) || defined(PROTO)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005843/*
5844 * Find a window for buffer "buf".
5845 * If found OK is returned and "wp" and "tp" are set to the window and tabpage.
5846 * If not found FAIL is returned.
5847 */
5848 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005849find_win_for_buf(
5850 buf_T *buf,
5851 win_T **wp,
5852 tabpage_T **tp)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005853{
5854 FOR_ALL_TAB_WINDOWS(*tp, *wp)
5855 if ((*wp)->w_buffer == buf)
5856 goto win_found;
5857 return FAIL;
5858win_found:
5859 return OK;
5860}
5861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863/*
5864 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5865 */
5866 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005867set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868{
5869 if (on != curbuf->b_p_bl)
5870 {
5871 curbuf->b_p_bl = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 if (on)
5873 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5874 else
5875 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 }
5877}
5878
5879/*
5880 * Read the file for "buf" again and check if the contents changed.
5881 * Return TRUE if it changed or this could not be checked.
5882 */
5883 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005884buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885{
5886 buf_T *newbuf;
5887 int differ = TRUE;
5888 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 exarg_T ea;
5891
5892 /* Allocate a buffer without putting it in the buffer list. */
5893 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5894 if (newbuf == NULL)
5895 return TRUE;
5896
5897 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5898 if (prep_exarg(&ea, buf) == FAIL)
5899 {
5900 wipe_buffer(newbuf, FALSE);
5901 return TRUE;
5902 }
5903
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 /* set curwin/curbuf to buf and save a few things */
5905 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906
Bram Moolenaar4770d092006-01-12 23:22:24 +00005907 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 && readfile(buf->b_ffname, buf->b_fname,
5909 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5910 &ea, READ_NEW | READ_DUMMY) == OK)
5911 {
5912 /* compare the two files line by line */
5913 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5914 {
5915 differ = FALSE;
5916 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5917 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5918 {
5919 differ = TRUE;
5920 break;
5921 }
5922 }
5923 }
5924 vim_free(ea.cmd);
5925
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 /* restore curwin/curbuf and a few other things */
5927 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928
5929 if (curbuf != newbuf) /* safety check */
5930 wipe_buffer(newbuf, FALSE);
5931
5932 return differ;
5933}
5934
5935/*
5936 * Wipe out a buffer and decrement the last buffer number if it was used for
5937 * this buffer. Call this to wipe out a temp buffer that does not contain any
5938 * marks.
5939 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005941wipe_buffer(
5942 buf_T *buf,
5943 int aucmd UNUSED) /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944{
5945 if (buf->b_fnum == top_file_num - 1)
5946 --top_file_num;
5947
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005949 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005950
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005951 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005952
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005954 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955}