blob: c7f361af0a5f75eb98929bb3104c5715fcd880c7 [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
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100294 /* Set last_changedtick to avoid triggering a TextChanged autocommand right
295 * after it was added. */
296 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
297#ifdef FEAT_INS_EXPAND
298 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
299#endif
300
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 /* require "!" to overwrite the file, because it wasn't read completely */
302#ifdef FEAT_EVAL
303 if (aborting())
304#else
305 if (got_int)
306#endif
307 curbuf->b_flags |= BF_READERR;
308
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000309#ifdef FEAT_FOLDING
310 /* Need to update automatic folding. Do this before the autocommands,
311 * they may use the fold info. */
312 foldUpdateAll(curwin);
313#endif
314
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 /* need to set w_topline, unless some autocommand already did that. */
316 if (!(curwin->w_valid & VALID_TOPLINE))
317 {
318 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100319#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100321#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100323#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100325#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327#endif
328
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100329 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 /*
332 * The autocommands may have changed the current buffer. Apply the
333 * modelines to the correct buffer, if it still exists and is loaded.
334 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200335 if (bufref_valid(&old_curbuf) && old_curbuf.br_buf->b_ml.ml_mfp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 {
337 aco_save_T aco;
338
339 /* Go to the buffer that was opened. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200340 aucmd_prepbuf(&aco, old_curbuf.br_buf);
Bram Moolenaara3227e22006-03-08 21:32:40 +0000341 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
343
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100344#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100346 &retval);
347#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350
351 /* restore curwin/curbuf and a few other things */
352 aucmd_restbuf(&aco);
353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 }
355
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 return retval;
357}
358
359/*
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200360 * Store "buf" in "bufref" and set the free count.
361 */
362 void
363set_bufref(bufref_T *bufref, buf_T *buf)
364{
365 bufref->br_buf = buf;
Bram Moolenaarfadacf02017-06-19 20:35:32 +0200366 bufref->br_fnum = buf == NULL ? 0 : buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200367 bufref->br_buf_free_count = buf_free_count;
368}
369
370/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200371 * Return TRUE if "bufref->br_buf" points to the same buffer as when
372 * set_bufref() was called and it is a valid buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200373 * Only goes through the buffer list if buf_free_count changed.
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200374 * Also checks if b_fnum is still the same, a :bwipe followed by :new might get
375 * the same allocated memory, but it's a different buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200376 */
377 int
378bufref_valid(bufref_T *bufref)
379{
380 return bufref->br_buf_free_count == buf_free_count
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200381 ? TRUE : buf_valid(bufref->br_buf)
382 && bufref->br_fnum == bufref->br_buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200383}
384
385/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200387 * This can be slow if there are many buffers, prefer using bufref_valid().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 */
389 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100390buf_valid(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391{
392 buf_T *bp;
393
Bram Moolenaar82404332016-07-10 17:00:38 +0200394 /* Assume that we more often have a recent buffer, start with the last
395 * one. */
396 for (bp = lastbuf; bp != NULL; bp = bp->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 if (bp == buf)
398 return TRUE;
399 return FALSE;
400}
401
402/*
Bram Moolenaar480778b2016-07-14 22:09:39 +0200403 * A hash table used to quickly lookup a buffer by its number.
404 */
405static hashtab_T buf_hashtab;
406
407 static void
408buf_hashtab_add(buf_T *buf)
409{
410 sprintf((char *)buf->b_key, "%x", buf->b_fnum);
411 if (hash_add(&buf_hashtab, buf->b_key) == FAIL)
412 EMSG(_("E931: Buffer cannot be registered"));
413}
414
415 static void
416buf_hashtab_remove(buf_T *buf)
417{
418 hashitem_T *hi = hash_find(&buf_hashtab, buf->b_key);
419
420 if (!HASHITEM_EMPTY(hi))
421 hash_remove(&buf_hashtab, hi);
422}
423
424/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 * Close the link to a buffer.
426 * "action" is used when there is no longer a window for the buffer.
427 * It can be:
428 * 0 buffer becomes hidden
429 * DOBUF_UNLOAD buffer is unloaded
430 * DOBUF_DELETE buffer is unloaded and removed from buffer list
431 * DOBUF_WIPE buffer is unloaded and really deleted
432 * When doing all but the first one on the current buffer, the caller should
433 * get a new buffer very soon!
434 *
435 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100436 *
437 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
438 * cause there to be only one window with this buffer. e.g. when ":quit" is
439 * supposed to close the window but autocommands close all other windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 */
441 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100442close_buffer(
443 win_T *win, /* if not NULL, set b_last_cursor */
444 buf_T *buf,
445 int action,
446 int abort_if_last UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100449 int nwindows;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200450 bufref_T bufref;
Bram Moolenaar3a117e12016-10-30 21:57:52 +0100451 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200452 win_T *the_curwin = curwin;
453 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 int unload_buf = (action != 0);
455 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
456 int wipe_buf = (action == DOBUF_WIPE);
457
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200458 /*
459 * Force unloading or deleting when 'bufhidden' says so.
460 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
461 * "hide" (otherwise we could never free or delete a buffer).
462 */
463 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
464 {
465 del_buf = TRUE;
466 unload_buf = TRUE;
467 }
468 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
469 {
470 del_buf = TRUE;
471 unload_buf = TRUE;
472 wipe_buf = TRUE;
473 }
474 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
475 unload_buf = TRUE;
476
Bram Moolenaar94053a52017-08-01 21:44:33 +0200477#ifdef FEAT_TERMINAL
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200478 if (bt_terminal(buf) && (buf->b_nwindows == 1 || del_buf))
Bram Moolenaar94053a52017-08-01 21:44:33 +0200479 {
480 if (term_job_running(buf->b_term))
481 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200482 if (wipe_buf || unload_buf)
483 /* Wiping out or unloading a terminal buffer kills the job. */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200484 free_terminal(buf);
485 else
486 {
487 /* The job keeps running, hide the buffer. */
488 del_buf = FALSE;
489 unload_buf = FALSE;
490 }
491 }
492 else
493 {
494 /* A terminal buffer is wiped out if the job has finished. */
495 del_buf = TRUE;
496 unload_buf = TRUE;
497 wipe_buf = TRUE;
498 }
499 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200502 /* Disallow deleting the buffer when it is locked (already being closed or
503 * halfway a command that relies on it). Unloading is allowed. */
504 if (buf->b_locked > 0 && (del_buf || wipe_buf))
505 {
506 EMSG(_("E937: Attempt to delete a buffer that is in use"));
507 return;
508 }
509
Bram Moolenaar4033c552017-09-16 20:54:51 +0200510 /* check no autocommands closed the window */
511 if (win != NULL && win_valid_any_tab(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 {
513 /* Set b_last_cursor when closing the last window for the buffer.
514 * Remember the last cursor position and window options of the buffer.
515 * This used to be only for the current window, but then options like
516 * 'foldmethod' may be lost with a ":only" command. */
517 if (buf->b_nwindows == 1)
518 set_last_cursor(win);
519 buflist_setfpos(buf, win,
520 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
521 win->w_cursor.col, TRUE);
522 }
523
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200524 set_bufref(&bufref, buf);
525
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 /* When the buffer is no longer in a window, trigger BufWinLeave */
527 if (buf->b_nwindows == 1)
528 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200529 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200530 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
531 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200532 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100533 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200534 /* Autocommands deleted the buffer. */
535aucmd_abort:
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100536 EMSG(_(e_auabort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 return;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100538 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200539 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200540 if (abort_if_last && one_window())
541 /* Autocommands made this the only window. */
542 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543
544 /* When the buffer becomes hidden, but is not unloaded, trigger
545 * BufHidden */
546 if (!unload_buf)
547 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200548 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200549 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
550 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200551 && !bufref_valid(&bufref))
Bram Moolenaar362ce482012-06-06 19:02:45 +0200552 /* Autocommands deleted the buffer. */
553 goto aucmd_abort;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200554 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200555 if (abort_if_last && one_window())
556 /* Autocommands made this the only window. */
557 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100559#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 if (aborting()) /* autocmds may abort script processing */
561 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100562#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200564
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200565 /* If the buffer was in curwin and the window has changed, go back to that
566 * window, if it still exists. This avoids that ":edit x" triggering a
567 * "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
568 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
569 {
570 block_autocmds();
571 goto_tabpage_win(the_curtab, the_curwin);
572 unblock_autocmds();
573 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200574
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 nwindows = buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576
577 /* decrease the link count from windows (unless not in any window) */
578 if (buf->b_nwindows > 0)
579 --buf->b_nwindows;
580
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100581#ifdef FEAT_DIFF
582 if (diffopt_hiddenoff() && !unload_buf && buf->b_nwindows == 0)
Bram Moolenaarb7407d32018-02-03 17:36:27 +0100583 diff_buf_delete(buf); /* Clear 'diff' for hidden buffer. */
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100584#endif
585
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 /* Return when a window is displaying the buffer or when it's not
587 * unloaded. */
588 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590
591 /* Always remove the buffer when there is no file name. */
592 if (buf->b_ffname == NULL)
593 del_buf = TRUE;
594
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200595 /* When closing the current buffer stop Visual mode before freeing
596 * anything. */
Bram Moolenaar4930a762016-09-11 14:39:53 +0200597 if (buf == curbuf && VIsual_active
Bram Moolenaar9a27c7f2016-09-09 12:57:09 +0200598#if defined(EXITFREE)
599 && !entered_free_all_mem
600#endif
601 )
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200602 end_visual_mode();
603
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 /*
605 * Free all things allocated for this buffer.
606 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
607 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 /* Remember if we are closing the current buffer. Restore the number of
609 * windows, so that autocommands in buf_freeall() don't get confused. */
610 is_curbuf = (buf == curbuf);
611 buf->b_nwindows = nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200613 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 /* Autocommands may have deleted the buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200616 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100618#ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000619 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100621#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 /*
624 * It's possible that autocommands change curbuf to the one being deleted.
625 * This might cause the previous curbuf to be deleted unexpectedly. But
626 * in some cases it's OK to delete the curbuf, because a new one is
627 * obtained anyway. Therefore only return if curbuf changed to the
628 * deleted buffer.
629 */
630 if (buf == curbuf && !is_curbuf)
631 return;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200632
Bram Moolenaar4033c552017-09-16 20:54:51 +0200633 if (win_valid_any_tab(win) && win->w_buffer == buf)
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200634 win->w_buffer = NULL; /* make sure we don't use the buffer now */
635
636 /* Autocommands may have opened or closed windows for this buffer.
637 * Decrement the count for the close we do here. */
638 if (buf->b_nwindows > 0)
639 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 /*
642 * Remove the buffer from the list.
643 */
644 if (wipe_buf)
645 {
646#ifdef FEAT_SUN_WORKSHOP
647 if (usingSunWorkShop)
648 workshop_file_closed_lineno((char *)buf->b_ffname,
649 (int)buf->b_last_cursor.lnum);
650#endif
651 vim_free(buf->b_ffname);
652 vim_free(buf->b_sfname);
653 if (buf->b_prev == NULL)
654 firstbuf = buf->b_next;
655 else
656 buf->b_prev->b_next = buf->b_next;
657 if (buf->b_next == NULL)
658 lastbuf = buf->b_prev;
659 else
660 buf->b_next->b_prev = buf->b_prev;
661 free_buffer(buf);
662 }
663 else
664 {
665 if (del_buf)
666 {
667 /* Free all internal variables and reset option values, to make
668 * ":bdel" compatible with Vim 5.7. */
669 free_buffer_stuff(buf, TRUE);
670
671 /* Make it look like a new buffer. */
672 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
673
674 /* Init the options when loaded again. */
675 buf->b_p_initialized = FALSE;
676 }
677 buf_clear_file(buf);
678 if (del_buf)
679 buf->b_p_bl = FALSE;
680 }
681}
682
683/*
684 * Make buffer not contain a file.
685 */
686 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100687buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688{
689 buf->b_ml.ml_line_count = 1;
690 unchanged(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 buf->b_p_eol = TRUE;
693 buf->b_start_eol = TRUE;
694#ifdef FEAT_MBYTE
695 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000696 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697#endif
698 buf->b_ml.ml_mfp = NULL;
699 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
700#ifdef FEAT_NETBEANS_INTG
701 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702#endif
703}
704
705/*
706 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200707 * the file. Careful: get here with "curwin" NULL when exiting.
708 * flags:
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200709 * BFA_DEL buffer is going to be deleted
710 * BFA_WIPE buffer is going to be wiped out
711 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100714buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200717 bufref_T bufref;
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200718 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaar5a497892016-09-03 16:29:04 +0200719 win_T *the_curwin = curwin;
720 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721
Bram Moolenaar5a497892016-09-03 16:29:04 +0200722 /* Make sure the buffer isn't closed by autocommands. */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200723 ++buf->b_locked;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200724 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200725 if (buf->b_ml.ml_mfp != NULL)
726 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200727 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
728 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200729 && !bufref_valid(&bufref))
730 /* autocommands deleted the buffer */
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200731 return;
732 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200733 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200735 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
736 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200737 && !bufref_valid(&bufref))
738 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 return;
740 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200741 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200743 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
744 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200745 && !bufref_valid(&bufref))
746 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 return;
748 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200749 --buf->b_locked;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200750
Bram Moolenaar5a497892016-09-03 16:29:04 +0200751 /* If the buffer was in curwin and the window has changed, go back to that
752 * window, if it still exists. This avoids that ":edit x" triggering a
753 * "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
754 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
755 {
756 block_autocmds();
757 goto_tabpage_win(the_curtab, the_curwin);
758 unblock_autocmds();
759 }
Bram Moolenaar5a497892016-09-03 16:29:04 +0200760
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100761#ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000762 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765
766 /*
767 * It's possible that autocommands change curbuf to the one being deleted.
768 * This might cause curbuf to be deleted unexpectedly. But in some cases
769 * it's OK to delete the curbuf, because a new one is obtained anyway.
770 * Therefore only return if curbuf changed to the deleted buffer.
771 */
772 if (buf == curbuf && !is_curbuf)
773 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774#ifdef FEAT_DIFF
775 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
776#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200777#ifdef FEAT_SYN_HL
Bram Moolenaar89c71222011-11-30 15:40:56 +0100778 /* Remove any ownsyntax, unless exiting. */
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200779 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaar89c71222011-11-30 15:40:56 +0100780 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200781#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000782
783#ifdef FEAT_FOLDING
784 /* No folds in an empty buffer. */
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000785 {
786 win_T *win;
787 tabpage_T *tp;
788
789 FOR_ALL_TAB_WINDOWS(tp, win)
790 if (win->w_buffer == buf)
791 clearFolding(win);
792 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000793#endif
794
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795#ifdef FEAT_TCL
796 tcl_buffer_free(buf);
797#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 ml_close(buf, TRUE); /* close and delete the memline/memfile */
799 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200800 if ((flags & BFA_KEEP_UNDO) == 0)
801 {
802 u_blockfree(buf); /* free the memory allocated for undo */
803 u_clearall(buf); /* reset all undo information */
804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200806 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000808 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809}
810
811/*
812 * Free a buffer structure and the things it contains related to the buffer
813 * itself (not the file, that must have been done already).
814 */
815 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100816free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200818 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200820#ifdef FEAT_EVAL
Bram Moolenaarb2c87502017-10-14 21:15:58 +0200821 /* b:changedtick uses an item in buf_T, remove it now */
822 dictitem_remove(buf->b_vars, (dictitem_T *)&buf->b_ct_di);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200823 unref_var_dict(buf->b_vars);
824#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200825#ifdef FEAT_LUA
826 lua_buffer_free(buf);
827#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000828#ifdef FEAT_MZSCHEME
829 mzscheme_buffer_free(buf);
830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831#ifdef FEAT_PERL
832 perl_buf_free(buf);
833#endif
834#ifdef FEAT_PYTHON
835 python_buffer_free(buf);
836#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200837#ifdef FEAT_PYTHON3
838 python3_buffer_free(buf);
839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840#ifdef FEAT_RUBY
841 ruby_buffer_free(buf);
842#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200843#ifdef FEAT_JOB_CHANNEL
844 channel_buffer_free(buf);
845#endif
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200846#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +0200847 free_terminal(buf);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200848#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200849
850 buf_hashtab_remove(buf);
851
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200852 aubuflocal_remove(buf);
853
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200854 if (autocmd_busy)
855 {
856 /* Do not free the buffer structure while autocommands are executing,
857 * it's still needed. Free it when autocmd_busy is reset. */
858 buf->b_next = au_pending_free_buf;
859 au_pending_free_buf = buf;
860 }
861 else
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200862 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863}
864
865/*
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100866 * Initializes b:changedtick.
Bram Moolenaar79518e22017-02-17 16:31:35 +0100867 */
868 static void
869init_changedtick(buf_T *buf)
870{
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100871 dictitem_T *di = (dictitem_T *)&buf->b_ct_di;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100872
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100873 di->di_flags = DI_FLAGS_FIX | DI_FLAGS_RO;
874 di->di_tv.v_type = VAR_NUMBER;
875 di->di_tv.v_lock = VAR_FIXED;
876 di->di_tv.vval.v_number = 0;
877
Bram Moolenaar92769c32017-02-25 15:41:37 +0100878#ifdef FEAT_EVAL
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100879 STRCPY(buf->b_ct_di.di_key, "changedtick");
880 (void)dict_add(buf->b_vars, di);
Bram Moolenaar92769c32017-02-25 15:41:37 +0100881#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +0100882}
883
884/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
886 */
887 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100888free_buffer_stuff(
889 buf_T *buf,
890 int free_options) /* free options as well */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891{
892 if (free_options)
893 {
894 clear_wininfo(buf); /* including window-local options */
895 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200896#ifdef FEAT_SPELL
897 ga_clear(&buf->b_s.b_langp);
898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 }
900#ifdef FEAT_EVAL
Bram Moolenaar79518e22017-02-17 16:31:35 +0100901 {
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100902 varnumber_T tick = CHANGEDTICK(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +0100903
904 vars_clear(&buf->b_vars->dv_hashtab); /* free all buffer variables */
905 hash_init(&buf->b_vars->dv_hashtab);
906 init_changedtick(buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100907 CHANGEDTICK(buf) = tick;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909#endif
910#ifdef FEAT_USR_CMDS
911 uc_clear(&buf->b_ucmds); /* clear local user commands */
912#endif
913#ifdef FEAT_SIGNS
914 buf_delete_signs(buf); /* delete any signs */
915#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000916#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200917 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919#ifdef FEAT_LOCALMAP
920 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
921 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
922#endif
923#ifdef FEAT_MBYTE
Bram Moolenaard23a8232018-02-10 18:45:26 +0100924 VIM_CLEAR(buf->b_start_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925#endif
926}
927
928/*
929 * Free the b_wininfo list for buffer "buf".
930 */
931 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100932clear_wininfo(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933{
934 wininfo_T *wip;
935
936 while (buf->b_wininfo != NULL)
937 {
938 wip = buf->b_wininfo;
939 buf->b_wininfo = wip->wi_next;
940 if (wip->wi_optset)
941 {
942 clear_winopt(&wip->wi_opt);
943#ifdef FEAT_FOLDING
944 deleteFoldRecurse(&wip->wi_folds);
945#endif
946 }
947 vim_free(wip);
948 }
949}
950
951#if defined(FEAT_LISTCMDS) || defined(PROTO)
952/*
953 * Go to another buffer. Handles the result of the ATTENTION dialog.
954 */
955 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100956goto_buffer(
957 exarg_T *eap,
958 int start,
959 int dir,
960 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961{
Bram Moolenaar4033c552017-09-16 20:54:51 +0200962# if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200963 bufref_T old_curbuf;
964
965 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966
967 swap_exists_action = SEA_DIALOG;
968# endif
969 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
970 start, dir, count, eap->forceit);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200971# if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
973 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100974# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000975 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000976
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000977 /* Reset the error/interrupt/exception state here so that
978 * aborting() returns FALSE when closing a window. */
979 enter_cleanup(&cs);
980# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000981
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000982 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 win_close(curwin, TRUE);
984 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000985 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000986
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100987# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000988 /* Restore the error/interrupt/exception state if not discarded by a
989 * new aborting error, interrupt, or uncaught exception. */
990 leave_cleanup(&cs);
991# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 }
993 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200994 handle_swap_exists(&old_curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995# endif
996}
997#endif
998
Bram Moolenaare64ac772005-12-07 20:54:59 +0000999#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000/*
1001 * Handle the situation of swap_exists_action being set.
1002 * It is allowed for "old_curbuf" to be NULL or invalid.
1003 */
1004 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001005handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006{
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001007# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001008 cleanup_T cs;
1009# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001010# ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001011 long old_tw = curbuf->b_p_tw;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001012# endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001013 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001014
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 if (swap_exists_action == SEA_QUIT)
1016 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001017# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001018 /* Reset the error/interrupt/exception state here so that
1019 * aborting() returns FALSE when closing a buffer. */
1020 enter_cleanup(&cs);
1021# endif
1022
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 /* User selected Quit at ATTENTION prompt. Go back to previous
1024 * buffer. If that buffer is gone or the same as the current one,
1025 * open a new, empty buffer. */
1026 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001027 swap_exists_did_quit = TRUE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001028 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001029 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1030 || old_curbuf->br_buf == curbuf)
1031 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
1032 else
1033 buf = old_curbuf->br_buf;
1034 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001035 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001036 enter_buffer(buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001037# ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001038 if (old_tw != curbuf->b_p_tw)
1039 check_colorcolumn(curwin);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001040# endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001043
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001044# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001045 /* Restore the error/interrupt/exception state if not discarded by a
1046 * new aborting error, interrupt, or uncaught exception. */
1047 leave_cleanup(&cs);
1048# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 }
1050 else if (swap_exists_action == SEA_RECOVER)
1051 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001052# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001053 /* Reset the error/interrupt/exception state here so that
1054 * aborting() returns FALSE when closing a buffer. */
1055 enter_cleanup(&cs);
1056# endif
1057
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 /* User selected Recover at ATTENTION prompt. */
1059 msg_scroll = TRUE;
1060 ml_recover();
1061 MSG_PUTS("\n"); /* don't overwrite the last message */
1062 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001063 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001064
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001065# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001066 /* Restore the error/interrupt/exception state if not discarded by a
1067 * new aborting error, interrupt, or uncaught exception. */
1068 leave_cleanup(&cs);
1069# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 }
1071 swap_exists_action = SEA_NONE;
1072}
1073#endif
1074
1075#if defined(FEAT_LISTCMDS) || defined(PROTO)
1076/*
1077 * do_bufdel() - delete or unload buffer(s)
1078 *
1079 * addr_count == 0: ":bdel" - delete current buffer
1080 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1081 * buffer "end_bnr", then any other arguments.
1082 * addr_count == 2: ":N,N bdel" - delete buffers in range
1083 *
1084 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1085 * DOBUF_DEL (":bdel")
1086 *
1087 * Returns error message or NULL
1088 */
1089 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001090do_bufdel(
1091 int command,
1092 char_u *arg, /* pointer to extra arguments */
1093 int addr_count,
1094 int start_bnr, /* first buffer number in a range */
1095 int end_bnr, /* buffer nr or last buffer nr in a range */
1096 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097{
1098 int do_current = 0; /* delete current buffer? */
1099 int deleted = 0; /* number of buffers deleted */
1100 char_u *errormsg = NULL; /* return value */
1101 int bnr; /* buffer number */
1102 char_u *p;
1103
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 if (addr_count == 0)
1105 {
1106 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1107 }
1108 else
1109 {
1110 if (addr_count == 2)
1111 {
1112 if (*arg) /* both range and argument is not allowed */
1113 return (char_u *)_(e_trailing);
1114 bnr = start_bnr;
1115 }
1116 else /* addr_count == 1 */
1117 bnr = end_bnr;
1118
1119 for ( ;!got_int; ui_breakcheck())
1120 {
1121 /*
1122 * delete the current buffer last, otherwise when the
1123 * current buffer is deleted, the next buffer becomes
1124 * the current one and will be loaded, which may then
1125 * also be deleted, etc.
1126 */
1127 if (bnr == curbuf->b_fnum)
1128 do_current = bnr;
1129 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
1130 forceit) == OK)
1131 ++deleted;
1132
1133 /*
1134 * find next buffer number to delete/unload
1135 */
1136 if (addr_count == 2)
1137 {
1138 if (++bnr > end_bnr)
1139 break;
1140 }
1141 else /* addr_count == 1 */
1142 {
1143 arg = skipwhite(arg);
1144 if (*arg == NUL)
1145 break;
1146 if (!VIM_ISDIGIT(*arg))
1147 {
1148 p = skiptowhite_esc(arg);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01001149 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE,
1150 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 if (bnr < 0) /* failed */
1152 break;
1153 arg = p;
1154 }
1155 else
1156 bnr = getdigits(&arg);
1157 }
1158 }
1159 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1160 FORWARD, do_current, forceit) == OK)
1161 ++deleted;
1162
1163 if (deleted == 0)
1164 {
1165 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001166 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001168 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00001170 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 errormsg = IObuff;
1172 }
1173 else if (deleted >= p_report)
1174 {
1175 if (command == DOBUF_UNLOAD)
1176 {
1177 if (deleted == 1)
1178 MSG(_("1 buffer unloaded"));
1179 else
1180 smsg((char_u *)_("%d buffers unloaded"), deleted);
1181 }
1182 else if (command == DOBUF_DEL)
1183 {
1184 if (deleted == 1)
1185 MSG(_("1 buffer deleted"));
1186 else
1187 smsg((char_u *)_("%d buffers deleted"), deleted);
1188 }
1189 else
1190 {
1191 if (deleted == 1)
1192 MSG(_("1 buffer wiped out"));
1193 else
1194 smsg((char_u *)_("%d buffers wiped out"), deleted);
1195 }
1196 }
1197 }
1198
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199
1200 return errormsg;
1201}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001202#endif /* FEAT_LISTCMDS */
1203
1204#if defined(FEAT_LISTCMDS) || defined(FEAT_PYTHON) \
1205 || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001207static int empty_curbuf(int close_others, int forceit, int action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001208
1209/*
1210 * Make the current buffer empty.
1211 * Used when it is wiped out and it's the last buffer.
1212 */
1213 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001214empty_curbuf(
1215 int close_others,
1216 int forceit,
1217 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001218{
1219 int retval;
1220 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001221 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001222
1223 if (action == DOBUF_UNLOAD)
1224 {
1225 EMSG(_("E90: Cannot unload last buffer"));
1226 return FAIL;
1227 }
1228
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001229 set_bufref(&bufref, buf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001230 if (close_others)
1231 /* Close any other windows on this buffer, then make it empty. */
Bram Moolenaara02471e2014-01-10 16:43:14 +01001232 close_windows(buf, TRUE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001233
1234 setpcmark();
1235 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1236 forceit ? ECMD_FORCEIT : 0, curwin);
1237
1238 /*
1239 * do_ecmd() may create a new buffer, then we have to delete
1240 * the old one. But do_ecmd() may have done that already, check
1241 * if the buffer still exists.
1242 */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001243 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001244 close_buffer(NULL, buf, action, FALSE);
1245 if (!close_others)
1246 need_fileinfo = FALSE;
1247 return retval;
1248}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249/*
1250 * Implementation of the commands for the buffer list.
1251 *
1252 * action == DOBUF_GOTO go to specified buffer
1253 * action == DOBUF_SPLIT split window and go to specified buffer
1254 * action == DOBUF_UNLOAD unload specified buffer(s)
1255 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1256 * action == DOBUF_WIPE delete specified buffer(s) really
1257 *
1258 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1259 * start == DOBUF_FIRST go to "count" buffer from first buffer
1260 * start == DOBUF_LAST go to "count" buffer from last buffer
1261 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1262 *
1263 * Return FAIL or OK.
1264 */
1265 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001266do_buffer(
1267 int action,
1268 int start,
1269 int dir, /* FORWARD or BACKWARD */
1270 int count, /* buffer number or number of buffers */
1271 int forceit) /* TRUE for :...! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272{
1273 buf_T *buf;
1274 buf_T *bp;
1275 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1276 || action == DOBUF_WIPE);
1277
1278 switch (start)
1279 {
1280 case DOBUF_FIRST: buf = firstbuf; break;
1281 case DOBUF_LAST: buf = lastbuf; break;
1282 default: buf = curbuf; break;
1283 }
1284 if (start == DOBUF_MOD) /* find next modified buffer */
1285 {
1286 while (count-- > 0)
1287 {
1288 do
1289 {
1290 buf = buf->b_next;
1291 if (buf == NULL)
1292 buf = firstbuf;
1293 }
1294 while (buf != curbuf && !bufIsChanged(buf));
1295 }
1296 if (!bufIsChanged(buf))
1297 {
1298 EMSG(_("E84: No modified buffer found"));
1299 return FAIL;
1300 }
1301 }
1302 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
1303 {
1304 while (buf != NULL && buf->b_fnum != count)
1305 buf = buf->b_next;
1306 }
1307 else
1308 {
1309 bp = NULL;
1310 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1311 {
1312 /* remember the buffer where we start, we come back there when all
1313 * buffers are unlisted. */
1314 if (bp == NULL)
1315 bp = buf;
1316 if (dir == FORWARD)
1317 {
1318 buf = buf->b_next;
1319 if (buf == NULL)
1320 buf = firstbuf;
1321 }
1322 else
1323 {
1324 buf = buf->b_prev;
1325 if (buf == NULL)
1326 buf = lastbuf;
1327 }
1328 /* don't count unlisted buffers */
1329 if (unload || buf->b_p_bl)
1330 {
1331 --count;
1332 bp = NULL; /* use this buffer as new starting point */
1333 }
1334 if (bp == buf)
1335 {
1336 /* back where we started, didn't find anything. */
1337 EMSG(_("E85: There is no listed buffer"));
1338 return FAIL;
1339 }
1340 }
1341 }
1342
1343 if (buf == NULL) /* could not find it */
1344 {
1345 if (start == DOBUF_FIRST)
1346 {
1347 /* don't warn when deleting */
1348 if (!unload)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01001349 EMSGN(_(e_nobufnr), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 }
1351 else if (dir == FORWARD)
1352 EMSG(_("E87: Cannot go beyond last buffer"));
1353 else
1354 EMSG(_("E88: Cannot go before first buffer"));
1355 return FAIL;
1356 }
1357
1358#ifdef FEAT_GUI
1359 need_mouse_correct = TRUE;
1360#endif
1361
1362#ifdef FEAT_LISTCMDS
1363 /*
1364 * delete buffer buf from memory and/or the list
1365 */
1366 if (unload)
1367 {
1368 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001369 bufref_T bufref;
1370
1371 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372
1373 /* When unloading or deleting a buffer that's already unloaded and
1374 * unlisted: fail silently. */
1375 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1376 return FAIL;
1377
1378 if (!forceit && bufIsChanged(buf))
1379 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001380# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 if ((p_confirm || cmdmod.confirm) && p_write)
1382 {
1383 dialog_changed(buf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001384 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 /* Autocommand deleted buffer, oops! It's not changed
1386 * now. */
1387 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001388 /* If it's still changed fail silently, the dialog already
1389 * mentioned why it fails. */
1390 if (bufIsChanged(buf))
1391 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001393 else
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001394# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 {
1396 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1397 buf->b_fnum);
1398 return FAIL;
1399 }
1400 }
1401
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001402 /* When closing the current buffer stop Visual mode. */
Bram Moolenaar4930a762016-09-11 14:39:53 +02001403 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001404 end_visual_mode();
1405
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 /*
1407 * If deleting the last (listed) buffer, make it empty.
1408 * The last (listed) buffer cannot be unloaded.
1409 */
Bram Moolenaar29323592016-07-24 22:04:11 +02001410 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 if (bp->b_p_bl && bp != buf)
1412 break;
1413 if (bp == NULL && buf == curbuf)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001414 return empty_curbuf(TRUE, forceit, action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 /*
1417 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001418 * (unless it's the only window). Repeat this so long as we end up in
1419 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001421 while (buf == curbuf
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001422 && !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
Bram Moolenaar459ca562016-11-10 18:16:33 +01001423 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001424 {
1425 if (win_close(curwin, FALSE) == FAIL)
1426 break;
1427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428
1429 /*
1430 * If the buffer to be deleted is not the current one, delete it here.
1431 */
1432 if (buf != curbuf)
1433 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001434 close_windows(buf, FALSE);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001435 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows <= 0)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001436 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 return OK;
1438 }
1439
1440 /*
1441 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001442 * There should be another, otherwise it would have been handled
1443 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001444 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 * Then prefer the buffer we most recently visited.
1446 * Else try to find one that is loaded, after the current buffer,
1447 * then before the current buffer.
1448 * Finally use any buffer.
1449 */
1450 buf = NULL; /* selected buffer */
1451 bp = NULL; /* used when no loaded buffer found */
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001452 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1453 buf = au_new_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454#ifdef FEAT_JUMPLIST
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001455 else if (curwin->w_jumplistlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 {
1457 int jumpidx;
1458
1459 jumpidx = curwin->w_jumplistidx - 1;
1460 if (jumpidx < 0)
1461 jumpidx = curwin->w_jumplistlen - 1;
1462
1463 forward = jumpidx;
1464 while (jumpidx != curwin->w_jumplistidx)
1465 {
1466 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1467 if (buf != NULL)
1468 {
1469 if (buf == curbuf || !buf->b_p_bl)
1470 buf = NULL; /* skip current and unlisted bufs */
1471 else if (buf->b_ml.ml_mfp == NULL)
1472 {
1473 /* skip unloaded buf, but may keep it for later */
1474 if (bp == NULL)
1475 bp = buf;
1476 buf = NULL;
1477 }
1478 }
1479 if (buf != NULL) /* found a valid buffer: stop searching */
1480 break;
1481 /* advance to older entry in jump list */
1482 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1483 break;
1484 if (--jumpidx < 0)
1485 jumpidx = curwin->w_jumplistlen - 1;
1486 if (jumpidx == forward) /* List exhausted for sure */
1487 break;
1488 }
1489 }
1490#endif
1491
1492 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1493 {
1494 forward = TRUE;
1495 buf = curbuf->b_next;
1496 for (;;)
1497 {
1498 if (buf == NULL)
1499 {
1500 if (!forward) /* tried both directions */
1501 break;
1502 buf = curbuf->b_prev;
1503 forward = FALSE;
1504 continue;
1505 }
1506 /* in non-help buffer, try to skip help buffers, and vv */
1507 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1508 {
1509 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1510 break;
1511 if (bp == NULL) /* remember unloaded buf for later */
1512 bp = buf;
1513 }
1514 if (forward)
1515 buf = buf->b_next;
1516 else
1517 buf = buf->b_prev;
1518 }
1519 }
1520 if (buf == NULL) /* No loaded buffer, use unloaded one */
1521 buf = bp;
1522 if (buf == NULL) /* No loaded buffer, find listed one */
1523 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001524 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 if (buf->b_p_bl && buf != curbuf)
1526 break;
1527 }
1528 if (buf == NULL) /* Still no buffer, just take one */
1529 {
1530 if (curbuf->b_next != NULL)
1531 buf = curbuf->b_next;
1532 else
1533 buf = curbuf->b_prev;
1534 }
1535 }
1536
Bram Moolenaara02471e2014-01-10 16:43:14 +01001537 if (buf == NULL)
1538 {
1539 /* Autocommands must have wiped out all other buffers. Only option
1540 * now is to make the current buffer empty. */
1541 return empty_curbuf(FALSE, forceit, action);
1542 }
1543
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 /*
1545 * make buf current buffer
1546 */
1547 if (action == DOBUF_SPLIT) /* split window first */
1548 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001549 /* If 'switchbuf' contains "useopen": jump to first window containing
1550 * "buf" if one exists */
1551 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001552 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001553 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001554 * page containing "buf" if one exists */
1555 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 return OK;
1557 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 return FAIL;
1559 }
1560#endif
1561
1562 /* go to current buffer - nothing to do */
1563 if (buf == curbuf)
1564 return OK;
1565
1566 /*
1567 * Check if the current buffer may be abandoned.
1568 */
1569 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1570 {
1571#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1572 if ((p_confirm || cmdmod.confirm) && p_write)
1573 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001574 bufref_T bufref;
1575
1576 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 dialog_changed(curbuf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001578 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 /* Autocommand deleted buffer, oops! */
1580 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 }
1582 if (bufIsChanged(curbuf))
1583#endif
1584 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001585 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 return FAIL;
1587 }
1588 }
1589
1590 /* Go to the other buffer. */
1591 set_curbuf(buf, action);
1592
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01001593#if defined(FEAT_LISTCMDS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001595 {
1596 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
1597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598#endif
1599
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001600#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 if (aborting()) /* autocmds may abort script processing */
1602 return FAIL;
1603#endif
1604
1605 return OK;
1606}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608
1609/*
1610 * Set current buffer to "buf". Executes autocommands and closes current
1611 * buffer. "action" tells how to close the current buffer:
1612 * DOBUF_GOTO free or hide it
1613 * DOBUF_SPLIT nothing
1614 * DOBUF_UNLOAD unload it
1615 * DOBUF_DEL delete it
1616 * DOBUF_WIPE wipe it out
1617 */
1618 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001619set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620{
1621 buf_T *prevbuf;
1622 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1623 || action == DOBUF_WIPE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001624#ifdef FEAT_SYN_HL
1625 long old_tw = curbuf->b_p_tw;
1626#endif
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001627 bufref_T newbufref;
1628 bufref_T prevbufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629
1630 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001631 if (!cmdmod.keepalt)
1632 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001633 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 /* Don't restart Select mode after switching to another buffer. */
1636 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001638 /* close_windows() or apply_autocmds() may change curbuf and wipe out "buf"
1639 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 prevbuf = curbuf;
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001641 set_bufref(&prevbufref, prevbuf);
1642 set_bufref(&newbufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001644 /* Autocommands may delete the curren buffer and/or the buffer we wan to go
1645 * to. In those cases don't close the buffer. */
Bram Moolenaar82404332016-07-10 17:00:38 +02001646 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001647 || (bufref_valid(&prevbufref)
1648 && bufref_valid(&newbufref)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001649#ifdef FEAT_EVAL
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001650 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001652 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001654#ifdef FEAT_SYN_HL
1655 if (prevbuf == curwin->w_buffer)
1656 reset_synblock(curwin);
1657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001659 close_windows(prevbuf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001660#if defined(FEAT_EVAL)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001661 if (bufref_valid(&prevbufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662#else
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001663 if (bufref_valid(&prevbufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001665 {
Bram Moolenaare25865a2012-07-06 16:22:02 +02001666 win_T *previouswin = curwin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001667 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001668 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1670 unload ? action : (action == DOBUF_GOTO
Bram Moolenaareb44a682017-08-03 22:44:55 +02001671 && !buf_hide(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001672 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaare25865a2012-07-06 16:22:02 +02001673 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaar9e931222012-06-20 11:55:01 +02001674 /* autocommands changed curwin, Grr! */
Bram Moolenaare25865a2012-07-06 16:22:02 +02001675 curwin = previouswin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 }
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001678 /* An autocommand may have deleted "buf", already entered it (e.g., when
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001679 * it did ":bunload") or aborted the script processing.
Bram Moolenaar9e931222012-06-20 11:55:01 +02001680 * If curwin->w_buffer is null, enter_buffer() will make it valid again */
1681 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001682#ifdef FEAT_EVAL
Bram Moolenaar4033c552017-09-16 20:54:51 +02001683 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001685 ) || curwin->w_buffer == NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001686 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001688#ifdef FEAT_SYN_HL
1689 if (old_tw != curbuf->b_p_tw)
1690 check_colorcolumn(curwin);
1691#endif
1692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693}
1694
1695/*
1696 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001697 * Old curbuf must have been abandoned already! This also means "curbuf" may
1698 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 */
1700 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001701enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702{
1703 /* Copy buffer and window local option values. Not for a help buffer. */
1704 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1705 if (!buf->b_help)
1706 get_winopts(buf);
1707#ifdef FEAT_FOLDING
1708 else
1709 /* Remove all folds in the window. */
1710 clearFolding(curwin);
1711 foldUpdateAll(curwin); /* update folds (later). */
1712#endif
1713
1714 /* Get the buffer in the current window. */
1715 curwin->w_buffer = buf;
1716 curbuf = buf;
1717 ++curbuf->b_nwindows;
1718
1719#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001720 if (curwin->w_p_diff)
1721 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722#endif
1723
Bram Moolenaara971b822011-09-14 14:43:25 +02001724#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001725 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001726#endif
1727
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 /* Cursor on first line by default. */
1729 curwin->w_cursor.lnum = 1;
1730 curwin->w_cursor.col = 0;
1731#ifdef FEAT_VIRTUALEDIT
1732 curwin->w_cursor.coladd = 0;
1733#endif
1734 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001735 curwin->w_topline_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001737 /* mark cursor position as being invalid */
1738 curwin->w_valid = 0;
1739
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 /* Make sure the buffer is loaded. */
1741 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001742 {
1743 /* If there is no filetype, allow for detecting one. Esp. useful for
1744 * ":ball" used in a autocommand. If there already is a filetype we
1745 * might prefer to keep it. */
1746 if (*curbuf->b_p_ft == NUL)
1747 did_filetype = FALSE;
1748
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001749 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 else
1752 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001753 if (!msg_silent)
1754 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001757#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1761 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 }
1763
1764 /* If autocommands did not change the cursor position, restore cursor lnum
1765 * and possibly cursor col. */
1766 if (curwin->w_cursor.lnum == 1 && inindent(0))
1767 buflist_getfpos();
1768
1769 check_arg_idx(curwin); /* check for valid arg_idx */
1770#ifdef FEAT_TITLE
1771 maketitle();
1772#endif
Bram Moolenaard4153d42008-11-15 15:06:17 +00001773 /* when autocmds didn't change it */
1774 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1776
Bram Moolenaar009b2592004-10-24 19:18:58 +00001777#ifdef FEAT_NETBEANS_INTG
1778 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001779 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001780#endif
1781
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001782 /* Change directories when the 'acd' option is set. */
1783 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784
1785#ifdef FEAT_KEYMAP
1786 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001787 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001789#ifdef FEAT_SPELL
1790 /* May need to set the spell language. Can only do this after the buffer
1791 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001792 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1793 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001794#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001795#ifdef FEAT_VIMINFO
1796 curbuf->b_last_used = vim_time();
1797#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001798
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 redraw_later(NOT_VALID);
1800}
1801
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001802#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1803/*
1804 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001805 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001806 */
1807 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001808do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001809{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001810 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001811 && curbuf->b_ffname != NULL
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001812 && vim_chdirfile(curbuf->b_ffname, "auto") == OK)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001813 shorten_fnames(TRUE);
1814}
1815#endif
1816
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001817 void
1818no_write_message(void)
1819{
1820#ifdef FEAT_TERMINAL
1821 if (term_job_running(curbuf->b_term))
1822 EMSG(_("E948: Job still running (add ! to end the job)"));
1823 else
1824#endif
1825 EMSG(_("E37: No write since last change (add ! to override)"));
1826}
1827
1828 void
Bram Moolenaar7a760922018-02-19 23:10:02 +01001829no_write_message_nobang(buf_T *buf UNUSED)
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001830{
1831#ifdef FEAT_TERMINAL
Bram Moolenaar7a760922018-02-19 23:10:02 +01001832 if (term_job_running(buf->b_term))
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001833 EMSG(_("E948: Job still running"));
1834 else
1835#endif
1836 EMSG(_("E37: No write since last change"));
1837}
1838
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839/*
1840 * functions for dealing with the buffer list
1841 */
1842
Bram Moolenaar480778b2016-07-14 22:09:39 +02001843static int top_file_num = 1; /* highest file number */
1844
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845/*
1846 * Add a file name to the buffer list. Return a pointer to the buffer.
1847 * If the same file name already exists return a pointer to that buffer.
1848 * If it does not exist, or if fname == NULL, a new entry is created.
1849 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1850 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1851 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001852 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001853 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
1854 * if the buffer already exists.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 * This is the ONLY way to create a new buffer.
1856 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001858buflist_new(
1859 char_u *ffname, /* full path of fname or relative */
1860 char_u *sfname, /* short fname or NULL */
1861 linenr_T lnum, /* preferred cursor line */
1862 int flags) /* BLN_ defines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863{
1864 buf_T *buf;
1865#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001866 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867#endif
1868
Bram Moolenaar480778b2016-07-14 22:09:39 +02001869 if (top_file_num == 1)
1870 hash_init(&buf_hashtab);
1871
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1873
1874 /*
1875 * If file name already exists in the list, update the entry.
1876 */
1877#ifdef UNIX
1878 /* On Unix we can use inode numbers when the file exists. Works better
1879 * for hard links. */
1880 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1881 st.st_dev = (dev_T)-1;
1882#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001883 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884#ifdef UNIX
1885 buflist_findname_stat(ffname, &st)
1886#else
1887 buflist_findname(ffname)
1888#endif
1889 ) != NULL)
1890 {
1891 vim_free(ffname);
1892 if (lnum != 0)
1893 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02001894
1895 if ((flags & BLN_NOOPT) == 0)
1896 /* copy the options now, if 'cpo' doesn't have 's' and not done
1897 * already */
1898 buf_copy_options(buf, 0);
1899
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1901 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001902 bufref_T bufref;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001903
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 buf->b_p_bl = TRUE;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001905 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001907 {
Bram Moolenaar82404332016-07-10 17:00:38 +02001908 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001909 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001910 return NULL;
1911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 }
1913 return buf;
1914 }
1915
1916 /*
1917 * If the current buffer has no name and no contents, use the current
1918 * buffer. Otherwise: Need to allocate a new buffer structure.
1919 *
1920 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001921 * (A spell file buffer is allocated in spell.c, but that's not a normal
1922 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 */
1924 buf = NULL;
1925 if ((flags & BLN_CURBUF)
1926 && curbuf != NULL
1927 && curbuf->b_ffname == NULL
1928 && curbuf->b_nwindows <= 1
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001929 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 {
1931 buf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 /* It's like this buffer is deleted. Watch out for autocommands that
1933 * change curbuf! If that happens, allocate a new buffer anyway. */
1934 if (curbuf->b_p_bl)
1935 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1936 if (buf == curbuf)
1937 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001938#ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001939 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 if (buf == curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 {
1944 /* Make sure 'bufhidden' and 'buftype' are empty */
1945 clear_string_option(&buf->b_p_bh);
1946 clear_string_option(&buf->b_p_bt);
1947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 }
1949 if (buf != curbuf || curbuf == NULL)
1950 {
1951 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1952 if (buf == NULL)
1953 {
1954 vim_free(ffname);
1955 return NULL;
1956 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02001957#ifdef FEAT_EVAL
1958 /* init b: variables */
1959 buf->b_vars = dict_alloc();
1960 if (buf->b_vars == NULL)
1961 {
1962 vim_free(ffname);
1963 vim_free(buf);
1964 return NULL;
1965 }
1966 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
1967#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01001968 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 }
1970
1971 if (ffname != NULL)
1972 {
1973 buf->b_ffname = ffname;
1974 buf->b_sfname = vim_strsave(sfname);
1975 }
1976
1977 clear_wininfo(buf);
1978 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1979
1980 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1981 || buf->b_wininfo == NULL)
1982 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001983 VIM_CLEAR(buf->b_ffname);
1984 VIM_CLEAR(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 if (buf != curbuf)
1986 free_buffer(buf);
1987 return NULL;
1988 }
1989
1990 if (buf == curbuf)
1991 {
1992 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001993 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 if (buf != curbuf) /* autocommands deleted the buffer! */
1995 return NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001996#if defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001997 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 return NULL;
1999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002001
2002 /* Init the options. */
2003 buf->b_p_initialized = FALSE;
2004 buf_copy_options(buf, BCO_ENTER);
2005
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006#ifdef FEAT_KEYMAP
2007 /* need to reload lmaps and set b:keymap_name */
2008 curbuf->b_kmap_state |= KEYMAP_INIT;
2009#endif
2010 }
2011 else
2012 {
2013 /*
2014 * put new buffer at the end of the buffer list
2015 */
2016 buf->b_next = NULL;
2017 if (firstbuf == NULL) /* buffer list is empty */
2018 {
2019 buf->b_prev = NULL;
2020 firstbuf = buf;
2021 }
2022 else /* append new buffer at end of list */
2023 {
2024 lastbuf->b_next = buf;
2025 buf->b_prev = lastbuf;
2026 }
2027 lastbuf = buf;
2028
2029 buf->b_fnum = top_file_num++;
2030 if (top_file_num < 0) /* wrap around (may cause duplicates) */
2031 {
2032 EMSG(_("W14: Warning: List of file names overflow"));
2033 if (emsg_silent == 0)
2034 {
2035 out_flush();
2036 ui_delay(3000L, TRUE); /* make sure it is noticed */
2037 }
2038 top_file_num = 1;
2039 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002040 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041
2042 /*
2043 * Always copy the options from the current buffer.
2044 */
2045 buf_copy_options(buf, BCO_ALWAYS);
2046 }
2047
2048 buf->b_wininfo->wi_fpos.lnum = lnum;
2049 buf->b_wininfo->wi_win = curwin;
2050
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002051#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002052 hash_init(&buf->b_s.b_keywtab);
2053 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054#endif
2055
2056 buf->b_fname = buf->b_sfname;
2057#ifdef UNIX
2058 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002059 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 else
2061 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002062 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 buf->b_dev = st.st_dev;
2064 buf->b_ino = st.st_ino;
2065 }
2066#endif
2067 buf->b_u_synced = TRUE;
2068 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002069 if (flags & BLN_DUMMY)
2070 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 buf_clear_file(buf);
2072 clrallmarks(buf); /* clear marks */
2073 fmarks_check_names(buf); /* check file marks for this file */
2074 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 if (!(flags & BLN_DUMMY))
2076 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002077 bufref_T bufref;
2078
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01002079 /* Tricky: these autocommands may change the buffer list. They could
2080 * also split the window with re-using the one empty buffer. This may
2081 * result in unexpectedly losing the empty buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002082 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002083 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002084 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002085 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002087 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002088 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002089 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002090 return NULL;
2091 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002092#ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002093 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097
2098 return buf;
2099}
2100
2101/*
2102 * Free the memory for the options of a buffer.
2103 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2104 * 'fileencoding'.
2105 */
2106 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002107free_buf_options(
2108 buf_T *buf,
2109 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110{
2111 if (free_p_ff)
2112 {
2113#ifdef FEAT_MBYTE
2114 clear_string_option(&buf->b_p_fenc);
2115#endif
2116 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 clear_string_option(&buf->b_p_bh);
2118 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 }
2120#ifdef FEAT_FIND_ID
2121 clear_string_option(&buf->b_p_def);
2122 clear_string_option(&buf->b_p_inc);
2123# ifdef FEAT_EVAL
2124 clear_string_option(&buf->b_p_inex);
2125# endif
2126#endif
2127#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
2128 clear_string_option(&buf->b_p_inde);
2129 clear_string_option(&buf->b_p_indk);
2130#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002131#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2132 clear_string_option(&buf->b_p_bexpr);
2133#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002134#if defined(FEAT_CRYPT)
2135 clear_string_option(&buf->b_p_cm);
2136#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002137 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002138#if defined(FEAT_EVAL)
2139 clear_string_option(&buf->b_p_fex);
2140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141#ifdef FEAT_CRYPT
2142 clear_string_option(&buf->b_p_key);
2143#endif
2144 clear_string_option(&buf->b_p_kp);
2145 clear_string_option(&buf->b_p_mps);
2146 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002147 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 clear_string_option(&buf->b_p_isk);
2149#ifdef FEAT_KEYMAP
2150 clear_string_option(&buf->b_p_keymap);
Bram Moolenaar50138322018-01-28 17:05:16 +01002151 keymap_clear(&buf->b_kmap_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 ga_clear(&buf->b_kmap_ga);
2153#endif
2154#ifdef FEAT_COMMENTS
2155 clear_string_option(&buf->b_p_com);
2156#endif
2157#ifdef FEAT_FOLDING
2158 clear_string_option(&buf->b_p_cms);
2159#endif
2160 clear_string_option(&buf->b_p_nf);
2161#ifdef FEAT_SYN_HL
2162 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002163 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002164#endif
2165#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002166 clear_string_option(&buf->b_s.b_p_spc);
2167 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002168 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002169 buf->b_s.b_cap_prog = NULL;
2170 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171#endif
2172#ifdef FEAT_SEARCHPATH
2173 clear_string_option(&buf->b_p_sua);
2174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 clear_string_option(&buf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176#ifdef FEAT_CINDENT
2177 clear_string_option(&buf->b_p_cink);
2178 clear_string_option(&buf->b_p_cino);
2179#endif
2180#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
2181 clear_string_option(&buf->b_p_cinw);
2182#endif
2183#ifdef FEAT_INS_EXPAND
2184 clear_string_option(&buf->b_p_cpt);
2185#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002186#ifdef FEAT_COMPL_FUNC
2187 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002188 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002189#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190#ifdef FEAT_QUICKFIX
2191 clear_string_option(&buf->b_p_gp);
2192 clear_string_option(&buf->b_p_mp);
2193 clear_string_option(&buf->b_p_efm);
2194#endif
2195 clear_string_option(&buf->b_p_ep);
2196 clear_string_option(&buf->b_p_path);
2197 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002198 clear_string_option(&buf->b_p_tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199#ifdef FEAT_INS_EXPAND
2200 clear_string_option(&buf->b_p_dict);
2201 clear_string_option(&buf->b_p_tsr);
2202#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002203#ifdef FEAT_TEXTOBJ
2204 clear_string_option(&buf->b_p_qe);
2205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002207 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002208#ifdef FEAT_LISP
2209 clear_string_option(&buf->b_p_lw);
2210#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002211 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002212#ifdef FEAT_MBYTE
2213 clear_string_option(&buf->b_p_menc);
2214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215}
2216
2217/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002218 * Get alternate file "n".
2219 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2220 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 * if (options & GETF_SETMARK) call setpcmark()
2222 * if (options & GETF_ALT) we are jumping to an alternate file.
2223 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2224 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002225 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 */
2227 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002228buflist_getfile(
2229 int n,
2230 linenr_T lnum,
2231 int options,
2232 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233{
2234 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 pos_T *fpos;
2237 colnr_T col;
2238
2239 buf = buflist_findnr(n);
2240 if (buf == NULL)
2241 {
2242 if ((options & GETF_ALT) && n == 0)
2243 EMSG(_(e_noalt));
2244 else
2245 EMSGN(_("E92: Buffer %ld not found"), n);
2246 return FAIL;
2247 }
2248
2249 /* if alternate file is the current buffer, nothing to do */
2250 if (buf == curbuf)
2251 return OK;
2252
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002253 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002254 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002255 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002257 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002258 if (curbuf_locked())
2259 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260
2261 /* altfpos may be changed by getfile(), get it now */
2262 if (lnum == 0)
2263 {
2264 fpos = buflist_findfpos(buf);
2265 lnum = fpos->lnum;
2266 col = fpos->col;
2267 }
2268 else
2269 col = 0;
2270
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 if (options & GETF_SWITCH)
2272 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002273 /* If 'switchbuf' contains "useopen": jump to first window containing
2274 * "buf" if one exists */
2275 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002277
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002278 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00002279 * page containing "buf" if one exists */
2280 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002281 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002282
2283 /* If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2284 * current buffer isn't empty: open new tab or window */
2285 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002286 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002288 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002289 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002290 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2291 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002293 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 }
2295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296
2297 ++RedrawingDisabled;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002298 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2299 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 {
2301 --RedrawingDisabled;
2302
2303 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
2304 if (!p_sol && col != 0)
2305 {
2306 curwin->w_cursor.col = col;
2307 check_cursor_col();
2308#ifdef FEAT_VIRTUALEDIT
2309 curwin->w_cursor.coladd = 0;
2310#endif
2311 curwin->w_set_curswant = TRUE;
2312 }
2313 return OK;
2314 }
2315 --RedrawingDisabled;
2316 return FAIL;
2317}
2318
2319/*
2320 * go to the last know line number for the current buffer
2321 */
2322 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002323buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324{
2325 pos_T *fpos;
2326
2327 fpos = buflist_findfpos(curbuf);
2328
2329 curwin->w_cursor.lnum = fpos->lnum;
2330 check_cursor_lnum();
2331
2332 if (p_sol)
2333 curwin->w_cursor.col = 0;
2334 else
2335 {
2336 curwin->w_cursor.col = fpos->col;
2337 check_cursor_col();
2338#ifdef FEAT_VIRTUALEDIT
2339 curwin->w_cursor.coladd = 0;
2340#endif
2341 curwin->w_set_curswant = TRUE;
2342 }
2343}
2344
Bram Moolenaar81695252004-12-29 20:58:21 +00002345#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2346/*
2347 * Find file in buffer list by name (it has to be for the current window).
2348 * Returns NULL if not found.
2349 */
2350 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002351buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002352{
2353 char_u *ffname;
2354 buf_T *buf = NULL;
2355
2356 /* First make the name into a full path name */
2357 ffname = FullName_save(fname,
2358#ifdef UNIX
2359 TRUE /* force expansion, get rid of symbolic links */
2360#else
2361 FALSE
2362#endif
2363 );
2364 if (ffname != NULL)
2365 {
2366 buf = buflist_findname(ffname);
2367 vim_free(ffname);
2368 }
2369 return buf;
2370}
2371#endif
2372
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373/*
2374 * Find file in buffer list by name (it has to be for the current window).
2375 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002376 * Skips dummy buffers.
2377 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 */
2379 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002380buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381{
2382#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002383 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384
2385 if (mch_stat((char *)ffname, &st) < 0)
2386 st.st_dev = (dev_T)-1;
2387 return buflist_findname_stat(ffname, &st);
2388}
2389
2390/*
2391 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2392 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002393 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 */
2395 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002396buflist_findname_stat(
2397 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002398 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399{
2400#endif
2401 buf_T *buf;
2402
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002403 /* Start at the last buffer, expect to find a match sooner. */
2404 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar81695252004-12-29 20:58:21 +00002405 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406#ifdef UNIX
2407 , stp
2408#endif
2409 ))
2410 return buf;
2411 return NULL;
2412}
2413
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002414#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) \
2415 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416/*
2417 * Find file in buffer list by a regexp pattern.
2418 * Return fnum of the found buffer.
2419 * Return < 0 for error.
2420 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002422buflist_findpat(
2423 char_u *pattern,
2424 char_u *pattern_end, /* pointer to first char after pattern */
2425 int unlisted, /* find unlisted buffers */
2426 int diffmode UNUSED, /* find diff-mode buffers only */
2427 int curtab_only) /* find buffers in current tab only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428{
2429 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 int match = -1;
2431 int find_listed;
2432 char_u *pat;
2433 char_u *patend;
2434 int attempt;
2435 char_u *p;
2436 int toggledollar;
2437
2438 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2439 {
2440 if (*pattern == '%')
2441 match = curbuf->b_fnum;
2442 else
2443 match = curwin->w_alt_fnum;
2444#ifdef FEAT_DIFF
2445 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2446 match = -1;
2447#endif
2448 }
2449
2450 /*
2451 * Try four ways of matching a listed buffer:
2452 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002453 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 * attempt == 2: with '$' at end (only match at end)
2455 * attempt == 3: with '^' at start and '$' at end (only full match)
2456 * Repeat this for finding an unlisted buffer if there was no matching
2457 * listed buffer.
2458 */
2459 else
2460 {
2461 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2462 if (pat == NULL)
2463 return -1;
2464 patend = pat + STRLEN(pat) - 1;
2465 toggledollar = (patend > pat && *patend == '$');
2466
2467 /* First try finding a listed buffer. If not found and "unlisted"
2468 * is TRUE, try finding an unlisted buffer. */
2469 find_listed = TRUE;
2470 for (;;)
2471 {
2472 for (attempt = 0; attempt <= 3; ++attempt)
2473 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002474 regmatch_T regmatch;
2475
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 /* may add '^' and '$' */
2477 if (toggledollar)
2478 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2479 p = pat;
2480 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2481 ++p;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002482 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2483 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 {
2485 vim_free(pat);
2486 return -1;
2487 }
2488
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002489 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 if (buf->b_p_bl == find_listed
2491#ifdef FEAT_DIFF
2492 && (!diffmode || diff_mode_buf(buf))
2493#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002494 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002496 if (curtab_only)
2497 {
2498 /* Ignore the match if the buffer is not open in
2499 * the current tab. */
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002500 win_T *wp;
2501
Bram Moolenaar29323592016-07-24 22:04:11 +02002502 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002503 if (wp->w_buffer == buf)
2504 break;
2505 if (wp == NULL)
2506 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 if (match >= 0) /* already found a match */
2509 {
2510 match = -2;
2511 break;
2512 }
2513 match = buf->b_fnum; /* remember first match */
2514 }
2515
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002516 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 if (match >= 0) /* found one match */
2518 break;
2519 }
2520
2521 /* Only search for unlisted buffers if there was no match with
2522 * a listed buffer. */
2523 if (!unlisted || !find_listed || match != -1)
2524 break;
2525 find_listed = FALSE;
2526 }
2527
2528 vim_free(pat);
2529 }
2530
2531 if (match == -2)
2532 EMSG2(_("E93: More than one match for %s"), pattern);
2533 else if (match < 0)
2534 EMSG2(_("E94: No matching buffer for %s"), pattern);
2535 return match;
2536}
2537#endif
2538
2539#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2540
2541/*
2542 * Find all buffer names that match.
2543 * For command line expansion of ":buf" and ":sbuf".
2544 * Return OK if matches found, FAIL otherwise.
2545 */
2546 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002547ExpandBufnames(
2548 char_u *pat,
2549 int *num_file,
2550 char_u ***file,
2551 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552{
2553 int count = 0;
2554 buf_T *buf;
2555 int round;
2556 char_u *p;
2557 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002558 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559
2560 *num_file = 0; /* return values in case of FAIL */
2561 *file = NULL;
2562
Bram Moolenaar05159a02005-02-26 23:04:13 +00002563 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2564 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002566 patc = alloc((unsigned)STRLEN(pat) + 11);
2567 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002569 STRCPY(patc, "\\(^\\|[\\/]\\)");
2570 STRCPY(patc + 11, pat + 1);
2571 }
2572 else
2573 patc = pat;
2574
2575 /*
2576 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002577 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002578 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002579 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002580 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002581 regmatch_T regmatch;
2582
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002583 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002584 break; /* there was no anchor, no need to try again */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002585 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2586 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002587 {
2588 if (patc != pat)
2589 vim_free(patc);
2590 return FAIL;
2591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592
2593 /*
2594 * round == 1: Count the matches.
2595 * round == 2: Build the array to keep the matches.
2596 */
2597 for (round = 1; round <= 2; ++round)
2598 {
2599 count = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002600 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 {
2602 if (!buf->b_p_bl) /* skip unlisted buffers */
2603 continue;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002604 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 if (p != NULL)
2606 {
2607 if (round == 1)
2608 ++count;
2609 else
2610 {
2611 if (options & WILD_HOME_REPLACE)
2612 p = home_replace_save(buf, p);
2613 else
2614 p = vim_strsave(p);
2615 (*file)[count++] = p;
2616 }
2617 }
2618 }
2619 if (count == 0) /* no match found, break here */
2620 break;
2621 if (round == 1)
2622 {
2623 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2624 if (*file == NULL)
2625 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002626 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002627 if (patc != pat)
2628 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 return FAIL;
2630 }
2631 }
2632 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002633 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 if (count) /* match(es) found, break here */
2635 break;
2636 }
2637
Bram Moolenaar05159a02005-02-26 23:04:13 +00002638 if (patc != pat)
2639 vim_free(patc);
2640
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 *num_file = count;
2642 return (count == 0 ? FAIL : OK);
2643}
2644
2645#endif /* FEAT_CMDL_COMPL */
2646
2647#ifdef HAVE_BUFLIST_MATCH
2648/*
2649 * Check for a match on the file name for buffer "buf" with regprog "prog".
2650 */
2651 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002652buflist_match(
2653 regmatch_T *rmp,
2654 buf_T *buf,
2655 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656{
2657 char_u *match;
2658
2659 /* First try the short file name, then the long file name. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002660 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002662 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663
2664 return match;
2665}
2666
2667/*
2668 * Try matching the regexp in "prog" with file name "name".
2669 * Return "name" when there is a match, NULL when not.
2670 */
2671 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002672fname_match(
2673 regmatch_T *rmp,
2674 char_u *name,
2675 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676{
2677 char_u *match = NULL;
2678 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679
2680 if (name != NULL)
2681 {
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002682 /* Ignore case when 'fileignorecase' or the argument is set. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002683 rmp->rm_ic = p_fic || ignore_case;
2684 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 match = name;
2686 else
2687 {
2688 /* Replace $(HOME) with '~' and try matching again. */
2689 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002690 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 match = name;
2692 vim_free(p);
2693 }
2694 }
2695
2696 return match;
2697}
2698#endif
2699
2700/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02002701 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 */
2703 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002704buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705{
Bram Moolenaar480778b2016-07-14 22:09:39 +02002706 char_u key[VIM_SIZEOF_INT * 2 + 1];
2707 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708
2709 if (nr == 0)
2710 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02002711 sprintf((char *)key, "%x", nr);
2712 hi = hash_find(&buf_hashtab, key);
2713
2714 if (!HASHITEM_EMPTY(hi))
2715 return (buf_T *)(hi->hi_key
2716 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 return NULL;
2718}
2719
2720/*
2721 * Get name of file 'n' in the buffer list.
2722 * When the file has no name an empty string is returned.
2723 * home_replace() is used to shorten the file name (used for marks).
2724 * Returns a pointer to allocated memory, of NULL when failed.
2725 */
2726 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002727buflist_nr2name(
2728 int n,
2729 int fullname,
2730 int helptail) /* for help buffers return tail only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731{
2732 buf_T *buf;
2733
2734 buf = buflist_findnr(n);
2735 if (buf == NULL)
2736 return NULL;
2737 return home_replace_save(helptail ? buf : NULL,
2738 fullname ? buf->b_ffname : buf->b_fname);
2739}
2740
2741/*
2742 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2743 * When "copy_options" is TRUE save the local window option values.
2744 * When "lnum" is 0 only do the options.
2745 */
2746 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002747buflist_setfpos(
2748 buf_T *buf,
2749 win_T *win,
2750 linenr_T lnum,
2751 colnr_T col,
2752 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753{
2754 wininfo_T *wip;
2755
2756 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2757 if (wip->wi_win == win)
2758 break;
2759 if (wip == NULL)
2760 {
2761 /* allocate a new entry */
2762 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2763 if (wip == NULL)
2764 return;
2765 wip->wi_win = win;
2766 if (lnum == 0) /* set lnum even when it's 0 */
2767 lnum = 1;
2768 }
2769 else
2770 {
2771 /* remove the entry from the list */
2772 if (wip->wi_prev)
2773 wip->wi_prev->wi_next = wip->wi_next;
2774 else
2775 buf->b_wininfo = wip->wi_next;
2776 if (wip->wi_next)
2777 wip->wi_next->wi_prev = wip->wi_prev;
2778 if (copy_options && wip->wi_optset)
2779 {
2780 clear_winopt(&wip->wi_opt);
2781#ifdef FEAT_FOLDING
2782 deleteFoldRecurse(&wip->wi_folds);
2783#endif
2784 }
2785 }
2786 if (lnum != 0)
2787 {
2788 wip->wi_fpos.lnum = lnum;
2789 wip->wi_fpos.col = col;
2790 }
2791 if (copy_options)
2792 {
2793 /* Save the window-specific option values. */
2794 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2795#ifdef FEAT_FOLDING
2796 wip->wi_fold_manual = win->w_fold_manual;
2797 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2798#endif
2799 wip->wi_optset = TRUE;
2800 }
2801
2802 /* insert the entry in front of the list */
2803 wip->wi_next = buf->b_wininfo;
2804 buf->b_wininfo = wip;
2805 wip->wi_prev = NULL;
2806 if (wip->wi_next)
2807 wip->wi_next->wi_prev = wip;
2808
2809 return;
2810}
2811
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002812#ifdef FEAT_DIFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01002813static int wininfo_other_tab_diff(wininfo_T *wip);
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002814
2815/*
2816 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2817 * page. That's because a diff is local to a tab page.
2818 */
2819 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002820wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002821{
2822 win_T *wp;
2823
2824 if (wip->wi_opt.wo_diff)
2825 {
Bram Moolenaar29323592016-07-24 22:04:11 +02002826 FOR_ALL_WINDOWS(wp)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002827 /* return FALSE when it's a window in the current tab page, thus
2828 * the buffer was in diff mode here */
2829 if (wip->wi_win == wp)
2830 return FALSE;
2831 return TRUE;
2832 }
2833 return FALSE;
2834}
2835#endif
2836
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837/*
2838 * Find info for the current window in buffer "buf".
2839 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002840 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2841 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 * Returns NULL when there isn't any info.
2843 */
2844 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002845find_wininfo(
2846 buf_T *buf,
2847 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848{
2849 wininfo_T *wip;
2850
2851 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002852 if (wip->wi_win == curwin
2853#ifdef FEAT_DIFF
2854 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2855#endif
2856 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002858
2859 /* If no wininfo for curwin, use the first in the list (that doesn't have
2860 * 'diff' set and is in another tab page). */
2861 if (wip == NULL)
2862 {
2863#ifdef FEAT_DIFF
2864 if (skip_diff_buffer)
2865 {
2866 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2867 if (!wininfo_other_tab_diff(wip))
2868 break;
2869 }
2870 else
2871#endif
2872 wip = buf->b_wininfo;
2873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 return wip;
2875}
2876
2877/*
2878 * Reset the local window options to the values last used in this window.
2879 * If the buffer wasn't used in this window before, use the values from
2880 * the most recently used window. If the values were never set, use the
2881 * global values for the window.
2882 */
2883 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002884get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885{
2886 wininfo_T *wip;
2887
2888 clear_winopt(&curwin->w_onebuf_opt);
2889#ifdef FEAT_FOLDING
2890 clearFolding(curwin);
2891#endif
2892
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002893 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 if (wip != NULL && wip->wi_optset)
2895 {
2896 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2897#ifdef FEAT_FOLDING
2898 curwin->w_fold_manual = wip->wi_fold_manual;
2899 curwin->w_foldinvalid = TRUE;
2900 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2901#endif
2902 }
2903 else
2904 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2905
2906#ifdef FEAT_FOLDING
2907 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2908 if (p_fdls >= 0)
2909 curwin->w_p_fdl = p_fdls;
2910#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002911#ifdef FEAT_SYN_HL
2912 check_colorcolumn(curwin);
2913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914}
2915
2916/*
2917 * Find the position (lnum and col) for the buffer 'buf' for the current
2918 * window.
2919 * Returns a pointer to no_position if no position is found.
2920 */
2921 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002922buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923{
2924 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002925 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002927 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 if (wip != NULL)
2929 return &(wip->wi_fpos);
2930 else
2931 return &no_position;
2932}
2933
2934/*
2935 * Find the lnum for the buffer 'buf' for the current window.
2936 */
2937 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01002938buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939{
2940 return buflist_findfpos(buf)->lnum;
2941}
2942
2943#if defined(FEAT_LISTCMDS) || defined(PROTO)
2944/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002945 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002948buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949{
2950 buf_T *buf;
2951 int len;
2952 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02002953 int ro_char;
2954 int changed_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955
2956 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2957 {
2958 /* skip unlisted buffers, unless ! was used */
Bram Moolenaard51cb702015-07-21 15:03:06 +02002959 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
2960 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
2961 || (vim_strchr(eap->arg, '+')
2962 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
2963 || (vim_strchr(eap->arg, 'a')
2964 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
2965 || (vim_strchr(eap->arg, 'h')
2966 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
2967 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
2968 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
2969 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
2970 || (vim_strchr(eap->arg, '%') && buf != curbuf)
2971 || (vim_strchr(eap->arg, '#')
2972 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02002975 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 else
2977 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02002978 if (message_filtered(NameBuff))
2979 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980
Bram Moolenaar304b64c2017-08-13 20:43:48 +02002981 changed_char = (buf->b_flags & BF_READERR) ? 'x'
2982 : (bufIsChanged(buf) ? '+' : ' ');
2983#ifdef FEAT_TERMINAL
2984 if (term_job_running(buf->b_term))
2985 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02002986 if (term_none_open(buf->b_term))
2987 ro_char = '?';
2988 else
2989 ro_char = 'R';
Bram Moolenaar304b64c2017-08-13 20:43:48 +02002990 changed_char = ' '; /* bufIsChanged() returns TRUE to avoid
2991 * closing, but it's not actually changed. */
2992 }
2993 else if (buf->b_term != NULL)
2994 ro_char = 'F';
2995 else
2996#endif
2997 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
2998
Bram Moolenaar77401ad2016-08-24 00:12:12 +02002999 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00003000 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 buf->b_fnum,
3002 buf->b_p_bl ? ' ' : 'u',
3003 buf == curbuf ? '%' :
3004 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3005 buf->b_ml.ml_mfp == NULL ? ' ' :
3006 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003007 ro_char,
3008 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003009 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003010 if (len > IOSIZE - 20)
3011 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012
3013 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 i = 40 - vim_strsize(IObuff);
3015 do
3016 {
3017 IObuff[len++] = ' ';
3018 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003019 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3020 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003021 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 msg_outtrans(IObuff);
3023 out_flush(); /* output one line at a time */
3024 ui_breakcheck();
3025 }
3026}
3027#endif
3028
3029/*
3030 * Get file name and line number for file 'fnum'.
3031 * Used by DoOneCmd() for translating '%' and '#'.
3032 * Used by insert_reg() and cmdline_paste() for '#' register.
3033 * Return FAIL if not found, OK for success.
3034 */
3035 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003036buflist_name_nr(
3037 int fnum,
3038 char_u **fname,
3039 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040{
3041 buf_T *buf;
3042
3043 buf = buflist_findnr(fnum);
3044 if (buf == NULL || buf->b_fname == NULL)
3045 return FAIL;
3046
3047 *fname = buf->b_fname;
3048 *lnum = buflist_findlnum(buf);
3049
3050 return OK;
3051}
3052
3053/*
3054 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
3055 * The file name with the full path is also remembered, for when :cd is used.
3056 * Returns FAIL for failure (file name already in use by other buffer)
3057 * OK otherwise.
3058 */
3059 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003060setfname(
3061 buf_T *buf,
3062 char_u *ffname,
3063 char_u *sfname,
3064 int message) /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065{
Bram Moolenaar81695252004-12-29 20:58:21 +00003066 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003068 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069#endif
3070
3071 if (ffname == NULL || *ffname == NUL)
3072 {
3073 /* Removing the name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01003074 VIM_CLEAR(buf->b_ffname);
3075 VIM_CLEAR(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076#ifdef UNIX
3077 st.st_dev = (dev_T)-1;
3078#endif
3079 }
3080 else
3081 {
3082 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
3083 if (ffname == NULL) /* out of memory */
3084 return FAIL;
3085
3086 /*
3087 * if the file name is already used in another buffer:
3088 * - if the buffer is loaded, fail
3089 * - if the buffer is not loaded, delete it from the list
3090 */
3091#ifdef UNIX
3092 if (mch_stat((char *)ffname, &st) < 0)
3093 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003094#endif
3095 if (!(buf->b_flags & BF_DUMMY))
3096#ifdef UNIX
3097 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003099 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100#endif
3101 if (obuf != NULL && obuf != buf)
3102 {
3103 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
3104 {
3105 if (message)
3106 EMSG(_("E95: Buffer with this name already exists"));
3107 vim_free(ffname);
3108 return FAIL;
3109 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01003110 /* delete from the list */
3111 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 }
3113 sfname = vim_strsave(sfname);
3114 if (ffname == NULL || sfname == NULL)
3115 {
3116 vim_free(sfname);
3117 vim_free(ffname);
3118 return FAIL;
3119 }
3120#ifdef USE_FNAME_CASE
3121# ifdef USE_LONG_FNAME
3122 if (USE_LONG_FNAME)
3123# endif
3124 fname_case(sfname, 0); /* set correct case for short file name */
3125#endif
3126 vim_free(buf->b_ffname);
3127 vim_free(buf->b_sfname);
3128 buf->b_ffname = ffname;
3129 buf->b_sfname = sfname;
3130 }
3131 buf->b_fname = buf->b_sfname;
3132#ifdef UNIX
3133 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003134 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 else
3136 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003137 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 buf->b_dev = st.st_dev;
3139 buf->b_ino = st.st_ino;
3140 }
3141#endif
3142
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144
3145 buf_name_changed(buf);
3146 return OK;
3147}
3148
3149/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003150 * Crude way of changing the name of a buffer. Use with care!
3151 * The name should be relative to the current directory.
3152 */
3153 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003154buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003155{
3156 buf_T *buf;
3157
3158 buf = buflist_findnr(fnum);
3159 if (buf != NULL)
3160 {
3161 vim_free(buf->b_sfname);
3162 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003163 buf->b_ffname = vim_strsave(name);
3164 buf->b_sfname = NULL;
3165 /* Allocate ffname and expand into full path. Also resolves .lnk
3166 * files on Win32. */
3167 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003168 buf->b_fname = buf->b_sfname;
3169 }
3170}
3171
3172/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 * Take care of what needs to be done when the name of buffer "buf" has
3174 * changed.
3175 */
3176 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003177buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178{
3179 /*
3180 * If the file name changed, also change the name of the swapfile
3181 */
3182 if (buf->b_ml.ml_mfp != NULL)
3183 ml_setname(buf);
3184
3185 if (curwin->w_buffer == buf)
3186 check_arg_idx(curwin); /* check file name for arg list */
3187#ifdef FEAT_TITLE
3188 maketitle(); /* set window title */
3189#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 status_redraw_all(); /* status lines need to be redrawn */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 fmarks_check_names(buf); /* check named file marks */
3192 ml_timestamp(buf); /* reset timestamp */
3193}
3194
3195/*
3196 * set alternate file name for current window
3197 *
3198 * Used by do_one_cmd(), do_write() and do_ecmd().
3199 * Return the buffer.
3200 */
3201 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003202setaltfname(
3203 char_u *ffname,
3204 char_u *sfname,
3205 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206{
3207 buf_T *buf;
3208
3209 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
3210 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003211 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 curwin->w_alt_fnum = buf->b_fnum;
3213 return buf;
3214}
3215
3216/*
3217 * Get alternate file name for current window.
3218 * Return NULL if there isn't any, and give error message if requested.
3219 */
3220 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003221getaltfname(
3222 int errmsg) /* give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223{
3224 char_u *fname;
3225 linenr_T dummy;
3226
3227 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3228 {
3229 if (errmsg)
3230 EMSG(_(e_noalt));
3231 return NULL;
3232 }
3233 return fname;
3234}
3235
3236/*
3237 * Add a file name to the buflist and return its number.
3238 * Uses same flags as buflist_new(), except BLN_DUMMY.
3239 *
3240 * used by qf_init(), main() and doarglist()
3241 */
3242 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003243buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244{
3245 buf_T *buf;
3246
3247 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3248 if (buf != NULL)
3249 return buf->b_fnum;
3250 return 0;
3251}
3252
3253#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3254/*
3255 * Adjust slashes in file names. Called after 'shellslash' was set.
3256 */
3257 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003258buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259{
3260 buf_T *bp;
3261
Bram Moolenaar29323592016-07-24 22:04:11 +02003262 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 {
3264 if (bp->b_ffname != NULL)
3265 slash_adjust(bp->b_ffname);
3266 if (bp->b_sfname != NULL)
3267 slash_adjust(bp->b_sfname);
3268 }
3269}
3270#endif
3271
3272/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003273 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 * Also save the local window option values.
3275 */
3276 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003277buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003279 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280}
3281
3282/*
3283 * Return TRUE if 'ffname' is not the same file as current file.
3284 * Fname must have a full path (expanded by mch_FullName()).
3285 */
3286 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003287otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288{
3289 return otherfile_buf(curbuf, ffname
3290#ifdef UNIX
3291 , NULL
3292#endif
3293 );
3294}
3295
3296 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003297otherfile_buf(
3298 buf_T *buf,
3299 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003301 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003303 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304{
3305 /* no name is different */
3306 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3307 return TRUE;
3308 if (fnamecmp(ffname, buf->b_ffname) == 0)
3309 return FALSE;
3310#ifdef UNIX
3311 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003312 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313
Bram Moolenaar8767f522016-07-01 17:17:39 +02003314 /* If no stat_T given, get it now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 if (stp == NULL)
3316 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003317 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 st.st_dev = (dev_T)-1;
3319 stp = &st;
3320 }
3321 /* Use dev/ino to check if the files are the same, even when the names
3322 * are different (possible with links). Still need to compare the
3323 * name above, for when the file doesn't exist yet.
3324 * Problem: The dev/ino changes when a file is deleted (and created
3325 * again) and remains the same when renamed/moved. We don't want to
3326 * mch_stat() each buffer each time, that would be too slow. Get the
3327 * dev/ino again when they appear to match, but not when they appear
3328 * to be different: Could skip a buffer when it's actually the same
3329 * file. */
3330 if (buf_same_ino(buf, stp))
3331 {
3332 buf_setino(buf);
3333 if (buf_same_ino(buf, stp))
3334 return FALSE;
3335 }
3336 }
3337#endif
3338 return TRUE;
3339}
3340
3341#if defined(UNIX) || defined(PROTO)
3342/*
3343 * Set inode and device number for a buffer.
3344 * Must always be called when b_fname is changed!.
3345 */
3346 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003347buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003349 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350
3351 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3352 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003353 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 buf->b_dev = st.st_dev;
3355 buf->b_ino = st.st_ino;
3356 }
3357 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003358 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359}
3360
3361/*
3362 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3363 */
3364 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003365buf_same_ino(
3366 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003367 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003369 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 && stp->st_dev == buf->b_dev
3371 && stp->st_ino == buf->b_ino);
3372}
3373#endif
3374
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003375/*
3376 * Print info about the current buffer.
3377 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003379fileinfo(
3380 int fullname, /* when non-zero print full path */
3381 int shorthelp,
3382 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383{
3384 char_u *name;
3385 int n;
3386 char_u *p;
3387 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003388 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389
3390 buffer = alloc(IOSIZE);
3391 if (buffer == NULL)
3392 return;
3393
3394 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3395 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003396 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 p = buffer + STRLEN(buffer);
3398 }
3399 else
3400 p = buffer;
3401
3402 *p++ = '"';
3403 if (buf_spname(curbuf) != NULL)
Bram Moolenaarec3cfeb2012-10-03 17:12:47 +02003404 vim_strncpy(p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 else
3406 {
3407 if (!fullname && curbuf->b_fname != NULL)
3408 name = curbuf->b_fname;
3409 else
3410 name = curbuf->b_ffname;
3411 home_replace(shorthelp ? curbuf : NULL, name, p,
3412 (int)(IOSIZE - (p - buffer)), TRUE);
3413 }
3414
Bram Moolenaara800b422010-06-27 01:15:55 +02003415 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 curbufIsChanged() ? (shortmess(SHM_MOD)
3417 ? " [+]" : _(" [Modified]")) : " ",
3418 (curbuf->b_flags & BF_NOTEDITED)
3419#ifdef FEAT_QUICKFIX
3420 && !bt_dontwrite(curbuf)
3421#endif
3422 ? _("[Not edited]") : "",
3423 (curbuf->b_flags & BF_NEW)
3424#ifdef FEAT_QUICKFIX
3425 && !bt_dontwrite(curbuf)
3426#endif
3427 ? _("[New file]") : "",
3428 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003429 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 : _("[readonly]")) : "",
3431 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3432 || curbuf->b_p_ro) ?
3433 " " : "");
3434 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3435 * causes an overflow, thus for large numbers divide instead. */
3436 if (curwin->w_cursor.lnum > 1000000L)
3437 n = (int)(((long)curwin->w_cursor.lnum) /
3438 ((long)curbuf->b_ml.ml_line_count / 100L));
3439 else
3440 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3441 (long)curbuf->b_ml.ml_line_count);
3442 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3443 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003444 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 }
3446#ifdef FEAT_CMDL_INFO
3447 else if (p_ru)
3448 {
3449 /* Current line and column are already on the screen -- webb */
3450 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003451 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003453 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 (long)curbuf->b_ml.ml_line_count, n);
3455 }
3456#endif
3457 else
3458 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003459 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 _("line %ld of %ld --%d%%-- col "),
3461 (long)curwin->w_cursor.lnum,
3462 (long)curbuf->b_ml.ml_line_count,
3463 n);
3464 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003465 len = STRLEN(buffer);
3466 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3468 }
3469
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003470 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471
3472 if (dont_truncate)
3473 {
3474 /* Temporarily set msg_scroll to avoid the message being truncated.
3475 * First call msg_start() to get the message in the right place. */
3476 msg_start();
3477 n = msg_scroll;
3478 msg_scroll = TRUE;
3479 msg(buffer);
3480 msg_scroll = n;
3481 }
3482 else
3483 {
3484 p = msg_trunc_attr(buffer, FALSE, 0);
3485 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 /* Need to repeat the message after redrawing when:
3487 * - When restart_edit is set (otherwise there will be a delay
3488 * before redrawing).
3489 * - When the screen was scrolled but there is no wait-return
3490 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003491 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 }
3493
3494 vim_free(buffer);
3495}
3496
3497 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003498col_print(
3499 char_u *buf,
3500 size_t buflen,
3501 int col,
3502 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503{
3504 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003505 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003507 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508}
3509
3510#if defined(FEAT_TITLE) || defined(PROTO)
3511/*
3512 * put file name in title bar of window and in icon title
3513 */
3514
3515static char_u *lasttitle = NULL;
3516static char_u *lasticon = NULL;
3517
3518 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003519maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520{
3521 char_u *p;
3522 char_u *t_str = NULL;
3523 char_u *i_name;
3524 char_u *i_str = NULL;
3525 int maxlen = 0;
3526 int len;
3527 int mustset;
3528 char_u buf[IOSIZE];
3529 int off;
3530
3531 if (!redrawing())
3532 {
3533 /* Postpone updating the title when 'lazyredraw' is set. */
3534 need_maketitle = TRUE;
3535 return;
3536 }
3537
3538 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003539 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 return;
3541
3542 if (p_title)
3543 {
3544 if (p_titlelen > 0)
3545 {
3546 maxlen = p_titlelen * Columns / 100;
3547 if (maxlen < 10)
3548 maxlen = 10;
3549 }
3550
3551 t_str = buf;
3552 if (*p_titlestring != NUL)
3553 {
3554#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003555 if (stl_syntax & STL_IN_TITLE)
3556 {
3557 int use_sandbox = FALSE;
3558 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003559
3560# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003561 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003562# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003563 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003565 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003566 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003567 if (called_emsg)
3568 set_string_option_direct((char_u *)"titlestring", -1,
3569 (char_u *)"", OPT_FREE, SID_ERROR);
3570 called_emsg |= save_called_emsg;
3571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 else
3573#endif
3574 t_str = p_titlestring;
3575 }
3576 else
3577 {
3578 /* format: "fname + (path) (1 of 2) - VIM" */
3579
Bram Moolenaar2c666692012-09-05 13:30:40 +02003580#define SPACE_FOR_FNAME (IOSIZE - 100)
3581#define SPACE_FOR_DIR (IOSIZE - 20)
3582#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003584 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar21554412017-07-24 21:44:43 +02003585#ifdef FEAT_TERMINAL
3586 else if (curbuf->b_term != NULL)
3587 {
3588 vim_strncpy(buf, term_get_status_text(curbuf->b_term),
3589 SPACE_FOR_FNAME);
3590 }
3591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 else
3593 {
3594 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003595 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 }
3598
Bram Moolenaar21554412017-07-24 21:44:43 +02003599#ifdef FEAT_TERMINAL
3600 if (curbuf->b_term == NULL)
3601#endif
3602 switch (bufIsChanged(curbuf)
3603 + (curbuf->b_p_ro * 2)
3604 + (!curbuf->b_p_ma * 4))
3605 {
3606 case 1: STRCAT(buf, " +"); break;
3607 case 2: STRCAT(buf, " ="); break;
3608 case 3: STRCAT(buf, " =+"); break;
3609 case 4:
3610 case 6: STRCAT(buf, " -"); break;
3611 case 5:
3612 case 7: STRCAT(buf, " -+"); break;
3613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614
Bram Moolenaar21554412017-07-24 21:44:43 +02003615 if (curbuf->b_fname != NULL
3616#ifdef FEAT_TERMINAL
3617 && curbuf->b_term == NULL
3618#endif
3619 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 {
3621 /* Get path of file, replace home dir with ~ */
3622 off = (int)STRLEN(buf);
3623 buf[off++] = ' ';
3624 buf[off++] = '(';
3625 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003626 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627#ifdef BACKSLASH_IN_FILENAME
3628 /* avoid "c:/name" to be reduced to "c" */
3629 if (isalpha(buf[off]) && buf[off + 1] == ':')
3630 off += 2;
3631#endif
3632 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003633 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 if (p == buf + off)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003635 {
Bram Moolenaar21554412017-07-24 21:44:43 +02003636 /* must be a help buffer */
3637 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003638 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003642
Bram Moolenaar2c666692012-09-05 13:30:40 +02003643 /* Translate unprintable chars and concatenate. Keep some
3644 * room for the server name. When there is no room (very long
3645 * file name) use (...). */
3646 if (off < SPACE_FOR_DIR)
3647 {
3648 p = transstr(buf + off);
3649 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3650 vim_free(p);
3651 }
3652 else
3653 {
3654 vim_strncpy(buf + off, (char_u *)"...",
3655 (size_t)(SPACE_FOR_ARGNR - off));
3656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 STRCAT(buf, ")");
3658 }
3659
Bram Moolenaar2c666692012-09-05 13:30:40 +02003660 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661
3662#if defined(FEAT_CLIENTSERVER)
3663 if (serverName != NULL)
3664 {
3665 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003666 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 }
3668 else
3669#endif
3670 STRCAT(buf, " - VIM");
3671
3672 if (maxlen > 0)
3673 {
3674 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003675 if (vim_strsize(buf) > maxlen)
3676 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 }
3678 }
3679 }
3680 mustset = ti_change(t_str, &lasttitle);
3681
3682 if (p_icon)
3683 {
3684 i_str = buf;
3685 if (*p_iconstring != NUL)
3686 {
3687#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003688 if (stl_syntax & STL_IN_ICON)
3689 {
3690 int use_sandbox = FALSE;
3691 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003692
3693# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003694 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003695# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003696 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003698 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003699 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003700 if (called_emsg)
3701 set_string_option_direct((char_u *)"iconstring", -1,
3702 (char_u *)"", OPT_FREE, SID_ERROR);
3703 called_emsg |= save_called_emsg;
3704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 else
3706#endif
3707 i_str = p_iconstring;
3708 }
3709 else
3710 {
3711 if (buf_spname(curbuf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003712 i_name = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 else /* use file name only in icon */
3714 i_name = gettail(curbuf->b_ffname);
3715 *i_str = NUL;
3716 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003717 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 if (len > 100)
3719 {
3720 len -= 100;
3721#ifdef FEAT_MBYTE
3722 if (has_mbyte)
3723 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3724#endif
3725 i_name += len;
3726 }
3727 STRCPY(i_str, i_name);
3728 trans_characters(i_str, IOSIZE);
3729 }
3730 }
3731
3732 mustset |= ti_change(i_str, &lasticon);
3733
3734 if (mustset)
3735 resettitle();
3736}
3737
3738/*
3739 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3740 * from "str" if it does.
3741 * Return TRUE when "*last" changed.
3742 */
3743 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003744ti_change(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745{
3746 if ((str == NULL) != (*last == NULL)
3747 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3748 {
3749 vim_free(*last);
3750 if (str == NULL)
3751 *last = NULL;
3752 else
3753 *last = vim_strsave(str);
3754 return TRUE;
3755 }
3756 return FALSE;
3757}
3758
3759/*
3760 * Put current window title back (used after calling a shell)
3761 */
3762 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003763resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764{
3765 mch_settitle(lasttitle, lasticon);
3766}
Bram Moolenaarea408852005-06-25 22:49:46 +00003767
3768# if defined(EXITFREE) || defined(PROTO)
3769 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003770free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00003771{
3772 vim_free(lasttitle);
3773 vim_free(lasticon);
3774}
3775# endif
3776
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777#endif /* FEAT_TITLE */
3778
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003779#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003781 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 * Return length of string in screen cells.
3783 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003784 * Normally works for window "wp", except when working for 'tabline' then it
3785 * is "curwin".
3786 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 * Items are drawn interspersed with the text that surrounds it
3788 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3789 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3790 *
3791 * If maxwidth is not zero, the string will be filled at any middle marker
3792 * or truncated if too long, fillchar is used for all whitespace.
3793 */
3794 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003795build_stl_str_hl(
3796 win_T *wp,
3797 char_u *out, /* buffer to write into != NameBuff */
3798 size_t outlen, /* length of out[] */
3799 char_u *fmt,
3800 int use_sandbox UNUSED, /* "fmt" was set insecurely, use sandbox */
3801 int fillchar,
3802 int maxwidth,
3803 struct stl_hlrec *hltab, /* return: HL attributes (can be NULL) */
3804 struct stl_hlrec *tabtab) /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805{
3806 char_u *p;
3807 char_u *s;
3808 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003809 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810#ifdef FEAT_EVAL
Bram Moolenaarba2929b2017-09-08 13:59:21 +02003811 win_T *save_curwin;
3812 buf_T *save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813#endif
3814 int empty_line;
3815 colnr_T virtcol;
3816 long l;
3817 long n;
3818 int prevchar_isflag;
3819 int prevchar_isitem;
3820 int itemisflag;
3821 int fillable;
3822 char_u *str;
3823 long num;
3824 int width;
3825 int itemcnt;
3826 int curitem;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02003827 int group_end_userhl;
3828 int group_start_userhl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 int groupitem[STL_MAX_ITEM];
3830 int groupdepth;
3831 struct stl_item
3832 {
3833 char_u *start;
3834 int minwid;
3835 int maxwid;
3836 enum
3837 {
3838 Normal,
3839 Empty,
3840 Group,
3841 Middle,
3842 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003843 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 Trunc
3845 } type;
3846 } item[STL_MAX_ITEM];
3847 int minwid;
3848 int maxwid;
3849 int zeropad;
3850 char_u base;
3851 char_u opt;
3852#define TMPLEN 70
3853 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003854 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003855 struct stl_hlrec *sp;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02003856 int save_must_redraw = must_redraw;
3857 int save_redr_type = curwin->w_redr_type;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003858
3859#ifdef FEAT_EVAL
3860 /*
3861 * When the format starts with "%!" then evaluate it as an expression and
3862 * use the result as the actual format string.
3863 */
3864 if (fmt[0] == '%' && fmt[1] == '!')
3865 {
3866 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3867 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003868 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003869 }
3870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871
3872 if (fillchar == 0)
3873 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003874#ifdef FEAT_MBYTE
3875 /* Can't handle a multi-byte fill character yet. */
3876 else if (mb_char2len(fillchar) > 1)
3877 fillchar = '-';
3878#endif
3879
Bram Moolenaar567199b2013-04-24 16:52:36 +02003880 /* Get line & check if empty (cursorpos will show "0-1"). Note that
3881 * p will become invalid when getting another buffer line. */
3882 p = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3883 empty_line = (*p == NUL);
3884
3885 /* Get the byte value now, in case we need it below. This is more
3886 * efficient than making a copy of the line. */
3887 if (wp->w_cursor.col > (colnr_T)STRLEN(p))
3888 byteval = 0;
3889 else
3890#ifdef FEAT_MBYTE
3891 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
3892#else
3893 byteval = p[wp->w_cursor.col];
3894#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895
3896 groupdepth = 0;
3897 p = out;
3898 curitem = 0;
3899 prevchar_isflag = TRUE;
3900 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003901 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003903 if (curitem == STL_MAX_ITEM)
3904 {
3905 /* There are too many items. Add the error code to the statusline
3906 * to give the user a hint about what went wrong. */
3907 if (p + 6 < out + outlen)
3908 {
3909 mch_memmove(p, " E541", (size_t)5);
3910 p += 5;
3911 }
3912 break;
3913 }
3914
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 if (*s != NUL && *s != '%')
3916 prevchar_isflag = prevchar_isitem = FALSE;
3917
3918 /*
3919 * Handle up to the next '%' or the end.
3920 */
3921 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3922 *p++ = *s++;
3923 if (*s == NUL || p + 1 >= out + outlen)
3924 break;
3925
3926 /*
3927 * Handle one '%' item.
3928 */
3929 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003930 if (*s == NUL) /* ignore trailing % */
3931 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 if (*s == '%')
3933 {
3934 if (p + 1 >= out + outlen)
3935 break;
3936 *p++ = *s++;
3937 prevchar_isflag = prevchar_isitem = FALSE;
3938 continue;
3939 }
3940 if (*s == STL_MIDDLEMARK)
3941 {
3942 s++;
3943 if (groupdepth > 0)
3944 continue;
3945 item[curitem].type = Middle;
3946 item[curitem++].start = p;
3947 continue;
3948 }
3949 if (*s == STL_TRUNCMARK)
3950 {
3951 s++;
3952 item[curitem].type = Trunc;
3953 item[curitem++].start = p;
3954 continue;
3955 }
3956 if (*s == ')')
3957 {
3958 s++;
3959 if (groupdepth < 1)
3960 continue;
3961 groupdepth--;
3962
3963 t = item[groupitem[groupdepth]].start;
3964 *p = NUL;
3965 l = vim_strsize(t);
3966 if (curitem > groupitem[groupdepth] + 1
3967 && item[groupitem[groupdepth]].minwid == 0)
3968 {
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02003969 /* remove group if all items are empty and highlight group
3970 * doesn't change */
3971 group_start_userhl = group_end_userhl = 0;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02003972 for (n = groupitem[groupdepth] - 1; n >= 0; n--)
3973 {
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02003974 if (item[n].type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02003975 {
3976 group_start_userhl = group_end_userhl = item[n].minwid;
3977 break;
3978 }
3979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02003981 {
3982 if (item[n].type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 break;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02003984 if (item[n].type == Highlight)
3985 group_end_userhl = item[n].minwid;
3986 }
3987 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 {
3989 p = t;
3990 l = 0;
3991 }
3992 }
3993 if (l > item[groupitem[groupdepth]].maxwid)
3994 {
3995 /* truncate, remove n bytes of text at the start */
3996#ifdef FEAT_MBYTE
3997 if (has_mbyte)
3998 {
3999 /* Find the first character that should be included. */
4000 n = 0;
4001 while (l >= item[groupitem[groupdepth]].maxwid)
4002 {
4003 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004004 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 }
4006 }
4007 else
4008#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004009 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010
4011 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004012 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 p = p - n + 1;
4014#ifdef FEAT_MBYTE
4015 /* Fill up space left over by half a double-wide char. */
4016 while (++l < item[groupitem[groupdepth]].minwid)
4017 *p++ = fillchar;
4018#endif
4019
4020 /* correct the start of the items for the truncation */
4021 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
4022 {
4023 item[l].start -= n;
4024 if (item[l].start < t)
4025 item[l].start = t;
4026 }
4027 }
4028 else if (abs(item[groupitem[groupdepth]].minwid) > l)
4029 {
4030 /* fill */
4031 n = item[groupitem[groupdepth]].minwid;
4032 if (n < 0)
4033 {
4034 /* fill by appending characters */
4035 n = 0 - n;
4036 while (l++ < n && p + 1 < out + outlen)
4037 *p++ = fillchar;
4038 }
4039 else
4040 {
4041 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004042 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 l = n - l;
4044 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004045 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 p += l;
4047 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
4048 item[n].start += l;
4049 for ( ; l > 0; l--)
4050 *t++ = fillchar;
4051 }
4052 }
4053 continue;
4054 }
4055 minwid = 0;
4056 maxwid = 9999;
4057 zeropad = FALSE;
4058 l = 1;
4059 if (*s == '0')
4060 {
4061 s++;
4062 zeropad = TRUE;
4063 }
4064 if (*s == '-')
4065 {
4066 s++;
4067 l = -1;
4068 }
4069 if (VIM_ISDIGIT(*s))
4070 {
4071 minwid = (int)getdigits(&s);
4072 if (minwid < 0) /* overflow */
4073 minwid = 0;
4074 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004075 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 {
4077 item[curitem].type = Highlight;
4078 item[curitem].start = p;
4079 item[curitem].minwid = minwid > 9 ? 1 : minwid;
4080 s++;
4081 curitem++;
4082 continue;
4083 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004084 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4085 {
4086 if (*s == STL_TABCLOSENR)
4087 {
4088 if (minwid == 0)
4089 {
4090 /* %X ends the close label, go back to the previously
4091 * define tab label nr. */
4092 for (n = curitem - 1; n >= 0; --n)
4093 if (item[n].type == TabPage && item[n].minwid >= 0)
4094 {
4095 minwid = item[n].minwid;
4096 break;
4097 }
4098 }
4099 else
4100 /* close nrs are stored as negative values */
4101 minwid = - minwid;
4102 }
4103 item[curitem].type = TabPage;
4104 item[curitem].start = p;
4105 item[curitem].minwid = minwid;
4106 s++;
4107 curitem++;
4108 continue;
4109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 if (*s == '.')
4111 {
4112 s++;
4113 if (VIM_ISDIGIT(*s))
4114 {
4115 maxwid = (int)getdigits(&s);
4116 if (maxwid <= 0) /* overflow */
4117 maxwid = 50;
4118 }
4119 }
4120 minwid = (minwid > 50 ? 50 : minwid) * l;
4121 if (*s == '(')
4122 {
4123 groupitem[groupdepth++] = curitem;
4124 item[curitem].type = Group;
4125 item[curitem].start = p;
4126 item[curitem].minwid = minwid;
4127 item[curitem].maxwid = maxwid;
4128 s++;
4129 curitem++;
4130 continue;
4131 }
4132 if (vim_strchr(STL_ALL, *s) == NULL)
4133 {
4134 s++;
4135 continue;
4136 }
4137 opt = *s++;
4138
4139 /* OK - now for the real work */
4140 base = 'D';
4141 itemisflag = FALSE;
4142 fillable = TRUE;
4143 num = -1;
4144 str = NULL;
4145 switch (opt)
4146 {
4147 case STL_FILEPATH:
4148 case STL_FULLPATH:
4149 case STL_FILENAME:
4150 fillable = FALSE; /* don't change ' ' to fillchar */
4151 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004152 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 else
4154 {
4155 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004156 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4158 }
4159 trans_characters(NameBuff, MAXPATHL);
4160 if (opt != STL_FILENAME)
4161 str = NameBuff;
4162 else
4163 str = gettail(NameBuff);
4164 break;
4165
4166 case STL_VIM_EXPR: /* '{' */
4167 itemisflag = TRUE;
4168 t = p;
4169 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
4170 *p++ = *s++;
4171 if (*s != '}') /* missing '}' or out of space */
4172 break;
4173 s++;
4174 *p = 0;
4175 p = t;
4176
4177#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004178 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 set_internal_string_var((char_u *)"actual_curbuf", tmp);
4180
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004181 save_curbuf = curbuf;
4182 save_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 curwin = wp;
4184 curbuf = wp->w_buffer;
4185
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004186 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004188 curwin = save_curwin;
4189 curbuf = save_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00004190 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191
4192 if (str != NULL && *str != 0)
4193 {
4194 if (*skipdigits(str) == NUL)
4195 {
4196 num = atoi((char *)str);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004197 VIM_CLEAR(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 itemisflag = FALSE;
4199 }
4200 }
4201#endif
4202 break;
4203
4204 case STL_LINE:
4205 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4206 ? 0L : (long)(wp->w_cursor.lnum);
4207 break;
4208
4209 case STL_NUMLINES:
4210 num = wp->w_buffer->b_ml.ml_line_count;
4211 break;
4212
4213 case STL_COLUMN:
4214 num = !(State & INSERT) && empty_line
4215 ? 0 : (int)wp->w_cursor.col + 1;
4216 break;
4217
4218 case STL_VIRTCOL:
4219 case STL_VIRTCOL_ALT:
4220 /* In list mode virtcol needs to be recomputed */
4221 virtcol = wp->w_virtcol;
4222 if (wp->w_p_list && lcs_tab1 == NUL)
4223 {
4224 wp->w_p_list = FALSE;
4225 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
4226 wp->w_p_list = TRUE;
4227 }
4228 ++virtcol;
4229 /* Don't display %V if it's the same as %c. */
4230 if (opt == STL_VIRTCOL_ALT
4231 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
4232 ? 0 : (int)wp->w_cursor.col + 1)))
4233 break;
4234 num = (long)virtcol;
4235 break;
4236
4237 case STL_PERCENTAGE:
4238 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4239 (long)wp->w_buffer->b_ml.ml_line_count);
4240 break;
4241
4242 case STL_ALTPERCENT:
4243 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004244 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 break;
4246
4247 case STL_ARGLISTSTAT:
4248 fillable = FALSE;
4249 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004250 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 str = tmp;
4252 break;
4253
4254 case STL_KEYMAP:
4255 fillable = FALSE;
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02004256 if (get_keymap_str(wp, (char_u *)"<%s>", tmp, TMPLEN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 str = tmp;
4258 break;
4259 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004260#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004261 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262#else
4263 num = 0;
4264#endif
4265 break;
4266
4267 case STL_BUFNO:
4268 num = wp->w_buffer->b_fnum;
4269 break;
4270
4271 case STL_OFFSET_X:
4272 base = 'X';
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004273 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 case STL_OFFSET:
4275#ifdef FEAT_BYTEOFF
4276 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4277 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4278 0L : l + 1 + (!(State & INSERT) && empty_line ?
4279 0 : (int)wp->w_cursor.col);
4280#endif
4281 break;
4282
4283 case STL_BYTEVAL_X:
4284 base = 'X';
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004285 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004287 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 if (num == NL)
4289 num = 0;
4290 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4291 num = NL;
4292 break;
4293
4294 case STL_ROFLAG:
4295 case STL_ROFLAG_ALT:
4296 itemisflag = TRUE;
4297 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004298 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 break;
4300
4301 case STL_HELPFLAG:
4302 case STL_HELPFLAG_ALT:
4303 itemisflag = TRUE;
4304 if (wp->w_buffer->b_help)
4305 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004306 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 break;
4308
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 case STL_FILETYPE:
4310 if (*wp->w_buffer->b_p_ft != NUL
4311 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4312 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004313 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
4314 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 str = tmp;
4316 }
4317 break;
4318
4319 case STL_FILETYPE_ALT:
4320 itemisflag = TRUE;
4321 if (*wp->w_buffer->b_p_ft != NUL
4322 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4323 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004324 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
4325 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 for (t = tmp; *t != 0; t++)
4327 *t = TOUPPER_LOC(*t);
4328 str = tmp;
4329 }
4330 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331
Bram Moolenaar4033c552017-09-16 20:54:51 +02004332#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 case STL_PREVIEWFLAG:
4334 case STL_PREVIEWFLAG_ALT:
4335 itemisflag = TRUE;
4336 if (wp->w_p_pvw)
4337 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4338 : _("[Preview]"));
4339 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004340
4341 case STL_QUICKFIX:
4342 if (bt_quickfix(wp->w_buffer))
4343 str = (char_u *)(wp->w_llist_ref
4344 ? _(msg_loclist)
4345 : _(msg_qflist));
4346 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347#endif
4348
4349 case STL_MODIFIED:
4350 case STL_MODIFIED_ALT:
4351 itemisflag = TRUE;
4352 switch ((opt == STL_MODIFIED_ALT)
4353 + bufIsChanged(wp->w_buffer) * 2
4354 + (!wp->w_buffer->b_p_ma) * 4)
4355 {
4356 case 2: str = (char_u *)"[+]"; break;
4357 case 3: str = (char_u *)",+"; break;
4358 case 4: str = (char_u *)"[-]"; break;
4359 case 5: str = (char_u *)",-"; break;
4360 case 6: str = (char_u *)"[+-]"; break;
4361 case 7: str = (char_u *)",+-"; break;
4362 }
4363 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004364
4365 case STL_HIGHLIGHT:
4366 t = s;
4367 while (*s != '#' && *s != NUL)
4368 ++s;
4369 if (*s == '#')
4370 {
4371 item[curitem].type = Highlight;
4372 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004373 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004374 curitem++;
4375 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004376 if (*s != NUL)
4377 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004378 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 }
4380
4381 item[curitem].start = p;
4382 item[curitem].type = Normal;
4383 if (str != NULL && *str)
4384 {
4385 t = str;
4386 if (itemisflag)
4387 {
4388 if ((t[0] && t[1])
4389 && ((!prevchar_isitem && *t == ',')
4390 || (prevchar_isflag && *t == ' ')))
4391 t++;
4392 prevchar_isflag = TRUE;
4393 }
4394 l = vim_strsize(t);
4395 if (l > 0)
4396 prevchar_isitem = TRUE;
4397 if (l > maxwid)
4398 {
4399 while (l >= maxwid)
4400#ifdef FEAT_MBYTE
4401 if (has_mbyte)
4402 {
4403 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004404 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 }
4406 else
4407#endif
4408 l -= byte2cells(*t++);
4409 if (p + 1 >= out + outlen)
4410 break;
4411 *p++ = '<';
4412 }
4413 if (minwid > 0)
4414 {
4415 for (; l < minwid && p + 1 < out + outlen; l++)
4416 {
4417 /* Don't put a "-" in front of a digit. */
4418 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4419 *p++ = ' ';
4420 else
4421 *p++ = fillchar;
4422 }
4423 minwid = 0;
4424 }
4425 else
4426 minwid *= -1;
4427 while (*t && p + 1 < out + outlen)
4428 {
4429 *p++ = *t++;
4430 /* Change a space by fillchar, unless fillchar is '-' and a
4431 * digit follows. */
4432 if (fillable && p[-1] == ' '
4433 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4434 p[-1] = fillchar;
4435 }
4436 for (; l < minwid && p + 1 < out + outlen; l++)
4437 *p++ = fillchar;
4438 }
4439 else if (num >= 0)
4440 {
4441 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4442 char_u nstr[20];
4443
4444 if (p + 20 >= out + outlen)
4445 break; /* not sufficient space */
4446 prevchar_isitem = TRUE;
4447 t = nstr;
4448 if (opt == STL_VIRTCOL_ALT)
4449 {
4450 *t++ = '-';
4451 minwid--;
4452 }
4453 *t++ = '%';
4454 if (zeropad)
4455 *t++ = '0';
4456 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004457 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 *t = 0;
4459
4460 for (n = num, l = 1; n >= nbase; n /= nbase)
4461 l++;
4462 if (opt == STL_VIRTCOL_ALT)
4463 l++;
4464 if (l > maxwid)
4465 {
4466 l += 2;
4467 n = l - maxwid;
4468 while (l-- > maxwid)
4469 num /= nbase;
4470 *t++ = '>';
4471 *t++ = '%';
4472 *t = t[-3];
4473 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004474 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4475 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 }
4477 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004478 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4479 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 p += STRLEN(p);
4481 }
4482 else
4483 item[curitem].type = Empty;
4484
4485 if (opt == STL_VIM_EXPR)
4486 vim_free(str);
4487
4488 if (num >= 0 || (!itemisflag && str && *str))
4489 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4490 curitem++;
4491 }
4492 *p = NUL;
4493 itemcnt = curitem;
4494
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004495#ifdef FEAT_EVAL
4496 if (usefmt != fmt)
4497 vim_free(usefmt);
4498#endif
4499
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 width = vim_strsize(out);
4501 if (maxwidth > 0 && width > maxwidth)
4502 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004503 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 l = 0;
4505 if (itemcnt == 0)
4506 s = out;
4507 else
4508 {
4509 for ( ; l < itemcnt; l++)
4510 if (item[l].type == Trunc)
4511 {
4512 /* Truncate at %< item. */
4513 s = item[l].start;
4514 break;
4515 }
4516 if (l == itemcnt)
4517 {
4518 /* No %< item, truncate first item. */
4519 s = item[0].start;
4520 l = 0;
4521 }
4522 }
4523
4524 if (width - vim_strsize(s) >= maxwidth)
4525 {
4526 /* Truncation mark is beyond max length */
4527#ifdef FEAT_MBYTE
4528 if (has_mbyte)
4529 {
4530 s = out;
4531 width = 0;
4532 for (;;)
4533 {
4534 width += ptr2cells(s);
4535 if (width >= maxwidth)
4536 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004537 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 }
4539 /* Fill up for half a double-wide character. */
4540 while (++width < maxwidth)
4541 *s++ = fillchar;
4542 }
4543 else
4544#endif
4545 s = out + maxwidth - 1;
4546 for (l = 0; l < itemcnt; l++)
4547 if (item[l].start > s)
4548 break;
4549 itemcnt = l;
4550 *s++ = '>';
4551 *s = 0;
4552 }
4553 else
4554 {
4555#ifdef FEAT_MBYTE
4556 if (has_mbyte)
4557 {
4558 n = 0;
4559 while (width >= maxwidth)
4560 {
4561 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004562 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 }
4564 }
4565 else
4566#endif
4567 n = width - maxwidth + 1;
4568 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004569 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 *s = '<';
4571
4572 /* Fill up for half a double-wide character. */
4573 while (++width < maxwidth)
4574 {
4575 s = s + STRLEN(s);
4576 *s++ = fillchar;
4577 *s = NUL;
4578 }
4579
4580 --n; /* count the '<' */
4581 for (; l < itemcnt; l++)
4582 {
4583 if (item[l].start - n >= s)
4584 item[l].start -= n;
4585 else
4586 item[l].start = s;
4587 }
4588 }
4589 width = maxwidth;
4590 }
4591 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4592 {
4593 /* Apply STL_MIDDLE if any */
4594 for (l = 0; l < itemcnt; l++)
4595 if (item[l].type == Middle)
4596 break;
4597 if (l < itemcnt)
4598 {
4599 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004600 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 for (s = item[l].start; s < p; s++)
4602 *s = fillchar;
4603 for (l++; l < itemcnt; l++)
4604 item[l].start += maxwidth - width;
4605 width = maxwidth;
4606 }
4607 }
4608
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004609 /* Store the info about highlighting. */
4610 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004612 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 for (l = 0; l < itemcnt; l++)
4614 {
4615 if (item[l].type == Highlight)
4616 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004617 sp->start = item[l].start;
4618 sp->userhl = item[l].minwid;
4619 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 }
4621 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004622 sp->start = NULL;
4623 sp->userhl = 0;
4624 }
4625
4626 /* Store the info about tab pages labels. */
4627 if (tabtab != NULL)
4628 {
4629 sp = tabtab;
4630 for (l = 0; l < itemcnt; l++)
4631 {
4632 if (item[l].type == TabPage)
4633 {
4634 sp->start = item[l].start;
4635 sp->userhl = item[l].minwid;
4636 sp++;
4637 }
4638 }
4639 sp->start = NULL;
4640 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 }
4642
Bram Moolenaar65ed1362017-09-30 16:00:14 +02004643 /* When inside update_screen we do not want redrawing a stausline, ruler,
4644 * title, etc. to trigger another redraw, it may cause an endless loop. */
4645 if (updating_screen)
4646 {
4647 must_redraw = save_must_redraw;
4648 curwin->w_redr_type = save_redr_type;
4649 }
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004650
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 return width;
4652}
4653#endif /* FEAT_STL_OPT */
4654
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004655#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4656 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004658 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4659 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 */
4661 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004662get_rel_pos(
4663 win_T *wp,
4664 char_u *buf,
4665 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666{
4667 long above; /* number of lines above window */
4668 long below; /* number of lines below window */
4669
Bram Moolenaar0027c212015-01-07 13:31:52 +01004670 if (buflen < 3) /* need at least 3 chars for writing */
4671 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 above = wp->w_topline - 1;
4673#ifdef FEAT_DIFF
4674 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02004675 if (wp->w_topline == 1 && wp->w_topfill >= 1)
4676 above = 0; /* All buffer lines are displayed and there is an
4677 * indication of filler lines, that can be considered
4678 * seeing all lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679#endif
4680 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4681 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004682 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4683 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004685 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004687 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 ? (int)(above / ((above + below) / 100L))
4689 : (int)(above * 100L / (above + below)));
4690}
4691#endif
4692
4693/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004694 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 * Return TRUE if it was appended.
4696 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004697 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004698append_arg_number(
4699 win_T *wp,
4700 char_u *buf,
4701 int buflen,
4702 int add_file) /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703{
4704 char_u *p;
4705
4706 if (ARGCOUNT <= 1) /* nothing to do */
4707 return FALSE;
4708
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004709 p = buf + STRLEN(buf); /* go to the end of the buffer */
4710 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 return FALSE;
4712 *p++ = ' ';
4713 *p++ = '(';
4714 if (add_file)
4715 {
4716 STRCPY(p, "file ");
4717 p += 5;
4718 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004719 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4720 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4722 return TRUE;
4723}
4724
4725/*
4726 * If fname is not a full path, make it a full path.
4727 * Returns pointer to allocated memory (NULL for failure).
4728 */
4729 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004730fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731{
4732 /*
4733 * Force expanding the path always for Unix, because symbolic links may
4734 * mess up the full path name, even though it starts with a '/'.
4735 * Also expand when there is ".." in the file name, try to remove it,
4736 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004737 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 * For MS-Windows also expand names like "longna~1" to "longname".
4739 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004740#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 return FullName_save(fname, TRUE);
4742#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004743 if (!vim_isAbsName(fname)
4744 || strstr((char *)fname, "..") != NULL
4745 || strstr((char *)fname, "//") != NULL
4746# ifdef BACKSLASH_IN_FILENAME
4747 || strstr((char *)fname, "\\\\") != NULL
4748# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004749# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004751# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 )
4753 return FullName_save(fname, FALSE);
4754
4755 fname = vim_strsave(fname);
4756
Bram Moolenaar9b942202007-10-03 12:31:33 +00004757# ifdef USE_FNAME_CASE
4758# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004760# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 {
4762 if (fname != NULL)
4763 fname_case(fname, 0); /* set correct case for file name */
4764 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004765# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766
4767 return fname;
4768#endif
4769}
4770
4771/*
4772 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4773 * "ffname" becomes a pointer to allocated memory (or NULL).
4774 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004776fname_expand(
4777 buf_T *buf UNUSED,
4778 char_u **ffname,
4779 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780{
4781 if (*ffname == NULL) /* if no file name given, nothing to do */
4782 return;
4783 if (*sfname == NULL) /* if no short file name given, use ffname */
4784 *sfname = *ffname;
4785 *ffname = fix_fname(*ffname); /* expand to full path */
4786
4787#ifdef FEAT_SHORTCUT
4788 if (!buf->b_p_bin)
4789 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004790 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791
4792 /* If the file name is a shortcut file, use the file it links to. */
4793 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004794 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 {
4796 vim_free(*ffname);
4797 *ffname = rfname;
4798 *sfname = rfname;
4799 }
4800 }
4801#endif
4802}
4803
4804/*
4805 * Get the file name for an argument list entry.
4806 */
4807 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004808alist_name(aentry_T *aep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809{
4810 buf_T *bp;
4811
4812 /* Use the name from the associated buffer if it exists. */
4813 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004814 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 return aep->ae_fname;
4816 return bp->b_fname;
4817}
4818
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819/*
4820 * do_arg_all(): Open up to 'count' windows, one for each argument.
4821 */
4822 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004823do_arg_all(
4824 int count,
4825 int forceit, /* hide buffers in current windows */
4826 int keep_tabs) /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827{
4828 int i;
4829 win_T *wp, *wpnext;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004830 char_u *opened; /* Array of weight for which args are open:
4831 * 0: not opened
4832 * 1: opened in other tab
4833 * 2: opened in curtab
4834 * 3: opened in curtab and curwin
4835 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004836 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 int use_firstwin = FALSE; /* use first window for arglist */
4838 int split_ret = OK;
4839 int p_ea_save;
4840 alist_T *alist; /* argument list to be used */
4841 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004842 tabpage_T *tpnext;
4843 int had_tab = cmdmod.tab;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004844 win_T *old_curwin, *last_curwin;
4845 tabpage_T *old_curtab, *last_curtab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004846 win_T *new_curwin = NULL;
4847 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848
4849 if (ARGCOUNT <= 0)
4850 {
4851 /* Don't give an error message. We don't want it when the ":all"
4852 * command is in the .vimrc. */
4853 return;
4854 }
4855 setpcmark();
4856
4857 opened_len = ARGCOUNT;
4858 opened = alloc_clear((unsigned)opened_len);
4859 if (opened == NULL)
4860 return;
4861
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004862 /* Autocommands may do anything to the argument list. Make sure it's not
4863 * freed while we are working here by "locking" it. We still have to
4864 * watch out for its size to be changed. */
4865 alist = curwin->w_alist;
4866 ++alist->al_refcount;
4867
4868 old_curwin = curwin;
4869 old_curtab = curtab;
4870
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004871# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004873# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874
4875 /*
4876 * Try closing all windows that are not in the argument list.
4877 * Also close windows that are not full width;
4878 * When 'hidden' or "forceit" set the buffer becomes hidden.
4879 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004880 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004882 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004883 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004884 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004886 tpnext = curtab->tp_next;
4887 for (wp = firstwin; wp != NULL; wp = wpnext)
4888 {
4889 wpnext = wp->w_next;
4890 buf = wp->w_buffer;
4891 if (buf->b_ffname == NULL
Bram Moolenaar5a030a52016-12-01 17:48:29 +01004892 || (!keep_tabs && (buf->b_nwindows > 1
4893 || wp->w_width != Columns)))
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004894 i = opened_len;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004895 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004897 /* check if the buffer in this window is in the arglist */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004898 for (i = 0; i < opened_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004900 if (i < alist->al_ga.ga_len
4901 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
4902 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
4903 buf->b_ffname, TRUE) & FPC_SAME))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004905 int weight = 1;
4906
4907 if (old_curtab == curtab)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004908 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004909 ++weight;
4910 if (old_curwin == wp)
4911 ++weight;
4912 }
4913
4914 if (weight > (int)opened[i])
4915 {
4916 opened[i] = (char_u)weight;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004917 if (i == 0)
4918 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004919 if (new_curwin != NULL)
4920 new_curwin->w_arg_idx = opened_len;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004921 new_curwin = wp;
4922 new_curtab = curtab;
4923 }
4924 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004925 else if (keep_tabs)
4926 i = opened_len;
4927
4928 if (wp->w_alist != alist)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004929 {
4930 /* Use the current argument list for all windows
4931 * containing a file from it. */
4932 alist_unlink(wp->w_alist);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004933 wp->w_alist = alist;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004934 ++wp->w_alist->al_refcount;
4935 }
4936 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 }
4939 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004940 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004942 if (i == opened_len && !keep_tabs)/* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02004944 if (buf_hide(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004945 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004947 /* If the buffer was changed, and we would like to hide it,
4948 * try autowriting. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02004949 if (!buf_hide(buf) && buf->b_nwindows <= 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004950 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02004952 bufref_T bufref;
4953
4954 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004955
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004956 (void)autowrite(buf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004957
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004958 /* check if autocommands removed the window */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02004959 if (!win_valid(wp) || !bufref_valid(&bufref))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004960 {
4961 wpnext = firstwin; /* start all over... */
4962 continue;
4963 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004964 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004965 /* don't close last window */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01004966 if (ONE_WINDOW
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004967 && (first_tabpage->tp_next == NULL || !had_tab))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004968 use_firstwin = TRUE;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004969 else
4970 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02004971 win_close(wp, !buf_hide(buf) && !bufIsChanged(buf));
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004972
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004973 /* check if autocommands removed the next window */
4974 if (!win_valid(wpnext))
4975 wpnext = firstwin; /* start all over... */
Bram Moolenaar4033c552017-09-16 20:54:51 +02004976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 }
4979 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004980
4981 /* Without the ":tab" modifier only do the current tab page. */
4982 if (had_tab == 0 || tpnext == NULL)
4983 break;
4984
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004985 /* check if autocommands removed the next tab page */
4986 if (!valid_tabpage(tpnext))
4987 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004988
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004989 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 }
4991
4992 /*
4993 * Open a window for files in the argument list that don't have one.
4994 * ARGCOUNT may change while doing this, because of autocommands.
4995 */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004996 if (count > opened_len || count <= 0)
4997 count = opened_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 /* Don't execute Win/Buf Enter/Leave autocommands here. */
5000 ++autocmd_no_enter;
5001 ++autocmd_no_leave;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005002 last_curwin = curwin;
5003 last_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005005 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
5006 * leaving an empty tab page when executed locally. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005007 if (keep_tabs && BUFEMPTY() && curbuf->b_nwindows == 1
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005008 && curbuf->b_ffname == NULL && !curbuf->b_changed)
5009 use_firstwin = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005010
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005011 for (i = 0; i < count && i < opened_len && !got_int; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 {
5013 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
5014 arg_had_last = TRUE;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005015 if (opened[i] > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 {
5017 /* Move the already present window to below the current window */
5018 if (curwin->w_arg_idx != i)
5019 {
5020 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
5021 {
5022 if (wpnext->w_arg_idx == i)
5023 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005024 if (keep_tabs)
5025 {
5026 new_curwin = wpnext;
5027 new_curtab = curtab;
5028 }
5029 else
5030 win_move_after(wpnext, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 break;
5032 }
5033 }
5034 }
5035 }
5036 else if (split_ret == OK)
5037 {
5038 if (!use_firstwin) /* split current window */
5039 {
5040 p_ea_save = p_ea;
5041 p_ea = TRUE; /* use space from all windows */
5042 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5043 p_ea = p_ea_save;
5044 if (split_ret == FAIL)
5045 continue;
5046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 else /* first window: do autocmd for leaving this buffer */
5048 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049
5050 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005051 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 */
5053 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005054 if (i == 0)
5055 {
5056 new_curwin = curwin;
5057 new_curtab = curtab;
5058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
5060 ECMD_ONE,
Bram Moolenaareb44a682017-08-03 22:44:55 +02005061 ((buf_hide(curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00005063 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 if (use_firstwin)
5065 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 use_firstwin = FALSE;
5067 }
5068 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005069
5070 /* When ":tab" was used open a new tab for a new window repeatedly. */
5071 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5072 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 }
5074
5075 /* Remove the "lock" on the argument list. */
5076 alist_unlink(alist);
5077
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 --autocmd_no_enter;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005079
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005080 /* restore last referenced tabpage's curwin */
5081 if (last_curtab != new_curtab)
5082 {
5083 if (valid_tabpage(last_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005084 goto_tabpage_tp(last_curtab, TRUE, TRUE);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005085 if (win_valid(last_curwin))
5086 win_enter(last_curwin, FALSE);
5087 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005088 /* to window with first arg */
5089 if (valid_tabpage(new_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005090 goto_tabpage_tp(new_curtab, TRUE, TRUE);
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005091 if (win_valid(new_curwin))
5092 win_enter(new_curwin, FALSE);
5093
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 vim_free(opened);
5096}
5097
5098# if defined(FEAT_LISTCMDS) || defined(PROTO)
5099/*
5100 * Open a window for a number of buffers.
5101 */
5102 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005103ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104{
5105 buf_T *buf;
5106 win_T *wp, *wpnext;
5107 int split_ret = OK;
5108 int p_ea_save;
5109 int open_wins = 0;
5110 int r;
5111 int count; /* Maximum number of windows to open. */
5112 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005113 int had_tab = cmdmod.tab;
5114 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115
5116 if (eap->addr_count == 0) /* make as many windows as possible */
5117 count = 9999;
5118 else
5119 count = eap->line2; /* make as many windows as specified */
5120 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5121 all = FALSE;
5122 else
5123 all = TRUE;
5124
5125 setpcmark();
5126
5127#ifdef FEAT_GUI
5128 need_mouse_correct = TRUE;
5129#endif
5130
5131 /*
5132 * Close superfluous windows (two windows for the same buffer).
5133 * Also close windows that are not full-width.
5134 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005135 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005136 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005137 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005139 tpnext = curtab->tp_next;
5140 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005142 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005143 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005144 || ((cmdmod.split & WSP_VERT)
5145 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005146 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005147 : wp->w_width != Columns)
Bram Moolenaar4033c552017-09-16 20:54:51 +02005148 || (had_tab > 0 && wp != firstwin)) && !ONE_WINDOW
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005149 && !(wp->w_closing || wp->w_buffer->b_locked > 0))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005150 {
5151 win_close(wp, FALSE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005152 wpnext = firstwin; /* just in case an autocommand does
5153 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005154 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005155 open_wins = 0;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005156 }
5157 else
5158 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005160
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005161 /* Without the ":tab" modifier only do the current tab page. */
5162 if (had_tab == 0 || tpnext == NULL)
5163 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005164 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 }
5166
5167 /*
5168 * Go through the buffer list. When a buffer doesn't have a window yet,
5169 * open one. Otherwise move the window to the right position.
5170 * Watch out for autocommands that delete buffers or windows!
5171 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 /* Don't execute Win/Buf Enter/Leave autocommands here. */
5173 ++autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 win_enter(lastwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5177 {
5178 /* Check if this buffer needs a window */
5179 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5180 continue;
5181
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005182 if (had_tab != 0)
5183 {
5184 /* With the ":tab" modifier don't move the window. */
5185 if (buf->b_nwindows > 0)
5186 wp = lastwin; /* buffer has a window, skip it */
5187 else
5188 wp = NULL;
5189 }
5190 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005191 {
5192 /* Check if this buffer already has a window */
Bram Moolenaar29323592016-07-24 22:04:11 +02005193 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005194 if (wp->w_buffer == buf)
5195 break;
5196 /* If the buffer already has a window, move it */
5197 if (wp != NULL)
5198 win_move_after(wp, curwin);
5199 }
5200
5201 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005203 bufref_T bufref;
5204
5205 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005206
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 /* Split the window and put the buffer in it */
5208 p_ea_save = p_ea;
5209 p_ea = TRUE; /* use space from all windows */
5210 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5211 ++open_wins;
5212 p_ea = p_ea_save;
5213 if (split_ret == FAIL)
5214 continue;
5215
5216 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005217#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 swap_exists_action = SEA_DIALOG;
5219#endif
5220 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005221 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005223 /* autocommands deleted the buffer!!! */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005224#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 swap_exists_action = SEA_NONE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 break;
5228 }
Bram Moolenaare64ac772005-12-07 20:54:59 +00005229#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 if (swap_exists_action == SEA_QUIT)
5231 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005232# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005233 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005234
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005235 /* Reset the error/interrupt/exception state here so that
5236 * aborting() returns FALSE when closing a window. */
5237 enter_cleanup(&cs);
5238# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005239
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005240 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 win_close(curwin, TRUE);
5242 --open_wins;
5243 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005244 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005245
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005246# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005247 /* Restore the error/interrupt/exception state if not
5248 * discarded by a new aborting error, interrupt, or uncaught
5249 * exception. */
5250 leave_cleanup(&cs);
5251# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 }
5253 else
5254 handle_swap_exists(NULL);
5255#endif
5256 }
5257
5258 ui_breakcheck();
5259 if (got_int)
5260 {
5261 (void)vgetc(); /* only break the file loading, not the rest */
5262 break;
5263 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005264#ifdef FEAT_EVAL
5265 /* Autocommands deleted the buffer or aborted script processing!!! */
5266 if (aborting())
5267 break;
5268#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005269 /* When ":tab" was used open a new tab for a new window repeatedly. */
5270 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5271 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 --autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 win_enter(firstwin, FALSE); /* back to first window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276
5277 /*
5278 * Close superfluous windows.
5279 */
5280 for (wp = lastwin; open_wins > count; )
5281 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005282 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 || autowrite(wp->w_buffer, FALSE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 if (!win_valid(wp))
5285 {
5286 /* BufWrite Autocommands made the window invalid, start over */
5287 wp = lastwin;
5288 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005289 else if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005291 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 --open_wins;
5293 wp = lastwin;
5294 }
5295 else
5296 {
5297 wp = wp->w_prev;
5298 if (wp == NULL)
5299 break;
5300 }
5301 }
5302}
5303# endif /* FEAT_LISTCMDS */
5304
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005306static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005307
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308/*
5309 * do_modelines() - process mode lines for the current file
5310 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005311 * "flags" can be:
5312 * OPT_WINONLY only set options local to window
5313 * OPT_NOWIN don't set options local to window
5314 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 * Returns immediately if the "ml" option isn't set.
5316 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005318do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005320 linenr_T lnum;
5321 int nmlines;
5322 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323
5324 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5325 return;
5326
5327 /* Disallow recursive entry here. Can happen when executing a modeline
5328 * triggers an autocommand, which reloads modelines with a ":do". */
5329 if (entered)
5330 return;
5331
5332 ++entered;
5333 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5334 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005335 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 nmlines = 0;
5337
5338 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5339 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005340 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 nmlines = 0;
5342 --entered;
5343}
5344
5345#include "version.h" /* for version number */
5346
5347/*
5348 * chk_modeline() - check a single line for a mode string
5349 * Return FAIL if an error encountered.
5350 */
5351 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005352chk_modeline(
5353 linenr_T lnum,
5354 int flags) /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355{
5356 char_u *s;
5357 char_u *e;
5358 char_u *linecopy; /* local copy of any modeline found */
5359 int prev;
5360 int vers;
5361 int end;
5362 int retval = OK;
5363 char_u *save_sourcing_name;
5364 linenr_T save_sourcing_lnum;
5365#ifdef FEAT_EVAL
5366 scid_T save_SID;
5367#endif
5368
5369 prev = -1;
5370 for (s = ml_get(lnum); *s != NUL; ++s)
5371 {
5372 if (prev == -1 || vim_isspace(prev))
5373 {
5374 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5375 || STRNCMP(s, "vi:", (size_t)3) == 0)
5376 break;
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005377 /* Accept both "vim" and "Vim". */
5378 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 {
5380 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5381 e = s + 4;
5382 else
5383 e = s + 3;
5384 vers = getdigits(&e);
5385 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005386 && (s[0] != 'V'
5387 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 && (s[3] == ':'
5389 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5390 || (VIM_VERSION_100 < vers && s[3] == '<')
5391 || (VIM_VERSION_100 > vers && s[3] == '>')
5392 || (VIM_VERSION_100 == vers && s[3] == '=')))
5393 break;
5394 }
5395 }
5396 prev = *s;
5397 }
5398
5399 if (*s)
5400 {
5401 do /* skip over "ex:", "vi:" or "vim:" */
5402 ++s;
5403 while (s[-1] != ':');
5404
5405 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5406 if (linecopy == NULL)
5407 return FAIL;
5408
5409 save_sourcing_lnum = sourcing_lnum;
5410 save_sourcing_name = sourcing_name;
5411 sourcing_lnum = lnum; /* prepare for emsg() */
5412 sourcing_name = (char_u *)"modelines";
5413
5414 end = FALSE;
5415 while (end == FALSE)
5416 {
5417 s = skipwhite(s);
5418 if (*s == NUL)
5419 break;
5420
5421 /*
5422 * Find end of set command: ':' or end of line.
5423 * Skip over "\:", replacing it with ":".
5424 */
5425 for (e = s; *e != ':' && *e != NUL; ++e)
5426 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005427 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 if (*e == NUL)
5429 end = TRUE;
5430
5431 /*
5432 * If there is a "set" command, require a terminating ':' and
5433 * ignore the stuff after the ':'.
5434 * "vi:set opt opt opt: foo" -- foo not interpreted
5435 * "vi:opt opt opt: foo" -- foo interpreted
5436 * Accept "se" for compatibility with Elvis.
5437 */
5438 if (STRNCMP(s, "set ", (size_t)4) == 0
5439 || STRNCMP(s, "se ", (size_t)3) == 0)
5440 {
5441 if (*e != ':') /* no terminating ':'? */
5442 break;
5443 end = TRUE;
5444 s = vim_strchr(s, ' ') + 1;
5445 }
5446 *e = NUL; /* truncate the set command */
5447
5448 if (*s != NUL) /* skip over an empty "::" */
5449 {
5450#ifdef FEAT_EVAL
5451 save_SID = current_SID;
5452 current_SID = SID_MODELINE;
5453#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005454 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455#ifdef FEAT_EVAL
5456 current_SID = save_SID;
5457#endif
5458 if (retval == FAIL) /* stop if error found */
5459 break;
5460 }
5461 s = e + 1; /* advance to next part */
5462 }
5463
5464 sourcing_lnum = save_sourcing_lnum;
5465 sourcing_name = save_sourcing_name;
5466
5467 vim_free(linecopy);
5468 }
5469 return retval;
5470}
5471
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005472#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005474read_viminfo_bufferlist(
5475 vir_T *virp,
5476 int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477{
5478 char_u *tab;
5479 linenr_T lnum;
5480 colnr_T col;
5481 buf_T *buf;
5482 char_u *sfname;
5483 char_u *xline;
5484
5485 /* Handle long line and escaped characters. */
5486 xline = viminfo_readstring(virp, 1, FALSE);
5487
5488 /* don't read in if there are files on the command-line or if writing: */
5489 if (xline != NULL && !writing && ARGCOUNT == 0
5490 && find_viminfo_parameter('%') != NULL)
5491 {
5492 /* Format is: <fname> Tab <lnum> Tab <col>.
5493 * Watch out for a Tab in the file name, work from the end. */
5494 lnum = 0;
5495 col = 0;
5496 tab = vim_strrchr(xline, '\t');
5497 if (tab != NULL)
5498 {
5499 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005500 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 tab = vim_strrchr(xline, '\t');
5502 if (tab != NULL)
5503 {
5504 *tab++ = '\0';
5505 lnum = atol((char *)tab);
5506 }
5507 }
5508
5509 /* Expand "~/" in the file name at "line + 1" to a full path.
5510 * Then try shortening it by comparing with the current directory */
5511 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005512 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513
5514 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5515 if (buf != NULL) /* just in case... */
5516 {
5517 buf->b_last_cursor.lnum = lnum;
5518 buf->b_last_cursor.col = col;
5519 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5520 }
5521 }
5522 vim_free(xline);
5523
5524 return viminfo_readline(virp);
5525}
5526
5527 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005528write_viminfo_bufferlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529{
5530 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005532 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005534 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535
5536 if (find_viminfo_parameter('%') == NULL)
5537 return;
5538
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005539 /* Without a number -1 is returned: do all buffers. */
5540 max_buffers = get_viminfo_parameter('%');
5541
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005543#define LINE_BUF_LEN (MAXPATHL + 40)
5544 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 if (line == NULL)
5546 return;
5547
Bram Moolenaarf740b292006-02-16 22:11:02 +00005548 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 set_last_cursor(win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005551 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar29323592016-07-24 22:04:11 +02005552 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 {
5554 if (buf->b_fname == NULL
5555 || !buf->b_p_bl
5556#ifdef FEAT_QUICKFIX
5557 || bt_quickfix(buf)
5558#endif
Bram Moolenaare6278052017-08-13 18:11:17 +02005559#ifdef FEAT_TERMINAL
5560 || bt_terminal(buf)
5561#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 || removable(buf->b_ffname))
5563 continue;
5564
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005565 if (max_buffers-- == 0)
5566 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 putc('%', fp);
5568 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005569 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 (long)buf->b_last_cursor.lnum,
5571 buf->b_last_cursor.col);
5572 viminfo_writestring(fp, line);
5573 }
5574 vim_free(line);
5575}
5576#endif
5577
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005578/*
5579 * Return TRUE if "buf" is the quickfix buffer.
5580 */
5581 int
5582bt_quickfix(buf_T *buf)
5583{
5584 return buf != NULL && buf->b_p_bt[0] == 'q';
5585}
5586
5587/*
5588 * Return TRUE if "buf" is a terminal buffer.
5589 */
5590 int
5591bt_terminal(buf_T *buf)
5592{
5593 return buf != NULL && buf->b_p_bt[0] == 't';
5594}
5595
5596/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005597 * Return TRUE if "buf" is a help buffer.
5598 */
5599 int
5600bt_help(buf_T *buf)
5601{
5602 return buf != NULL && buf->b_help;
5603}
5604
5605/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005606 * Return TRUE if "buf" is a "nofile", "acwrite" or "terminal" buffer.
5607 * This means the buffer name is not a file name.
5608 */
5609 int
5610bt_nofile(buf_T *buf)
5611{
5612 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5613 || buf->b_p_bt[0] == 'a'
5614 || buf->b_p_bt[0] == 't');
5615}
5616
5617/*
5618 * Return TRUE if "buf" is a "nowrite", "nofile" or "terminal" buffer.
5619 */
5620 int
5621bt_dontwrite(buf_T *buf)
5622{
5623 return buf != NULL && (buf->b_p_bt[0] == 'n' || buf->b_p_bt[0] == 't');
5624}
5625
5626 int
5627bt_dontwrite_msg(buf_T *buf)
5628{
5629 if (bt_dontwrite(buf))
5630 {
5631 EMSG(_("E382: Cannot write, 'buftype' option is set"));
5632 return TRUE;
5633 }
5634 return FALSE;
5635}
5636
5637/*
5638 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
5639 * and 'bufhidden'.
5640 */
5641 int
5642buf_hide(buf_T *buf)
5643{
5644 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
5645 switch (buf->b_p_bh[0])
5646 {
5647 case 'u': /* "unload" */
5648 case 'w': /* "wipe" */
5649 case 'd': return FALSE; /* "delete" */
5650 case 'h': return TRUE; /* "hide" */
5651 }
5652 return (p_hid || cmdmod.hide);
5653}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654
5655/*
5656 * Return special buffer name.
5657 * Returns NULL when the buffer has a normal file name.
5658 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005659 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005660buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661{
Bram Moolenaar4033c552017-09-16 20:54:51 +02005662#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005664 {
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005665 win_T *win;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005666 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005667
5668 /*
5669 * For location list window, w_llist_ref points to the location list.
5670 * For quickfix window, w_llist_ref is NULL.
5671 */
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005672 if (find_win_for_buf(buf, &win, &tp) == OK && win->w_llist_ref != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005673 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005674 else
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005675 return (char_u *)_(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02005678
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 /* There is no _file_ when 'buftype' is "nofile", b_sfname
Bram Moolenaar21554412017-07-24 21:44:43 +02005680 * contains the name as specified by the user. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 if (bt_nofile(buf))
5682 {
Bram Moolenaar21554412017-07-24 21:44:43 +02005683#ifdef FEAT_TERMINAL
5684 if (buf->b_term != NULL)
5685 return term_get_status_text(buf->b_term);
5686#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02005687 if (buf->b_fname != NULL)
5688 return buf->b_fname;
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005689 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 }
Bram Moolenaar21554412017-07-24 21:44:43 +02005691
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005693 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 return NULL;
5695}
5696
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02005697#if defined(FEAT_JOB_CHANNEL) \
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005698 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5699 || defined(PROTO)
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02005700# define SWITCH_TO_WIN
5701
5702/*
5703 * Find a window that contains "buf" and switch to it.
5704 * If there is no such window, use the current window and change "curbuf".
5705 * Caller must initialize save_curbuf to NULL.
5706 * restore_win_for_buf() MUST be called later!
5707 */
5708 void
5709switch_to_win_for_buf(
5710 buf_T *buf,
5711 win_T **save_curwinp,
5712 tabpage_T **save_curtabp,
5713 bufref_T *save_curbuf)
5714{
5715 win_T *wp;
5716 tabpage_T *tp;
5717
5718 if (find_win_for_buf(buf, &wp, &tp) == FAIL)
5719 switch_buffer(save_curbuf, buf);
5720 else if (switch_win(save_curwinp, save_curtabp, wp, tp, TRUE) == FAIL)
5721 {
5722 restore_win(*save_curwinp, *save_curtabp, TRUE);
5723 switch_buffer(save_curbuf, buf);
5724 }
5725}
5726
5727 void
5728restore_win_for_buf(
5729 win_T *save_curwin,
5730 tabpage_T *save_curtab,
5731 bufref_T *save_curbuf)
5732{
5733 if (save_curbuf->br_buf == NULL)
5734 restore_win(save_curwin, save_curtab, TRUE);
5735 else
5736 restore_buffer(save_curbuf);
5737}
5738#endif
5739
Bram Moolenaar4033c552017-09-16 20:54:51 +02005740#if defined(FEAT_QUICKFIX) || defined(SWITCH_TO_WIN) || defined(PROTO)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005741/*
5742 * Find a window for buffer "buf".
5743 * If found OK is returned and "wp" and "tp" are set to the window and tabpage.
5744 * If not found FAIL is returned.
5745 */
5746 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005747find_win_for_buf(
5748 buf_T *buf,
5749 win_T **wp,
5750 tabpage_T **tp)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005751{
5752 FOR_ALL_TAB_WINDOWS(*tp, *wp)
5753 if ((*wp)->w_buffer == buf)
5754 goto win_found;
5755 return FAIL;
5756win_found:
5757 return OK;
5758}
5759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760
5761#if defined(FEAT_SIGNS) || defined(PROTO)
5762/*
5763 * Insert the sign into the signlist.
5764 */
5765 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005766insert_sign(
5767 buf_T *buf, /* buffer to store sign in */
5768 signlist_T *prev, /* previous sign entry */
5769 signlist_T *next, /* next sign entry */
5770 int id, /* sign ID */
5771 linenr_T lnum, /* line number which gets the mark */
5772 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773{
5774 signlist_T *newsign;
5775
5776 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5777 if (newsign != NULL)
5778 {
5779 newsign->id = id;
5780 newsign->lnum = lnum;
5781 newsign->typenr = typenr;
5782 newsign->next = next;
5783#ifdef FEAT_NETBEANS_INTG
5784 newsign->prev = prev;
5785 if (next != NULL)
5786 next->prev = newsign;
5787#endif
5788
5789 if (prev == NULL)
5790 {
5791 /* When adding first sign need to redraw the windows to create the
5792 * column for signs. */
5793 if (buf->b_signlist == NULL)
5794 {
5795 redraw_buf_later(buf, NOT_VALID);
5796 changed_cline_bef_curs();
5797 }
5798
5799 /* first sign in signlist */
5800 buf->b_signlist = newsign;
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01005801#ifdef FEAT_NETBEANS_INTG
5802 if (netbeans_active())
5803 buf->b_has_sign_column = TRUE;
5804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 }
5806 else
5807 prev->next = newsign;
5808 }
5809}
5810
5811/*
5812 * Add the sign into the signlist. Find the right spot to do it though.
5813 */
5814 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005815buf_addsign(
5816 buf_T *buf, /* buffer to store sign in */
5817 int id, /* sign ID */
5818 linenr_T lnum, /* line number which gets the mark */
5819 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820{
5821 signlist_T *sign; /* a sign in the signlist */
5822 signlist_T *prev; /* the previous sign */
5823
5824 prev = NULL;
5825 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5826 {
5827 if (lnum == sign->lnum && id == sign->id)
5828 {
5829 sign->typenr = typenr;
5830 return;
5831 }
5832 else if (
5833#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5834 id < 0 &&
5835#endif
5836 lnum < sign->lnum)
5837 {
5838#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5839 /* XXX - GRP: Is this because of sign slide problem? Or is it
5840 * really needed? Or is it because we allow multiple signs per
5841 * line? If so, should I add that feature to FEAT_SIGNS?
5842 */
5843 while (prev != NULL && prev->lnum == lnum)
5844 prev = prev->prev;
5845 if (prev == NULL)
5846 sign = buf->b_signlist;
5847 else
5848 sign = prev->next;
5849#endif
5850 insert_sign(buf, prev, sign, id, lnum, typenr);
5851 return;
5852 }
5853 prev = sign;
5854 }
5855#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5856 /* XXX - GRP: See previous comment */
5857 while (prev != NULL && prev->lnum == lnum)
5858 prev = prev->prev;
5859 if (prev == NULL)
5860 sign = buf->b_signlist;
5861 else
5862 sign = prev->next;
5863#endif
5864 insert_sign(buf, prev, sign, id, lnum, typenr);
5865
5866 return;
5867}
5868
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005869/*
5870 * For an existing, placed sign "markId" change the type to "typenr".
5871 * Returns the line number of the sign, or zero if the sign is not found.
5872 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005873 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005874buf_change_sign_type(
5875 buf_T *buf, /* buffer to store sign in */
5876 int markId, /* sign ID */
5877 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878{
5879 signlist_T *sign; /* a sign in the signlist */
5880
5881 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5882 {
5883 if (sign->id == markId)
5884 {
5885 sign->typenr = typenr;
5886 return sign->lnum;
5887 }
5888 }
5889
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005890 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891}
5892
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005893 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005894buf_getsigntype(
5895 buf_T *buf,
5896 linenr_T lnum,
5897 int type) /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898{
5899 signlist_T *sign; /* a sign in a b_signlist */
5900
5901 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5902 if (sign->lnum == lnum
5903 && (type == SIGN_ANY
5904# ifdef FEAT_SIGN_ICONS
5905 || (type == SIGN_ICON
5906 && sign_get_image(sign->typenr) != NULL)
5907# endif
5908 || (type == SIGN_TEXT
5909 && sign_get_text(sign->typenr) != NULL)
5910 || (type == SIGN_LINEHL
5911 && sign_get_attr(sign->typenr, TRUE) != 0)))
5912 return sign->typenr;
5913 return 0;
5914}
5915
5916
5917 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005918buf_delsign(
5919 buf_T *buf, /* buffer sign is stored in */
5920 int id) /* sign id */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921{
5922 signlist_T **lastp; /* pointer to pointer to current sign */
5923 signlist_T *sign; /* a sign in a b_signlist */
5924 signlist_T *next; /* the next sign in a b_signlist */
5925 linenr_T lnum; /* line number whose sign was deleted */
5926
5927 lastp = &buf->b_signlist;
5928 lnum = 0;
5929 for (sign = buf->b_signlist; sign != NULL; sign = next)
5930 {
5931 next = sign->next;
5932 if (sign->id == id)
5933 {
5934 *lastp = next;
5935#ifdef FEAT_NETBEANS_INTG
5936 if (next != NULL)
5937 next->prev = sign->prev;
5938#endif
5939 lnum = sign->lnum;
5940 vim_free(sign);
5941 break;
5942 }
5943 else
5944 lastp = &sign->next;
5945 }
5946
5947 /* When deleted the last sign need to redraw the windows to remove the
5948 * sign column. */
5949 if (buf->b_signlist == NULL)
5950 {
5951 redraw_buf_later(buf, NOT_VALID);
5952 changed_cline_bef_curs();
5953 }
5954
5955 return lnum;
5956}
5957
5958
5959/*
5960 * Find the line number of the sign with the requested id. If the sign does
5961 * not exist, return 0 as the line number. This will still let the correct file
5962 * get loaded.
5963 */
5964 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005965buf_findsign(
5966 buf_T *buf, /* buffer to store sign in */
5967 int id) /* sign ID */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968{
5969 signlist_T *sign; /* a sign in the signlist */
5970
5971 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5972 if (sign->id == id)
5973 return sign->lnum;
5974
5975 return 0;
5976}
5977
5978 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005979buf_findsign_id(
5980 buf_T *buf, /* buffer whose sign we are searching for */
5981 linenr_T lnum) /* line number of sign */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982{
5983 signlist_T *sign; /* a sign in the signlist */
5984
5985 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5986 if (sign->lnum == lnum)
5987 return sign->id;
5988
5989 return 0;
5990}
5991
5992
5993# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5994/* see if a given type of sign exists on a specific line */
5995 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005996buf_findsigntype_id(
5997 buf_T *buf, /* buffer whose sign we are searching for */
5998 linenr_T lnum, /* line number of sign */
5999 int typenr) /* sign type number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000{
6001 signlist_T *sign; /* a sign in the signlist */
6002
6003 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
6004 if (sign->lnum == lnum && sign->typenr == typenr)
6005 return sign->id;
6006
6007 return 0;
6008}
6009
6010
6011# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
6012/* return the number of icons on the given line */
6013 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006014buf_signcount(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015{
6016 signlist_T *sign; /* a sign in the signlist */
6017 int count = 0;
6018
6019 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
6020 if (sign->lnum == lnum)
6021 if (sign_get_image(sign->typenr) != NULL)
6022 count++;
6023
6024 return count;
6025}
6026# endif /* FEAT_SIGN_ICONS */
6027# endif /* FEAT_NETBEANS_INTG */
6028
6029
6030/*
6031 * Delete signs in buffer "buf".
6032 */
Bram Moolenaarf65e5662012-07-10 15:18:22 +02006033 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006034buf_delete_signs(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035{
6036 signlist_T *next;
6037
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02006038 /* When deleting the last sign need to redraw the windows to remove the
Bram Moolenaar4e036c92014-07-16 16:30:28 +02006039 * sign column. Not when curwin is NULL (this means we're exiting). */
6040 if (buf->b_signlist != NULL && curwin != NULL)
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02006041 {
6042 redraw_buf_later(buf, NOT_VALID);
6043 changed_cline_bef_curs();
6044 }
6045
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 while (buf->b_signlist != NULL)
6047 {
6048 next = buf->b_signlist->next;
6049 vim_free(buf->b_signlist);
6050 buf->b_signlist = next;
6051 }
6052}
6053
6054/*
6055 * Delete all signs in all buffers.
6056 */
6057 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006058buf_delete_all_signs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059{
6060 buf_T *buf; /* buffer we are checking for signs */
6061
Bram Moolenaar29323592016-07-24 22:04:11 +02006062 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 if (buf->b_signlist != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 buf_delete_signs(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065}
6066
6067/*
6068 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
6069 */
6070 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006071sign_list_placed(buf_T *rbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072{
6073 buf_T *buf;
6074 signlist_T *p;
6075 char lbuf[BUFSIZ];
6076
6077 MSG_PUTS_TITLE(_("\n--- Signs ---"));
6078 msg_putchar('\n');
6079 if (rbuf == NULL)
6080 buf = firstbuf;
6081 else
6082 buf = rbuf;
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01006083 while (buf != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 {
6085 if (buf->b_signlist != NULL)
6086 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00006087 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006088 MSG_PUTS_ATTR(lbuf, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 msg_putchar('\n');
6090 }
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01006091 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00006093 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
6095 MSG_PUTS(lbuf);
6096 msg_putchar('\n');
6097 }
6098 if (rbuf != NULL)
6099 break;
6100 buf = buf->b_next;
6101 }
6102}
6103
6104/*
6105 * Adjust a placed sign for inserted/deleted lines.
6106 */
6107 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006108sign_mark_adjust(
6109 linenr_T line1,
6110 linenr_T line2,
6111 long amount,
6112 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113{
6114 signlist_T *sign; /* a sign in a b_signlist */
6115
6116 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
6117 {
6118 if (sign->lnum >= line1 && sign->lnum <= line2)
6119 {
6120 if (amount == MAXLNUM)
6121 sign->lnum = line1;
6122 else
6123 sign->lnum += amount;
6124 }
6125 else if (sign->lnum > line2)
6126 sign->lnum += amount_after;
6127 }
6128}
6129#endif /* FEAT_SIGNS */
6130
6131/*
6132 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
6133 */
6134 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006135set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136{
6137 if (on != curbuf->b_p_bl)
6138 {
6139 curbuf->b_p_bl = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 if (on)
6141 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
6142 else
6143 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 }
6145}
6146
6147/*
6148 * Read the file for "buf" again and check if the contents changed.
6149 * Return TRUE if it changed or this could not be checked.
6150 */
6151 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006152buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153{
6154 buf_T *newbuf;
6155 int differ = TRUE;
6156 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 exarg_T ea;
6159
6160 /* Allocate a buffer without putting it in the buffer list. */
6161 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6162 if (newbuf == NULL)
6163 return TRUE;
6164
6165 /* Force the 'fileencoding' and 'fileformat' to be equal. */
6166 if (prep_exarg(&ea, buf) == FAIL)
6167 {
6168 wipe_buffer(newbuf, FALSE);
6169 return TRUE;
6170 }
6171
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 /* set curwin/curbuf to buf and save a few things */
6173 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174
Bram Moolenaar4770d092006-01-12 23:22:24 +00006175 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 && readfile(buf->b_ffname, buf->b_fname,
6177 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
6178 &ea, READ_NEW | READ_DUMMY) == OK)
6179 {
6180 /* compare the two files line by line */
6181 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
6182 {
6183 differ = FALSE;
6184 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6185 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
6186 {
6187 differ = TRUE;
6188 break;
6189 }
6190 }
6191 }
6192 vim_free(ea.cmd);
6193
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 /* restore curwin/curbuf and a few other things */
6195 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196
6197 if (curbuf != newbuf) /* safety check */
6198 wipe_buffer(newbuf, FALSE);
6199
6200 return differ;
6201}
6202
6203/*
6204 * Wipe out a buffer and decrement the last buffer number if it was used for
6205 * this buffer. Call this to wipe out a temp buffer that does not contain any
6206 * marks.
6207 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006209wipe_buffer(
6210 buf_T *buf,
6211 int aucmd UNUSED) /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212{
6213 if (buf->b_fnum == top_file_num - 1)
6214 --top_file_num;
6215
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006217 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006218
Bram Moolenaar42ec6562012-02-22 14:58:37 +01006219 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006220
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006222 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223}