blob: 3285577412fc518e8c1c7d01a91ac46d38ad5415 [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
30#if defined(FEAT_CMDL_COMPL) || defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010031static char_u *buflist_match(regmatch_T *rmp, buf_T *buf, int ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +000032# define HAVE_BUFLIST_MATCH
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010033static char_u *fname_match(regmatch_T *rmp, char_u *name, int ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +000034#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010035static void buflist_setfpos(buf_T *buf, win_T *win, linenr_T lnum, colnr_T col, int copy_options);
36static wininfo_T *find_wininfo(buf_T *buf, int skip_diff_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +020038static buf_T *buflist_findname_stat(char_u *ffname, stat_T *st);
39static int otherfile_buf(buf_T *buf, char_u *ffname, stat_T *stp);
40static int buf_same_ino(buf_T *buf, stat_T *stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000041#else
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010042static int otherfile_buf(buf_T *buf, char_u *ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000043#endif
44#ifdef FEAT_TITLE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010045static int ti_change(char_u *str, char_u **last);
Bram Moolenaar071d4272004-06-13 20:20:40 +000046#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010047static int append_arg_number(win_T *wp, char_u *buf, int buflen, int add_file);
48static void free_buffer(buf_T *);
49static void free_buffer_stuff(buf_T *buf, int free_options);
50static void clear_wininfo(buf_T *buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000051
52#ifdef UNIX
53# define dev_T dev_t
54#else
55# define dev_T unsigned
56#endif
57
58#if defined(FEAT_SIGNS)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010059static void insert_sign(buf_T *buf, signlist_T *prev, signlist_T *next, int id, linenr_T lnum, int typenr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000060#endif
61
Bram Moolenaar4033c552017-09-16 20:54:51 +020062#if defined(FEAT_QUICKFIX)
Bram Moolenaar7fd73202010-07-25 16:58:46 +020063static char *msg_loclist = N_("[Location List]");
64static char *msg_qflist = N_("[Quickfix List]");
65#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +010066static char *e_auabort = N_("E855: Autocommands caused command to abort");
Bram Moolenaar7fd73202010-07-25 16:58:46 +020067
Bram Moolenaarb25f9a92016-07-10 18:21:50 +020068/* Number of times free_buffer() was called. */
69static int buf_free_count = 0;
70
Bram Moolenaarf71d7b92016-08-09 22:14:05 +020071/* Read data from buffer for retrying. */
72 static int
73read_buffer(
74 int read_stdin, /* read file from stdin, otherwise fifo */
75 exarg_T *eap, /* for forced 'ff' and 'fenc' or NULL */
76 int flags) /* extra flags for readfile() */
77{
78 int retval = OK;
79 linenr_T line_count;
80
81 /*
82 * Read from the buffer which the text is already filled in and append at
83 * the end. This makes it possible to retry when 'fileformat' or
84 * 'fileencoding' was guessed wrong.
85 */
86 line_count = curbuf->b_ml.ml_line_count;
87 retval = readfile(
88 read_stdin ? NULL : curbuf->b_ffname,
89 read_stdin ? NULL : curbuf->b_fname,
90 (linenr_T)line_count, (linenr_T)0, (linenr_T)MAXLNUM, eap,
91 flags | READ_BUFFER);
92 if (retval == OK)
93 {
94 /* Delete the binary lines. */
95 while (--line_count >= 0)
96 ml_delete((linenr_T)1, FALSE);
97 }
98 else
99 {
100 /* Delete the converted lines. */
101 while (curbuf->b_ml.ml_line_count > line_count)
102 ml_delete(line_count, FALSE);
103 }
104 /* Put the cursor on the first line. */
105 curwin->w_cursor.lnum = 1;
106 curwin->w_cursor.col = 0;
107
108 if (read_stdin)
109 {
110 /* Set or reset 'modified' before executing autocommands, so that
111 * it can be changed there. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100112 if (!readonlymode && !BUFEMPTY())
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200113 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100114 else if (retval == OK)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200115 unchanged(curbuf, FALSE);
116
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100117 if (retval == OK)
118 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100119#ifdef FEAT_EVAL
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100120 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100121 curbuf, &retval);
122#else
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100123 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200124#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100125 }
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200126 }
127 return retval;
128}
129
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200131 * Open current buffer, that is: open the memfile and read the file into
132 * memory.
133 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 */
135 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100136open_buffer(
137 int read_stdin, /* read file from stdin */
138 exarg_T *eap, /* for forced 'ff' and 'fenc' or NULL */
139 int flags) /* extra flags for readfile() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140{
141 int retval = OK;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200142 bufref_T old_curbuf;
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100143#ifdef FEAT_SYN_HL
144 long old_tw = curbuf->b_p_tw;
145#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200146 int read_fifo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147
148 /*
149 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
150 * When re-entering the same buffer, it should not change, because the
151 * user may have reset the flag by hand.
152 */
153 if (readonlymode && curbuf->b_ffname != NULL
154 && (curbuf->b_flags & BF_NEVERLOADED))
155 curbuf->b_p_ro = TRUE;
156
Bram Moolenaar4770d092006-01-12 23:22:24 +0000157 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 {
159 /*
160 * There MUST be a memfile, otherwise we can't do anything
161 * If we can't create one for the current buffer, take another buffer
162 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100163 close_buffer(NULL, curbuf, 0, FALSE);
Bram Moolenaar29323592016-07-24 22:04:11 +0200164 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165 if (curbuf->b_ml.ml_mfp != NULL)
166 break;
167 /*
168 * if there is no memfile at all, exit
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000169 * This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 */
171 if (curbuf == NULL)
172 {
173 EMSG(_("E82: Cannot allocate any buffer, exiting..."));
174 getout(2);
175 }
176 EMSG(_("E83: Cannot allocate buffer, using other one..."));
177 enter_buffer(curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100178#ifdef FEAT_SYN_HL
179 if (old_tw != curbuf->b_p_tw)
180 check_colorcolumn(curwin);
181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 return FAIL;
183 }
184
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 /* The autocommands in readfile() may change the buffer, but only AFTER
186 * reading the file. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200187 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 modified_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189
190 /* mark cursor position as being invalid */
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100191 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192
193 if (curbuf->b_ffname != NULL
194#ifdef FEAT_NETBEANS_INTG
195 && netbeansReadFile
196#endif
197 )
198 {
Bram Moolenaar426dd022016-03-15 15:09:29 +0100199 int old_msg_silent = msg_silent;
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200200#ifdef UNIX
201 int save_bin = curbuf->b_p_bin;
202 int perm;
203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204#ifdef FEAT_NETBEANS_INTG
205 int oldFire = netbeansFireChanges;
206
207 netbeansFireChanges = 0;
208#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200209#ifdef UNIX
210 perm = mch_getperm(curbuf->b_ffname);
211 if (perm >= 0 && (0
212# ifdef S_ISFIFO
213 || S_ISFIFO(perm)
214# endif
215# ifdef S_ISSOCK
216 || S_ISSOCK(perm)
217# endif
Bram Moolenaarf04507d2016-08-20 15:05:39 +0200218# ifdef OPEN_CHR_FILES
219 || (S_ISCHR(perm) && is_dev_fd_file(curbuf->b_ffname))
220# endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200221 ))
222 read_fifo = TRUE;
223 if (read_fifo)
224 curbuf->b_p_bin = TRUE;
225#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100226 if (shortmess(SHM_FILEINFO))
227 msg_silent = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200229 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200230 flags | READ_NEW | (read_fifo ? READ_FIFO : 0));
231#ifdef UNIX
232 if (read_fifo)
233 {
234 curbuf->b_p_bin = save_bin;
235 if (retval == OK)
236 retval = read_buffer(FALSE, eap, flags);
237 }
238#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100239 msg_silent = old_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240#ifdef FEAT_NETBEANS_INTG
241 netbeansFireChanges = oldFire;
242#endif
243 /* Help buffer is filtered. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +0200244 if (bt_help(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 fix_help_buffer();
246 }
247 else if (read_stdin)
248 {
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200249 int save_bin = curbuf->b_p_bin;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250
251 /*
252 * First read the text in binary mode into the buffer.
253 * Then read from that same buffer and append at the end. This makes
254 * it possible to retry when 'fileformat' or 'fileencoding' was
255 * guessed wrong.
256 */
257 curbuf->b_p_bin = TRUE;
258 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200259 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
260 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 curbuf->b_p_bin = save_bin;
262 if (retval == OK)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200263 retval = read_buffer(TRUE, eap, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264 }
265
266 /* if first time loading this buffer, init b_chartab[] */
267 if (curbuf->b_flags & BF_NEVERLOADED)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100268 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 (void)buf_init_chartab(curbuf, FALSE);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100270#ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100271 parse_cino(curbuf);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100272#endif
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274
275 /*
276 * Set/reset the Changed flag first, autocmds may change the buffer.
277 * Apply the automatic commands, before processing the modelines.
278 * So the modelines have priority over auto commands.
279 */
280 /* When reading stdin, the buffer contents always needs writing, so set
281 * the changed flag. Unless in readonly mode: "ls | gview -".
282 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000283 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284 || modified_was_set /* ":set modified" used in autocmd */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100285#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000288 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100290 else if (retval == OK && !read_stdin && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 unchanged(curbuf, FALSE);
292 save_file_ff(curbuf); /* keep this fileformat */
293
294 /* require "!" to overwrite the file, because it wasn't read completely */
295#ifdef FEAT_EVAL
296 if (aborting())
297#else
298 if (got_int)
299#endif
300 curbuf->b_flags |= BF_READERR;
301
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000302#ifdef FEAT_FOLDING
303 /* Need to update automatic folding. Do this before the autocommands,
304 * they may use the fold info. */
305 foldUpdateAll(curwin);
306#endif
307
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 /* need to set w_topline, unless some autocommand already did that. */
309 if (!(curwin->w_valid & VALID_TOPLINE))
310 {
311 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100312#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100316#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100318#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320#endif
321
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100322 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 /*
325 * The autocommands may have changed the current buffer. Apply the
326 * modelines to the correct buffer, if it still exists and is loaded.
327 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200328 if (bufref_valid(&old_curbuf) && old_curbuf.br_buf->b_ml.ml_mfp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 {
330 aco_save_T aco;
331
332 /* Go to the buffer that was opened. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200333 aucmd_prepbuf(&aco, old_curbuf.br_buf);
Bram Moolenaara3227e22006-03-08 21:32:40 +0000334 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
336
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100337#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100339 &retval);
340#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343
344 /* restore curwin/curbuf and a few other things */
345 aucmd_restbuf(&aco);
346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 }
348
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 return retval;
350}
351
352/*
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200353 * Store "buf" in "bufref" and set the free count.
354 */
355 void
356set_bufref(bufref_T *bufref, buf_T *buf)
357{
358 bufref->br_buf = buf;
Bram Moolenaarfadacf02017-06-19 20:35:32 +0200359 bufref->br_fnum = buf == NULL ? 0 : buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200360 bufref->br_buf_free_count = buf_free_count;
361}
362
363/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200364 * Return TRUE if "bufref->br_buf" points to the same buffer as when
365 * set_bufref() was called and it is a valid buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200366 * Only goes through the buffer list if buf_free_count changed.
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200367 * Also checks if b_fnum is still the same, a :bwipe followed by :new might get
368 * the same allocated memory, but it's a different buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200369 */
370 int
371bufref_valid(bufref_T *bufref)
372{
373 return bufref->br_buf_free_count == buf_free_count
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200374 ? TRUE : buf_valid(bufref->br_buf)
375 && bufref->br_fnum == bufref->br_buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200376}
377
378/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200380 * This can be slow if there are many buffers, prefer using bufref_valid().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 */
382 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100383buf_valid(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384{
385 buf_T *bp;
386
Bram Moolenaar82404332016-07-10 17:00:38 +0200387 /* Assume that we more often have a recent buffer, start with the last
388 * one. */
389 for (bp = lastbuf; bp != NULL; bp = bp->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 if (bp == buf)
391 return TRUE;
392 return FALSE;
393}
394
395/*
Bram Moolenaar480778b2016-07-14 22:09:39 +0200396 * A hash table used to quickly lookup a buffer by its number.
397 */
398static hashtab_T buf_hashtab;
399
400 static void
401buf_hashtab_add(buf_T *buf)
402{
403 sprintf((char *)buf->b_key, "%x", buf->b_fnum);
404 if (hash_add(&buf_hashtab, buf->b_key) == FAIL)
405 EMSG(_("E931: Buffer cannot be registered"));
406}
407
408 static void
409buf_hashtab_remove(buf_T *buf)
410{
411 hashitem_T *hi = hash_find(&buf_hashtab, buf->b_key);
412
413 if (!HASHITEM_EMPTY(hi))
414 hash_remove(&buf_hashtab, hi);
415}
416
417/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418 * Close the link to a buffer.
419 * "action" is used when there is no longer a window for the buffer.
420 * It can be:
421 * 0 buffer becomes hidden
422 * DOBUF_UNLOAD buffer is unloaded
423 * DOBUF_DELETE buffer is unloaded and removed from buffer list
424 * DOBUF_WIPE buffer is unloaded and really deleted
425 * When doing all but the first one on the current buffer, the caller should
426 * get a new buffer very soon!
427 *
428 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100429 *
430 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
431 * cause there to be only one window with this buffer. e.g. when ":quit" is
432 * supposed to close the window but autocommands close all other windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 */
434 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100435close_buffer(
436 win_T *win, /* if not NULL, set b_last_cursor */
437 buf_T *buf,
438 int action,
439 int abort_if_last UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100442 int nwindows;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200443 bufref_T bufref;
Bram Moolenaar3a117e12016-10-30 21:57:52 +0100444 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200445 win_T *the_curwin = curwin;
446 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447 int unload_buf = (action != 0);
448 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
449 int wipe_buf = (action == DOBUF_WIPE);
450
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200451 /*
452 * Force unloading or deleting when 'bufhidden' says so.
453 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
454 * "hide" (otherwise we could never free or delete a buffer).
455 */
456 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
457 {
458 del_buf = TRUE;
459 unload_buf = TRUE;
460 }
461 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
462 {
463 del_buf = TRUE;
464 unload_buf = TRUE;
465 wipe_buf = TRUE;
466 }
467 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
468 unload_buf = TRUE;
469
Bram Moolenaar94053a52017-08-01 21:44:33 +0200470#ifdef FEAT_TERMINAL
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200471 if (bt_terminal(buf) && (buf->b_nwindows == 1 || del_buf))
Bram Moolenaar94053a52017-08-01 21:44:33 +0200472 {
473 if (term_job_running(buf->b_term))
474 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200475 if (wipe_buf || unload_buf)
476 /* Wiping out or unloading a terminal buffer kills the job. */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200477 free_terminal(buf);
478 else
479 {
480 /* The job keeps running, hide the buffer. */
481 del_buf = FALSE;
482 unload_buf = FALSE;
483 }
484 }
485 else
486 {
487 /* A terminal buffer is wiped out if the job has finished. */
488 del_buf = TRUE;
489 unload_buf = TRUE;
490 wipe_buf = TRUE;
491 }
492 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200493#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200495 /* Disallow deleting the buffer when it is locked (already being closed or
496 * halfway a command that relies on it). Unloading is allowed. */
497 if (buf->b_locked > 0 && (del_buf || wipe_buf))
498 {
499 EMSG(_("E937: Attempt to delete a buffer that is in use"));
500 return;
501 }
502
Bram Moolenaar4033c552017-09-16 20:54:51 +0200503 /* check no autocommands closed the window */
504 if (win != NULL && win_valid_any_tab(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 {
506 /* Set b_last_cursor when closing the last window for the buffer.
507 * Remember the last cursor position and window options of the buffer.
508 * This used to be only for the current window, but then options like
509 * 'foldmethod' may be lost with a ":only" command. */
510 if (buf->b_nwindows == 1)
511 set_last_cursor(win);
512 buflist_setfpos(buf, win,
513 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
514 win->w_cursor.col, TRUE);
515 }
516
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200517 set_bufref(&bufref, buf);
518
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 /* When the buffer is no longer in a window, trigger BufWinLeave */
520 if (buf->b_nwindows == 1)
521 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200522 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200523 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
524 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200525 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100526 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200527 /* Autocommands deleted the buffer. */
528aucmd_abort:
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100529 EMSG(_(e_auabort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 return;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100531 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200532 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200533 if (abort_if_last && one_window())
534 /* Autocommands made this the only window. */
535 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536
537 /* When the buffer becomes hidden, but is not unloaded, trigger
538 * BufHidden */
539 if (!unload_buf)
540 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200541 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200542 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
543 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200544 && !bufref_valid(&bufref))
Bram Moolenaar362ce482012-06-06 19:02:45 +0200545 /* Autocommands deleted the buffer. */
546 goto aucmd_abort;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200547 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200548 if (abort_if_last && one_window())
549 /* Autocommands made this the only window. */
550 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100552#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 if (aborting()) /* autocmds may abort script processing */
554 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200557
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200558 /* If the buffer was in curwin and the window has changed, go back to that
559 * window, if it still exists. This avoids that ":edit x" triggering a
560 * "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
561 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
562 {
563 block_autocmds();
564 goto_tabpage_win(the_curtab, the_curwin);
565 unblock_autocmds();
566 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200567
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 nwindows = buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569
570 /* decrease the link count from windows (unless not in any window) */
571 if (buf->b_nwindows > 0)
572 --buf->b_nwindows;
573
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100574#ifdef FEAT_DIFF
575 if (diffopt_hiddenoff() && !unload_buf && buf->b_nwindows == 0)
Bram Moolenaarb7407d32018-02-03 17:36:27 +0100576 diff_buf_delete(buf); /* Clear 'diff' for hidden buffer. */
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100577#endif
578
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 /* Return when a window is displaying the buffer or when it's not
580 * unloaded. */
581 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583
584 /* Always remove the buffer when there is no file name. */
585 if (buf->b_ffname == NULL)
586 del_buf = TRUE;
587
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200588 /* When closing the current buffer stop Visual mode before freeing
589 * anything. */
Bram Moolenaar4930a762016-09-11 14:39:53 +0200590 if (buf == curbuf && VIsual_active
Bram Moolenaar9a27c7f2016-09-09 12:57:09 +0200591#if defined(EXITFREE)
592 && !entered_free_all_mem
593#endif
594 )
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200595 end_visual_mode();
596
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 /*
598 * Free all things allocated for this buffer.
599 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
600 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 /* Remember if we are closing the current buffer. Restore the number of
602 * windows, so that autocommands in buf_freeall() don't get confused. */
603 is_curbuf = (buf == curbuf);
604 buf->b_nwindows = nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200606 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 /* Autocommands may have deleted the buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200609 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100611#ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000612 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 /*
617 * It's possible that autocommands change curbuf to the one being deleted.
618 * This might cause the previous curbuf to be deleted unexpectedly. But
619 * in some cases it's OK to delete the curbuf, because a new one is
620 * obtained anyway. Therefore only return if curbuf changed to the
621 * deleted buffer.
622 */
623 if (buf == curbuf && !is_curbuf)
624 return;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200625
Bram Moolenaar4033c552017-09-16 20:54:51 +0200626 if (win_valid_any_tab(win) && win->w_buffer == buf)
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200627 win->w_buffer = NULL; /* make sure we don't use the buffer now */
628
629 /* Autocommands may have opened or closed windows for this buffer.
630 * Decrement the count for the close we do here. */
631 if (buf->b_nwindows > 0)
632 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 /*
635 * Remove the buffer from the list.
636 */
637 if (wipe_buf)
638 {
639#ifdef FEAT_SUN_WORKSHOP
640 if (usingSunWorkShop)
641 workshop_file_closed_lineno((char *)buf->b_ffname,
642 (int)buf->b_last_cursor.lnum);
643#endif
644 vim_free(buf->b_ffname);
645 vim_free(buf->b_sfname);
646 if (buf->b_prev == NULL)
647 firstbuf = buf->b_next;
648 else
649 buf->b_prev->b_next = buf->b_next;
650 if (buf->b_next == NULL)
651 lastbuf = buf->b_prev;
652 else
653 buf->b_next->b_prev = buf->b_prev;
654 free_buffer(buf);
655 }
656 else
657 {
658 if (del_buf)
659 {
660 /* Free all internal variables and reset option values, to make
661 * ":bdel" compatible with Vim 5.7. */
662 free_buffer_stuff(buf, TRUE);
663
664 /* Make it look like a new buffer. */
665 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
666
667 /* Init the options when loaded again. */
668 buf->b_p_initialized = FALSE;
669 }
670 buf_clear_file(buf);
671 if (del_buf)
672 buf->b_p_bl = FALSE;
673 }
674}
675
676/*
677 * Make buffer not contain a file.
678 */
679 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100680buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681{
682 buf->b_ml.ml_line_count = 1;
683 unchanged(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 buf->b_p_eol = TRUE;
686 buf->b_start_eol = TRUE;
687#ifdef FEAT_MBYTE
688 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000689 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690#endif
691 buf->b_ml.ml_mfp = NULL;
692 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
693#ifdef FEAT_NETBEANS_INTG
694 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695#endif
696}
697
698/*
699 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200700 * the file. Careful: get here with "curwin" NULL when exiting.
701 * flags:
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200702 * BFA_DEL buffer is going to be deleted
703 * BFA_WIPE buffer is going to be wiped out
704 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100707buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200710 bufref_T bufref;
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200711 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaar5a497892016-09-03 16:29:04 +0200712 win_T *the_curwin = curwin;
713 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714
Bram Moolenaar5a497892016-09-03 16:29:04 +0200715 /* Make sure the buffer isn't closed by autocommands. */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200716 ++buf->b_locked;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200717 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200718 if (buf->b_ml.ml_mfp != NULL)
719 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200720 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
721 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200722 && !bufref_valid(&bufref))
723 /* autocommands deleted the buffer */
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200724 return;
725 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200726 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200728 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
729 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200730 && !bufref_valid(&bufref))
731 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 return;
733 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200734 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200736 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
737 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200738 && !bufref_valid(&bufref))
739 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 return;
741 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200742 --buf->b_locked;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200743
Bram Moolenaar5a497892016-09-03 16:29:04 +0200744 /* If the buffer was in curwin and the window has changed, go back to that
745 * window, if it still exists. This avoids that ":edit x" triggering a
746 * "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
747 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
748 {
749 block_autocmds();
750 goto_tabpage_win(the_curtab, the_curwin);
751 unblock_autocmds();
752 }
Bram Moolenaar5a497892016-09-03 16:29:04 +0200753
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100754#ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000755 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758
759 /*
760 * It's possible that autocommands change curbuf to the one being deleted.
761 * This might cause curbuf to be deleted unexpectedly. But in some cases
762 * it's OK to delete the curbuf, because a new one is obtained anyway.
763 * Therefore only return if curbuf changed to the deleted buffer.
764 */
765 if (buf == curbuf && !is_curbuf)
766 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767#ifdef FEAT_DIFF
768 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
769#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200770#ifdef FEAT_SYN_HL
Bram Moolenaar89c71222011-11-30 15:40:56 +0100771 /* Remove any ownsyntax, unless exiting. */
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200772 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaar89c71222011-11-30 15:40:56 +0100773 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200774#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000775
776#ifdef FEAT_FOLDING
777 /* No folds in an empty buffer. */
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000778 {
779 win_T *win;
780 tabpage_T *tp;
781
782 FOR_ALL_TAB_WINDOWS(tp, win)
783 if (win->w_buffer == buf)
784 clearFolding(win);
785 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000786#endif
787
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788#ifdef FEAT_TCL
789 tcl_buffer_free(buf);
790#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 ml_close(buf, TRUE); /* close and delete the memline/memfile */
792 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200793 if ((flags & BFA_KEEP_UNDO) == 0)
794 {
795 u_blockfree(buf); /* free the memory allocated for undo */
796 u_clearall(buf); /* reset all undo information */
797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200799 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000801 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802}
803
804/*
805 * Free a buffer structure and the things it contains related to the buffer
806 * itself (not the file, that must have been done already).
807 */
808 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100809free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200811 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200813#ifdef FEAT_EVAL
Bram Moolenaarb2c87502017-10-14 21:15:58 +0200814 /* b:changedtick uses an item in buf_T, remove it now */
815 dictitem_remove(buf->b_vars, (dictitem_T *)&buf->b_ct_di);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200816 unref_var_dict(buf->b_vars);
817#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200818#ifdef FEAT_LUA
819 lua_buffer_free(buf);
820#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000821#ifdef FEAT_MZSCHEME
822 mzscheme_buffer_free(buf);
823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824#ifdef FEAT_PERL
825 perl_buf_free(buf);
826#endif
827#ifdef FEAT_PYTHON
828 python_buffer_free(buf);
829#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200830#ifdef FEAT_PYTHON3
831 python3_buffer_free(buf);
832#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833#ifdef FEAT_RUBY
834 ruby_buffer_free(buf);
835#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200836#ifdef FEAT_JOB_CHANNEL
837 channel_buffer_free(buf);
838#endif
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200839#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +0200840 free_terminal(buf);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200841#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200842
843 buf_hashtab_remove(buf);
844
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200845 aubuflocal_remove(buf);
846
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200847 if (autocmd_busy)
848 {
849 /* Do not free the buffer structure while autocommands are executing,
850 * it's still needed. Free it when autocmd_busy is reset. */
851 buf->b_next = au_pending_free_buf;
852 au_pending_free_buf = buf;
853 }
854 else
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200855 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856}
857
858/*
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100859 * Initializes b:changedtick.
Bram Moolenaar79518e22017-02-17 16:31:35 +0100860 */
861 static void
862init_changedtick(buf_T *buf)
863{
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100864 dictitem_T *di = (dictitem_T *)&buf->b_ct_di;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100865
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100866 di->di_flags = DI_FLAGS_FIX | DI_FLAGS_RO;
867 di->di_tv.v_type = VAR_NUMBER;
868 di->di_tv.v_lock = VAR_FIXED;
869 di->di_tv.vval.v_number = 0;
870
Bram Moolenaar92769c32017-02-25 15:41:37 +0100871#ifdef FEAT_EVAL
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100872 STRCPY(buf->b_ct_di.di_key, "changedtick");
873 (void)dict_add(buf->b_vars, di);
Bram Moolenaar92769c32017-02-25 15:41:37 +0100874#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +0100875}
876
877/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
879 */
880 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100881free_buffer_stuff(
882 buf_T *buf,
883 int free_options) /* free options as well */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884{
885 if (free_options)
886 {
887 clear_wininfo(buf); /* including window-local options */
888 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200889#ifdef FEAT_SPELL
890 ga_clear(&buf->b_s.b_langp);
891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 }
893#ifdef FEAT_EVAL
Bram Moolenaar79518e22017-02-17 16:31:35 +0100894 {
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100895 varnumber_T tick = CHANGEDTICK(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +0100896
897 vars_clear(&buf->b_vars->dv_hashtab); /* free all buffer variables */
898 hash_init(&buf->b_vars->dv_hashtab);
899 init_changedtick(buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100900 CHANGEDTICK(buf) = tick;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902#endif
903#ifdef FEAT_USR_CMDS
904 uc_clear(&buf->b_ucmds); /* clear local user commands */
905#endif
906#ifdef FEAT_SIGNS
907 buf_delete_signs(buf); /* delete any signs */
908#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000909#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200910 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912#ifdef FEAT_LOCALMAP
913 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
914 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
915#endif
916#ifdef FEAT_MBYTE
Bram Moolenaard23a8232018-02-10 18:45:26 +0100917 VIM_CLEAR(buf->b_start_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918#endif
919}
920
921/*
922 * Free the b_wininfo list for buffer "buf".
923 */
924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100925clear_wininfo(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926{
927 wininfo_T *wip;
928
929 while (buf->b_wininfo != NULL)
930 {
931 wip = buf->b_wininfo;
932 buf->b_wininfo = wip->wi_next;
933 if (wip->wi_optset)
934 {
935 clear_winopt(&wip->wi_opt);
936#ifdef FEAT_FOLDING
937 deleteFoldRecurse(&wip->wi_folds);
938#endif
939 }
940 vim_free(wip);
941 }
942}
943
944#if defined(FEAT_LISTCMDS) || defined(PROTO)
945/*
946 * Go to another buffer. Handles the result of the ATTENTION dialog.
947 */
948 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100949goto_buffer(
950 exarg_T *eap,
951 int start,
952 int dir,
953 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954{
Bram Moolenaar4033c552017-09-16 20:54:51 +0200955# if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200956 bufref_T old_curbuf;
957
958 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959
960 swap_exists_action = SEA_DIALOG;
961# endif
962 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
963 start, dir, count, eap->forceit);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200964# if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
966 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100967# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000968 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000969
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000970 /* Reset the error/interrupt/exception state here so that
971 * aborting() returns FALSE when closing a window. */
972 enter_cleanup(&cs);
973# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000974
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000975 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 win_close(curwin, TRUE);
977 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000978 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000979
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100980# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000981 /* Restore the error/interrupt/exception state if not discarded by a
982 * new aborting error, interrupt, or uncaught exception. */
983 leave_cleanup(&cs);
984# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 }
986 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200987 handle_swap_exists(&old_curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988# endif
989}
990#endif
991
Bram Moolenaare64ac772005-12-07 20:54:59 +0000992#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993/*
994 * Handle the situation of swap_exists_action being set.
995 * It is allowed for "old_curbuf" to be NULL or invalid.
996 */
997 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200998handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999{
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001000# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001001 cleanup_T cs;
1002# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001003# ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001004 long old_tw = curbuf->b_p_tw;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001005# endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001006 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001007
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 if (swap_exists_action == SEA_QUIT)
1009 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001010# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001011 /* Reset the error/interrupt/exception state here so that
1012 * aborting() returns FALSE when closing a buffer. */
1013 enter_cleanup(&cs);
1014# endif
1015
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 /* User selected Quit at ATTENTION prompt. Go back to previous
1017 * buffer. If that buffer is gone or the same as the current one,
1018 * open a new, empty buffer. */
1019 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001020 swap_exists_did_quit = TRUE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001021 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001022 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1023 || old_curbuf->br_buf == curbuf)
1024 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
1025 else
1026 buf = old_curbuf->br_buf;
1027 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001028 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001029 enter_buffer(buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001030# ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001031 if (old_tw != curbuf->b_p_tw)
1032 check_colorcolumn(curwin);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001033# endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001036
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001037# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001038 /* Restore the error/interrupt/exception state if not discarded by a
1039 * new aborting error, interrupt, or uncaught exception. */
1040 leave_cleanup(&cs);
1041# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 }
1043 else if (swap_exists_action == SEA_RECOVER)
1044 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001045# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001046 /* Reset the error/interrupt/exception state here so that
1047 * aborting() returns FALSE when closing a buffer. */
1048 enter_cleanup(&cs);
1049# endif
1050
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 /* User selected Recover at ATTENTION prompt. */
1052 msg_scroll = TRUE;
1053 ml_recover();
1054 MSG_PUTS("\n"); /* don't overwrite the last message */
1055 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001056 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001057
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001058# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001059 /* Restore the error/interrupt/exception state if not discarded by a
1060 * new aborting error, interrupt, or uncaught exception. */
1061 leave_cleanup(&cs);
1062# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 }
1064 swap_exists_action = SEA_NONE;
1065}
1066#endif
1067
1068#if defined(FEAT_LISTCMDS) || defined(PROTO)
1069/*
1070 * do_bufdel() - delete or unload buffer(s)
1071 *
1072 * addr_count == 0: ":bdel" - delete current buffer
1073 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1074 * buffer "end_bnr", then any other arguments.
1075 * addr_count == 2: ":N,N bdel" - delete buffers in range
1076 *
1077 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1078 * DOBUF_DEL (":bdel")
1079 *
1080 * Returns error message or NULL
1081 */
1082 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001083do_bufdel(
1084 int command,
1085 char_u *arg, /* pointer to extra arguments */
1086 int addr_count,
1087 int start_bnr, /* first buffer number in a range */
1088 int end_bnr, /* buffer nr or last buffer nr in a range */
1089 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090{
1091 int do_current = 0; /* delete current buffer? */
1092 int deleted = 0; /* number of buffers deleted */
1093 char_u *errormsg = NULL; /* return value */
1094 int bnr; /* buffer number */
1095 char_u *p;
1096
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 if (addr_count == 0)
1098 {
1099 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1100 }
1101 else
1102 {
1103 if (addr_count == 2)
1104 {
1105 if (*arg) /* both range and argument is not allowed */
1106 return (char_u *)_(e_trailing);
1107 bnr = start_bnr;
1108 }
1109 else /* addr_count == 1 */
1110 bnr = end_bnr;
1111
1112 for ( ;!got_int; ui_breakcheck())
1113 {
1114 /*
1115 * delete the current buffer last, otherwise when the
1116 * current buffer is deleted, the next buffer becomes
1117 * the current one and will be loaded, which may then
1118 * also be deleted, etc.
1119 */
1120 if (bnr == curbuf->b_fnum)
1121 do_current = bnr;
1122 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
1123 forceit) == OK)
1124 ++deleted;
1125
1126 /*
1127 * find next buffer number to delete/unload
1128 */
1129 if (addr_count == 2)
1130 {
1131 if (++bnr > end_bnr)
1132 break;
1133 }
1134 else /* addr_count == 1 */
1135 {
1136 arg = skipwhite(arg);
1137 if (*arg == NUL)
1138 break;
1139 if (!VIM_ISDIGIT(*arg))
1140 {
1141 p = skiptowhite_esc(arg);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01001142 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE,
1143 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 if (bnr < 0) /* failed */
1145 break;
1146 arg = p;
1147 }
1148 else
1149 bnr = getdigits(&arg);
1150 }
1151 }
1152 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1153 FORWARD, do_current, forceit) == OK)
1154 ++deleted;
1155
1156 if (deleted == 0)
1157 {
1158 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001159 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001161 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00001163 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 errormsg = IObuff;
1165 }
1166 else if (deleted >= p_report)
1167 {
1168 if (command == DOBUF_UNLOAD)
1169 {
1170 if (deleted == 1)
1171 MSG(_("1 buffer unloaded"));
1172 else
1173 smsg((char_u *)_("%d buffers unloaded"), deleted);
1174 }
1175 else if (command == DOBUF_DEL)
1176 {
1177 if (deleted == 1)
1178 MSG(_("1 buffer deleted"));
1179 else
1180 smsg((char_u *)_("%d buffers deleted"), deleted);
1181 }
1182 else
1183 {
1184 if (deleted == 1)
1185 MSG(_("1 buffer wiped out"));
1186 else
1187 smsg((char_u *)_("%d buffers wiped out"), deleted);
1188 }
1189 }
1190 }
1191
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192
1193 return errormsg;
1194}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001195#endif /* FEAT_LISTCMDS */
1196
1197#if defined(FEAT_LISTCMDS) || defined(FEAT_PYTHON) \
1198 || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001200static int empty_curbuf(int close_others, int forceit, int action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001201
1202/*
1203 * Make the current buffer empty.
1204 * Used when it is wiped out and it's the last buffer.
1205 */
1206 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001207empty_curbuf(
1208 int close_others,
1209 int forceit,
1210 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001211{
1212 int retval;
1213 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001214 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001215
1216 if (action == DOBUF_UNLOAD)
1217 {
1218 EMSG(_("E90: Cannot unload last buffer"));
1219 return FAIL;
1220 }
1221
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001222 set_bufref(&bufref, buf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001223 if (close_others)
1224 /* Close any other windows on this buffer, then make it empty. */
Bram Moolenaara02471e2014-01-10 16:43:14 +01001225 close_windows(buf, TRUE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001226
1227 setpcmark();
1228 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1229 forceit ? ECMD_FORCEIT : 0, curwin);
1230
1231 /*
1232 * do_ecmd() may create a new buffer, then we have to delete
1233 * the old one. But do_ecmd() may have done that already, check
1234 * if the buffer still exists.
1235 */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001236 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001237 close_buffer(NULL, buf, action, FALSE);
1238 if (!close_others)
1239 need_fileinfo = FALSE;
1240 return retval;
1241}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242/*
1243 * Implementation of the commands for the buffer list.
1244 *
1245 * action == DOBUF_GOTO go to specified buffer
1246 * action == DOBUF_SPLIT split window and go to specified buffer
1247 * action == DOBUF_UNLOAD unload specified buffer(s)
1248 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1249 * action == DOBUF_WIPE delete specified buffer(s) really
1250 *
1251 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1252 * start == DOBUF_FIRST go to "count" buffer from first buffer
1253 * start == DOBUF_LAST go to "count" buffer from last buffer
1254 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1255 *
1256 * Return FAIL or OK.
1257 */
1258 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001259do_buffer(
1260 int action,
1261 int start,
1262 int dir, /* FORWARD or BACKWARD */
1263 int count, /* buffer number or number of buffers */
1264 int forceit) /* TRUE for :...! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265{
1266 buf_T *buf;
1267 buf_T *bp;
1268 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1269 || action == DOBUF_WIPE);
1270
1271 switch (start)
1272 {
1273 case DOBUF_FIRST: buf = firstbuf; break;
1274 case DOBUF_LAST: buf = lastbuf; break;
1275 default: buf = curbuf; break;
1276 }
1277 if (start == DOBUF_MOD) /* find next modified buffer */
1278 {
1279 while (count-- > 0)
1280 {
1281 do
1282 {
1283 buf = buf->b_next;
1284 if (buf == NULL)
1285 buf = firstbuf;
1286 }
1287 while (buf != curbuf && !bufIsChanged(buf));
1288 }
1289 if (!bufIsChanged(buf))
1290 {
1291 EMSG(_("E84: No modified buffer found"));
1292 return FAIL;
1293 }
1294 }
1295 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
1296 {
1297 while (buf != NULL && buf->b_fnum != count)
1298 buf = buf->b_next;
1299 }
1300 else
1301 {
1302 bp = NULL;
1303 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1304 {
1305 /* remember the buffer where we start, we come back there when all
1306 * buffers are unlisted. */
1307 if (bp == NULL)
1308 bp = buf;
1309 if (dir == FORWARD)
1310 {
1311 buf = buf->b_next;
1312 if (buf == NULL)
1313 buf = firstbuf;
1314 }
1315 else
1316 {
1317 buf = buf->b_prev;
1318 if (buf == NULL)
1319 buf = lastbuf;
1320 }
1321 /* don't count unlisted buffers */
1322 if (unload || buf->b_p_bl)
1323 {
1324 --count;
1325 bp = NULL; /* use this buffer as new starting point */
1326 }
1327 if (bp == buf)
1328 {
1329 /* back where we started, didn't find anything. */
1330 EMSG(_("E85: There is no listed buffer"));
1331 return FAIL;
1332 }
1333 }
1334 }
1335
1336 if (buf == NULL) /* could not find it */
1337 {
1338 if (start == DOBUF_FIRST)
1339 {
1340 /* don't warn when deleting */
1341 if (!unload)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01001342 EMSGN(_(e_nobufnr), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 }
1344 else if (dir == FORWARD)
1345 EMSG(_("E87: Cannot go beyond last buffer"));
1346 else
1347 EMSG(_("E88: Cannot go before first buffer"));
1348 return FAIL;
1349 }
1350
1351#ifdef FEAT_GUI
1352 need_mouse_correct = TRUE;
1353#endif
1354
1355#ifdef FEAT_LISTCMDS
1356 /*
1357 * delete buffer buf from memory and/or the list
1358 */
1359 if (unload)
1360 {
1361 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001362 bufref_T bufref;
1363
1364 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365
1366 /* When unloading or deleting a buffer that's already unloaded and
1367 * unlisted: fail silently. */
1368 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1369 return FAIL;
1370
1371 if (!forceit && bufIsChanged(buf))
1372 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001373# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 if ((p_confirm || cmdmod.confirm) && p_write)
1375 {
1376 dialog_changed(buf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001377 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 /* Autocommand deleted buffer, oops! It's not changed
1379 * now. */
1380 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001381 /* If it's still changed fail silently, the dialog already
1382 * mentioned why it fails. */
1383 if (bufIsChanged(buf))
1384 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001386 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001387# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 {
1389 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1390 buf->b_fnum);
1391 return FAIL;
1392 }
1393 }
1394
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001395 /* When closing the current buffer stop Visual mode. */
Bram Moolenaar4930a762016-09-11 14:39:53 +02001396 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001397 end_visual_mode();
1398
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 /*
1400 * If deleting the last (listed) buffer, make it empty.
1401 * The last (listed) buffer cannot be unloaded.
1402 */
Bram Moolenaar29323592016-07-24 22:04:11 +02001403 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 if (bp->b_p_bl && bp != buf)
1405 break;
1406 if (bp == NULL && buf == curbuf)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001407 return empty_curbuf(TRUE, forceit, action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 /*
1410 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001411 * (unless it's the only window). Repeat this so long as we end up in
1412 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001414 while (buf == curbuf
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001415 && !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
Bram Moolenaar459ca562016-11-10 18:16:33 +01001416 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001417 {
1418 if (win_close(curwin, FALSE) == FAIL)
1419 break;
1420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421
1422 /*
1423 * If the buffer to be deleted is not the current one, delete it here.
1424 */
1425 if (buf != curbuf)
1426 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001427 close_windows(buf, FALSE);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001428 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows <= 0)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001429 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 return OK;
1431 }
1432
1433 /*
1434 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001435 * There should be another, otherwise it would have been handled
1436 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001437 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 * Then prefer the buffer we most recently visited.
1439 * Else try to find one that is loaded, after the current buffer,
1440 * then before the current buffer.
1441 * Finally use any buffer.
1442 */
1443 buf = NULL; /* selected buffer */
1444 bp = NULL; /* used when no loaded buffer found */
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001445 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1446 buf = au_new_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447#ifdef FEAT_JUMPLIST
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001448 else if (curwin->w_jumplistlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 {
1450 int jumpidx;
1451
1452 jumpidx = curwin->w_jumplistidx - 1;
1453 if (jumpidx < 0)
1454 jumpidx = curwin->w_jumplistlen - 1;
1455
1456 forward = jumpidx;
1457 while (jumpidx != curwin->w_jumplistidx)
1458 {
1459 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1460 if (buf != NULL)
1461 {
1462 if (buf == curbuf || !buf->b_p_bl)
1463 buf = NULL; /* skip current and unlisted bufs */
1464 else if (buf->b_ml.ml_mfp == NULL)
1465 {
1466 /* skip unloaded buf, but may keep it for later */
1467 if (bp == NULL)
1468 bp = buf;
1469 buf = NULL;
1470 }
1471 }
1472 if (buf != NULL) /* found a valid buffer: stop searching */
1473 break;
1474 /* advance to older entry in jump list */
1475 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1476 break;
1477 if (--jumpidx < 0)
1478 jumpidx = curwin->w_jumplistlen - 1;
1479 if (jumpidx == forward) /* List exhausted for sure */
1480 break;
1481 }
1482 }
1483#endif
1484
1485 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1486 {
1487 forward = TRUE;
1488 buf = curbuf->b_next;
1489 for (;;)
1490 {
1491 if (buf == NULL)
1492 {
1493 if (!forward) /* tried both directions */
1494 break;
1495 buf = curbuf->b_prev;
1496 forward = FALSE;
1497 continue;
1498 }
1499 /* in non-help buffer, try to skip help buffers, and vv */
1500 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1501 {
1502 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1503 break;
1504 if (bp == NULL) /* remember unloaded buf for later */
1505 bp = buf;
1506 }
1507 if (forward)
1508 buf = buf->b_next;
1509 else
1510 buf = buf->b_prev;
1511 }
1512 }
1513 if (buf == NULL) /* No loaded buffer, use unloaded one */
1514 buf = bp;
1515 if (buf == NULL) /* No loaded buffer, find listed one */
1516 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001517 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 if (buf->b_p_bl && buf != curbuf)
1519 break;
1520 }
1521 if (buf == NULL) /* Still no buffer, just take one */
1522 {
1523 if (curbuf->b_next != NULL)
1524 buf = curbuf->b_next;
1525 else
1526 buf = curbuf->b_prev;
1527 }
1528 }
1529
Bram Moolenaara02471e2014-01-10 16:43:14 +01001530 if (buf == NULL)
1531 {
1532 /* Autocommands must have wiped out all other buffers. Only option
1533 * now is to make the current buffer empty. */
1534 return empty_curbuf(FALSE, forceit, action);
1535 }
1536
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 /*
1538 * make buf current buffer
1539 */
1540 if (action == DOBUF_SPLIT) /* split window first */
1541 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001542 /* If 'switchbuf' contains "useopen": jump to first window containing
1543 * "buf" if one exists */
1544 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001545 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001546 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001547 * page containing "buf" if one exists */
1548 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 return OK;
1550 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 return FAIL;
1552 }
1553#endif
1554
1555 /* go to current buffer - nothing to do */
1556 if (buf == curbuf)
1557 return OK;
1558
1559 /*
1560 * Check if the current buffer may be abandoned.
1561 */
1562 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1563 {
1564#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1565 if ((p_confirm || cmdmod.confirm) && p_write)
1566 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001567 bufref_T bufref;
1568
1569 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 dialog_changed(curbuf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001571 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 /* Autocommand deleted buffer, oops! */
1573 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 }
1575 if (bufIsChanged(curbuf))
1576#endif
1577 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001578 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 return FAIL;
1580 }
1581 }
1582
1583 /* Go to the other buffer. */
1584 set_curbuf(buf, action);
1585
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001586#if defined(FEAT_LISTCMDS) \
1587 && (defined(FEAT_SCROLLBIND) || defined(FEAT_CURSORBIND))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001589 {
1590 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
1591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592#endif
1593
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 Moolenaar8c0e3222013-06-16 17:32:40 +02001601#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602
1603/*
1604 * Set current buffer to "buf". Executes autocommands and closes current
1605 * buffer. "action" tells how to close the current buffer:
1606 * DOBUF_GOTO free or hide it
1607 * DOBUF_SPLIT nothing
1608 * DOBUF_UNLOAD unload it
1609 * DOBUF_DEL delete it
1610 * DOBUF_WIPE wipe it out
1611 */
1612 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001613set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614{
1615 buf_T *prevbuf;
1616 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1617 || action == DOBUF_WIPE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001618#ifdef FEAT_SYN_HL
1619 long old_tw = curbuf->b_p_tw;
1620#endif
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001621 bufref_T newbufref;
1622 bufref_T prevbufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623
1624 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001625 if (!cmdmod.keepalt)
1626 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001627 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 /* Don't restart Select mode after switching to another buffer. */
1630 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001632 /* close_windows() or apply_autocmds() may change curbuf and wipe out "buf"
1633 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 prevbuf = curbuf;
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001635 set_bufref(&prevbufref, prevbuf);
1636 set_bufref(&newbufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001638 /* Autocommands may delete the curren buffer and/or the buffer we wan to go
1639 * to. In those cases don't close the buffer. */
Bram Moolenaar82404332016-07-10 17:00:38 +02001640 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001641 || (bufref_valid(&prevbufref)
1642 && bufref_valid(&newbufref)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001643#ifdef FEAT_EVAL
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001644 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001646 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001648#ifdef FEAT_SYN_HL
1649 if (prevbuf == curwin->w_buffer)
1650 reset_synblock(curwin);
1651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001653 close_windows(prevbuf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001654#if defined(FEAT_EVAL)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001655 if (bufref_valid(&prevbufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656#else
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001657 if (bufref_valid(&prevbufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001659 {
Bram Moolenaare25865a2012-07-06 16:22:02 +02001660 win_T *previouswin = curwin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001661 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001662 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1664 unload ? action : (action == DOBUF_GOTO
Bram Moolenaareb44a682017-08-03 22:44:55 +02001665 && !buf_hide(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001666 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaare25865a2012-07-06 16:22:02 +02001667 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaar9e931222012-06-20 11:55:01 +02001668 /* autocommands changed curwin, Grr! */
Bram Moolenaare25865a2012-07-06 16:22:02 +02001669 curwin = previouswin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 }
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001672 /* An autocommand may have deleted "buf", already entered it (e.g., when
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001673 * it did ":bunload") or aborted the script processing.
Bram Moolenaar9e931222012-06-20 11:55:01 +02001674 * If curwin->w_buffer is null, enter_buffer() will make it valid again */
1675 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001676#ifdef FEAT_EVAL
Bram Moolenaar4033c552017-09-16 20:54:51 +02001677 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001679 ) || curwin->w_buffer == NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001680 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001682#ifdef FEAT_SYN_HL
1683 if (old_tw != curbuf->b_p_tw)
1684 check_colorcolumn(curwin);
1685#endif
1686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687}
1688
1689/*
1690 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001691 * Old curbuf must have been abandoned already! This also means "curbuf" may
1692 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 */
1694 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001695enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696{
1697 /* Copy buffer and window local option values. Not for a help buffer. */
1698 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1699 if (!buf->b_help)
1700 get_winopts(buf);
1701#ifdef FEAT_FOLDING
1702 else
1703 /* Remove all folds in the window. */
1704 clearFolding(curwin);
1705 foldUpdateAll(curwin); /* update folds (later). */
1706#endif
1707
1708 /* Get the buffer in the current window. */
1709 curwin->w_buffer = buf;
1710 curbuf = buf;
1711 ++curbuf->b_nwindows;
1712
1713#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001714 if (curwin->w_p_diff)
1715 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716#endif
1717
Bram Moolenaara971b822011-09-14 14:43:25 +02001718#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001719 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001720#endif
1721
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 /* Cursor on first line by default. */
1723 curwin->w_cursor.lnum = 1;
1724 curwin->w_cursor.col = 0;
1725#ifdef FEAT_VIRTUALEDIT
1726 curwin->w_cursor.coladd = 0;
1727#endif
1728 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001729 curwin->w_topline_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001731 /* mark cursor position as being invalid */
1732 curwin->w_valid = 0;
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 Moolenaar55b7cf82006-09-09 12:52:42 +00001747 if (!msg_silent)
1748 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001751#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1755 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 }
1757
1758 /* If autocommands did not change the cursor position, restore cursor lnum
1759 * and possibly cursor col. */
1760 if (curwin->w_cursor.lnum == 1 && inindent(0))
1761 buflist_getfpos();
1762
1763 check_arg_idx(curwin); /* check for valid arg_idx */
1764#ifdef FEAT_TITLE
1765 maketitle();
1766#endif
Bram Moolenaard4153d42008-11-15 15:06:17 +00001767 /* when autocmds didn't change it */
1768 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1770
Bram Moolenaar009b2592004-10-24 19:18:58 +00001771#ifdef FEAT_NETBEANS_INTG
1772 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001773 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001774#endif
1775
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001776 /* Change directories when the 'acd' option is set. */
1777 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778
1779#ifdef FEAT_KEYMAP
1780 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001781 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001783#ifdef FEAT_SPELL
1784 /* May need to set the spell language. Can only do this after the buffer
1785 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001786 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1787 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001788#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001789#ifdef FEAT_VIMINFO
1790 curbuf->b_last_used = vim_time();
1791#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001792
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 redraw_later(NOT_VALID);
1794}
1795
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001796#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1797/*
1798 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001799 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001800 */
1801 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001802do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001803{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001804 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001805 && curbuf->b_ffname != NULL
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001806 && vim_chdirfile(curbuf->b_ffname, "auto") == OK)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001807 shorten_fnames(TRUE);
1808}
1809#endif
1810
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001811 void
1812no_write_message(void)
1813{
1814#ifdef FEAT_TERMINAL
1815 if (term_job_running(curbuf->b_term))
1816 EMSG(_("E948: Job still running (add ! to end the job)"));
1817 else
1818#endif
1819 EMSG(_("E37: No write since last change (add ! to override)"));
1820}
1821
1822 void
Bram Moolenaar7a760922018-02-19 23:10:02 +01001823no_write_message_nobang(buf_T *buf UNUSED)
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001824{
1825#ifdef FEAT_TERMINAL
Bram Moolenaar7a760922018-02-19 23:10:02 +01001826 if (term_job_running(buf->b_term))
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001827 EMSG(_("E948: Job still running"));
1828 else
1829#endif
1830 EMSG(_("E37: No write since last change"));
1831}
1832
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833/*
1834 * functions for dealing with the buffer list
1835 */
1836
Bram Moolenaar480778b2016-07-14 22:09:39 +02001837static int top_file_num = 1; /* highest file number */
1838
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839/*
1840 * Add a file name to the buffer list. Return a pointer to the buffer.
1841 * If the same file name already exists return a pointer to that buffer.
1842 * If it does not exist, or if fname == NULL, a new entry is created.
1843 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1844 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1845 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001846 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001847 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
1848 * if the buffer already exists.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 * This is the ONLY way to create a new buffer.
1850 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001852buflist_new(
1853 char_u *ffname, /* full path of fname or relative */
1854 char_u *sfname, /* short fname or NULL */
1855 linenr_T lnum, /* preferred cursor line */
1856 int flags) /* BLN_ defines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857{
1858 buf_T *buf;
1859#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001860 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861#endif
1862
Bram Moolenaar480778b2016-07-14 22:09:39 +02001863 if (top_file_num == 1)
1864 hash_init(&buf_hashtab);
1865
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1867
1868 /*
1869 * If file name already exists in the list, update the entry.
1870 */
1871#ifdef UNIX
1872 /* On Unix we can use inode numbers when the file exists. Works better
1873 * for hard links. */
1874 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1875 st.st_dev = (dev_T)-1;
1876#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001877 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878#ifdef UNIX
1879 buflist_findname_stat(ffname, &st)
1880#else
1881 buflist_findname(ffname)
1882#endif
1883 ) != NULL)
1884 {
1885 vim_free(ffname);
1886 if (lnum != 0)
1887 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02001888
1889 if ((flags & BLN_NOOPT) == 0)
1890 /* copy the options now, if 'cpo' doesn't have 's' and not done
1891 * already */
1892 buf_copy_options(buf, 0);
1893
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1895 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001896 bufref_T bufref;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001897
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 buf->b_p_bl = TRUE;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001899 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001901 {
Bram Moolenaar82404332016-07-10 17:00:38 +02001902 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001903 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001904 return NULL;
1905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 }
1907 return buf;
1908 }
1909
1910 /*
1911 * If the current buffer has no name and no contents, use the current
1912 * buffer. Otherwise: Need to allocate a new buffer structure.
1913 *
1914 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001915 * (A spell file buffer is allocated in spell.c, but that's not a normal
1916 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 */
1918 buf = NULL;
1919 if ((flags & BLN_CURBUF)
1920 && curbuf != NULL
1921 && curbuf->b_ffname == NULL
1922 && curbuf->b_nwindows <= 1
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001923 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 {
1925 buf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 /* It's like this buffer is deleted. Watch out for autocommands that
1927 * change curbuf! If that happens, allocate a new buffer anyway. */
1928 if (curbuf->b_p_bl)
1929 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1930 if (buf == curbuf)
1931 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001932#ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001933 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 if (buf == curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 {
1938 /* Make sure 'bufhidden' and 'buftype' are empty */
1939 clear_string_option(&buf->b_p_bh);
1940 clear_string_option(&buf->b_p_bt);
1941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 }
1943 if (buf != curbuf || curbuf == NULL)
1944 {
1945 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1946 if (buf == NULL)
1947 {
1948 vim_free(ffname);
1949 return NULL;
1950 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02001951#ifdef FEAT_EVAL
1952 /* init b: variables */
1953 buf->b_vars = dict_alloc();
1954 if (buf->b_vars == NULL)
1955 {
1956 vim_free(ffname);
1957 vim_free(buf);
1958 return NULL;
1959 }
1960 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
1961#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01001962 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 }
1964
1965 if (ffname != NULL)
1966 {
1967 buf->b_ffname = ffname;
1968 buf->b_sfname = vim_strsave(sfname);
1969 }
1970
1971 clear_wininfo(buf);
1972 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1973
1974 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1975 || buf->b_wininfo == NULL)
1976 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001977 VIM_CLEAR(buf->b_ffname);
1978 VIM_CLEAR(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 if (buf != curbuf)
1980 free_buffer(buf);
1981 return NULL;
1982 }
1983
1984 if (buf == curbuf)
1985 {
1986 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001987 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 if (buf != curbuf) /* autocommands deleted the buffer! */
1989 return NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001990#if defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001991 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 return NULL;
1993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01001995
1996 /* Init the options. */
1997 buf->b_p_initialized = FALSE;
1998 buf_copy_options(buf, BCO_ENTER);
1999
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000#ifdef FEAT_KEYMAP
2001 /* need to reload lmaps and set b:keymap_name */
2002 curbuf->b_kmap_state |= KEYMAP_INIT;
2003#endif
2004 }
2005 else
2006 {
2007 /*
2008 * put new buffer at the end of the buffer list
2009 */
2010 buf->b_next = NULL;
2011 if (firstbuf == NULL) /* buffer list is empty */
2012 {
2013 buf->b_prev = NULL;
2014 firstbuf = buf;
2015 }
2016 else /* append new buffer at end of list */
2017 {
2018 lastbuf->b_next = buf;
2019 buf->b_prev = lastbuf;
2020 }
2021 lastbuf = buf;
2022
2023 buf->b_fnum = top_file_num++;
2024 if (top_file_num < 0) /* wrap around (may cause duplicates) */
2025 {
2026 EMSG(_("W14: Warning: List of file names overflow"));
2027 if (emsg_silent == 0)
2028 {
2029 out_flush();
2030 ui_delay(3000L, TRUE); /* make sure it is noticed */
2031 }
2032 top_file_num = 1;
2033 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002034 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035
2036 /*
2037 * Always copy the options from the current buffer.
2038 */
2039 buf_copy_options(buf, BCO_ALWAYS);
2040 }
2041
2042 buf->b_wininfo->wi_fpos.lnum = lnum;
2043 buf->b_wininfo->wi_win = curwin;
2044
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002045#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002046 hash_init(&buf->b_s.b_keywtab);
2047 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048#endif
2049
2050 buf->b_fname = buf->b_sfname;
2051#ifdef UNIX
2052 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002053 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 else
2055 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002056 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 buf->b_dev = st.st_dev;
2058 buf->b_ino = st.st_ino;
2059 }
2060#endif
2061 buf->b_u_synced = TRUE;
2062 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002063 if (flags & BLN_DUMMY)
2064 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 buf_clear_file(buf);
2066 clrallmarks(buf); /* clear marks */
2067 fmarks_check_names(buf); /* check file marks for this file */
2068 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 if (!(flags & BLN_DUMMY))
2070 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002071 bufref_T bufref;
2072
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01002073 /* Tricky: these autocommands may change the buffer list. They could
2074 * also split the window with re-using the one empty buffer. This may
2075 * result in unexpectedly losing the empty buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002076 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002077 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002078 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002079 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002081 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002082 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002083 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002084 return NULL;
2085 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002086#ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002087 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091
2092 return buf;
2093}
2094
2095/*
2096 * Free the memory for the options of a buffer.
2097 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2098 * 'fileencoding'.
2099 */
2100 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002101free_buf_options(
2102 buf_T *buf,
2103 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104{
2105 if (free_p_ff)
2106 {
2107#ifdef FEAT_MBYTE
2108 clear_string_option(&buf->b_p_fenc);
2109#endif
2110 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 clear_string_option(&buf->b_p_bh);
2112 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 }
2114#ifdef FEAT_FIND_ID
2115 clear_string_option(&buf->b_p_def);
2116 clear_string_option(&buf->b_p_inc);
2117# ifdef FEAT_EVAL
2118 clear_string_option(&buf->b_p_inex);
2119# endif
2120#endif
2121#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
2122 clear_string_option(&buf->b_p_inde);
2123 clear_string_option(&buf->b_p_indk);
2124#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002125#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2126 clear_string_option(&buf->b_p_bexpr);
2127#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002128#if defined(FEAT_CRYPT)
2129 clear_string_option(&buf->b_p_cm);
2130#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002131 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002132#if defined(FEAT_EVAL)
2133 clear_string_option(&buf->b_p_fex);
2134#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135#ifdef FEAT_CRYPT
2136 clear_string_option(&buf->b_p_key);
2137#endif
2138 clear_string_option(&buf->b_p_kp);
2139 clear_string_option(&buf->b_p_mps);
2140 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002141 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 clear_string_option(&buf->b_p_isk);
2143#ifdef FEAT_KEYMAP
2144 clear_string_option(&buf->b_p_keymap);
Bram Moolenaar50138322018-01-28 17:05:16 +01002145 keymap_clear(&buf->b_kmap_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 ga_clear(&buf->b_kmap_ga);
2147#endif
2148#ifdef FEAT_COMMENTS
2149 clear_string_option(&buf->b_p_com);
2150#endif
2151#ifdef FEAT_FOLDING
2152 clear_string_option(&buf->b_p_cms);
2153#endif
2154 clear_string_option(&buf->b_p_nf);
2155#ifdef FEAT_SYN_HL
2156 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002157 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002158#endif
2159#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002160 clear_string_option(&buf->b_s.b_p_spc);
2161 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002162 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002163 buf->b_s.b_cap_prog = NULL;
2164 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165#endif
2166#ifdef FEAT_SEARCHPATH
2167 clear_string_option(&buf->b_p_sua);
2168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 clear_string_option(&buf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170#ifdef FEAT_CINDENT
2171 clear_string_option(&buf->b_p_cink);
2172 clear_string_option(&buf->b_p_cino);
2173#endif
2174#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
2175 clear_string_option(&buf->b_p_cinw);
2176#endif
2177#ifdef FEAT_INS_EXPAND
2178 clear_string_option(&buf->b_p_cpt);
2179#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002180#ifdef FEAT_COMPL_FUNC
2181 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002182 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184#ifdef FEAT_QUICKFIX
2185 clear_string_option(&buf->b_p_gp);
2186 clear_string_option(&buf->b_p_mp);
2187 clear_string_option(&buf->b_p_efm);
2188#endif
2189 clear_string_option(&buf->b_p_ep);
2190 clear_string_option(&buf->b_p_path);
2191 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002192 clear_string_option(&buf->b_p_tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193#ifdef FEAT_INS_EXPAND
2194 clear_string_option(&buf->b_p_dict);
2195 clear_string_option(&buf->b_p_tsr);
2196#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002197#ifdef FEAT_TEXTOBJ
2198 clear_string_option(&buf->b_p_qe);
2199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002201 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002202#ifdef FEAT_LISP
2203 clear_string_option(&buf->b_p_lw);
2204#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002205 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002206#ifdef FEAT_MBYTE
2207 clear_string_option(&buf->b_p_menc);
2208#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209}
2210
2211/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002212 * Get alternate file "n".
2213 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2214 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 * if (options & GETF_SETMARK) call setpcmark()
2216 * if (options & GETF_ALT) we are jumping to an alternate file.
2217 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2218 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002219 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 */
2221 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002222buflist_getfile(
2223 int n,
2224 linenr_T lnum,
2225 int options,
2226 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227{
2228 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 pos_T *fpos;
2231 colnr_T col;
2232
2233 buf = buflist_findnr(n);
2234 if (buf == NULL)
2235 {
2236 if ((options & GETF_ALT) && n == 0)
2237 EMSG(_(e_noalt));
2238 else
2239 EMSGN(_("E92: Buffer %ld not found"), n);
2240 return FAIL;
2241 }
2242
2243 /* if alternate file is the current buffer, nothing to do */
2244 if (buf == curbuf)
2245 return OK;
2246
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002247 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002248 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002249 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002251 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002252 if (curbuf_locked())
2253 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254
2255 /* altfpos may be changed by getfile(), get it now */
2256 if (lnum == 0)
2257 {
2258 fpos = buflist_findfpos(buf);
2259 lnum = fpos->lnum;
2260 col = fpos->col;
2261 }
2262 else
2263 col = 0;
2264
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 if (options & GETF_SWITCH)
2266 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002267 /* If 'switchbuf' contains "useopen": jump to first window containing
2268 * "buf" if one exists */
2269 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002271
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002272 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00002273 * page containing "buf" if one exists */
2274 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002275 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002276
2277 /* If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2278 * current buffer isn't empty: open new tab or window */
2279 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002280 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002282 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002283 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002284 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2285 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002287 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 }
2289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290
2291 ++RedrawingDisabled;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002292 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2293 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 {
2295 --RedrawingDisabled;
2296
2297 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
2298 if (!p_sol && col != 0)
2299 {
2300 curwin->w_cursor.col = col;
2301 check_cursor_col();
2302#ifdef FEAT_VIRTUALEDIT
2303 curwin->w_cursor.coladd = 0;
2304#endif
2305 curwin->w_set_curswant = TRUE;
2306 }
2307 return OK;
2308 }
2309 --RedrawingDisabled;
2310 return FAIL;
2311}
2312
2313/*
2314 * go to the last know line number for the current buffer
2315 */
2316 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002317buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318{
2319 pos_T *fpos;
2320
2321 fpos = buflist_findfpos(curbuf);
2322
2323 curwin->w_cursor.lnum = fpos->lnum;
2324 check_cursor_lnum();
2325
2326 if (p_sol)
2327 curwin->w_cursor.col = 0;
2328 else
2329 {
2330 curwin->w_cursor.col = fpos->col;
2331 check_cursor_col();
2332#ifdef FEAT_VIRTUALEDIT
2333 curwin->w_cursor.coladd = 0;
2334#endif
2335 curwin->w_set_curswant = TRUE;
2336 }
2337}
2338
Bram Moolenaar81695252004-12-29 20:58:21 +00002339#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2340/*
2341 * Find file in buffer list by name (it has to be for the current window).
2342 * Returns NULL if not found.
2343 */
2344 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002345buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002346{
2347 char_u *ffname;
2348 buf_T *buf = NULL;
2349
2350 /* First make the name into a full path name */
2351 ffname = FullName_save(fname,
2352#ifdef UNIX
2353 TRUE /* force expansion, get rid of symbolic links */
2354#else
2355 FALSE
2356#endif
2357 );
2358 if (ffname != NULL)
2359 {
2360 buf = buflist_findname(ffname);
2361 vim_free(ffname);
2362 }
2363 return buf;
2364}
2365#endif
2366
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367/*
2368 * Find file in buffer list by name (it has to be for the current window).
2369 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002370 * Skips dummy buffers.
2371 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 */
2373 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002374buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375{
2376#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002377 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378
2379 if (mch_stat((char *)ffname, &st) < 0)
2380 st.st_dev = (dev_T)-1;
2381 return buflist_findname_stat(ffname, &st);
2382}
2383
2384/*
2385 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2386 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002387 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 */
2389 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002390buflist_findname_stat(
2391 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002392 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393{
2394#endif
2395 buf_T *buf;
2396
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002397 /* Start at the last buffer, expect to find a match sooner. */
2398 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar81695252004-12-29 20:58:21 +00002399 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400#ifdef UNIX
2401 , stp
2402#endif
2403 ))
2404 return buf;
2405 return NULL;
2406}
2407
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002408#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) \
2409 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410/*
2411 * Find file in buffer list by a regexp pattern.
2412 * Return fnum of the found buffer.
2413 * Return < 0 for error.
2414 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002416buflist_findpat(
2417 char_u *pattern,
2418 char_u *pattern_end, /* pointer to first char after pattern */
2419 int unlisted, /* find unlisted buffers */
2420 int diffmode UNUSED, /* find diff-mode buffers only */
2421 int curtab_only) /* find buffers in current tab only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422{
2423 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 int match = -1;
2425 int find_listed;
2426 char_u *pat;
2427 char_u *patend;
2428 int attempt;
2429 char_u *p;
2430 int toggledollar;
2431
2432 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2433 {
2434 if (*pattern == '%')
2435 match = curbuf->b_fnum;
2436 else
2437 match = curwin->w_alt_fnum;
2438#ifdef FEAT_DIFF
2439 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2440 match = -1;
2441#endif
2442 }
2443
2444 /*
2445 * Try four ways of matching a listed buffer:
2446 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002447 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 * attempt == 2: with '$' at end (only match at end)
2449 * attempt == 3: with '^' at start and '$' at end (only full match)
2450 * Repeat this for finding an unlisted buffer if there was no matching
2451 * listed buffer.
2452 */
2453 else
2454 {
2455 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2456 if (pat == NULL)
2457 return -1;
2458 patend = pat + STRLEN(pat) - 1;
2459 toggledollar = (patend > pat && *patend == '$');
2460
2461 /* First try finding a listed buffer. If not found and "unlisted"
2462 * is TRUE, try finding an unlisted buffer. */
2463 find_listed = TRUE;
2464 for (;;)
2465 {
2466 for (attempt = 0; attempt <= 3; ++attempt)
2467 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002468 regmatch_T regmatch;
2469
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 /* may add '^' and '$' */
2471 if (toggledollar)
2472 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2473 p = pat;
2474 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2475 ++p;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002476 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2477 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 {
2479 vim_free(pat);
2480 return -1;
2481 }
2482
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002483 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 if (buf->b_p_bl == find_listed
2485#ifdef FEAT_DIFF
2486 && (!diffmode || diff_mode_buf(buf))
2487#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002488 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002490 if (curtab_only)
2491 {
2492 /* Ignore the match if the buffer is not open in
2493 * the current tab. */
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002494 win_T *wp;
2495
Bram Moolenaar29323592016-07-24 22:04:11 +02002496 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002497 if (wp->w_buffer == buf)
2498 break;
2499 if (wp == NULL)
2500 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 if (match >= 0) /* already found a match */
2503 {
2504 match = -2;
2505 break;
2506 }
2507 match = buf->b_fnum; /* remember first match */
2508 }
2509
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002510 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 if (match >= 0) /* found one match */
2512 break;
2513 }
2514
2515 /* Only search for unlisted buffers if there was no match with
2516 * a listed buffer. */
2517 if (!unlisted || !find_listed || match != -1)
2518 break;
2519 find_listed = FALSE;
2520 }
2521
2522 vim_free(pat);
2523 }
2524
2525 if (match == -2)
2526 EMSG2(_("E93: More than one match for %s"), pattern);
2527 else if (match < 0)
2528 EMSG2(_("E94: No matching buffer for %s"), pattern);
2529 return match;
2530}
2531#endif
2532
2533#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2534
2535/*
2536 * Find all buffer names that match.
2537 * For command line expansion of ":buf" and ":sbuf".
2538 * Return OK if matches found, FAIL otherwise.
2539 */
2540 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002541ExpandBufnames(
2542 char_u *pat,
2543 int *num_file,
2544 char_u ***file,
2545 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546{
2547 int count = 0;
2548 buf_T *buf;
2549 int round;
2550 char_u *p;
2551 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002552 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553
2554 *num_file = 0; /* return values in case of FAIL */
2555 *file = NULL;
2556
Bram Moolenaar05159a02005-02-26 23:04:13 +00002557 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2558 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002560 patc = alloc((unsigned)STRLEN(pat) + 11);
2561 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002563 STRCPY(patc, "\\(^\\|[\\/]\\)");
2564 STRCPY(patc + 11, pat + 1);
2565 }
2566 else
2567 patc = pat;
2568
2569 /*
2570 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002571 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002572 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002573 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002574 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002575 regmatch_T regmatch;
2576
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002577 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002578 break; /* there was no anchor, no need to try again */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002579 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2580 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002581 {
2582 if (patc != pat)
2583 vim_free(patc);
2584 return FAIL;
2585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586
2587 /*
2588 * round == 1: Count the matches.
2589 * round == 2: Build the array to keep the matches.
2590 */
2591 for (round = 1; round <= 2; ++round)
2592 {
2593 count = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002594 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 {
2596 if (!buf->b_p_bl) /* skip unlisted buffers */
2597 continue;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002598 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 if (p != NULL)
2600 {
2601 if (round == 1)
2602 ++count;
2603 else
2604 {
2605 if (options & WILD_HOME_REPLACE)
2606 p = home_replace_save(buf, p);
2607 else
2608 p = vim_strsave(p);
2609 (*file)[count++] = p;
2610 }
2611 }
2612 }
2613 if (count == 0) /* no match found, break here */
2614 break;
2615 if (round == 1)
2616 {
2617 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2618 if (*file == NULL)
2619 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002620 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002621 if (patc != pat)
2622 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 return FAIL;
2624 }
2625 }
2626 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002627 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 if (count) /* match(es) found, break here */
2629 break;
2630 }
2631
Bram Moolenaar05159a02005-02-26 23:04:13 +00002632 if (patc != pat)
2633 vim_free(patc);
2634
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 *num_file = count;
2636 return (count == 0 ? FAIL : OK);
2637}
2638
2639#endif /* FEAT_CMDL_COMPL */
2640
2641#ifdef HAVE_BUFLIST_MATCH
2642/*
2643 * Check for a match on the file name for buffer "buf" with regprog "prog".
2644 */
2645 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002646buflist_match(
2647 regmatch_T *rmp,
2648 buf_T *buf,
2649 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650{
2651 char_u *match;
2652
2653 /* First try the short file name, then the long file name. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002654 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002656 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657
2658 return match;
2659}
2660
2661/*
2662 * Try matching the regexp in "prog" with file name "name".
2663 * Return "name" when there is a match, NULL when not.
2664 */
2665 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002666fname_match(
2667 regmatch_T *rmp,
2668 char_u *name,
2669 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670{
2671 char_u *match = NULL;
2672 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673
2674 if (name != NULL)
2675 {
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002676 /* Ignore case when 'fileignorecase' or the argument is set. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002677 rmp->rm_ic = p_fic || ignore_case;
2678 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 match = name;
2680 else
2681 {
2682 /* Replace $(HOME) with '~' and try matching again. */
2683 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002684 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 match = name;
2686 vim_free(p);
2687 }
2688 }
2689
2690 return match;
2691}
2692#endif
2693
2694/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02002695 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 */
2697 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002698buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699{
Bram Moolenaar480778b2016-07-14 22:09:39 +02002700 char_u key[VIM_SIZEOF_INT * 2 + 1];
2701 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702
2703 if (nr == 0)
2704 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02002705 sprintf((char *)key, "%x", nr);
2706 hi = hash_find(&buf_hashtab, key);
2707
2708 if (!HASHITEM_EMPTY(hi))
2709 return (buf_T *)(hi->hi_key
2710 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 return NULL;
2712}
2713
2714/*
2715 * Get name of file 'n' in the buffer list.
2716 * When the file has no name an empty string is returned.
2717 * home_replace() is used to shorten the file name (used for marks).
2718 * Returns a pointer to allocated memory, of NULL when failed.
2719 */
2720 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002721buflist_nr2name(
2722 int n,
2723 int fullname,
2724 int helptail) /* for help buffers return tail only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725{
2726 buf_T *buf;
2727
2728 buf = buflist_findnr(n);
2729 if (buf == NULL)
2730 return NULL;
2731 return home_replace_save(helptail ? buf : NULL,
2732 fullname ? buf->b_ffname : buf->b_fname);
2733}
2734
2735/*
2736 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2737 * When "copy_options" is TRUE save the local window option values.
2738 * When "lnum" is 0 only do the options.
2739 */
2740 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002741buflist_setfpos(
2742 buf_T *buf,
2743 win_T *win,
2744 linenr_T lnum,
2745 colnr_T col,
2746 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747{
2748 wininfo_T *wip;
2749
2750 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2751 if (wip->wi_win == win)
2752 break;
2753 if (wip == NULL)
2754 {
2755 /* allocate a new entry */
2756 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2757 if (wip == NULL)
2758 return;
2759 wip->wi_win = win;
2760 if (lnum == 0) /* set lnum even when it's 0 */
2761 lnum = 1;
2762 }
2763 else
2764 {
2765 /* remove the entry from the list */
2766 if (wip->wi_prev)
2767 wip->wi_prev->wi_next = wip->wi_next;
2768 else
2769 buf->b_wininfo = wip->wi_next;
2770 if (wip->wi_next)
2771 wip->wi_next->wi_prev = wip->wi_prev;
2772 if (copy_options && wip->wi_optset)
2773 {
2774 clear_winopt(&wip->wi_opt);
2775#ifdef FEAT_FOLDING
2776 deleteFoldRecurse(&wip->wi_folds);
2777#endif
2778 }
2779 }
2780 if (lnum != 0)
2781 {
2782 wip->wi_fpos.lnum = lnum;
2783 wip->wi_fpos.col = col;
2784 }
2785 if (copy_options)
2786 {
2787 /* Save the window-specific option values. */
2788 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2789#ifdef FEAT_FOLDING
2790 wip->wi_fold_manual = win->w_fold_manual;
2791 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2792#endif
2793 wip->wi_optset = TRUE;
2794 }
2795
2796 /* insert the entry in front of the list */
2797 wip->wi_next = buf->b_wininfo;
2798 buf->b_wininfo = wip;
2799 wip->wi_prev = NULL;
2800 if (wip->wi_next)
2801 wip->wi_next->wi_prev = wip;
2802
2803 return;
2804}
2805
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002806#ifdef FEAT_DIFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01002807static int wininfo_other_tab_diff(wininfo_T *wip);
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002808
2809/*
2810 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2811 * page. That's because a diff is local to a tab page.
2812 */
2813 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002814wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002815{
2816 win_T *wp;
2817
2818 if (wip->wi_opt.wo_diff)
2819 {
Bram Moolenaar29323592016-07-24 22:04:11 +02002820 FOR_ALL_WINDOWS(wp)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002821 /* return FALSE when it's a window in the current tab page, thus
2822 * the buffer was in diff mode here */
2823 if (wip->wi_win == wp)
2824 return FALSE;
2825 return TRUE;
2826 }
2827 return FALSE;
2828}
2829#endif
2830
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831/*
2832 * Find info for the current window in buffer "buf".
2833 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002834 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2835 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 * Returns NULL when there isn't any info.
2837 */
2838 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002839find_wininfo(
2840 buf_T *buf,
2841 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842{
2843 wininfo_T *wip;
2844
2845 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002846 if (wip->wi_win == curwin
2847#ifdef FEAT_DIFF
2848 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2849#endif
2850 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002852
2853 /* If no wininfo for curwin, use the first in the list (that doesn't have
2854 * 'diff' set and is in another tab page). */
2855 if (wip == NULL)
2856 {
2857#ifdef FEAT_DIFF
2858 if (skip_diff_buffer)
2859 {
2860 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2861 if (!wininfo_other_tab_diff(wip))
2862 break;
2863 }
2864 else
2865#endif
2866 wip = buf->b_wininfo;
2867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 return wip;
2869}
2870
2871/*
2872 * Reset the local window options to the values last used in this window.
2873 * If the buffer wasn't used in this window before, use the values from
2874 * the most recently used window. If the values were never set, use the
2875 * global values for the window.
2876 */
2877 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002878get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879{
2880 wininfo_T *wip;
2881
2882 clear_winopt(&curwin->w_onebuf_opt);
2883#ifdef FEAT_FOLDING
2884 clearFolding(curwin);
2885#endif
2886
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002887 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 if (wip != NULL && wip->wi_optset)
2889 {
2890 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2891#ifdef FEAT_FOLDING
2892 curwin->w_fold_manual = wip->wi_fold_manual;
2893 curwin->w_foldinvalid = TRUE;
2894 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2895#endif
2896 }
2897 else
2898 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2899
2900#ifdef FEAT_FOLDING
2901 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2902 if (p_fdls >= 0)
2903 curwin->w_p_fdl = p_fdls;
2904#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002905#ifdef FEAT_SYN_HL
2906 check_colorcolumn(curwin);
2907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908}
2909
2910/*
2911 * Find the position (lnum and col) for the buffer 'buf' for the current
2912 * window.
2913 * Returns a pointer to no_position if no position is found.
2914 */
2915 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002916buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917{
2918 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002919 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002921 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 if (wip != NULL)
2923 return &(wip->wi_fpos);
2924 else
2925 return &no_position;
2926}
2927
2928/*
2929 * Find the lnum for the buffer 'buf' for the current window.
2930 */
2931 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01002932buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933{
2934 return buflist_findfpos(buf)->lnum;
2935}
2936
2937#if defined(FEAT_LISTCMDS) || defined(PROTO)
2938/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002939 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002942buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943{
2944 buf_T *buf;
2945 int len;
2946 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02002947 int ro_char;
2948 int changed_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949
2950 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2951 {
2952 /* skip unlisted buffers, unless ! was used */
Bram Moolenaard51cb702015-07-21 15:03:06 +02002953 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
2954 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
2955 || (vim_strchr(eap->arg, '+')
2956 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
2957 || (vim_strchr(eap->arg, 'a')
2958 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
2959 || (vim_strchr(eap->arg, 'h')
2960 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
2961 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
2962 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
2963 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
2964 || (vim_strchr(eap->arg, '%') && buf != curbuf)
2965 || (vim_strchr(eap->arg, '#')
2966 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02002969 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 else
2971 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02002972 if (message_filtered(NameBuff))
2973 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974
Bram Moolenaar304b64c2017-08-13 20:43:48 +02002975 changed_char = (buf->b_flags & BF_READERR) ? 'x'
2976 : (bufIsChanged(buf) ? '+' : ' ');
2977#ifdef FEAT_TERMINAL
2978 if (term_job_running(buf->b_term))
2979 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02002980 if (term_none_open(buf->b_term))
2981 ro_char = '?';
2982 else
2983 ro_char = 'R';
Bram Moolenaar304b64c2017-08-13 20:43:48 +02002984 changed_char = ' '; /* bufIsChanged() returns TRUE to avoid
2985 * closing, but it's not actually changed. */
2986 }
2987 else if (buf->b_term != NULL)
2988 ro_char = 'F';
2989 else
2990#endif
2991 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
2992
Bram Moolenaar77401ad2016-08-24 00:12:12 +02002993 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00002994 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 buf->b_fnum,
2996 buf->b_p_bl ? ' ' : 'u',
2997 buf == curbuf ? '%' :
2998 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2999 buf->b_ml.ml_mfp == NULL ? ' ' :
3000 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003001 ro_char,
3002 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003003 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003004 if (len > IOSIZE - 20)
3005 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006
3007 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 i = 40 - vim_strsize(IObuff);
3009 do
3010 {
3011 IObuff[len++] = ' ';
3012 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003013 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3014 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003015 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 msg_outtrans(IObuff);
3017 out_flush(); /* output one line at a time */
3018 ui_breakcheck();
3019 }
3020}
3021#endif
3022
3023/*
3024 * Get file name and line number for file 'fnum'.
3025 * Used by DoOneCmd() for translating '%' and '#'.
3026 * Used by insert_reg() and cmdline_paste() for '#' register.
3027 * Return FAIL if not found, OK for success.
3028 */
3029 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003030buflist_name_nr(
3031 int fnum,
3032 char_u **fname,
3033 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034{
3035 buf_T *buf;
3036
3037 buf = buflist_findnr(fnum);
3038 if (buf == NULL || buf->b_fname == NULL)
3039 return FAIL;
3040
3041 *fname = buf->b_fname;
3042 *lnum = buflist_findlnum(buf);
3043
3044 return OK;
3045}
3046
3047/*
3048 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
3049 * The file name with the full path is also remembered, for when :cd is used.
3050 * Returns FAIL for failure (file name already in use by other buffer)
3051 * OK otherwise.
3052 */
3053 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003054setfname(
3055 buf_T *buf,
3056 char_u *ffname,
3057 char_u *sfname,
3058 int message) /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059{
Bram Moolenaar81695252004-12-29 20:58:21 +00003060 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003062 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063#endif
3064
3065 if (ffname == NULL || *ffname == NUL)
3066 {
3067 /* Removing the name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01003068 VIM_CLEAR(buf->b_ffname);
3069 VIM_CLEAR(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070#ifdef UNIX
3071 st.st_dev = (dev_T)-1;
3072#endif
3073 }
3074 else
3075 {
3076 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
3077 if (ffname == NULL) /* out of memory */
3078 return FAIL;
3079
3080 /*
3081 * if the file name is already used in another buffer:
3082 * - if the buffer is loaded, fail
3083 * - if the buffer is not loaded, delete it from the list
3084 */
3085#ifdef UNIX
3086 if (mch_stat((char *)ffname, &st) < 0)
3087 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003088#endif
3089 if (!(buf->b_flags & BF_DUMMY))
3090#ifdef UNIX
3091 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003093 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094#endif
3095 if (obuf != NULL && obuf != buf)
3096 {
3097 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
3098 {
3099 if (message)
3100 EMSG(_("E95: Buffer with this name already exists"));
3101 vim_free(ffname);
3102 return FAIL;
3103 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01003104 /* delete from the list */
3105 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 }
3107 sfname = vim_strsave(sfname);
3108 if (ffname == NULL || sfname == NULL)
3109 {
3110 vim_free(sfname);
3111 vim_free(ffname);
3112 return FAIL;
3113 }
3114#ifdef USE_FNAME_CASE
3115# ifdef USE_LONG_FNAME
3116 if (USE_LONG_FNAME)
3117# endif
3118 fname_case(sfname, 0); /* set correct case for short file name */
3119#endif
3120 vim_free(buf->b_ffname);
3121 vim_free(buf->b_sfname);
3122 buf->b_ffname = ffname;
3123 buf->b_sfname = sfname;
3124 }
3125 buf->b_fname = buf->b_sfname;
3126#ifdef UNIX
3127 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003128 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 else
3130 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003131 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 buf->b_dev = st.st_dev;
3133 buf->b_ino = st.st_ino;
3134 }
3135#endif
3136
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138
3139 buf_name_changed(buf);
3140 return OK;
3141}
3142
3143/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003144 * Crude way of changing the name of a buffer. Use with care!
3145 * The name should be relative to the current directory.
3146 */
3147 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003148buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003149{
3150 buf_T *buf;
3151
3152 buf = buflist_findnr(fnum);
3153 if (buf != NULL)
3154 {
3155 vim_free(buf->b_sfname);
3156 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003157 buf->b_ffname = vim_strsave(name);
3158 buf->b_sfname = NULL;
3159 /* Allocate ffname and expand into full path. Also resolves .lnk
3160 * files on Win32. */
3161 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003162 buf->b_fname = buf->b_sfname;
3163 }
3164}
3165
3166/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 * Take care of what needs to be done when the name of buffer "buf" has
3168 * changed.
3169 */
3170 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003171buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172{
3173 /*
3174 * If the file name changed, also change the name of the swapfile
3175 */
3176 if (buf->b_ml.ml_mfp != NULL)
3177 ml_setname(buf);
3178
3179 if (curwin->w_buffer == buf)
3180 check_arg_idx(curwin); /* check file name for arg list */
3181#ifdef FEAT_TITLE
3182 maketitle(); /* set window title */
3183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 status_redraw_all(); /* status lines need to be redrawn */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 fmarks_check_names(buf); /* check named file marks */
3186 ml_timestamp(buf); /* reset timestamp */
3187}
3188
3189/*
3190 * set alternate file name for current window
3191 *
3192 * Used by do_one_cmd(), do_write() and do_ecmd().
3193 * Return the buffer.
3194 */
3195 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003196setaltfname(
3197 char_u *ffname,
3198 char_u *sfname,
3199 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200{
3201 buf_T *buf;
3202
3203 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
3204 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003205 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 curwin->w_alt_fnum = buf->b_fnum;
3207 return buf;
3208}
3209
3210/*
3211 * Get alternate file name for current window.
3212 * Return NULL if there isn't any, and give error message if requested.
3213 */
3214 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003215getaltfname(
3216 int errmsg) /* give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217{
3218 char_u *fname;
3219 linenr_T dummy;
3220
3221 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3222 {
3223 if (errmsg)
3224 EMSG(_(e_noalt));
3225 return NULL;
3226 }
3227 return fname;
3228}
3229
3230/*
3231 * Add a file name to the buflist and return its number.
3232 * Uses same flags as buflist_new(), except BLN_DUMMY.
3233 *
3234 * used by qf_init(), main() and doarglist()
3235 */
3236 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003237buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238{
3239 buf_T *buf;
3240
3241 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3242 if (buf != NULL)
3243 return buf->b_fnum;
3244 return 0;
3245}
3246
3247#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3248/*
3249 * Adjust slashes in file names. Called after 'shellslash' was set.
3250 */
3251 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003252buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253{
3254 buf_T *bp;
3255
Bram Moolenaar29323592016-07-24 22:04:11 +02003256 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 {
3258 if (bp->b_ffname != NULL)
3259 slash_adjust(bp->b_ffname);
3260 if (bp->b_sfname != NULL)
3261 slash_adjust(bp->b_sfname);
3262 }
3263}
3264#endif
3265
3266/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003267 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 * Also save the local window option values.
3269 */
3270 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003271buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003273 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274}
3275
3276/*
3277 * Return TRUE if 'ffname' is not the same file as current file.
3278 * Fname must have a full path (expanded by mch_FullName()).
3279 */
3280 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003281otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282{
3283 return otherfile_buf(curbuf, ffname
3284#ifdef UNIX
3285 , NULL
3286#endif
3287 );
3288}
3289
3290 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003291otherfile_buf(
3292 buf_T *buf,
3293 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003295 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003297 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298{
3299 /* no name is different */
3300 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3301 return TRUE;
3302 if (fnamecmp(ffname, buf->b_ffname) == 0)
3303 return FALSE;
3304#ifdef UNIX
3305 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003306 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307
Bram Moolenaar8767f522016-07-01 17:17:39 +02003308 /* If no stat_T given, get it now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 if (stp == NULL)
3310 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003311 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 st.st_dev = (dev_T)-1;
3313 stp = &st;
3314 }
3315 /* Use dev/ino to check if the files are the same, even when the names
3316 * are different (possible with links). Still need to compare the
3317 * name above, for when the file doesn't exist yet.
3318 * Problem: The dev/ino changes when a file is deleted (and created
3319 * again) and remains the same when renamed/moved. We don't want to
3320 * mch_stat() each buffer each time, that would be too slow. Get the
3321 * dev/ino again when they appear to match, but not when they appear
3322 * to be different: Could skip a buffer when it's actually the same
3323 * file. */
3324 if (buf_same_ino(buf, stp))
3325 {
3326 buf_setino(buf);
3327 if (buf_same_ino(buf, stp))
3328 return FALSE;
3329 }
3330 }
3331#endif
3332 return TRUE;
3333}
3334
3335#if defined(UNIX) || defined(PROTO)
3336/*
3337 * Set inode and device number for a buffer.
3338 * Must always be called when b_fname is changed!.
3339 */
3340 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003341buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003343 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344
3345 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3346 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003347 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 buf->b_dev = st.st_dev;
3349 buf->b_ino = st.st_ino;
3350 }
3351 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003352 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353}
3354
3355/*
3356 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3357 */
3358 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003359buf_same_ino(
3360 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003361 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003363 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 && stp->st_dev == buf->b_dev
3365 && stp->st_ino == buf->b_ino);
3366}
3367#endif
3368
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003369/*
3370 * Print info about the current buffer.
3371 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003373fileinfo(
3374 int fullname, /* when non-zero print full path */
3375 int shorthelp,
3376 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377{
3378 char_u *name;
3379 int n;
3380 char_u *p;
3381 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003382 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383
3384 buffer = alloc(IOSIZE);
3385 if (buffer == NULL)
3386 return;
3387
3388 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3389 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003390 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 p = buffer + STRLEN(buffer);
3392 }
3393 else
3394 p = buffer;
3395
3396 *p++ = '"';
3397 if (buf_spname(curbuf) != NULL)
Bram Moolenaarec3cfeb2012-10-03 17:12:47 +02003398 vim_strncpy(p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 else
3400 {
3401 if (!fullname && curbuf->b_fname != NULL)
3402 name = curbuf->b_fname;
3403 else
3404 name = curbuf->b_ffname;
3405 home_replace(shorthelp ? curbuf : NULL, name, p,
3406 (int)(IOSIZE - (p - buffer)), TRUE);
3407 }
3408
Bram Moolenaara800b422010-06-27 01:15:55 +02003409 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 curbufIsChanged() ? (shortmess(SHM_MOD)
3411 ? " [+]" : _(" [Modified]")) : " ",
3412 (curbuf->b_flags & BF_NOTEDITED)
3413#ifdef FEAT_QUICKFIX
3414 && !bt_dontwrite(curbuf)
3415#endif
3416 ? _("[Not edited]") : "",
3417 (curbuf->b_flags & BF_NEW)
3418#ifdef FEAT_QUICKFIX
3419 && !bt_dontwrite(curbuf)
3420#endif
3421 ? _("[New file]") : "",
3422 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003423 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 : _("[readonly]")) : "",
3425 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3426 || curbuf->b_p_ro) ?
3427 " " : "");
3428 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3429 * causes an overflow, thus for large numbers divide instead. */
3430 if (curwin->w_cursor.lnum > 1000000L)
3431 n = (int)(((long)curwin->w_cursor.lnum) /
3432 ((long)curbuf->b_ml.ml_line_count / 100L));
3433 else
3434 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3435 (long)curbuf->b_ml.ml_line_count);
3436 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3437 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003438 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 }
3440#ifdef FEAT_CMDL_INFO
3441 else if (p_ru)
3442 {
3443 /* Current line and column are already on the screen -- webb */
3444 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003445 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003447 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 (long)curbuf->b_ml.ml_line_count, n);
3449 }
3450#endif
3451 else
3452 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003453 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 _("line %ld of %ld --%d%%-- col "),
3455 (long)curwin->w_cursor.lnum,
3456 (long)curbuf->b_ml.ml_line_count,
3457 n);
3458 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003459 len = STRLEN(buffer);
3460 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3462 }
3463
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003464 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465
3466 if (dont_truncate)
3467 {
3468 /* Temporarily set msg_scroll to avoid the message being truncated.
3469 * First call msg_start() to get the message in the right place. */
3470 msg_start();
3471 n = msg_scroll;
3472 msg_scroll = TRUE;
3473 msg(buffer);
3474 msg_scroll = n;
3475 }
3476 else
3477 {
3478 p = msg_trunc_attr(buffer, FALSE, 0);
3479 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 /* Need to repeat the message after redrawing when:
3481 * - When restart_edit is set (otherwise there will be a delay
3482 * before redrawing).
3483 * - When the screen was scrolled but there is no wait-return
3484 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003485 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 }
3487
3488 vim_free(buffer);
3489}
3490
3491 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003492col_print(
3493 char_u *buf,
3494 size_t buflen,
3495 int col,
3496 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497{
3498 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003499 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003501 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502}
3503
3504#if defined(FEAT_TITLE) || defined(PROTO)
3505/*
3506 * put file name in title bar of window and in icon title
3507 */
3508
3509static char_u *lasttitle = NULL;
3510static char_u *lasticon = NULL;
3511
3512 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003513maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514{
3515 char_u *p;
3516 char_u *t_str = NULL;
3517 char_u *i_name;
3518 char_u *i_str = NULL;
3519 int maxlen = 0;
3520 int len;
3521 int mustset;
3522 char_u buf[IOSIZE];
3523 int off;
3524
3525 if (!redrawing())
3526 {
3527 /* Postpone updating the title when 'lazyredraw' is set. */
3528 need_maketitle = TRUE;
3529 return;
3530 }
3531
3532 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003533 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 return;
3535
3536 if (p_title)
3537 {
3538 if (p_titlelen > 0)
3539 {
3540 maxlen = p_titlelen * Columns / 100;
3541 if (maxlen < 10)
3542 maxlen = 10;
3543 }
3544
3545 t_str = buf;
3546 if (*p_titlestring != NUL)
3547 {
3548#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003549 if (stl_syntax & STL_IN_TITLE)
3550 {
3551 int use_sandbox = FALSE;
3552 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003553
3554# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003555 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003556# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003557 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003559 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003560 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003561 if (called_emsg)
3562 set_string_option_direct((char_u *)"titlestring", -1,
3563 (char_u *)"", OPT_FREE, SID_ERROR);
3564 called_emsg |= save_called_emsg;
3565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 else
3567#endif
3568 t_str = p_titlestring;
3569 }
3570 else
3571 {
3572 /* format: "fname + (path) (1 of 2) - VIM" */
3573
Bram Moolenaar2c666692012-09-05 13:30:40 +02003574#define SPACE_FOR_FNAME (IOSIZE - 100)
3575#define SPACE_FOR_DIR (IOSIZE - 20)
3576#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003578 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar21554412017-07-24 21:44:43 +02003579#ifdef FEAT_TERMINAL
3580 else if (curbuf->b_term != NULL)
3581 {
3582 vim_strncpy(buf, term_get_status_text(curbuf->b_term),
3583 SPACE_FOR_FNAME);
3584 }
3585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 else
3587 {
3588 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003589 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 }
3592
Bram Moolenaar21554412017-07-24 21:44:43 +02003593#ifdef FEAT_TERMINAL
3594 if (curbuf->b_term == NULL)
3595#endif
3596 switch (bufIsChanged(curbuf)
3597 + (curbuf->b_p_ro * 2)
3598 + (!curbuf->b_p_ma * 4))
3599 {
3600 case 1: STRCAT(buf, " +"); break;
3601 case 2: STRCAT(buf, " ="); break;
3602 case 3: STRCAT(buf, " =+"); break;
3603 case 4:
3604 case 6: STRCAT(buf, " -"); break;
3605 case 5:
3606 case 7: STRCAT(buf, " -+"); break;
3607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608
Bram Moolenaar21554412017-07-24 21:44:43 +02003609 if (curbuf->b_fname != NULL
3610#ifdef FEAT_TERMINAL
3611 && curbuf->b_term == NULL
3612#endif
3613 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 {
3615 /* Get path of file, replace home dir with ~ */
3616 off = (int)STRLEN(buf);
3617 buf[off++] = ' ';
3618 buf[off++] = '(';
3619 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003620 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621#ifdef BACKSLASH_IN_FILENAME
3622 /* avoid "c:/name" to be reduced to "c" */
3623 if (isalpha(buf[off]) && buf[off + 1] == ':')
3624 off += 2;
3625#endif
3626 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003627 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 if (p == buf + off)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003629 {
Bram Moolenaar21554412017-07-24 21:44:43 +02003630 /* must be a help buffer */
3631 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003632 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003636
Bram Moolenaar2c666692012-09-05 13:30:40 +02003637 /* Translate unprintable chars and concatenate. Keep some
3638 * room for the server name. When there is no room (very long
3639 * file name) use (...). */
3640 if (off < SPACE_FOR_DIR)
3641 {
3642 p = transstr(buf + off);
3643 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3644 vim_free(p);
3645 }
3646 else
3647 {
3648 vim_strncpy(buf + off, (char_u *)"...",
3649 (size_t)(SPACE_FOR_ARGNR - off));
3650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 STRCAT(buf, ")");
3652 }
3653
Bram Moolenaar2c666692012-09-05 13:30:40 +02003654 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655
3656#if defined(FEAT_CLIENTSERVER)
3657 if (serverName != NULL)
3658 {
3659 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003660 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 }
3662 else
3663#endif
3664 STRCAT(buf, " - VIM");
3665
3666 if (maxlen > 0)
3667 {
3668 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003669 if (vim_strsize(buf) > maxlen)
3670 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 }
3672 }
3673 }
3674 mustset = ti_change(t_str, &lasttitle);
3675
3676 if (p_icon)
3677 {
3678 i_str = buf;
3679 if (*p_iconstring != NUL)
3680 {
3681#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003682 if (stl_syntax & STL_IN_ICON)
3683 {
3684 int use_sandbox = FALSE;
3685 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003686
3687# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003688 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003689# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003690 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003692 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003693 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003694 if (called_emsg)
3695 set_string_option_direct((char_u *)"iconstring", -1,
3696 (char_u *)"", OPT_FREE, SID_ERROR);
3697 called_emsg |= save_called_emsg;
3698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 else
3700#endif
3701 i_str = p_iconstring;
3702 }
3703 else
3704 {
3705 if (buf_spname(curbuf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003706 i_name = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 else /* use file name only in icon */
3708 i_name = gettail(curbuf->b_ffname);
3709 *i_str = NUL;
3710 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003711 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 if (len > 100)
3713 {
3714 len -= 100;
3715#ifdef FEAT_MBYTE
3716 if (has_mbyte)
3717 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3718#endif
3719 i_name += len;
3720 }
3721 STRCPY(i_str, i_name);
3722 trans_characters(i_str, IOSIZE);
3723 }
3724 }
3725
3726 mustset |= ti_change(i_str, &lasticon);
3727
3728 if (mustset)
3729 resettitle();
3730}
3731
3732/*
3733 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3734 * from "str" if it does.
3735 * Return TRUE when "*last" changed.
3736 */
3737 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003738ti_change(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739{
3740 if ((str == NULL) != (*last == NULL)
3741 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3742 {
3743 vim_free(*last);
3744 if (str == NULL)
3745 *last = NULL;
3746 else
3747 *last = vim_strsave(str);
3748 return TRUE;
3749 }
3750 return FALSE;
3751}
3752
3753/*
3754 * Put current window title back (used after calling a shell)
3755 */
3756 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003757resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758{
3759 mch_settitle(lasttitle, lasticon);
3760}
Bram Moolenaarea408852005-06-25 22:49:46 +00003761
3762# if defined(EXITFREE) || defined(PROTO)
3763 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003764free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00003765{
3766 vim_free(lasttitle);
3767 vim_free(lasticon);
3768}
3769# endif
3770
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771#endif /* FEAT_TITLE */
3772
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003773#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003775 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 * Return length of string in screen cells.
3777 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003778 * Normally works for window "wp", except when working for 'tabline' then it
3779 * is "curwin".
3780 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 * Items are drawn interspersed with the text that surrounds it
3782 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3783 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3784 *
3785 * If maxwidth is not zero, the string will be filled at any middle marker
3786 * or truncated if too long, fillchar is used for all whitespace.
3787 */
3788 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003789build_stl_str_hl(
3790 win_T *wp,
3791 char_u *out, /* buffer to write into != NameBuff */
3792 size_t outlen, /* length of out[] */
3793 char_u *fmt,
3794 int use_sandbox UNUSED, /* "fmt" was set insecurely, use sandbox */
3795 int fillchar,
3796 int maxwidth,
3797 struct stl_hlrec *hltab, /* return: HL attributes (can be NULL) */
3798 struct stl_hlrec *tabtab) /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799{
3800 char_u *p;
3801 char_u *s;
3802 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003803 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804#ifdef FEAT_EVAL
Bram Moolenaarba2929b2017-09-08 13:59:21 +02003805 win_T *save_curwin;
3806 buf_T *save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807#endif
3808 int empty_line;
3809 colnr_T virtcol;
3810 long l;
3811 long n;
3812 int prevchar_isflag;
3813 int prevchar_isitem;
3814 int itemisflag;
3815 int fillable;
3816 char_u *str;
3817 long num;
3818 int width;
3819 int itemcnt;
3820 int curitem;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02003821 int group_end_userhl;
3822 int group_start_userhl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 int groupitem[STL_MAX_ITEM];
3824 int groupdepth;
3825 struct stl_item
3826 {
3827 char_u *start;
3828 int minwid;
3829 int maxwid;
3830 enum
3831 {
3832 Normal,
3833 Empty,
3834 Group,
3835 Middle,
3836 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003837 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 Trunc
3839 } type;
3840 } item[STL_MAX_ITEM];
3841 int minwid;
3842 int maxwid;
3843 int zeropad;
3844 char_u base;
3845 char_u opt;
3846#define TMPLEN 70
3847 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003848 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003849 struct stl_hlrec *sp;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02003850 int save_must_redraw = must_redraw;
3851 int save_redr_type = curwin->w_redr_type;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003852
3853#ifdef FEAT_EVAL
3854 /*
3855 * When the format starts with "%!" then evaluate it as an expression and
3856 * use the result as the actual format string.
3857 */
3858 if (fmt[0] == '%' && fmt[1] == '!')
3859 {
3860 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3861 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003862 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003863 }
3864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865
3866 if (fillchar == 0)
3867 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003868#ifdef FEAT_MBYTE
3869 /* Can't handle a multi-byte fill character yet. */
3870 else if (mb_char2len(fillchar) > 1)
3871 fillchar = '-';
3872#endif
3873
Bram Moolenaar567199b2013-04-24 16:52:36 +02003874 /* Get line & check if empty (cursorpos will show "0-1"). Note that
3875 * p will become invalid when getting another buffer line. */
3876 p = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3877 empty_line = (*p == NUL);
3878
3879 /* Get the byte value now, in case we need it below. This is more
3880 * efficient than making a copy of the line. */
3881 if (wp->w_cursor.col > (colnr_T)STRLEN(p))
3882 byteval = 0;
3883 else
3884#ifdef FEAT_MBYTE
3885 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
3886#else
3887 byteval = p[wp->w_cursor.col];
3888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889
3890 groupdepth = 0;
3891 p = out;
3892 curitem = 0;
3893 prevchar_isflag = TRUE;
3894 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003895 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003897 if (curitem == STL_MAX_ITEM)
3898 {
3899 /* There are too many items. Add the error code to the statusline
3900 * to give the user a hint about what went wrong. */
3901 if (p + 6 < out + outlen)
3902 {
3903 mch_memmove(p, " E541", (size_t)5);
3904 p += 5;
3905 }
3906 break;
3907 }
3908
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 if (*s != NUL && *s != '%')
3910 prevchar_isflag = prevchar_isitem = FALSE;
3911
3912 /*
3913 * Handle up to the next '%' or the end.
3914 */
3915 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3916 *p++ = *s++;
3917 if (*s == NUL || p + 1 >= out + outlen)
3918 break;
3919
3920 /*
3921 * Handle one '%' item.
3922 */
3923 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003924 if (*s == NUL) /* ignore trailing % */
3925 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 if (*s == '%')
3927 {
3928 if (p + 1 >= out + outlen)
3929 break;
3930 *p++ = *s++;
3931 prevchar_isflag = prevchar_isitem = FALSE;
3932 continue;
3933 }
3934 if (*s == STL_MIDDLEMARK)
3935 {
3936 s++;
3937 if (groupdepth > 0)
3938 continue;
3939 item[curitem].type = Middle;
3940 item[curitem++].start = p;
3941 continue;
3942 }
3943 if (*s == STL_TRUNCMARK)
3944 {
3945 s++;
3946 item[curitem].type = Trunc;
3947 item[curitem++].start = p;
3948 continue;
3949 }
3950 if (*s == ')')
3951 {
3952 s++;
3953 if (groupdepth < 1)
3954 continue;
3955 groupdepth--;
3956
3957 t = item[groupitem[groupdepth]].start;
3958 *p = NUL;
3959 l = vim_strsize(t);
3960 if (curitem > groupitem[groupdepth] + 1
3961 && item[groupitem[groupdepth]].minwid == 0)
3962 {
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02003963 /* remove group if all items are empty and highlight group
3964 * doesn't change */
3965 group_start_userhl = group_end_userhl = 0;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02003966 for (n = groupitem[groupdepth] - 1; n >= 0; n--)
3967 {
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02003968 if (item[n].type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02003969 {
3970 group_start_userhl = group_end_userhl = item[n].minwid;
3971 break;
3972 }
3973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02003975 {
3976 if (item[n].type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 break;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02003978 if (item[n].type == Highlight)
3979 group_end_userhl = item[n].minwid;
3980 }
3981 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 {
3983 p = t;
3984 l = 0;
3985 }
3986 }
3987 if (l > item[groupitem[groupdepth]].maxwid)
3988 {
3989 /* truncate, remove n bytes of text at the start */
3990#ifdef FEAT_MBYTE
3991 if (has_mbyte)
3992 {
3993 /* Find the first character that should be included. */
3994 n = 0;
3995 while (l >= item[groupitem[groupdepth]].maxwid)
3996 {
3997 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003998 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 }
4000 }
4001 else
4002#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004003 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004
4005 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004006 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 p = p - n + 1;
4008#ifdef FEAT_MBYTE
4009 /* Fill up space left over by half a double-wide char. */
4010 while (++l < item[groupitem[groupdepth]].minwid)
4011 *p++ = fillchar;
4012#endif
4013
4014 /* correct the start of the items for the truncation */
4015 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
4016 {
4017 item[l].start -= n;
4018 if (item[l].start < t)
4019 item[l].start = t;
4020 }
4021 }
4022 else if (abs(item[groupitem[groupdepth]].minwid) > l)
4023 {
4024 /* fill */
4025 n = item[groupitem[groupdepth]].minwid;
4026 if (n < 0)
4027 {
4028 /* fill by appending characters */
4029 n = 0 - n;
4030 while (l++ < n && p + 1 < out + outlen)
4031 *p++ = fillchar;
4032 }
4033 else
4034 {
4035 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004036 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 l = n - l;
4038 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004039 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 p += l;
4041 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
4042 item[n].start += l;
4043 for ( ; l > 0; l--)
4044 *t++ = fillchar;
4045 }
4046 }
4047 continue;
4048 }
4049 minwid = 0;
4050 maxwid = 9999;
4051 zeropad = FALSE;
4052 l = 1;
4053 if (*s == '0')
4054 {
4055 s++;
4056 zeropad = TRUE;
4057 }
4058 if (*s == '-')
4059 {
4060 s++;
4061 l = -1;
4062 }
4063 if (VIM_ISDIGIT(*s))
4064 {
4065 minwid = (int)getdigits(&s);
4066 if (minwid < 0) /* overflow */
4067 minwid = 0;
4068 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004069 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 {
4071 item[curitem].type = Highlight;
4072 item[curitem].start = p;
4073 item[curitem].minwid = minwid > 9 ? 1 : minwid;
4074 s++;
4075 curitem++;
4076 continue;
4077 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004078 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4079 {
4080 if (*s == STL_TABCLOSENR)
4081 {
4082 if (minwid == 0)
4083 {
4084 /* %X ends the close label, go back to the previously
4085 * define tab label nr. */
4086 for (n = curitem - 1; n >= 0; --n)
4087 if (item[n].type == TabPage && item[n].minwid >= 0)
4088 {
4089 minwid = item[n].minwid;
4090 break;
4091 }
4092 }
4093 else
4094 /* close nrs are stored as negative values */
4095 minwid = - minwid;
4096 }
4097 item[curitem].type = TabPage;
4098 item[curitem].start = p;
4099 item[curitem].minwid = minwid;
4100 s++;
4101 curitem++;
4102 continue;
4103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 if (*s == '.')
4105 {
4106 s++;
4107 if (VIM_ISDIGIT(*s))
4108 {
4109 maxwid = (int)getdigits(&s);
4110 if (maxwid <= 0) /* overflow */
4111 maxwid = 50;
4112 }
4113 }
4114 minwid = (minwid > 50 ? 50 : minwid) * l;
4115 if (*s == '(')
4116 {
4117 groupitem[groupdepth++] = curitem;
4118 item[curitem].type = Group;
4119 item[curitem].start = p;
4120 item[curitem].minwid = minwid;
4121 item[curitem].maxwid = maxwid;
4122 s++;
4123 curitem++;
4124 continue;
4125 }
4126 if (vim_strchr(STL_ALL, *s) == NULL)
4127 {
4128 s++;
4129 continue;
4130 }
4131 opt = *s++;
4132
4133 /* OK - now for the real work */
4134 base = 'D';
4135 itemisflag = FALSE;
4136 fillable = TRUE;
4137 num = -1;
4138 str = NULL;
4139 switch (opt)
4140 {
4141 case STL_FILEPATH:
4142 case STL_FULLPATH:
4143 case STL_FILENAME:
4144 fillable = FALSE; /* don't change ' ' to fillchar */
4145 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004146 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 else
4148 {
4149 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004150 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4152 }
4153 trans_characters(NameBuff, MAXPATHL);
4154 if (opt != STL_FILENAME)
4155 str = NameBuff;
4156 else
4157 str = gettail(NameBuff);
4158 break;
4159
4160 case STL_VIM_EXPR: /* '{' */
4161 itemisflag = TRUE;
4162 t = p;
4163 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
4164 *p++ = *s++;
4165 if (*s != '}') /* missing '}' or out of space */
4166 break;
4167 s++;
4168 *p = 0;
4169 p = t;
4170
4171#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004172 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 set_internal_string_var((char_u *)"actual_curbuf", tmp);
4174
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004175 save_curbuf = curbuf;
4176 save_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 curwin = wp;
4178 curbuf = wp->w_buffer;
4179
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004180 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004182 curwin = save_curwin;
4183 curbuf = save_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00004184 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185
4186 if (str != NULL && *str != 0)
4187 {
4188 if (*skipdigits(str) == NUL)
4189 {
4190 num = atoi((char *)str);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004191 VIM_CLEAR(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 itemisflag = FALSE;
4193 }
4194 }
4195#endif
4196 break;
4197
4198 case STL_LINE:
4199 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4200 ? 0L : (long)(wp->w_cursor.lnum);
4201 break;
4202
4203 case STL_NUMLINES:
4204 num = wp->w_buffer->b_ml.ml_line_count;
4205 break;
4206
4207 case STL_COLUMN:
4208 num = !(State & INSERT) && empty_line
4209 ? 0 : (int)wp->w_cursor.col + 1;
4210 break;
4211
4212 case STL_VIRTCOL:
4213 case STL_VIRTCOL_ALT:
4214 /* In list mode virtcol needs to be recomputed */
4215 virtcol = wp->w_virtcol;
4216 if (wp->w_p_list && lcs_tab1 == NUL)
4217 {
4218 wp->w_p_list = FALSE;
4219 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
4220 wp->w_p_list = TRUE;
4221 }
4222 ++virtcol;
4223 /* Don't display %V if it's the same as %c. */
4224 if (opt == STL_VIRTCOL_ALT
4225 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
4226 ? 0 : (int)wp->w_cursor.col + 1)))
4227 break;
4228 num = (long)virtcol;
4229 break;
4230
4231 case STL_PERCENTAGE:
4232 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4233 (long)wp->w_buffer->b_ml.ml_line_count);
4234 break;
4235
4236 case STL_ALTPERCENT:
4237 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004238 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 break;
4240
4241 case STL_ARGLISTSTAT:
4242 fillable = FALSE;
4243 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004244 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 str = tmp;
4246 break;
4247
4248 case STL_KEYMAP:
4249 fillable = FALSE;
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02004250 if (get_keymap_str(wp, (char_u *)"<%s>", tmp, TMPLEN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 str = tmp;
4252 break;
4253 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004254#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004255 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256#else
4257 num = 0;
4258#endif
4259 break;
4260
4261 case STL_BUFNO:
4262 num = wp->w_buffer->b_fnum;
4263 break;
4264
4265 case STL_OFFSET_X:
4266 base = 'X';
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004267 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 case STL_OFFSET:
4269#ifdef FEAT_BYTEOFF
4270 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4271 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4272 0L : l + 1 + (!(State & INSERT) && empty_line ?
4273 0 : (int)wp->w_cursor.col);
4274#endif
4275 break;
4276
4277 case STL_BYTEVAL_X:
4278 base = 'X';
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004279 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004281 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 if (num == NL)
4283 num = 0;
4284 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4285 num = NL;
4286 break;
4287
4288 case STL_ROFLAG:
4289 case STL_ROFLAG_ALT:
4290 itemisflag = TRUE;
4291 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004292 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 break;
4294
4295 case STL_HELPFLAG:
4296 case STL_HELPFLAG_ALT:
4297 itemisflag = TRUE;
4298 if (wp->w_buffer->b_help)
4299 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004300 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 break;
4302
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 case STL_FILETYPE:
4304 if (*wp->w_buffer->b_p_ft != NUL
4305 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4306 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004307 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
4308 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 str = tmp;
4310 }
4311 break;
4312
4313 case STL_FILETYPE_ALT:
4314 itemisflag = TRUE;
4315 if (*wp->w_buffer->b_p_ft != NUL
4316 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4317 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004318 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
4319 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 for (t = tmp; *t != 0; t++)
4321 *t = TOUPPER_LOC(*t);
4322 str = tmp;
4323 }
4324 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325
Bram Moolenaar4033c552017-09-16 20:54:51 +02004326#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 case STL_PREVIEWFLAG:
4328 case STL_PREVIEWFLAG_ALT:
4329 itemisflag = TRUE;
4330 if (wp->w_p_pvw)
4331 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4332 : _("[Preview]"));
4333 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004334
4335 case STL_QUICKFIX:
4336 if (bt_quickfix(wp->w_buffer))
4337 str = (char_u *)(wp->w_llist_ref
4338 ? _(msg_loclist)
4339 : _(msg_qflist));
4340 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341#endif
4342
4343 case STL_MODIFIED:
4344 case STL_MODIFIED_ALT:
4345 itemisflag = TRUE;
4346 switch ((opt == STL_MODIFIED_ALT)
4347 + bufIsChanged(wp->w_buffer) * 2
4348 + (!wp->w_buffer->b_p_ma) * 4)
4349 {
4350 case 2: str = (char_u *)"[+]"; break;
4351 case 3: str = (char_u *)",+"; break;
4352 case 4: str = (char_u *)"[-]"; break;
4353 case 5: str = (char_u *)",-"; break;
4354 case 6: str = (char_u *)"[+-]"; break;
4355 case 7: str = (char_u *)",+-"; break;
4356 }
4357 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004358
4359 case STL_HIGHLIGHT:
4360 t = s;
4361 while (*s != '#' && *s != NUL)
4362 ++s;
4363 if (*s == '#')
4364 {
4365 item[curitem].type = Highlight;
4366 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004367 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004368 curitem++;
4369 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004370 if (*s != NUL)
4371 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004372 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 }
4374
4375 item[curitem].start = p;
4376 item[curitem].type = Normal;
4377 if (str != NULL && *str)
4378 {
4379 t = str;
4380 if (itemisflag)
4381 {
4382 if ((t[0] && t[1])
4383 && ((!prevchar_isitem && *t == ',')
4384 || (prevchar_isflag && *t == ' ')))
4385 t++;
4386 prevchar_isflag = TRUE;
4387 }
4388 l = vim_strsize(t);
4389 if (l > 0)
4390 prevchar_isitem = TRUE;
4391 if (l > maxwid)
4392 {
4393 while (l >= maxwid)
4394#ifdef FEAT_MBYTE
4395 if (has_mbyte)
4396 {
4397 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004398 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 }
4400 else
4401#endif
4402 l -= byte2cells(*t++);
4403 if (p + 1 >= out + outlen)
4404 break;
4405 *p++ = '<';
4406 }
4407 if (minwid > 0)
4408 {
4409 for (; l < minwid && p + 1 < out + outlen; l++)
4410 {
4411 /* Don't put a "-" in front of a digit. */
4412 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4413 *p++ = ' ';
4414 else
4415 *p++ = fillchar;
4416 }
4417 minwid = 0;
4418 }
4419 else
4420 minwid *= -1;
4421 while (*t && p + 1 < out + outlen)
4422 {
4423 *p++ = *t++;
4424 /* Change a space by fillchar, unless fillchar is '-' and a
4425 * digit follows. */
4426 if (fillable && p[-1] == ' '
4427 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4428 p[-1] = fillchar;
4429 }
4430 for (; l < minwid && p + 1 < out + outlen; l++)
4431 *p++ = fillchar;
4432 }
4433 else if (num >= 0)
4434 {
4435 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4436 char_u nstr[20];
4437
4438 if (p + 20 >= out + outlen)
4439 break; /* not sufficient space */
4440 prevchar_isitem = TRUE;
4441 t = nstr;
4442 if (opt == STL_VIRTCOL_ALT)
4443 {
4444 *t++ = '-';
4445 minwid--;
4446 }
4447 *t++ = '%';
4448 if (zeropad)
4449 *t++ = '0';
4450 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004451 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 *t = 0;
4453
4454 for (n = num, l = 1; n >= nbase; n /= nbase)
4455 l++;
4456 if (opt == STL_VIRTCOL_ALT)
4457 l++;
4458 if (l > maxwid)
4459 {
4460 l += 2;
4461 n = l - maxwid;
4462 while (l-- > maxwid)
4463 num /= nbase;
4464 *t++ = '>';
4465 *t++ = '%';
4466 *t = t[-3];
4467 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004468 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4469 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 }
4471 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004472 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4473 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 p += STRLEN(p);
4475 }
4476 else
4477 item[curitem].type = Empty;
4478
4479 if (opt == STL_VIM_EXPR)
4480 vim_free(str);
4481
4482 if (num >= 0 || (!itemisflag && str && *str))
4483 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4484 curitem++;
4485 }
4486 *p = NUL;
4487 itemcnt = curitem;
4488
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004489#ifdef FEAT_EVAL
4490 if (usefmt != fmt)
4491 vim_free(usefmt);
4492#endif
4493
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 width = vim_strsize(out);
4495 if (maxwidth > 0 && width > maxwidth)
4496 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004497 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 l = 0;
4499 if (itemcnt == 0)
4500 s = out;
4501 else
4502 {
4503 for ( ; l < itemcnt; l++)
4504 if (item[l].type == Trunc)
4505 {
4506 /* Truncate at %< item. */
4507 s = item[l].start;
4508 break;
4509 }
4510 if (l == itemcnt)
4511 {
4512 /* No %< item, truncate first item. */
4513 s = item[0].start;
4514 l = 0;
4515 }
4516 }
4517
4518 if (width - vim_strsize(s) >= maxwidth)
4519 {
4520 /* Truncation mark is beyond max length */
4521#ifdef FEAT_MBYTE
4522 if (has_mbyte)
4523 {
4524 s = out;
4525 width = 0;
4526 for (;;)
4527 {
4528 width += ptr2cells(s);
4529 if (width >= maxwidth)
4530 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004531 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 }
4533 /* Fill up for half a double-wide character. */
4534 while (++width < maxwidth)
4535 *s++ = fillchar;
4536 }
4537 else
4538#endif
4539 s = out + maxwidth - 1;
4540 for (l = 0; l < itemcnt; l++)
4541 if (item[l].start > s)
4542 break;
4543 itemcnt = l;
4544 *s++ = '>';
4545 *s = 0;
4546 }
4547 else
4548 {
4549#ifdef FEAT_MBYTE
4550 if (has_mbyte)
4551 {
4552 n = 0;
4553 while (width >= maxwidth)
4554 {
4555 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004556 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 }
4558 }
4559 else
4560#endif
4561 n = width - maxwidth + 1;
4562 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004563 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 *s = '<';
4565
4566 /* Fill up for half a double-wide character. */
4567 while (++width < maxwidth)
4568 {
4569 s = s + STRLEN(s);
4570 *s++ = fillchar;
4571 *s = NUL;
4572 }
4573
4574 --n; /* count the '<' */
4575 for (; l < itemcnt; l++)
4576 {
4577 if (item[l].start - n >= s)
4578 item[l].start -= n;
4579 else
4580 item[l].start = s;
4581 }
4582 }
4583 width = maxwidth;
4584 }
4585 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4586 {
4587 /* Apply STL_MIDDLE if any */
4588 for (l = 0; l < itemcnt; l++)
4589 if (item[l].type == Middle)
4590 break;
4591 if (l < itemcnt)
4592 {
4593 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004594 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 for (s = item[l].start; s < p; s++)
4596 *s = fillchar;
4597 for (l++; l < itemcnt; l++)
4598 item[l].start += maxwidth - width;
4599 width = maxwidth;
4600 }
4601 }
4602
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004603 /* Store the info about highlighting. */
4604 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004606 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 for (l = 0; l < itemcnt; l++)
4608 {
4609 if (item[l].type == Highlight)
4610 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004611 sp->start = item[l].start;
4612 sp->userhl = item[l].minwid;
4613 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 }
4615 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004616 sp->start = NULL;
4617 sp->userhl = 0;
4618 }
4619
4620 /* Store the info about tab pages labels. */
4621 if (tabtab != NULL)
4622 {
4623 sp = tabtab;
4624 for (l = 0; l < itemcnt; l++)
4625 {
4626 if (item[l].type == TabPage)
4627 {
4628 sp->start = item[l].start;
4629 sp->userhl = item[l].minwid;
4630 sp++;
4631 }
4632 }
4633 sp->start = NULL;
4634 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 }
4636
Bram Moolenaar65ed1362017-09-30 16:00:14 +02004637 /* When inside update_screen we do not want redrawing a stausline, ruler,
4638 * title, etc. to trigger another redraw, it may cause an endless loop. */
4639 if (updating_screen)
4640 {
4641 must_redraw = save_must_redraw;
4642 curwin->w_redr_type = save_redr_type;
4643 }
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004644
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 return width;
4646}
4647#endif /* FEAT_STL_OPT */
4648
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004649#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4650 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004652 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4653 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 */
4655 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004656get_rel_pos(
4657 win_T *wp,
4658 char_u *buf,
4659 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660{
4661 long above; /* number of lines above window */
4662 long below; /* number of lines below window */
4663
Bram Moolenaar0027c212015-01-07 13:31:52 +01004664 if (buflen < 3) /* need at least 3 chars for writing */
4665 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 above = wp->w_topline - 1;
4667#ifdef FEAT_DIFF
4668 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02004669 if (wp->w_topline == 1 && wp->w_topfill >= 1)
4670 above = 0; /* All buffer lines are displayed and there is an
4671 * indication of filler lines, that can be considered
4672 * seeing all lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673#endif
4674 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4675 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004676 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4677 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004679 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004681 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 ? (int)(above / ((above + below) / 100L))
4683 : (int)(above * 100L / (above + below)));
4684}
4685#endif
4686
4687/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004688 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 * Return TRUE if it was appended.
4690 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004691 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004692append_arg_number(
4693 win_T *wp,
4694 char_u *buf,
4695 int buflen,
4696 int add_file) /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697{
4698 char_u *p;
4699
4700 if (ARGCOUNT <= 1) /* nothing to do */
4701 return FALSE;
4702
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004703 p = buf + STRLEN(buf); /* go to the end of the buffer */
4704 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 return FALSE;
4706 *p++ = ' ';
4707 *p++ = '(';
4708 if (add_file)
4709 {
4710 STRCPY(p, "file ");
4711 p += 5;
4712 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004713 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4714 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4716 return TRUE;
4717}
4718
4719/*
4720 * If fname is not a full path, make it a full path.
4721 * Returns pointer to allocated memory (NULL for failure).
4722 */
4723 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004724fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725{
4726 /*
4727 * Force expanding the path always for Unix, because symbolic links may
4728 * mess up the full path name, even though it starts with a '/'.
4729 * Also expand when there is ".." in the file name, try to remove it,
4730 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004731 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 * For MS-Windows also expand names like "longna~1" to "longname".
4733 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004734#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 return FullName_save(fname, TRUE);
4736#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004737 if (!vim_isAbsName(fname)
4738 || strstr((char *)fname, "..") != NULL
4739 || strstr((char *)fname, "//") != NULL
4740# ifdef BACKSLASH_IN_FILENAME
4741 || strstr((char *)fname, "\\\\") != NULL
4742# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004743# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004745# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 )
4747 return FullName_save(fname, FALSE);
4748
4749 fname = vim_strsave(fname);
4750
Bram Moolenaar9b942202007-10-03 12:31:33 +00004751# ifdef USE_FNAME_CASE
4752# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004754# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 {
4756 if (fname != NULL)
4757 fname_case(fname, 0); /* set correct case for file name */
4758 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004759# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760
4761 return fname;
4762#endif
4763}
4764
4765/*
4766 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4767 * "ffname" becomes a pointer to allocated memory (or NULL).
4768 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004770fname_expand(
4771 buf_T *buf UNUSED,
4772 char_u **ffname,
4773 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774{
4775 if (*ffname == NULL) /* if no file name given, nothing to do */
4776 return;
4777 if (*sfname == NULL) /* if no short file name given, use ffname */
4778 *sfname = *ffname;
4779 *ffname = fix_fname(*ffname); /* expand to full path */
4780
4781#ifdef FEAT_SHORTCUT
4782 if (!buf->b_p_bin)
4783 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004784 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785
4786 /* If the file name is a shortcut file, use the file it links to. */
4787 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004788 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 {
4790 vim_free(*ffname);
4791 *ffname = rfname;
4792 *sfname = rfname;
4793 }
4794 }
4795#endif
4796}
4797
4798/*
4799 * Get the file name for an argument list entry.
4800 */
4801 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004802alist_name(aentry_T *aep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803{
4804 buf_T *bp;
4805
4806 /* Use the name from the associated buffer if it exists. */
4807 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004808 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 return aep->ae_fname;
4810 return bp->b_fname;
4811}
4812
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813/*
4814 * do_arg_all(): Open up to 'count' windows, one for each argument.
4815 */
4816 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004817do_arg_all(
4818 int count,
4819 int forceit, /* hide buffers in current windows */
4820 int keep_tabs) /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821{
4822 int i;
4823 win_T *wp, *wpnext;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004824 char_u *opened; /* Array of weight for which args are open:
4825 * 0: not opened
4826 * 1: opened in other tab
4827 * 2: opened in curtab
4828 * 3: opened in curtab and curwin
4829 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004830 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 int use_firstwin = FALSE; /* use first window for arglist */
4832 int split_ret = OK;
4833 int p_ea_save;
4834 alist_T *alist; /* argument list to be used */
4835 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004836 tabpage_T *tpnext;
4837 int had_tab = cmdmod.tab;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004838 win_T *old_curwin, *last_curwin;
4839 tabpage_T *old_curtab, *last_curtab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004840 win_T *new_curwin = NULL;
4841 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842
4843 if (ARGCOUNT <= 0)
4844 {
4845 /* Don't give an error message. We don't want it when the ":all"
4846 * command is in the .vimrc. */
4847 return;
4848 }
4849 setpcmark();
4850
4851 opened_len = ARGCOUNT;
4852 opened = alloc_clear((unsigned)opened_len);
4853 if (opened == NULL)
4854 return;
4855
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004856 /* Autocommands may do anything to the argument list. Make sure it's not
4857 * freed while we are working here by "locking" it. We still have to
4858 * watch out for its size to be changed. */
4859 alist = curwin->w_alist;
4860 ++alist->al_refcount;
4861
4862 old_curwin = curwin;
4863 old_curtab = curtab;
4864
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004865# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004867# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868
4869 /*
4870 * Try closing all windows that are not in the argument list.
4871 * Also close windows that are not full width;
4872 * When 'hidden' or "forceit" set the buffer becomes hidden.
4873 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004874 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004876 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004877 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004878 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004880 tpnext = curtab->tp_next;
4881 for (wp = firstwin; wp != NULL; wp = wpnext)
4882 {
4883 wpnext = wp->w_next;
4884 buf = wp->w_buffer;
4885 if (buf->b_ffname == NULL
Bram Moolenaar5a030a52016-12-01 17:48:29 +01004886 || (!keep_tabs && (buf->b_nwindows > 1
4887 || wp->w_width != Columns)))
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004888 i = opened_len;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004889 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004891 /* check if the buffer in this window is in the arglist */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004892 for (i = 0; i < opened_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004894 if (i < alist->al_ga.ga_len
4895 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
4896 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
4897 buf->b_ffname, TRUE) & FPC_SAME))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004899 int weight = 1;
4900
4901 if (old_curtab == curtab)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004902 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004903 ++weight;
4904 if (old_curwin == wp)
4905 ++weight;
4906 }
4907
4908 if (weight > (int)opened[i])
4909 {
4910 opened[i] = (char_u)weight;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004911 if (i == 0)
4912 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004913 if (new_curwin != NULL)
4914 new_curwin->w_arg_idx = opened_len;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004915 new_curwin = wp;
4916 new_curtab = curtab;
4917 }
4918 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004919 else if (keep_tabs)
4920 i = opened_len;
4921
4922 if (wp->w_alist != alist)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004923 {
4924 /* Use the current argument list for all windows
4925 * containing a file from it. */
4926 alist_unlink(wp->w_alist);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004927 wp->w_alist = alist;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004928 ++wp->w_alist->al_refcount;
4929 }
4930 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 }
4933 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004934 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004936 if (i == opened_len && !keep_tabs)/* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02004938 if (buf_hide(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004939 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004941 /* If the buffer was changed, and we would like to hide it,
4942 * try autowriting. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02004943 if (!buf_hide(buf) && buf->b_nwindows <= 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004944 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02004946 bufref_T bufref;
4947
4948 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004949
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004950 (void)autowrite(buf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004951
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004952 /* check if autocommands removed the window */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02004953 if (!win_valid(wp) || !bufref_valid(&bufref))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004954 {
4955 wpnext = firstwin; /* start all over... */
4956 continue;
4957 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004958 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004959 /* don't close last window */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01004960 if (ONE_WINDOW
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004961 && (first_tabpage->tp_next == NULL || !had_tab))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004962 use_firstwin = TRUE;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004963 else
4964 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02004965 win_close(wp, !buf_hide(buf) && !bufIsChanged(buf));
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004966
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004967 /* check if autocommands removed the next window */
4968 if (!win_valid(wpnext))
4969 wpnext = firstwin; /* start all over... */
Bram Moolenaar4033c552017-09-16 20:54:51 +02004970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 }
4973 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004974
4975 /* Without the ":tab" modifier only do the current tab page. */
4976 if (had_tab == 0 || tpnext == NULL)
4977 break;
4978
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004979 /* check if autocommands removed the next tab page */
4980 if (!valid_tabpage(tpnext))
4981 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004982
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004983 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 }
4985
4986 /*
4987 * Open a window for files in the argument list that don't have one.
4988 * ARGCOUNT may change while doing this, because of autocommands.
4989 */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004990 if (count > opened_len || count <= 0)
4991 count = opened_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4994 ++autocmd_no_enter;
4995 ++autocmd_no_leave;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004996 last_curwin = curwin;
4997 last_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004999 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
5000 * leaving an empty tab page when executed locally. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005001 if (keep_tabs && BUFEMPTY() && curbuf->b_nwindows == 1
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005002 && curbuf->b_ffname == NULL && !curbuf->b_changed)
5003 use_firstwin = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005004
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005005 for (i = 0; i < count && i < opened_len && !got_int; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 {
5007 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
5008 arg_had_last = TRUE;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005009 if (opened[i] > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 {
5011 /* Move the already present window to below the current window */
5012 if (curwin->w_arg_idx != i)
5013 {
5014 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
5015 {
5016 if (wpnext->w_arg_idx == i)
5017 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005018 if (keep_tabs)
5019 {
5020 new_curwin = wpnext;
5021 new_curtab = curtab;
5022 }
5023 else
5024 win_move_after(wpnext, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 break;
5026 }
5027 }
5028 }
5029 }
5030 else if (split_ret == OK)
5031 {
5032 if (!use_firstwin) /* split current window */
5033 {
5034 p_ea_save = p_ea;
5035 p_ea = TRUE; /* use space from all windows */
5036 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5037 p_ea = p_ea_save;
5038 if (split_ret == FAIL)
5039 continue;
5040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 else /* first window: do autocmd for leaving this buffer */
5042 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043
5044 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005045 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 */
5047 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005048 if (i == 0)
5049 {
5050 new_curwin = curwin;
5051 new_curtab = curtab;
5052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
5054 ECMD_ONE,
Bram Moolenaareb44a682017-08-03 22:44:55 +02005055 ((buf_hide(curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00005057 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 if (use_firstwin)
5059 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 use_firstwin = FALSE;
5061 }
5062 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005063
5064 /* When ":tab" was used open a new tab for a new window repeatedly. */
5065 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5066 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 }
5068
5069 /* Remove the "lock" on the argument list. */
5070 alist_unlink(alist);
5071
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 --autocmd_no_enter;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005073
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005074 /* restore last referenced tabpage's curwin */
5075 if (last_curtab != new_curtab)
5076 {
5077 if (valid_tabpage(last_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005078 goto_tabpage_tp(last_curtab, TRUE, TRUE);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005079 if (win_valid(last_curwin))
5080 win_enter(last_curwin, FALSE);
5081 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005082 /* to window with first arg */
5083 if (valid_tabpage(new_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005084 goto_tabpage_tp(new_curtab, TRUE, TRUE);
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005085 if (win_valid(new_curwin))
5086 win_enter(new_curwin, FALSE);
5087
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 vim_free(opened);
5090}
5091
5092# if defined(FEAT_LISTCMDS) || defined(PROTO)
5093/*
5094 * Open a window for a number of buffers.
5095 */
5096 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005097ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098{
5099 buf_T *buf;
5100 win_T *wp, *wpnext;
5101 int split_ret = OK;
5102 int p_ea_save;
5103 int open_wins = 0;
5104 int r;
5105 int count; /* Maximum number of windows to open. */
5106 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005107 int had_tab = cmdmod.tab;
5108 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109
5110 if (eap->addr_count == 0) /* make as many windows as possible */
5111 count = 9999;
5112 else
5113 count = eap->line2; /* make as many windows as specified */
5114 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5115 all = FALSE;
5116 else
5117 all = TRUE;
5118
5119 setpcmark();
5120
5121#ifdef FEAT_GUI
5122 need_mouse_correct = TRUE;
5123#endif
5124
5125 /*
5126 * Close superfluous windows (two windows for the same buffer).
5127 * Also close windows that are not full-width.
5128 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005129 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005130 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005131 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005133 tpnext = curtab->tp_next;
5134 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005136 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005137 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005138 || ((cmdmod.split & WSP_VERT)
5139 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005140 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005141 : wp->w_width != Columns)
Bram Moolenaar4033c552017-09-16 20:54:51 +02005142 || (had_tab > 0 && wp != firstwin)) && !ONE_WINDOW
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005143 && !(wp->w_closing || wp->w_buffer->b_locked > 0))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005144 {
5145 win_close(wp, FALSE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005146 wpnext = firstwin; /* just in case an autocommand does
5147 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005148 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005149 open_wins = 0;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005150 }
5151 else
5152 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005154
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005155 /* Without the ":tab" modifier only do the current tab page. */
5156 if (had_tab == 0 || tpnext == NULL)
5157 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005158 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 }
5160
5161 /*
5162 * Go through the buffer list. When a buffer doesn't have a window yet,
5163 * open one. Otherwise move the window to the right position.
5164 * Watch out for autocommands that delete buffers or windows!
5165 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 /* Don't execute Win/Buf Enter/Leave autocommands here. */
5167 ++autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 win_enter(lastwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5171 {
5172 /* Check if this buffer needs a window */
5173 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5174 continue;
5175
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005176 if (had_tab != 0)
5177 {
5178 /* With the ":tab" modifier don't move the window. */
5179 if (buf->b_nwindows > 0)
5180 wp = lastwin; /* buffer has a window, skip it */
5181 else
5182 wp = NULL;
5183 }
5184 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005185 {
5186 /* Check if this buffer already has a window */
Bram Moolenaar29323592016-07-24 22:04:11 +02005187 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005188 if (wp->w_buffer == buf)
5189 break;
5190 /* If the buffer already has a window, move it */
5191 if (wp != NULL)
5192 win_move_after(wp, curwin);
5193 }
5194
5195 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005197 bufref_T bufref;
5198
5199 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005200
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 /* Split the window and put the buffer in it */
5202 p_ea_save = p_ea;
5203 p_ea = TRUE; /* use space from all windows */
5204 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5205 ++open_wins;
5206 p_ea = p_ea_save;
5207 if (split_ret == FAIL)
5208 continue;
5209
5210 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005211#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 swap_exists_action = SEA_DIALOG;
5213#endif
5214 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005215 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005217 /* autocommands deleted the buffer!!! */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005218#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 swap_exists_action = SEA_NONE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 break;
5222 }
Bram Moolenaare64ac772005-12-07 20:54:59 +00005223#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 if (swap_exists_action == SEA_QUIT)
5225 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005226# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005227 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005228
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005229 /* Reset the error/interrupt/exception state here so that
5230 * aborting() returns FALSE when closing a window. */
5231 enter_cleanup(&cs);
5232# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005233
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005234 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 win_close(curwin, TRUE);
5236 --open_wins;
5237 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005238 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005239
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005240# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005241 /* Restore the error/interrupt/exception state if not
5242 * discarded by a new aborting error, interrupt, or uncaught
5243 * exception. */
5244 leave_cleanup(&cs);
5245# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 }
5247 else
5248 handle_swap_exists(NULL);
5249#endif
5250 }
5251
5252 ui_breakcheck();
5253 if (got_int)
5254 {
5255 (void)vgetc(); /* only break the file loading, not the rest */
5256 break;
5257 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005258#ifdef FEAT_EVAL
5259 /* Autocommands deleted the buffer or aborted script processing!!! */
5260 if (aborting())
5261 break;
5262#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005263 /* When ":tab" was used open a new tab for a new window repeatedly. */
5264 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5265 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 --autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 win_enter(firstwin, FALSE); /* back to first window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270
5271 /*
5272 * Close superfluous windows.
5273 */
5274 for (wp = lastwin; open_wins > count; )
5275 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005276 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 || autowrite(wp->w_buffer, FALSE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 if (!win_valid(wp))
5279 {
5280 /* BufWrite Autocommands made the window invalid, start over */
5281 wp = lastwin;
5282 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005283 else if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005285 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 --open_wins;
5287 wp = lastwin;
5288 }
5289 else
5290 {
5291 wp = wp->w_prev;
5292 if (wp == NULL)
5293 break;
5294 }
5295 }
5296}
5297# endif /* FEAT_LISTCMDS */
5298
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005300static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005301
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302/*
5303 * do_modelines() - process mode lines for the current file
5304 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005305 * "flags" can be:
5306 * OPT_WINONLY only set options local to window
5307 * OPT_NOWIN don't set options local to window
5308 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 * Returns immediately if the "ml" option isn't set.
5310 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005312do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005314 linenr_T lnum;
5315 int nmlines;
5316 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317
5318 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5319 return;
5320
5321 /* Disallow recursive entry here. Can happen when executing a modeline
5322 * triggers an autocommand, which reloads modelines with a ":do". */
5323 if (entered)
5324 return;
5325
5326 ++entered;
5327 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5328 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005329 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 nmlines = 0;
5331
5332 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5333 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005334 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 nmlines = 0;
5336 --entered;
5337}
5338
5339#include "version.h" /* for version number */
5340
5341/*
5342 * chk_modeline() - check a single line for a mode string
5343 * Return FAIL if an error encountered.
5344 */
5345 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005346chk_modeline(
5347 linenr_T lnum,
5348 int flags) /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349{
5350 char_u *s;
5351 char_u *e;
5352 char_u *linecopy; /* local copy of any modeline found */
5353 int prev;
5354 int vers;
5355 int end;
5356 int retval = OK;
5357 char_u *save_sourcing_name;
5358 linenr_T save_sourcing_lnum;
5359#ifdef FEAT_EVAL
5360 scid_T save_SID;
5361#endif
5362
5363 prev = -1;
5364 for (s = ml_get(lnum); *s != NUL; ++s)
5365 {
5366 if (prev == -1 || vim_isspace(prev))
5367 {
5368 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5369 || STRNCMP(s, "vi:", (size_t)3) == 0)
5370 break;
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005371 /* Accept both "vim" and "Vim". */
5372 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 {
5374 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5375 e = s + 4;
5376 else
5377 e = s + 3;
5378 vers = getdigits(&e);
5379 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005380 && (s[0] != 'V'
5381 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 && (s[3] == ':'
5383 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5384 || (VIM_VERSION_100 < vers && s[3] == '<')
5385 || (VIM_VERSION_100 > vers && s[3] == '>')
5386 || (VIM_VERSION_100 == vers && s[3] == '=')))
5387 break;
5388 }
5389 }
5390 prev = *s;
5391 }
5392
5393 if (*s)
5394 {
5395 do /* skip over "ex:", "vi:" or "vim:" */
5396 ++s;
5397 while (s[-1] != ':');
5398
5399 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5400 if (linecopy == NULL)
5401 return FAIL;
5402
5403 save_sourcing_lnum = sourcing_lnum;
5404 save_sourcing_name = sourcing_name;
5405 sourcing_lnum = lnum; /* prepare for emsg() */
5406 sourcing_name = (char_u *)"modelines";
5407
5408 end = FALSE;
5409 while (end == FALSE)
5410 {
5411 s = skipwhite(s);
5412 if (*s == NUL)
5413 break;
5414
5415 /*
5416 * Find end of set command: ':' or end of line.
5417 * Skip over "\:", replacing it with ":".
5418 */
5419 for (e = s; *e != ':' && *e != NUL; ++e)
5420 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005421 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 if (*e == NUL)
5423 end = TRUE;
5424
5425 /*
5426 * If there is a "set" command, require a terminating ':' and
5427 * ignore the stuff after the ':'.
5428 * "vi:set opt opt opt: foo" -- foo not interpreted
5429 * "vi:opt opt opt: foo" -- foo interpreted
5430 * Accept "se" for compatibility with Elvis.
5431 */
5432 if (STRNCMP(s, "set ", (size_t)4) == 0
5433 || STRNCMP(s, "se ", (size_t)3) == 0)
5434 {
5435 if (*e != ':') /* no terminating ':'? */
5436 break;
5437 end = TRUE;
5438 s = vim_strchr(s, ' ') + 1;
5439 }
5440 *e = NUL; /* truncate the set command */
5441
5442 if (*s != NUL) /* skip over an empty "::" */
5443 {
5444#ifdef FEAT_EVAL
5445 save_SID = current_SID;
5446 current_SID = SID_MODELINE;
5447#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005448 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449#ifdef FEAT_EVAL
5450 current_SID = save_SID;
5451#endif
5452 if (retval == FAIL) /* stop if error found */
5453 break;
5454 }
5455 s = e + 1; /* advance to next part */
5456 }
5457
5458 sourcing_lnum = save_sourcing_lnum;
5459 sourcing_name = save_sourcing_name;
5460
5461 vim_free(linecopy);
5462 }
5463 return retval;
5464}
5465
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005466#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005468read_viminfo_bufferlist(
5469 vir_T *virp,
5470 int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471{
5472 char_u *tab;
5473 linenr_T lnum;
5474 colnr_T col;
5475 buf_T *buf;
5476 char_u *sfname;
5477 char_u *xline;
5478
5479 /* Handle long line and escaped characters. */
5480 xline = viminfo_readstring(virp, 1, FALSE);
5481
5482 /* don't read in if there are files on the command-line or if writing: */
5483 if (xline != NULL && !writing && ARGCOUNT == 0
5484 && find_viminfo_parameter('%') != NULL)
5485 {
5486 /* Format is: <fname> Tab <lnum> Tab <col>.
5487 * Watch out for a Tab in the file name, work from the end. */
5488 lnum = 0;
5489 col = 0;
5490 tab = vim_strrchr(xline, '\t');
5491 if (tab != NULL)
5492 {
5493 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005494 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 tab = vim_strrchr(xline, '\t');
5496 if (tab != NULL)
5497 {
5498 *tab++ = '\0';
5499 lnum = atol((char *)tab);
5500 }
5501 }
5502
5503 /* Expand "~/" in the file name at "line + 1" to a full path.
5504 * Then try shortening it by comparing with the current directory */
5505 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005506 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507
5508 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5509 if (buf != NULL) /* just in case... */
5510 {
5511 buf->b_last_cursor.lnum = lnum;
5512 buf->b_last_cursor.col = col;
5513 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5514 }
5515 }
5516 vim_free(xline);
5517
5518 return viminfo_readline(virp);
5519}
5520
5521 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005522write_viminfo_bufferlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523{
5524 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005526 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005528 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529
5530 if (find_viminfo_parameter('%') == NULL)
5531 return;
5532
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005533 /* Without a number -1 is returned: do all buffers. */
5534 max_buffers = get_viminfo_parameter('%');
5535
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005537#define LINE_BUF_LEN (MAXPATHL + 40)
5538 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 if (line == NULL)
5540 return;
5541
Bram Moolenaarf740b292006-02-16 22:11:02 +00005542 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 set_last_cursor(win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005545 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar29323592016-07-24 22:04:11 +02005546 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 {
5548 if (buf->b_fname == NULL
5549 || !buf->b_p_bl
5550#ifdef FEAT_QUICKFIX
5551 || bt_quickfix(buf)
5552#endif
Bram Moolenaare6278052017-08-13 18:11:17 +02005553#ifdef FEAT_TERMINAL
5554 || bt_terminal(buf)
5555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 || removable(buf->b_ffname))
5557 continue;
5558
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005559 if (max_buffers-- == 0)
5560 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 putc('%', fp);
5562 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005563 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 (long)buf->b_last_cursor.lnum,
5565 buf->b_last_cursor.col);
5566 viminfo_writestring(fp, line);
5567 }
5568 vim_free(line);
5569}
5570#endif
5571
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005572/*
5573 * Return TRUE if "buf" is the quickfix buffer.
5574 */
5575 int
5576bt_quickfix(buf_T *buf)
5577{
5578 return buf != NULL && buf->b_p_bt[0] == 'q';
5579}
5580
5581/*
5582 * Return TRUE if "buf" is a terminal buffer.
5583 */
5584 int
5585bt_terminal(buf_T *buf)
5586{
5587 return buf != NULL && buf->b_p_bt[0] == 't';
5588}
5589
5590/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005591 * Return TRUE if "buf" is a help buffer.
5592 */
5593 int
5594bt_help(buf_T *buf)
5595{
5596 return buf != NULL && buf->b_help;
5597}
5598
5599/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005600 * Return TRUE if "buf" is a "nofile", "acwrite" or "terminal" buffer.
5601 * This means the buffer name is not a file name.
5602 */
5603 int
5604bt_nofile(buf_T *buf)
5605{
5606 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5607 || buf->b_p_bt[0] == 'a'
5608 || buf->b_p_bt[0] == 't');
5609}
5610
5611/*
5612 * Return TRUE if "buf" is a "nowrite", "nofile" or "terminal" buffer.
5613 */
5614 int
5615bt_dontwrite(buf_T *buf)
5616{
5617 return buf != NULL && (buf->b_p_bt[0] == 'n' || buf->b_p_bt[0] == 't');
5618}
5619
5620 int
5621bt_dontwrite_msg(buf_T *buf)
5622{
5623 if (bt_dontwrite(buf))
5624 {
5625 EMSG(_("E382: Cannot write, 'buftype' option is set"));
5626 return TRUE;
5627 }
5628 return FALSE;
5629}
5630
5631/*
5632 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
5633 * and 'bufhidden'.
5634 */
5635 int
5636buf_hide(buf_T *buf)
5637{
5638 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
5639 switch (buf->b_p_bh[0])
5640 {
5641 case 'u': /* "unload" */
5642 case 'w': /* "wipe" */
5643 case 'd': return FALSE; /* "delete" */
5644 case 'h': return TRUE; /* "hide" */
5645 }
5646 return (p_hid || cmdmod.hide);
5647}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648
5649/*
5650 * Return special buffer name.
5651 * Returns NULL when the buffer has a normal file name.
5652 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005653 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005654buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655{
Bram Moolenaar4033c552017-09-16 20:54:51 +02005656#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005658 {
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005659 win_T *win;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005660 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005661
5662 /*
5663 * For location list window, w_llist_ref points to the location list.
5664 * For quickfix window, w_llist_ref is NULL.
5665 */
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005666 if (find_win_for_buf(buf, &win, &tp) == OK && win->w_llist_ref != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005667 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005668 else
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005669 return (char_u *)_(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02005672
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 /* There is no _file_ when 'buftype' is "nofile", b_sfname
Bram Moolenaar21554412017-07-24 21:44:43 +02005674 * contains the name as specified by the user. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 if (bt_nofile(buf))
5676 {
Bram Moolenaar21554412017-07-24 21:44:43 +02005677#ifdef FEAT_TERMINAL
5678 if (buf->b_term != NULL)
5679 return term_get_status_text(buf->b_term);
5680#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02005681 if (buf->b_fname != NULL)
5682 return buf->b_fname;
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005683 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 }
Bram Moolenaar21554412017-07-24 21:44:43 +02005685
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005687 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 return NULL;
5689}
5690
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02005691#if defined(FEAT_JOB_CHANNEL) \
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005692 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5693 || defined(PROTO)
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02005694# define SWITCH_TO_WIN
5695
5696/*
5697 * Find a window that contains "buf" and switch to it.
5698 * If there is no such window, use the current window and change "curbuf".
5699 * Caller must initialize save_curbuf to NULL.
5700 * restore_win_for_buf() MUST be called later!
5701 */
5702 void
5703switch_to_win_for_buf(
5704 buf_T *buf,
5705 win_T **save_curwinp,
5706 tabpage_T **save_curtabp,
5707 bufref_T *save_curbuf)
5708{
5709 win_T *wp;
5710 tabpage_T *tp;
5711
5712 if (find_win_for_buf(buf, &wp, &tp) == FAIL)
5713 switch_buffer(save_curbuf, buf);
5714 else if (switch_win(save_curwinp, save_curtabp, wp, tp, TRUE) == FAIL)
5715 {
5716 restore_win(*save_curwinp, *save_curtabp, TRUE);
5717 switch_buffer(save_curbuf, buf);
5718 }
5719}
5720
5721 void
5722restore_win_for_buf(
5723 win_T *save_curwin,
5724 tabpage_T *save_curtab,
5725 bufref_T *save_curbuf)
5726{
5727 if (save_curbuf->br_buf == NULL)
5728 restore_win(save_curwin, save_curtab, TRUE);
5729 else
5730 restore_buffer(save_curbuf);
5731}
5732#endif
5733
Bram Moolenaar4033c552017-09-16 20:54:51 +02005734#if defined(FEAT_QUICKFIX) || defined(SWITCH_TO_WIN) || defined(PROTO)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005735/*
5736 * Find a window for buffer "buf".
5737 * If found OK is returned and "wp" and "tp" are set to the window and tabpage.
5738 * If not found FAIL is returned.
5739 */
5740 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005741find_win_for_buf(
5742 buf_T *buf,
5743 win_T **wp,
5744 tabpage_T **tp)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005745{
5746 FOR_ALL_TAB_WINDOWS(*tp, *wp)
5747 if ((*wp)->w_buffer == buf)
5748 goto win_found;
5749 return FAIL;
5750win_found:
5751 return OK;
5752}
5753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754
5755#if defined(FEAT_SIGNS) || defined(PROTO)
5756/*
5757 * Insert the sign into the signlist.
5758 */
5759 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005760insert_sign(
5761 buf_T *buf, /* buffer to store sign in */
5762 signlist_T *prev, /* previous sign entry */
5763 signlist_T *next, /* next sign entry */
5764 int id, /* sign ID */
5765 linenr_T lnum, /* line number which gets the mark */
5766 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767{
5768 signlist_T *newsign;
5769
5770 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5771 if (newsign != NULL)
5772 {
5773 newsign->id = id;
5774 newsign->lnum = lnum;
5775 newsign->typenr = typenr;
5776 newsign->next = next;
5777#ifdef FEAT_NETBEANS_INTG
5778 newsign->prev = prev;
5779 if (next != NULL)
5780 next->prev = newsign;
5781#endif
5782
5783 if (prev == NULL)
5784 {
5785 /* When adding first sign need to redraw the windows to create the
5786 * column for signs. */
5787 if (buf->b_signlist == NULL)
5788 {
5789 redraw_buf_later(buf, NOT_VALID);
5790 changed_cline_bef_curs();
5791 }
5792
5793 /* first sign in signlist */
5794 buf->b_signlist = newsign;
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01005795#ifdef FEAT_NETBEANS_INTG
5796 if (netbeans_active())
5797 buf->b_has_sign_column = TRUE;
5798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 }
5800 else
5801 prev->next = newsign;
5802 }
5803}
5804
5805/*
5806 * Add the sign into the signlist. Find the right spot to do it though.
5807 */
5808 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005809buf_addsign(
5810 buf_T *buf, /* buffer to store sign in */
5811 int id, /* sign ID */
5812 linenr_T lnum, /* line number which gets the mark */
5813 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814{
5815 signlist_T *sign; /* a sign in the signlist */
5816 signlist_T *prev; /* the previous sign */
5817
5818 prev = NULL;
5819 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5820 {
5821 if (lnum == sign->lnum && id == sign->id)
5822 {
5823 sign->typenr = typenr;
5824 return;
5825 }
5826 else if (
5827#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5828 id < 0 &&
5829#endif
5830 lnum < sign->lnum)
5831 {
5832#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5833 /* XXX - GRP: Is this because of sign slide problem? Or is it
5834 * really needed? Or is it because we allow multiple signs per
5835 * line? If so, should I add that feature to FEAT_SIGNS?
5836 */
5837 while (prev != NULL && prev->lnum == lnum)
5838 prev = prev->prev;
5839 if (prev == NULL)
5840 sign = buf->b_signlist;
5841 else
5842 sign = prev->next;
5843#endif
5844 insert_sign(buf, prev, sign, id, lnum, typenr);
5845 return;
5846 }
5847 prev = sign;
5848 }
5849#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5850 /* XXX - GRP: See previous comment */
5851 while (prev != NULL && prev->lnum == lnum)
5852 prev = prev->prev;
5853 if (prev == NULL)
5854 sign = buf->b_signlist;
5855 else
5856 sign = prev->next;
5857#endif
5858 insert_sign(buf, prev, sign, id, lnum, typenr);
5859
5860 return;
5861}
5862
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005863/*
5864 * For an existing, placed sign "markId" change the type to "typenr".
5865 * Returns the line number of the sign, or zero if the sign is not found.
5866 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005867 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005868buf_change_sign_type(
5869 buf_T *buf, /* buffer to store sign in */
5870 int markId, /* sign ID */
5871 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872{
5873 signlist_T *sign; /* a sign in the signlist */
5874
5875 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5876 {
5877 if (sign->id == markId)
5878 {
5879 sign->typenr = typenr;
5880 return sign->lnum;
5881 }
5882 }
5883
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005884 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885}
5886
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005887 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005888buf_getsigntype(
5889 buf_T *buf,
5890 linenr_T lnum,
5891 int type) /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892{
5893 signlist_T *sign; /* a sign in a b_signlist */
5894
5895 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5896 if (sign->lnum == lnum
5897 && (type == SIGN_ANY
5898# ifdef FEAT_SIGN_ICONS
5899 || (type == SIGN_ICON
5900 && sign_get_image(sign->typenr) != NULL)
5901# endif
5902 || (type == SIGN_TEXT
5903 && sign_get_text(sign->typenr) != NULL)
5904 || (type == SIGN_LINEHL
5905 && sign_get_attr(sign->typenr, TRUE) != 0)))
5906 return sign->typenr;
5907 return 0;
5908}
5909
5910
5911 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005912buf_delsign(
5913 buf_T *buf, /* buffer sign is stored in */
5914 int id) /* sign id */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915{
5916 signlist_T **lastp; /* pointer to pointer to current sign */
5917 signlist_T *sign; /* a sign in a b_signlist */
5918 signlist_T *next; /* the next sign in a b_signlist */
5919 linenr_T lnum; /* line number whose sign was deleted */
5920
5921 lastp = &buf->b_signlist;
5922 lnum = 0;
5923 for (sign = buf->b_signlist; sign != NULL; sign = next)
5924 {
5925 next = sign->next;
5926 if (sign->id == id)
5927 {
5928 *lastp = next;
5929#ifdef FEAT_NETBEANS_INTG
5930 if (next != NULL)
5931 next->prev = sign->prev;
5932#endif
5933 lnum = sign->lnum;
5934 vim_free(sign);
5935 break;
5936 }
5937 else
5938 lastp = &sign->next;
5939 }
5940
5941 /* When deleted the last sign need to redraw the windows to remove the
5942 * sign column. */
5943 if (buf->b_signlist == NULL)
5944 {
5945 redraw_buf_later(buf, NOT_VALID);
5946 changed_cline_bef_curs();
5947 }
5948
5949 return lnum;
5950}
5951
5952
5953/*
5954 * Find the line number of the sign with the requested id. If the sign does
5955 * not exist, return 0 as the line number. This will still let the correct file
5956 * get loaded.
5957 */
5958 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005959buf_findsign(
5960 buf_T *buf, /* buffer to store sign in */
5961 int id) /* sign ID */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962{
5963 signlist_T *sign; /* a sign in the signlist */
5964
5965 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5966 if (sign->id == id)
5967 return sign->lnum;
5968
5969 return 0;
5970}
5971
5972 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005973buf_findsign_id(
5974 buf_T *buf, /* buffer whose sign we are searching for */
5975 linenr_T lnum) /* line number of sign */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976{
5977 signlist_T *sign; /* a sign in the signlist */
5978
5979 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5980 if (sign->lnum == lnum)
5981 return sign->id;
5982
5983 return 0;
5984}
5985
5986
5987# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5988/* see if a given type of sign exists on a specific line */
5989 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005990buf_findsigntype_id(
5991 buf_T *buf, /* buffer whose sign we are searching for */
5992 linenr_T lnum, /* line number of sign */
5993 int typenr) /* sign type number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994{
5995 signlist_T *sign; /* a sign in the signlist */
5996
5997 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5998 if (sign->lnum == lnum && sign->typenr == typenr)
5999 return sign->id;
6000
6001 return 0;
6002}
6003
6004
6005# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
6006/* return the number of icons on the given line */
6007 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006008buf_signcount(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009{
6010 signlist_T *sign; /* a sign in the signlist */
6011 int count = 0;
6012
6013 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
6014 if (sign->lnum == lnum)
6015 if (sign_get_image(sign->typenr) != NULL)
6016 count++;
6017
6018 return count;
6019}
6020# endif /* FEAT_SIGN_ICONS */
6021# endif /* FEAT_NETBEANS_INTG */
6022
6023
6024/*
6025 * Delete signs in buffer "buf".
6026 */
Bram Moolenaarf65e5662012-07-10 15:18:22 +02006027 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006028buf_delete_signs(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029{
6030 signlist_T *next;
6031
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02006032 /* When deleting the last sign need to redraw the windows to remove the
Bram Moolenaar4e036c92014-07-16 16:30:28 +02006033 * sign column. Not when curwin is NULL (this means we're exiting). */
6034 if (buf->b_signlist != NULL && curwin != NULL)
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02006035 {
6036 redraw_buf_later(buf, NOT_VALID);
6037 changed_cline_bef_curs();
6038 }
6039
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 while (buf->b_signlist != NULL)
6041 {
6042 next = buf->b_signlist->next;
6043 vim_free(buf->b_signlist);
6044 buf->b_signlist = next;
6045 }
6046}
6047
6048/*
6049 * Delete all signs in all buffers.
6050 */
6051 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006052buf_delete_all_signs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053{
6054 buf_T *buf; /* buffer we are checking for signs */
6055
Bram Moolenaar29323592016-07-24 22:04:11 +02006056 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 if (buf->b_signlist != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 buf_delete_signs(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059}
6060
6061/*
6062 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
6063 */
6064 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006065sign_list_placed(buf_T *rbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066{
6067 buf_T *buf;
6068 signlist_T *p;
6069 char lbuf[BUFSIZ];
6070
6071 MSG_PUTS_TITLE(_("\n--- Signs ---"));
6072 msg_putchar('\n');
6073 if (rbuf == NULL)
6074 buf = firstbuf;
6075 else
6076 buf = rbuf;
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01006077 while (buf != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 {
6079 if (buf->b_signlist != NULL)
6080 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00006081 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006082 MSG_PUTS_ATTR(lbuf, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 msg_putchar('\n');
6084 }
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01006085 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00006087 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
6089 MSG_PUTS(lbuf);
6090 msg_putchar('\n');
6091 }
6092 if (rbuf != NULL)
6093 break;
6094 buf = buf->b_next;
6095 }
6096}
6097
6098/*
6099 * Adjust a placed sign for inserted/deleted lines.
6100 */
6101 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006102sign_mark_adjust(
6103 linenr_T line1,
6104 linenr_T line2,
6105 long amount,
6106 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107{
6108 signlist_T *sign; /* a sign in a b_signlist */
6109
6110 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
6111 {
6112 if (sign->lnum >= line1 && sign->lnum <= line2)
6113 {
6114 if (amount == MAXLNUM)
6115 sign->lnum = line1;
6116 else
6117 sign->lnum += amount;
6118 }
6119 else if (sign->lnum > line2)
6120 sign->lnum += amount_after;
6121 }
6122}
6123#endif /* FEAT_SIGNS */
6124
6125/*
6126 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
6127 */
6128 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006129set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130{
6131 if (on != curbuf->b_p_bl)
6132 {
6133 curbuf->b_p_bl = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 if (on)
6135 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
6136 else
6137 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 }
6139}
6140
6141/*
6142 * Read the file for "buf" again and check if the contents changed.
6143 * Return TRUE if it changed or this could not be checked.
6144 */
6145 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006146buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147{
6148 buf_T *newbuf;
6149 int differ = TRUE;
6150 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 exarg_T ea;
6153
6154 /* Allocate a buffer without putting it in the buffer list. */
6155 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6156 if (newbuf == NULL)
6157 return TRUE;
6158
6159 /* Force the 'fileencoding' and 'fileformat' to be equal. */
6160 if (prep_exarg(&ea, buf) == FAIL)
6161 {
6162 wipe_buffer(newbuf, FALSE);
6163 return TRUE;
6164 }
6165
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 /* set curwin/curbuf to buf and save a few things */
6167 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168
Bram Moolenaar4770d092006-01-12 23:22:24 +00006169 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 && readfile(buf->b_ffname, buf->b_fname,
6171 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
6172 &ea, READ_NEW | READ_DUMMY) == OK)
6173 {
6174 /* compare the two files line by line */
6175 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
6176 {
6177 differ = FALSE;
6178 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6179 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
6180 {
6181 differ = TRUE;
6182 break;
6183 }
6184 }
6185 }
6186 vim_free(ea.cmd);
6187
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 /* restore curwin/curbuf and a few other things */
6189 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190
6191 if (curbuf != newbuf) /* safety check */
6192 wipe_buffer(newbuf, FALSE);
6193
6194 return differ;
6195}
6196
6197/*
6198 * Wipe out a buffer and decrement the last buffer number if it was used for
6199 * this buffer. Call this to wipe out a temp buffer that does not contain any
6200 * marks.
6201 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006203wipe_buffer(
6204 buf_T *buf,
6205 int aucmd UNUSED) /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206{
6207 if (buf->b_fnum == top_file_num - 1)
6208 --top_file_num;
6209
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006211 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006212
Bram Moolenaar42ec6562012-02-22 14:58:37 +01006213 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006214
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006216 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217}