blob: dd618337ef5bcb7aab486286f7678f9bca4bd874 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * buffer.c: functions for dealing with the buffer structure
12 */
13
14/*
15 * The buffer list is a double linked list of all buffers.
16 * Each buffer can be in one of these states:
17 * never loaded: BF_NEVERLOADED is set, only the file name is valid
18 * not loaded: b_ml.ml_mfp == NULL, no memfile allocated
19 * hidden: b_nwindows == 0, loaded but not displayed in a window
20 * normal: loaded and displayed in a window
21 *
22 * Instead of storing file names all over the place, each file name is
23 * stored in the buffer list. It can be referenced by a number.
24 *
25 * The current implementation remembers all file names ever used.
26 */
27
Bram Moolenaar071d4272004-06-13 20:20:40 +000028#include "vim.h"
29
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010030static char_u *buflist_match(regmatch_T *rmp, buf_T *buf, int ignore_case);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010031static char_u *fname_match(regmatch_T *rmp, char_u *name, int ignore_case);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010032static void buflist_setfpos(buf_T *buf, win_T *win, linenr_T lnum, colnr_T col, int copy_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +000033#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +020034static buf_T *buflist_findname_stat(char_u *ffname, stat_T *st);
35static int otherfile_buf(buf_T *buf, char_u *ffname, stat_T *stp);
36static int buf_same_ino(buf_T *buf, stat_T *stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037#else
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010038static int otherfile_buf(buf_T *buf, char_u *ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000039#endif
40#ifdef FEAT_TITLE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010041static int ti_change(char_u *str, char_u **last);
Bram Moolenaar071d4272004-06-13 20:20:40 +000042#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010043static int append_arg_number(win_T *wp, char_u *buf, int buflen, int add_file);
44static void free_buffer(buf_T *);
45static void free_buffer_stuff(buf_T *buf, int free_options);
46static void clear_wininfo(buf_T *buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000047
48#ifdef UNIX
49# define dev_T dev_t
50#else
51# define dev_T unsigned
52#endif
53
54#if defined(FEAT_SIGNS)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010055static 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 +000056#endif
57
Bram Moolenaar4033c552017-09-16 20:54:51 +020058#if defined(FEAT_QUICKFIX)
Bram Moolenaar7fd73202010-07-25 16:58:46 +020059static char *msg_loclist = N_("[Location List]");
60static char *msg_qflist = N_("[Quickfix List]");
61#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +010062static char *e_auabort = N_("E855: Autocommands caused command to abort");
Bram Moolenaar7fd73202010-07-25 16:58:46 +020063
Bram Moolenaarb25f9a92016-07-10 18:21:50 +020064/* Number of times free_buffer() was called. */
65static int buf_free_count = 0;
66
Bram Moolenaarf71d7b92016-08-09 22:14:05 +020067/* Read data from buffer for retrying. */
68 static int
69read_buffer(
70 int read_stdin, /* read file from stdin, otherwise fifo */
71 exarg_T *eap, /* for forced 'ff' and 'fenc' or NULL */
72 int flags) /* extra flags for readfile() */
73{
74 int retval = OK;
75 linenr_T line_count;
76
77 /*
78 * Read from the buffer which the text is already filled in and append at
79 * the end. This makes it possible to retry when 'fileformat' or
80 * 'fileencoding' was guessed wrong.
81 */
82 line_count = curbuf->b_ml.ml_line_count;
83 retval = readfile(
84 read_stdin ? NULL : curbuf->b_ffname,
85 read_stdin ? NULL : curbuf->b_fname,
86 (linenr_T)line_count, (linenr_T)0, (linenr_T)MAXLNUM, eap,
87 flags | READ_BUFFER);
88 if (retval == OK)
89 {
90 /* Delete the binary lines. */
91 while (--line_count >= 0)
92 ml_delete((linenr_T)1, FALSE);
93 }
94 else
95 {
96 /* Delete the converted lines. */
97 while (curbuf->b_ml.ml_line_count > line_count)
98 ml_delete(line_count, FALSE);
99 }
100 /* Put the cursor on the first line. */
101 curwin->w_cursor.lnum = 1;
102 curwin->w_cursor.col = 0;
103
104 if (read_stdin)
105 {
106 /* Set or reset 'modified' before executing autocommands, so that
107 * it can be changed there. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100108 if (!readonlymode && !BUFEMPTY())
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200109 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100110 else if (retval == OK)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200111 unchanged(curbuf, FALSE);
112
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100113 if (retval == OK)
114 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100115#ifdef FEAT_EVAL
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100116 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100117 curbuf, &retval);
118#else
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100119 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200120#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100121 }
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200122 }
123 return retval;
124}
125
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200127 * Open current buffer, that is: open the memfile and read the file into
128 * memory.
129 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130 */
131 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100132open_buffer(
133 int read_stdin, /* read file from stdin */
134 exarg_T *eap, /* for forced 'ff' and 'fenc' or NULL */
135 int flags) /* extra flags for readfile() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136{
137 int retval = OK;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200138 bufref_T old_curbuf;
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100139#ifdef FEAT_SYN_HL
140 long old_tw = curbuf->b_p_tw;
141#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200142 int read_fifo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143
144 /*
145 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
146 * When re-entering the same buffer, it should not change, because the
147 * user may have reset the flag by hand.
148 */
149 if (readonlymode && curbuf->b_ffname != NULL
150 && (curbuf->b_flags & BF_NEVERLOADED))
151 curbuf->b_p_ro = TRUE;
152
Bram Moolenaar4770d092006-01-12 23:22:24 +0000153 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154 {
155 /*
156 * There MUST be a memfile, otherwise we can't do anything
157 * If we can't create one for the current buffer, take another buffer
158 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100159 close_buffer(NULL, curbuf, 0, FALSE);
Bram Moolenaar29323592016-07-24 22:04:11 +0200160 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 if (curbuf->b_ml.ml_mfp != NULL)
162 break;
163 /*
164 * if there is no memfile at all, exit
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000165 * This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166 */
167 if (curbuf == NULL)
168 {
169 EMSG(_("E82: Cannot allocate any buffer, exiting..."));
170 getout(2);
171 }
172 EMSG(_("E83: Cannot allocate buffer, using other one..."));
173 enter_buffer(curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100174#ifdef FEAT_SYN_HL
175 if (old_tw != curbuf->b_p_tw)
176 check_colorcolumn(curwin);
177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178 return FAIL;
179 }
180
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 /* The autocommands in readfile() may change the buffer, but only AFTER
182 * reading the file. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200183 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 modified_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185
186 /* mark cursor position as being invalid */
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100187 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188
189 if (curbuf->b_ffname != NULL
190#ifdef FEAT_NETBEANS_INTG
191 && netbeansReadFile
192#endif
193 )
194 {
Bram Moolenaar426dd022016-03-15 15:09:29 +0100195 int old_msg_silent = msg_silent;
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200196#ifdef UNIX
197 int save_bin = curbuf->b_p_bin;
198 int perm;
199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200#ifdef FEAT_NETBEANS_INTG
201 int oldFire = netbeansFireChanges;
202
203 netbeansFireChanges = 0;
204#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200205#ifdef UNIX
206 perm = mch_getperm(curbuf->b_ffname);
207 if (perm >= 0 && (0
208# ifdef S_ISFIFO
209 || S_ISFIFO(perm)
210# endif
211# ifdef S_ISSOCK
212 || S_ISSOCK(perm)
213# endif
Bram Moolenaarf04507d2016-08-20 15:05:39 +0200214# ifdef OPEN_CHR_FILES
215 || (S_ISCHR(perm) && is_dev_fd_file(curbuf->b_ffname))
216# endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200217 ))
218 read_fifo = TRUE;
219 if (read_fifo)
220 curbuf->b_p_bin = TRUE;
221#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100222 if (shortmess(SHM_FILEINFO))
223 msg_silent = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200225 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200226 flags | READ_NEW | (read_fifo ? READ_FIFO : 0));
227#ifdef UNIX
228 if (read_fifo)
229 {
230 curbuf->b_p_bin = save_bin;
231 if (retval == OK)
232 retval = read_buffer(FALSE, eap, flags);
233 }
234#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100235 msg_silent = old_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236#ifdef FEAT_NETBEANS_INTG
237 netbeansFireChanges = oldFire;
238#endif
239 /* Help buffer is filtered. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +0200240 if (bt_help(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 fix_help_buffer();
242 }
243 else if (read_stdin)
244 {
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200245 int save_bin = curbuf->b_p_bin;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246
247 /*
248 * First read the text in binary mode into the buffer.
249 * Then read from that same buffer and append at the end. This makes
250 * it possible to retry when 'fileformat' or 'fileencoding' was
251 * guessed wrong.
252 */
253 curbuf->b_p_bin = TRUE;
254 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200255 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
256 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 curbuf->b_p_bin = save_bin;
258 if (retval == OK)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200259 retval = read_buffer(TRUE, eap, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 }
261
262 /* if first time loading this buffer, init b_chartab[] */
263 if (curbuf->b_flags & BF_NEVERLOADED)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100264 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 (void)buf_init_chartab(curbuf, FALSE);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100266#ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100267 parse_cino(curbuf);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100268#endif
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270
271 /*
272 * Set/reset the Changed flag first, autocmds may change the buffer.
273 * Apply the automatic commands, before processing the modelines.
274 * So the modelines have priority over auto commands.
275 */
276 /* When reading stdin, the buffer contents always needs writing, so set
277 * the changed flag. Unless in readonly mode: "ls | gview -".
278 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000279 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280 || modified_was_set /* ":set modified" used in autocmd */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100281#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000284 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100286 else if (retval == OK && !read_stdin && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 unchanged(curbuf, FALSE);
288 save_file_ff(curbuf); /* keep this fileformat */
289
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100290 /* Set last_changedtick to avoid triggering a TextChanged autocommand right
291 * after it was added. */
292 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
293#ifdef FEAT_INS_EXPAND
294 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
295#endif
296
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297 /* require "!" to overwrite the file, because it wasn't read completely */
298#ifdef FEAT_EVAL
299 if (aborting())
300#else
301 if (got_int)
302#endif
303 curbuf->b_flags |= BF_READERR;
304
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000305#ifdef FEAT_FOLDING
306 /* Need to update automatic folding. Do this before the autocommands,
307 * they may use the fold info. */
308 foldUpdateAll(curwin);
309#endif
310
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 /* need to set w_topline, unless some autocommand already did that. */
312 if (!(curwin->w_valid & VALID_TOPLINE))
313 {
314 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100315#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100319#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100321#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323#endif
324
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100325 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 /*
328 * The autocommands may have changed the current buffer. Apply the
329 * modelines to the correct buffer, if it still exists and is loaded.
330 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200331 if (bufref_valid(&old_curbuf) && old_curbuf.br_buf->b_ml.ml_mfp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332 {
333 aco_save_T aco;
334
335 /* Go to the buffer that was opened. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200336 aucmd_prepbuf(&aco, old_curbuf.br_buf);
Bram Moolenaara3227e22006-03-08 21:32:40 +0000337 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
339
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100340#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100342 &retval);
343#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346
347 /* restore curwin/curbuf and a few other things */
348 aucmd_restbuf(&aco);
349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 }
351
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 return retval;
353}
354
355/*
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200356 * Store "buf" in "bufref" and set the free count.
357 */
358 void
359set_bufref(bufref_T *bufref, buf_T *buf)
360{
361 bufref->br_buf = buf;
Bram Moolenaarfadacf02017-06-19 20:35:32 +0200362 bufref->br_fnum = buf == NULL ? 0 : buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200363 bufref->br_buf_free_count = buf_free_count;
364}
365
366/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200367 * Return TRUE if "bufref->br_buf" points to the same buffer as when
368 * set_bufref() was called and it is a valid buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200369 * Only goes through the buffer list if buf_free_count changed.
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200370 * Also checks if b_fnum is still the same, a :bwipe followed by :new might get
371 * the same allocated memory, but it's a different buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200372 */
373 int
374bufref_valid(bufref_T *bufref)
375{
376 return bufref->br_buf_free_count == buf_free_count
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200377 ? TRUE : buf_valid(bufref->br_buf)
378 && bufref->br_fnum == bufref->br_buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200379}
380
381/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200383 * This can be slow if there are many buffers, prefer using bufref_valid().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384 */
385 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100386buf_valid(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387{
388 buf_T *bp;
389
Bram Moolenaar82404332016-07-10 17:00:38 +0200390 /* Assume that we more often have a recent buffer, start with the last
391 * one. */
392 for (bp = lastbuf; bp != NULL; bp = bp->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 if (bp == buf)
394 return TRUE;
395 return FALSE;
396}
397
398/*
Bram Moolenaar480778b2016-07-14 22:09:39 +0200399 * A hash table used to quickly lookup a buffer by its number.
400 */
401static hashtab_T buf_hashtab;
402
403 static void
404buf_hashtab_add(buf_T *buf)
405{
406 sprintf((char *)buf->b_key, "%x", buf->b_fnum);
407 if (hash_add(&buf_hashtab, buf->b_key) == FAIL)
408 EMSG(_("E931: Buffer cannot be registered"));
409}
410
411 static void
412buf_hashtab_remove(buf_T *buf)
413{
414 hashitem_T *hi = hash_find(&buf_hashtab, buf->b_key);
415
416 if (!HASHITEM_EMPTY(hi))
417 hash_remove(&buf_hashtab, hi);
418}
419
Bram Moolenaara997b452018-04-17 23:24:06 +0200420static char *e_buflocked = N_("E937: Attempt to delete a buffer that is in use");
421
Bram Moolenaar480778b2016-07-14 22:09:39 +0200422/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 * Close the link to a buffer.
424 * "action" is used when there is no longer a window for the buffer.
425 * It can be:
426 * 0 buffer becomes hidden
427 * DOBUF_UNLOAD buffer is unloaded
428 * DOBUF_DELETE buffer is unloaded and removed from buffer list
429 * DOBUF_WIPE buffer is unloaded and really deleted
430 * When doing all but the first one on the current buffer, the caller should
431 * get a new buffer very soon!
432 *
433 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100434 *
435 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
436 * cause there to be only one window with this buffer. e.g. when ":quit" is
437 * supposed to close the window but autocommands close all other windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 */
439 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100440close_buffer(
441 win_T *win, /* if not NULL, set b_last_cursor */
442 buf_T *buf,
443 int action,
444 int abort_if_last UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100447 int nwindows;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200448 bufref_T bufref;
Bram Moolenaar3a117e12016-10-30 21:57:52 +0100449 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200450 win_T *the_curwin = curwin;
451 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 int unload_buf = (action != 0);
453 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
454 int wipe_buf = (action == DOBUF_WIPE);
455
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200456 /*
457 * Force unloading or deleting when 'bufhidden' says so.
458 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
459 * "hide" (otherwise we could never free or delete a buffer).
460 */
461 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
462 {
463 del_buf = TRUE;
464 unload_buf = TRUE;
465 }
466 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
467 {
468 del_buf = TRUE;
469 unload_buf = TRUE;
470 wipe_buf = TRUE;
471 }
472 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
473 unload_buf = TRUE;
474
Bram Moolenaar94053a52017-08-01 21:44:33 +0200475#ifdef FEAT_TERMINAL
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200476 if (bt_terminal(buf) && (buf->b_nwindows == 1 || del_buf))
Bram Moolenaar94053a52017-08-01 21:44:33 +0200477 {
478 if (term_job_running(buf->b_term))
479 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200480 if (wipe_buf || unload_buf)
Bram Moolenaara997b452018-04-17 23:24:06 +0200481 {
482 if (buf->b_locked)
483 {
484 EMSG(_(e_buflocked));
485 return;
486 }
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200487 /* Wiping out or unloading a terminal buffer kills the job. */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200488 free_terminal(buf);
Bram Moolenaara997b452018-04-17 23:24:06 +0200489 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200490 else
491 {
492 /* The job keeps running, hide the buffer. */
493 del_buf = FALSE;
494 unload_buf = FALSE;
495 }
496 }
497 else
498 {
499 /* A terminal buffer is wiped out if the job has finished. */
500 del_buf = TRUE;
501 unload_buf = TRUE;
502 wipe_buf = TRUE;
503 }
504 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200505#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200507 /* Disallow deleting the buffer when it is locked (already being closed or
508 * halfway a command that relies on it). Unloading is allowed. */
509 if (buf->b_locked > 0 && (del_buf || wipe_buf))
510 {
Bram Moolenaara997b452018-04-17 23:24:06 +0200511 EMSG(_(e_buflocked));
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200512 return;
513 }
514
Bram Moolenaar4033c552017-09-16 20:54:51 +0200515 /* check no autocommands closed the window */
516 if (win != NULL && win_valid_any_tab(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 {
518 /* Set b_last_cursor when closing the last window for the buffer.
519 * Remember the last cursor position and window options of the buffer.
520 * This used to be only for the current window, but then options like
521 * 'foldmethod' may be lost with a ":only" command. */
522 if (buf->b_nwindows == 1)
523 set_last_cursor(win);
524 buflist_setfpos(buf, win,
525 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
526 win->w_cursor.col, TRUE);
527 }
528
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200529 set_bufref(&bufref, buf);
530
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 /* When the buffer is no longer in a window, trigger BufWinLeave */
532 if (buf->b_nwindows == 1)
533 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200534 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200535 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
536 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200537 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100538 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200539 /* Autocommands deleted the buffer. */
540aucmd_abort:
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100541 EMSG(_(e_auabort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 return;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100543 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200544 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200545 if (abort_if_last && one_window())
546 /* Autocommands made this the only window. */
547 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548
549 /* When the buffer becomes hidden, but is not unloaded, trigger
550 * BufHidden */
551 if (!unload_buf)
552 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200553 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200554 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
555 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200556 && !bufref_valid(&bufref))
Bram Moolenaar362ce482012-06-06 19:02:45 +0200557 /* Autocommands deleted the buffer. */
558 goto aucmd_abort;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200559 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200560 if (abort_if_last && one_window())
561 /* Autocommands made this the only window. */
562 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100564#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 if (aborting()) /* autocmds may abort script processing */
566 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200569
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200570 /* If the buffer was in curwin and the window has changed, go back to that
571 * window, if it still exists. This avoids that ":edit x" triggering a
572 * "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
573 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
574 {
575 block_autocmds();
576 goto_tabpage_win(the_curtab, the_curwin);
577 unblock_autocmds();
578 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200579
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 nwindows = buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581
582 /* decrease the link count from windows (unless not in any window) */
583 if (buf->b_nwindows > 0)
584 --buf->b_nwindows;
585
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100586#ifdef FEAT_DIFF
587 if (diffopt_hiddenoff() && !unload_buf && buf->b_nwindows == 0)
Bram Moolenaarb7407d32018-02-03 17:36:27 +0100588 diff_buf_delete(buf); /* Clear 'diff' for hidden buffer. */
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100589#endif
590
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 /* Return when a window is displaying the buffer or when it's not
592 * unloaded. */
593 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595
596 /* Always remove the buffer when there is no file name. */
597 if (buf->b_ffname == NULL)
598 del_buf = TRUE;
599
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200600 /* When closing the current buffer stop Visual mode before freeing
601 * anything. */
Bram Moolenaar4930a762016-09-11 14:39:53 +0200602 if (buf == curbuf && VIsual_active
Bram Moolenaar9a27c7f2016-09-09 12:57:09 +0200603#if defined(EXITFREE)
604 && !entered_free_all_mem
605#endif
606 )
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200607 end_visual_mode();
608
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 /*
610 * Free all things allocated for this buffer.
611 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
612 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 /* Remember if we are closing the current buffer. Restore the number of
614 * windows, so that autocommands in buf_freeall() don't get confused. */
615 is_curbuf = (buf == curbuf);
616 buf->b_nwindows = nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200618 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 /* Autocommands may have deleted the buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200621 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100623#ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000624 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100626#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 /*
629 * It's possible that autocommands change curbuf to the one being deleted.
630 * This might cause the previous curbuf to be deleted unexpectedly. But
631 * in some cases it's OK to delete the curbuf, because a new one is
632 * obtained anyway. Therefore only return if curbuf changed to the
633 * deleted buffer.
634 */
635 if (buf == curbuf && !is_curbuf)
636 return;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200637
Bram Moolenaar4033c552017-09-16 20:54:51 +0200638 if (win_valid_any_tab(win) && win->w_buffer == buf)
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200639 win->w_buffer = NULL; /* make sure we don't use the buffer now */
640
641 /* Autocommands may have opened or closed windows for this buffer.
642 * Decrement the count for the close we do here. */
643 if (buf->b_nwindows > 0)
644 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 /*
647 * Remove the buffer from the list.
648 */
649 if (wipe_buf)
650 {
651#ifdef FEAT_SUN_WORKSHOP
652 if (usingSunWorkShop)
653 workshop_file_closed_lineno((char *)buf->b_ffname,
654 (int)buf->b_last_cursor.lnum);
655#endif
656 vim_free(buf->b_ffname);
657 vim_free(buf->b_sfname);
658 if (buf->b_prev == NULL)
659 firstbuf = buf->b_next;
660 else
661 buf->b_prev->b_next = buf->b_next;
662 if (buf->b_next == NULL)
663 lastbuf = buf->b_prev;
664 else
665 buf->b_next->b_prev = buf->b_prev;
666 free_buffer(buf);
667 }
668 else
669 {
670 if (del_buf)
671 {
672 /* Free all internal variables and reset option values, to make
673 * ":bdel" compatible with Vim 5.7. */
674 free_buffer_stuff(buf, TRUE);
675
676 /* Make it look like a new buffer. */
677 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
678
679 /* Init the options when loaded again. */
680 buf->b_p_initialized = FALSE;
681 }
682 buf_clear_file(buf);
683 if (del_buf)
684 buf->b_p_bl = FALSE;
685 }
686}
687
688/*
689 * Make buffer not contain a file.
690 */
691 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100692buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693{
694 buf->b_ml.ml_line_count = 1;
695 unchanged(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 buf->b_p_eol = TRUE;
698 buf->b_start_eol = TRUE;
699#ifdef FEAT_MBYTE
700 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000701 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702#endif
703 buf->b_ml.ml_mfp = NULL;
704 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
705#ifdef FEAT_NETBEANS_INTG
706 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707#endif
708}
709
710/*
711 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200712 * the file. Careful: get here with "curwin" NULL when exiting.
713 * flags:
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200714 * BFA_DEL buffer is going to be deleted
715 * BFA_WIPE buffer is going to be wiped out
716 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100719buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200722 bufref_T bufref;
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200723 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaar5a497892016-09-03 16:29:04 +0200724 win_T *the_curwin = curwin;
725 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726
Bram Moolenaar5a497892016-09-03 16:29:04 +0200727 /* Make sure the buffer isn't closed by autocommands. */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200728 ++buf->b_locked;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200729 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200730 if (buf->b_ml.ml_mfp != NULL)
731 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200732 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
733 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200734 && !bufref_valid(&bufref))
735 /* autocommands deleted the buffer */
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200736 return;
737 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200738 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200740 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
741 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200742 && !bufref_valid(&bufref))
743 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 return;
745 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200746 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200748 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
749 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200750 && !bufref_valid(&bufref))
751 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 return;
753 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200754 --buf->b_locked;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200755
Bram Moolenaar5a497892016-09-03 16:29:04 +0200756 /* If the buffer was in curwin and the window has changed, go back to that
757 * window, if it still exists. This avoids that ":edit x" triggering a
758 * "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
759 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
760 {
761 block_autocmds();
762 goto_tabpage_win(the_curtab, the_curwin);
763 unblock_autocmds();
764 }
Bram Moolenaar5a497892016-09-03 16:29:04 +0200765
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100766#ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000767 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100769#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770
771 /*
772 * It's possible that autocommands change curbuf to the one being deleted.
773 * This might cause curbuf to be deleted unexpectedly. But in some cases
774 * it's OK to delete the curbuf, because a new one is obtained anyway.
775 * Therefore only return if curbuf changed to the deleted buffer.
776 */
777 if (buf == curbuf && !is_curbuf)
778 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779#ifdef FEAT_DIFF
780 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
781#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200782#ifdef FEAT_SYN_HL
Bram Moolenaar89c71222011-11-30 15:40:56 +0100783 /* Remove any ownsyntax, unless exiting. */
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200784 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaar89c71222011-11-30 15:40:56 +0100785 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200786#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000787
788#ifdef FEAT_FOLDING
789 /* No folds in an empty buffer. */
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000790 {
791 win_T *win;
792 tabpage_T *tp;
793
794 FOR_ALL_TAB_WINDOWS(tp, win)
795 if (win->w_buffer == buf)
796 clearFolding(win);
797 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000798#endif
799
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800#ifdef FEAT_TCL
801 tcl_buffer_free(buf);
802#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 ml_close(buf, TRUE); /* close and delete the memline/memfile */
804 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200805 if ((flags & BFA_KEEP_UNDO) == 0)
806 {
807 u_blockfree(buf); /* free the memory allocated for undo */
808 u_clearall(buf); /* reset all undo information */
809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200811 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000813 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814}
815
816/*
817 * Free a buffer structure and the things it contains related to the buffer
818 * itself (not the file, that must have been done already).
819 */
820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100821free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200823 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200825#ifdef FEAT_EVAL
Bram Moolenaarb2c87502017-10-14 21:15:58 +0200826 /* b:changedtick uses an item in buf_T, remove it now */
827 dictitem_remove(buf->b_vars, (dictitem_T *)&buf->b_ct_di);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200828 unref_var_dict(buf->b_vars);
829#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200830#ifdef FEAT_LUA
831 lua_buffer_free(buf);
832#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000833#ifdef FEAT_MZSCHEME
834 mzscheme_buffer_free(buf);
835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836#ifdef FEAT_PERL
837 perl_buf_free(buf);
838#endif
839#ifdef FEAT_PYTHON
840 python_buffer_free(buf);
841#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200842#ifdef FEAT_PYTHON3
843 python3_buffer_free(buf);
844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845#ifdef FEAT_RUBY
846 ruby_buffer_free(buf);
847#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200848#ifdef FEAT_JOB_CHANNEL
849 channel_buffer_free(buf);
850#endif
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200851#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +0200852 free_terminal(buf);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200853#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200854
855 buf_hashtab_remove(buf);
856
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200857 aubuflocal_remove(buf);
858
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200859 if (autocmd_busy)
860 {
861 /* Do not free the buffer structure while autocommands are executing,
862 * it's still needed. Free it when autocmd_busy is reset. */
863 buf->b_next = au_pending_free_buf;
864 au_pending_free_buf = buf;
865 }
866 else
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200867 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868}
869
870/*
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100871 * Initializes b:changedtick.
Bram Moolenaar79518e22017-02-17 16:31:35 +0100872 */
873 static void
874init_changedtick(buf_T *buf)
875{
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100876 dictitem_T *di = (dictitem_T *)&buf->b_ct_di;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100877
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100878 di->di_flags = DI_FLAGS_FIX | DI_FLAGS_RO;
879 di->di_tv.v_type = VAR_NUMBER;
880 di->di_tv.v_lock = VAR_FIXED;
881 di->di_tv.vval.v_number = 0;
882
Bram Moolenaar92769c32017-02-25 15:41:37 +0100883#ifdef FEAT_EVAL
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100884 STRCPY(buf->b_ct_di.di_key, "changedtick");
885 (void)dict_add(buf->b_vars, di);
Bram Moolenaar92769c32017-02-25 15:41:37 +0100886#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +0100887}
888
889/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
891 */
892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100893free_buffer_stuff(
894 buf_T *buf,
895 int free_options) /* free options as well */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896{
897 if (free_options)
898 {
899 clear_wininfo(buf); /* including window-local options */
900 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200901#ifdef FEAT_SPELL
902 ga_clear(&buf->b_s.b_langp);
903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 }
905#ifdef FEAT_EVAL
Bram Moolenaar79518e22017-02-17 16:31:35 +0100906 {
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100907 varnumber_T tick = CHANGEDTICK(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +0100908
909 vars_clear(&buf->b_vars->dv_hashtab); /* free all buffer variables */
910 hash_init(&buf->b_vars->dv_hashtab);
911 init_changedtick(buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100912 CHANGEDTICK(buf) = tick;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914#endif
915#ifdef FEAT_USR_CMDS
916 uc_clear(&buf->b_ucmds); /* clear local user commands */
917#endif
918#ifdef FEAT_SIGNS
919 buf_delete_signs(buf); /* delete any signs */
920#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000921#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200922 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924#ifdef FEAT_LOCALMAP
925 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
926 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
927#endif
928#ifdef FEAT_MBYTE
Bram Moolenaard23a8232018-02-10 18:45:26 +0100929 VIM_CLEAR(buf->b_start_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930#endif
931}
932
933/*
934 * Free the b_wininfo list for buffer "buf".
935 */
936 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100937clear_wininfo(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938{
939 wininfo_T *wip;
940
941 while (buf->b_wininfo != NULL)
942 {
943 wip = buf->b_wininfo;
944 buf->b_wininfo = wip->wi_next;
945 if (wip->wi_optset)
946 {
947 clear_winopt(&wip->wi_opt);
948#ifdef FEAT_FOLDING
949 deleteFoldRecurse(&wip->wi_folds);
950#endif
951 }
952 vim_free(wip);
953 }
954}
955
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956/*
957 * Go to another buffer. Handles the result of the ATTENTION dialog.
958 */
959 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100960goto_buffer(
961 exarg_T *eap,
962 int start,
963 int dir,
964 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965{
Bram Moolenaar0c72fe42018-03-29 16:04:08 +0200966#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200967 bufref_T old_curbuf;
968
969 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970
971 swap_exists_action = SEA_DIALOG;
Bram Moolenaar0c72fe42018-03-29 16:04:08 +0200972#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
974 start, dir, count, eap->forceit);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +0200975#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
977 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +0200978# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000979 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000980
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000981 /* Reset the error/interrupt/exception state here so that
982 * aborting() returns FALSE when closing a window. */
983 enter_cleanup(&cs);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +0200984# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000985
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000986 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 win_close(curwin, TRUE);
988 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000989 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000990
Bram Moolenaar0c72fe42018-03-29 16:04:08 +0200991# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000992 /* Restore the error/interrupt/exception state if not discarded by a
993 * new aborting error, interrupt, or uncaught exception. */
994 leave_cleanup(&cs);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +0200995# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 }
997 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200998 handle_swap_exists(&old_curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999#endif
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001000}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001
Bram Moolenaare64ac772005-12-07 20:54:59 +00001002#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003/*
1004 * Handle the situation of swap_exists_action being set.
1005 * It is allowed for "old_curbuf" to be NULL or invalid.
1006 */
1007 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001008handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009{
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001010# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001011 cleanup_T cs;
1012# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001013# ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001014 long old_tw = curbuf->b_p_tw;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001015# endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001016 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001017
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 if (swap_exists_action == SEA_QUIT)
1019 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001020# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001021 /* Reset the error/interrupt/exception state here so that
1022 * aborting() returns FALSE when closing a buffer. */
1023 enter_cleanup(&cs);
1024# endif
1025
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 /* User selected Quit at ATTENTION prompt. Go back to previous
1027 * buffer. If that buffer is gone or the same as the current one,
1028 * open a new, empty buffer. */
1029 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001030 swap_exists_did_quit = TRUE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001031 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001032 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1033 || old_curbuf->br_buf == curbuf)
1034 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
1035 else
1036 buf = old_curbuf->br_buf;
1037 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001038 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001039 enter_buffer(buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001040# ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001041 if (old_tw != curbuf->b_p_tw)
1042 check_colorcolumn(curwin);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001043# endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001046
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001047# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001048 /* Restore the error/interrupt/exception state if not discarded by a
1049 * new aborting error, interrupt, or uncaught exception. */
1050 leave_cleanup(&cs);
1051# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 }
1053 else if (swap_exists_action == SEA_RECOVER)
1054 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001055# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001056 /* Reset the error/interrupt/exception state here so that
1057 * aborting() returns FALSE when closing a buffer. */
1058 enter_cleanup(&cs);
1059# endif
1060
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 /* User selected Recover at ATTENTION prompt. */
1062 msg_scroll = TRUE;
1063 ml_recover();
1064 MSG_PUTS("\n"); /* don't overwrite the last message */
1065 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001066 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001067
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001068# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001069 /* Restore the error/interrupt/exception state if not discarded by a
1070 * new aborting error, interrupt, or uncaught exception. */
1071 leave_cleanup(&cs);
1072# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 }
1074 swap_exists_action = SEA_NONE;
1075}
1076#endif
1077
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078/*
1079 * do_bufdel() - delete or unload buffer(s)
1080 *
1081 * addr_count == 0: ":bdel" - delete current buffer
1082 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1083 * buffer "end_bnr", then any other arguments.
1084 * addr_count == 2: ":N,N bdel" - delete buffers in range
1085 *
1086 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1087 * DOBUF_DEL (":bdel")
1088 *
1089 * Returns error message or NULL
1090 */
1091 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001092do_bufdel(
1093 int command,
1094 char_u *arg, /* pointer to extra arguments */
1095 int addr_count,
1096 int start_bnr, /* first buffer number in a range */
1097 int end_bnr, /* buffer nr or last buffer nr in a range */
1098 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099{
1100 int do_current = 0; /* delete current buffer? */
1101 int deleted = 0; /* number of buffers deleted */
1102 char_u *errormsg = NULL; /* return value */
1103 int bnr; /* buffer number */
1104 char_u *p;
1105
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 if (addr_count == 0)
1107 {
1108 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1109 }
1110 else
1111 {
1112 if (addr_count == 2)
1113 {
1114 if (*arg) /* both range and argument is not allowed */
1115 return (char_u *)_(e_trailing);
1116 bnr = start_bnr;
1117 }
1118 else /* addr_count == 1 */
1119 bnr = end_bnr;
1120
1121 for ( ;!got_int; ui_breakcheck())
1122 {
1123 /*
1124 * delete the current buffer last, otherwise when the
1125 * current buffer is deleted, the next buffer becomes
1126 * the current one and will be loaded, which may then
1127 * also be deleted, etc.
1128 */
1129 if (bnr == curbuf->b_fnum)
1130 do_current = bnr;
1131 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
1132 forceit) == OK)
1133 ++deleted;
1134
1135 /*
1136 * find next buffer number to delete/unload
1137 */
1138 if (addr_count == 2)
1139 {
1140 if (++bnr > end_bnr)
1141 break;
1142 }
1143 else /* addr_count == 1 */
1144 {
1145 arg = skipwhite(arg);
1146 if (*arg == NUL)
1147 break;
1148 if (!VIM_ISDIGIT(*arg))
1149 {
1150 p = skiptowhite_esc(arg);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01001151 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE,
1152 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 if (bnr < 0) /* failed */
1154 break;
1155 arg = p;
1156 }
1157 else
1158 bnr = getdigits(&arg);
1159 }
1160 }
1161 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1162 FORWARD, do_current, forceit) == OK)
1163 ++deleted;
1164
1165 if (deleted == 0)
1166 {
1167 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001168 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001170 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00001172 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 errormsg = IObuff;
1174 }
1175 else if (deleted >= p_report)
1176 {
1177 if (command == DOBUF_UNLOAD)
1178 {
1179 if (deleted == 1)
1180 MSG(_("1 buffer unloaded"));
1181 else
1182 smsg((char_u *)_("%d buffers unloaded"), deleted);
1183 }
1184 else if (command == DOBUF_DEL)
1185 {
1186 if (deleted == 1)
1187 MSG(_("1 buffer deleted"));
1188 else
1189 smsg((char_u *)_("%d buffers deleted"), deleted);
1190 }
1191 else
1192 {
1193 if (deleted == 1)
1194 MSG(_("1 buffer wiped out"));
1195 else
1196 smsg((char_u *)_("%d buffers wiped out"), deleted);
1197 }
1198 }
1199 }
1200
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201
1202 return errormsg;
1203}
1204
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001205static int empty_curbuf(int close_others, int forceit, int action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001206
1207/*
1208 * Make the current buffer empty.
1209 * Used when it is wiped out and it's the last buffer.
1210 */
1211 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001212empty_curbuf(
1213 int close_others,
1214 int forceit,
1215 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001216{
1217 int retval;
1218 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001219 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001220
1221 if (action == DOBUF_UNLOAD)
1222 {
1223 EMSG(_("E90: Cannot unload last buffer"));
1224 return FAIL;
1225 }
1226
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001227 set_bufref(&bufref, buf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001228 if (close_others)
1229 /* Close any other windows on this buffer, then make it empty. */
Bram Moolenaara02471e2014-01-10 16:43:14 +01001230 close_windows(buf, TRUE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001231
1232 setpcmark();
1233 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1234 forceit ? ECMD_FORCEIT : 0, curwin);
1235
1236 /*
1237 * do_ecmd() may create a new buffer, then we have to delete
1238 * the old one. But do_ecmd() may have done that already, check
1239 * if the buffer still exists.
1240 */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001241 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001242 close_buffer(NULL, buf, action, FALSE);
1243 if (!close_others)
1244 need_fileinfo = FALSE;
1245 return retval;
1246}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247/*
1248 * Implementation of the commands for the buffer list.
1249 *
1250 * action == DOBUF_GOTO go to specified buffer
1251 * action == DOBUF_SPLIT split window and go to specified buffer
1252 * action == DOBUF_UNLOAD unload specified buffer(s)
1253 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1254 * action == DOBUF_WIPE delete specified buffer(s) really
1255 *
1256 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1257 * start == DOBUF_FIRST go to "count" buffer from first buffer
1258 * start == DOBUF_LAST go to "count" buffer from last buffer
1259 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1260 *
1261 * Return FAIL or OK.
1262 */
1263 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001264do_buffer(
1265 int action,
1266 int start,
1267 int dir, /* FORWARD or BACKWARD */
1268 int count, /* buffer number or number of buffers */
1269 int forceit) /* TRUE for :...! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270{
1271 buf_T *buf;
1272 buf_T *bp;
1273 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1274 || action == DOBUF_WIPE);
1275
1276 switch (start)
1277 {
1278 case DOBUF_FIRST: buf = firstbuf; break;
1279 case DOBUF_LAST: buf = lastbuf; break;
1280 default: buf = curbuf; break;
1281 }
1282 if (start == DOBUF_MOD) /* find next modified buffer */
1283 {
1284 while (count-- > 0)
1285 {
1286 do
1287 {
1288 buf = buf->b_next;
1289 if (buf == NULL)
1290 buf = firstbuf;
1291 }
1292 while (buf != curbuf && !bufIsChanged(buf));
1293 }
1294 if (!bufIsChanged(buf))
1295 {
1296 EMSG(_("E84: No modified buffer found"));
1297 return FAIL;
1298 }
1299 }
1300 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
1301 {
1302 while (buf != NULL && buf->b_fnum != count)
1303 buf = buf->b_next;
1304 }
1305 else
1306 {
1307 bp = NULL;
1308 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1309 {
1310 /* remember the buffer where we start, we come back there when all
1311 * buffers are unlisted. */
1312 if (bp == NULL)
1313 bp = buf;
1314 if (dir == FORWARD)
1315 {
1316 buf = buf->b_next;
1317 if (buf == NULL)
1318 buf = firstbuf;
1319 }
1320 else
1321 {
1322 buf = buf->b_prev;
1323 if (buf == NULL)
1324 buf = lastbuf;
1325 }
1326 /* don't count unlisted buffers */
1327 if (unload || buf->b_p_bl)
1328 {
1329 --count;
1330 bp = NULL; /* use this buffer as new starting point */
1331 }
1332 if (bp == buf)
1333 {
1334 /* back where we started, didn't find anything. */
1335 EMSG(_("E85: There is no listed buffer"));
1336 return FAIL;
1337 }
1338 }
1339 }
1340
1341 if (buf == NULL) /* could not find it */
1342 {
1343 if (start == DOBUF_FIRST)
1344 {
1345 /* don't warn when deleting */
1346 if (!unload)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01001347 EMSGN(_(e_nobufnr), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 }
1349 else if (dir == FORWARD)
1350 EMSG(_("E87: Cannot go beyond last buffer"));
1351 else
1352 EMSG(_("E88: Cannot go before first buffer"));
1353 return FAIL;
1354 }
1355
1356#ifdef FEAT_GUI
1357 need_mouse_correct = TRUE;
1358#endif
1359
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 /*
1361 * delete buffer buf from memory and/or the list
1362 */
1363 if (unload)
1364 {
1365 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001366 bufref_T bufref;
1367
Bram Moolenaara997b452018-04-17 23:24:06 +02001368 if (buf->b_locked)
1369 {
1370 EMSG(_(e_buflocked));
1371 return FAIL;
1372 }
1373
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001374 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375
1376 /* When unloading or deleting a buffer that's already unloaded and
1377 * unlisted: fail silently. */
1378 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1379 return FAIL;
1380
1381 if (!forceit && bufIsChanged(buf))
1382 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001383#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 if ((p_confirm || cmdmod.confirm) && p_write)
1385 {
1386 dialog_changed(buf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001387 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 /* Autocommand deleted buffer, oops! It's not changed
1389 * now. */
1390 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001391 /* If it's still changed fail silently, the dialog already
1392 * mentioned why it fails. */
1393 if (bufIsChanged(buf))
1394 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001396 else
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 {
1399 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1400 buf->b_fnum);
1401 return FAIL;
1402 }
1403 }
1404
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001405 /* When closing the current buffer stop Visual mode. */
Bram Moolenaar4930a762016-09-11 14:39:53 +02001406 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001407 end_visual_mode();
1408
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 /*
1410 * If deleting the last (listed) buffer, make it empty.
1411 * The last (listed) buffer cannot be unloaded.
1412 */
Bram Moolenaar29323592016-07-24 22:04:11 +02001413 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 if (bp->b_p_bl && bp != buf)
1415 break;
1416 if (bp == NULL && buf == curbuf)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001417 return empty_curbuf(TRUE, forceit, action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 /*
1420 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001421 * (unless it's the only window). Repeat this so long as we end up in
1422 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001424 while (buf == curbuf
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001425 && !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
Bram Moolenaar459ca562016-11-10 18:16:33 +01001426 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001427 {
1428 if (win_close(curwin, FALSE) == FAIL)
1429 break;
1430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431
1432 /*
1433 * If the buffer to be deleted is not the current one, delete it here.
1434 */
1435 if (buf != curbuf)
1436 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001437 close_windows(buf, FALSE);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001438 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows <= 0)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001439 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 return OK;
1441 }
1442
1443 /*
1444 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001445 * There should be another, otherwise it would have been handled
1446 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001447 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 * Then prefer the buffer we most recently visited.
1449 * Else try to find one that is loaded, after the current buffer,
1450 * then before the current buffer.
1451 * Finally use any buffer.
1452 */
1453 buf = NULL; /* selected buffer */
1454 bp = NULL; /* used when no loaded buffer found */
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001455 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1456 buf = au_new_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457#ifdef FEAT_JUMPLIST
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001458 else if (curwin->w_jumplistlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 {
1460 int jumpidx;
1461
1462 jumpidx = curwin->w_jumplistidx - 1;
1463 if (jumpidx < 0)
1464 jumpidx = curwin->w_jumplistlen - 1;
1465
1466 forward = jumpidx;
1467 while (jumpidx != curwin->w_jumplistidx)
1468 {
1469 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1470 if (buf != NULL)
1471 {
1472 if (buf == curbuf || !buf->b_p_bl)
1473 buf = NULL; /* skip current and unlisted bufs */
1474 else if (buf->b_ml.ml_mfp == NULL)
1475 {
1476 /* skip unloaded buf, but may keep it for later */
1477 if (bp == NULL)
1478 bp = buf;
1479 buf = NULL;
1480 }
1481 }
1482 if (buf != NULL) /* found a valid buffer: stop searching */
1483 break;
1484 /* advance to older entry in jump list */
1485 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1486 break;
1487 if (--jumpidx < 0)
1488 jumpidx = curwin->w_jumplistlen - 1;
1489 if (jumpidx == forward) /* List exhausted for sure */
1490 break;
1491 }
1492 }
1493#endif
1494
1495 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1496 {
1497 forward = TRUE;
1498 buf = curbuf->b_next;
1499 for (;;)
1500 {
1501 if (buf == NULL)
1502 {
1503 if (!forward) /* tried both directions */
1504 break;
1505 buf = curbuf->b_prev;
1506 forward = FALSE;
1507 continue;
1508 }
1509 /* in non-help buffer, try to skip help buffers, and vv */
1510 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1511 {
1512 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1513 break;
1514 if (bp == NULL) /* remember unloaded buf for later */
1515 bp = buf;
1516 }
1517 if (forward)
1518 buf = buf->b_next;
1519 else
1520 buf = buf->b_prev;
1521 }
1522 }
1523 if (buf == NULL) /* No loaded buffer, use unloaded one */
1524 buf = bp;
1525 if (buf == NULL) /* No loaded buffer, find listed one */
1526 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001527 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 if (buf->b_p_bl && buf != curbuf)
1529 break;
1530 }
1531 if (buf == NULL) /* Still no buffer, just take one */
1532 {
1533 if (curbuf->b_next != NULL)
1534 buf = curbuf->b_next;
1535 else
1536 buf = curbuf->b_prev;
1537 }
1538 }
1539
Bram Moolenaara02471e2014-01-10 16:43:14 +01001540 if (buf == NULL)
1541 {
1542 /* Autocommands must have wiped out all other buffers. Only option
1543 * now is to make the current buffer empty. */
1544 return empty_curbuf(FALSE, forceit, action);
1545 }
1546
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 /*
1548 * make buf current buffer
1549 */
1550 if (action == DOBUF_SPLIT) /* split window first */
1551 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001552 /* If 'switchbuf' contains "useopen": jump to first window containing
1553 * "buf" if one exists */
1554 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001555 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001556 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001557 * page containing "buf" if one exists */
1558 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 return OK;
1560 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 return FAIL;
1562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563
1564 /* go to current buffer - nothing to do */
1565 if (buf == curbuf)
1566 return OK;
1567
1568 /*
1569 * Check if the current buffer may be abandoned.
1570 */
1571 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1572 {
1573#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1574 if ((p_confirm || cmdmod.confirm) && p_write)
1575 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001576 bufref_T bufref;
1577
1578 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 dialog_changed(curbuf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001580 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 /* Autocommand deleted buffer, oops! */
1582 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 }
1584 if (bufIsChanged(curbuf))
1585#endif
1586 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001587 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 return FAIL;
1589 }
1590 }
1591
1592 /* Go to the other buffer. */
1593 set_curbuf(buf, action);
1594
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001596 {
1597 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
1598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599
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 Moolenaar071d4272004-06-13 20:20:40 +00001607
1608/*
1609 * Set current buffer to "buf". Executes autocommands and closes current
1610 * buffer. "action" tells how to close the current buffer:
1611 * DOBUF_GOTO free or hide it
1612 * DOBUF_SPLIT nothing
1613 * DOBUF_UNLOAD unload it
1614 * DOBUF_DEL delete it
1615 * DOBUF_WIPE wipe it out
1616 */
1617 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001618set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619{
1620 buf_T *prevbuf;
1621 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1622 || action == DOBUF_WIPE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001623#ifdef FEAT_SYN_HL
1624 long old_tw = curbuf->b_p_tw;
1625#endif
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001626 bufref_T newbufref;
1627 bufref_T prevbufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628
1629 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001630 if (!cmdmod.keepalt)
1631 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001632 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 /* Don't restart Select mode after switching to another buffer. */
1635 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001637 /* close_windows() or apply_autocmds() may change curbuf and wipe out "buf"
1638 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 prevbuf = curbuf;
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001640 set_bufref(&prevbufref, prevbuf);
1641 set_bufref(&newbufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001643 /* Autocommands may delete the curren buffer and/or the buffer we wan to go
1644 * to. In those cases don't close the buffer. */
Bram Moolenaar82404332016-07-10 17:00:38 +02001645 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001646 || (bufref_valid(&prevbufref)
1647 && bufref_valid(&newbufref)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001648#ifdef FEAT_EVAL
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001649 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001651 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001653#ifdef FEAT_SYN_HL
1654 if (prevbuf == curwin->w_buffer)
1655 reset_synblock(curwin);
1656#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001658 close_windows(prevbuf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001659#if defined(FEAT_EVAL)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001660 if (bufref_valid(&prevbufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661#else
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001662 if (bufref_valid(&prevbufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001664 {
Bram Moolenaare25865a2012-07-06 16:22:02 +02001665 win_T *previouswin = curwin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001666 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001667 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1669 unload ? action : (action == DOBUF_GOTO
Bram Moolenaareb44a682017-08-03 22:44:55 +02001670 && !buf_hide(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001671 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaare25865a2012-07-06 16:22:02 +02001672 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaar9e931222012-06-20 11:55:01 +02001673 /* autocommands changed curwin, Grr! */
Bram Moolenaare25865a2012-07-06 16:22:02 +02001674 curwin = previouswin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 }
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001677 /* An autocommand may have deleted "buf", already entered it (e.g., when
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001678 * it did ":bunload") or aborted the script processing.
Bram Moolenaar9e931222012-06-20 11:55:01 +02001679 * If curwin->w_buffer is null, enter_buffer() will make it valid again */
1680 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001681#ifdef FEAT_EVAL
Bram Moolenaar4033c552017-09-16 20:54:51 +02001682 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001684 ) || curwin->w_buffer == NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001685 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001687#ifdef FEAT_SYN_HL
1688 if (old_tw != curbuf->b_p_tw)
1689 check_colorcolumn(curwin);
1690#endif
1691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692}
1693
1694/*
1695 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001696 * Old curbuf must have been abandoned already! This also means "curbuf" may
1697 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 */
1699 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001700enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701{
1702 /* Copy buffer and window local option values. Not for a help buffer. */
1703 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1704 if (!buf->b_help)
1705 get_winopts(buf);
1706#ifdef FEAT_FOLDING
1707 else
1708 /* Remove all folds in the window. */
1709 clearFolding(curwin);
1710 foldUpdateAll(curwin); /* update folds (later). */
1711#endif
1712
1713 /* Get the buffer in the current window. */
1714 curwin->w_buffer = buf;
1715 curbuf = buf;
1716 ++curbuf->b_nwindows;
1717
1718#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001719 if (curwin->w_p_diff)
1720 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721#endif
1722
Bram Moolenaara971b822011-09-14 14:43:25 +02001723#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001724 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001725#endif
1726
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 /* Cursor on first line by default. */
1728 curwin->w_cursor.lnum = 1;
1729 curwin->w_cursor.col = 0;
1730#ifdef FEAT_VIRTUALEDIT
1731 curwin->w_cursor.coladd = 0;
1732#endif
1733 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001734 curwin->w_topline_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001736 /* mark cursor position as being invalid */
1737 curwin->w_valid = 0;
1738
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 /* Make sure the buffer is loaded. */
1740 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001741 {
1742 /* If there is no filetype, allow for detecting one. Esp. useful for
1743 * ":ball" used in a autocommand. If there already is a filetype we
1744 * might prefer to keep it. */
1745 if (*curbuf->b_p_ft == NUL)
1746 did_filetype = FALSE;
1747
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001748 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 else
1751 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001752 if (!msg_silent)
1753 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001756#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001758#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1760 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 }
1762
1763 /* If autocommands did not change the cursor position, restore cursor lnum
1764 * and possibly cursor col. */
1765 if (curwin->w_cursor.lnum == 1 && inindent(0))
1766 buflist_getfpos();
1767
1768 check_arg_idx(curwin); /* check for valid arg_idx */
1769#ifdef FEAT_TITLE
1770 maketitle();
1771#endif
Bram Moolenaard4153d42008-11-15 15:06:17 +00001772 /* when autocmds didn't change it */
1773 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1775
Bram Moolenaar009b2592004-10-24 19:18:58 +00001776#ifdef FEAT_NETBEANS_INTG
1777 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001778 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001779#endif
1780
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001781 /* Change directories when the 'acd' option is set. */
Bram Moolenaar6f470022018-04-10 18:47:20 +02001782 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783
1784#ifdef FEAT_KEYMAP
1785 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001786 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001788#ifdef FEAT_SPELL
1789 /* May need to set the spell language. Can only do this after the buffer
1790 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001791 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1792 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001793#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001794#ifdef FEAT_VIMINFO
1795 curbuf->b_last_used = vim_time();
1796#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001797
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 redraw_later(NOT_VALID);
1799}
1800
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001801#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1802/*
1803 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001804 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001805 */
1806 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001807do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001808{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001809 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001810 && curbuf->b_ffname != NULL
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001811 && vim_chdirfile(curbuf->b_ffname, "auto") == OK)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001812 shorten_fnames(TRUE);
1813}
1814#endif
1815
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001816 void
1817no_write_message(void)
1818{
1819#ifdef FEAT_TERMINAL
1820 if (term_job_running(curbuf->b_term))
1821 EMSG(_("E948: Job still running (add ! to end the job)"));
1822 else
1823#endif
1824 EMSG(_("E37: No write since last change (add ! to override)"));
1825}
1826
1827 void
Bram Moolenaar7a760922018-02-19 23:10:02 +01001828no_write_message_nobang(buf_T *buf UNUSED)
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001829{
1830#ifdef FEAT_TERMINAL
Bram Moolenaar7a760922018-02-19 23:10:02 +01001831 if (term_job_running(buf->b_term))
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001832 EMSG(_("E948: Job still running"));
1833 else
1834#endif
1835 EMSG(_("E37: No write since last change"));
1836}
1837
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838/*
1839 * functions for dealing with the buffer list
1840 */
1841
Bram Moolenaar480778b2016-07-14 22:09:39 +02001842static int top_file_num = 1; /* highest file number */
1843
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844/*
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001845 * Return TRUE if the current buffer is empty, unnamed, unmodified and used in
1846 * only one window. That means it can be re-used.
1847 */
1848 int
1849curbuf_reusable(void)
1850{
1851 return (curbuf != NULL
1852 && curbuf->b_ffname == NULL
1853 && curbuf->b_nwindows <= 1
1854 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY())
1855 && !curbufIsChanged());
1856}
1857
1858/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 * Add a file name to the buffer list. Return a pointer to the buffer.
1860 * If the same file name already exists return a pointer to that buffer.
1861 * If it does not exist, or if fname == NULL, a new entry is created.
1862 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1863 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1864 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001865 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001866 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
1867 * if the buffer already exists.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 * This is the ONLY way to create a new buffer.
1869 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001871buflist_new(
1872 char_u *ffname, /* full path of fname or relative */
1873 char_u *sfname, /* short fname or NULL */
1874 linenr_T lnum, /* preferred cursor line */
1875 int flags) /* BLN_ defines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876{
1877 buf_T *buf;
1878#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001879 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880#endif
1881
Bram Moolenaar480778b2016-07-14 22:09:39 +02001882 if (top_file_num == 1)
1883 hash_init(&buf_hashtab);
1884
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1886
1887 /*
1888 * If file name already exists in the list, update the entry.
1889 */
1890#ifdef UNIX
1891 /* On Unix we can use inode numbers when the file exists. Works better
1892 * for hard links. */
1893 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1894 st.st_dev = (dev_T)-1;
1895#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001896 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897#ifdef UNIX
1898 buflist_findname_stat(ffname, &st)
1899#else
1900 buflist_findname(ffname)
1901#endif
1902 ) != NULL)
1903 {
1904 vim_free(ffname);
1905 if (lnum != 0)
1906 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02001907
1908 if ((flags & BLN_NOOPT) == 0)
1909 /* copy the options now, if 'cpo' doesn't have 's' and not done
1910 * already */
1911 buf_copy_options(buf, 0);
1912
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1914 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001915 bufref_T bufref;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001916
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 buf->b_p_bl = TRUE;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001918 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001920 {
Bram Moolenaar82404332016-07-10 17:00:38 +02001921 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001922 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001923 return NULL;
1924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 }
1926 return buf;
1927 }
1928
1929 /*
1930 * If the current buffer has no name and no contents, use the current
1931 * buffer. Otherwise: Need to allocate a new buffer structure.
1932 *
1933 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001934 * (A spell file buffer is allocated in spell.c, but that's not a normal
1935 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 */
1937 buf = NULL;
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001938 if ((flags & BLN_CURBUF) && curbuf_reusable())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 {
1940 buf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 /* It's like this buffer is deleted. Watch out for autocommands that
1942 * change curbuf! If that happens, allocate a new buffer anyway. */
1943 if (curbuf->b_p_bl)
1944 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1945 if (buf == curbuf)
1946 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001947#ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001948 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 if (buf == curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 {
1953 /* Make sure 'bufhidden' and 'buftype' are empty */
1954 clear_string_option(&buf->b_p_bh);
1955 clear_string_option(&buf->b_p_bt);
1956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 }
1958 if (buf != curbuf || curbuf == NULL)
1959 {
1960 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1961 if (buf == NULL)
1962 {
1963 vim_free(ffname);
1964 return NULL;
1965 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02001966#ifdef FEAT_EVAL
1967 /* init b: variables */
1968 buf->b_vars = dict_alloc();
1969 if (buf->b_vars == NULL)
1970 {
1971 vim_free(ffname);
1972 vim_free(buf);
1973 return NULL;
1974 }
1975 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
1976#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01001977 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 }
1979
1980 if (ffname != NULL)
1981 {
1982 buf->b_ffname = ffname;
1983 buf->b_sfname = vim_strsave(sfname);
1984 }
1985
1986 clear_wininfo(buf);
1987 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1988
1989 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1990 || buf->b_wininfo == NULL)
1991 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001992 VIM_CLEAR(buf->b_ffname);
1993 VIM_CLEAR(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 if (buf != curbuf)
1995 free_buffer(buf);
1996 return NULL;
1997 }
1998
1999 if (buf == curbuf)
2000 {
2001 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002002 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 if (buf != curbuf) /* autocommands deleted the buffer! */
2004 return NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002005#if defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002006 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 return NULL;
2008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002010
2011 /* Init the options. */
2012 buf->b_p_initialized = FALSE;
2013 buf_copy_options(buf, BCO_ENTER);
2014
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015#ifdef FEAT_KEYMAP
2016 /* need to reload lmaps and set b:keymap_name */
2017 curbuf->b_kmap_state |= KEYMAP_INIT;
2018#endif
2019 }
2020 else
2021 {
2022 /*
2023 * put new buffer at the end of the buffer list
2024 */
2025 buf->b_next = NULL;
2026 if (firstbuf == NULL) /* buffer list is empty */
2027 {
2028 buf->b_prev = NULL;
2029 firstbuf = buf;
2030 }
2031 else /* append new buffer at end of list */
2032 {
2033 lastbuf->b_next = buf;
2034 buf->b_prev = lastbuf;
2035 }
2036 lastbuf = buf;
2037
2038 buf->b_fnum = top_file_num++;
2039 if (top_file_num < 0) /* wrap around (may cause duplicates) */
2040 {
2041 EMSG(_("W14: Warning: List of file names overflow"));
2042 if (emsg_silent == 0)
2043 {
2044 out_flush();
2045 ui_delay(3000L, TRUE); /* make sure it is noticed */
2046 }
2047 top_file_num = 1;
2048 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002049 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050
2051 /*
2052 * Always copy the options from the current buffer.
2053 */
2054 buf_copy_options(buf, BCO_ALWAYS);
2055 }
2056
2057 buf->b_wininfo->wi_fpos.lnum = lnum;
2058 buf->b_wininfo->wi_win = curwin;
2059
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002060#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002061 hash_init(&buf->b_s.b_keywtab);
2062 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063#endif
2064
2065 buf->b_fname = buf->b_sfname;
2066#ifdef UNIX
2067 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002068 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 else
2070 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002071 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 buf->b_dev = st.st_dev;
2073 buf->b_ino = st.st_ino;
2074 }
2075#endif
2076 buf->b_u_synced = TRUE;
2077 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002078 if (flags & BLN_DUMMY)
2079 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 buf_clear_file(buf);
2081 clrallmarks(buf); /* clear marks */
2082 fmarks_check_names(buf); /* check file marks for this file */
2083 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 if (!(flags & BLN_DUMMY))
2085 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002086 bufref_T bufref;
2087
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01002088 /* Tricky: these autocommands may change the buffer list. They could
2089 * also split the window with re-using the one empty buffer. This may
2090 * result in unexpectedly losing the empty buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002091 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002092 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002093 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002094 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002096 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002097 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002098 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002099 return NULL;
2100 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002101#ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002102 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106
2107 return buf;
2108}
2109
2110/*
2111 * Free the memory for the options of a buffer.
2112 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2113 * 'fileencoding'.
2114 */
2115 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002116free_buf_options(
2117 buf_T *buf,
2118 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119{
2120 if (free_p_ff)
2121 {
2122#ifdef FEAT_MBYTE
2123 clear_string_option(&buf->b_p_fenc);
2124#endif
2125 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 clear_string_option(&buf->b_p_bh);
2127 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 }
2129#ifdef FEAT_FIND_ID
2130 clear_string_option(&buf->b_p_def);
2131 clear_string_option(&buf->b_p_inc);
2132# ifdef FEAT_EVAL
2133 clear_string_option(&buf->b_p_inex);
2134# endif
2135#endif
2136#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
2137 clear_string_option(&buf->b_p_inde);
2138 clear_string_option(&buf->b_p_indk);
2139#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002140#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2141 clear_string_option(&buf->b_p_bexpr);
2142#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002143#if defined(FEAT_CRYPT)
2144 clear_string_option(&buf->b_p_cm);
2145#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002146 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002147#if defined(FEAT_EVAL)
2148 clear_string_option(&buf->b_p_fex);
2149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150#ifdef FEAT_CRYPT
2151 clear_string_option(&buf->b_p_key);
2152#endif
2153 clear_string_option(&buf->b_p_kp);
2154 clear_string_option(&buf->b_p_mps);
2155 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002156 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 clear_string_option(&buf->b_p_isk);
2158#ifdef FEAT_KEYMAP
2159 clear_string_option(&buf->b_p_keymap);
Bram Moolenaar50138322018-01-28 17:05:16 +01002160 keymap_clear(&buf->b_kmap_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 ga_clear(&buf->b_kmap_ga);
2162#endif
2163#ifdef FEAT_COMMENTS
2164 clear_string_option(&buf->b_p_com);
2165#endif
2166#ifdef FEAT_FOLDING
2167 clear_string_option(&buf->b_p_cms);
2168#endif
2169 clear_string_option(&buf->b_p_nf);
2170#ifdef FEAT_SYN_HL
2171 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002172 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002173#endif
2174#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002175 clear_string_option(&buf->b_s.b_p_spc);
2176 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002177 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002178 buf->b_s.b_cap_prog = NULL;
2179 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180#endif
2181#ifdef FEAT_SEARCHPATH
2182 clear_string_option(&buf->b_p_sua);
2183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 clear_string_option(&buf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185#ifdef FEAT_CINDENT
2186 clear_string_option(&buf->b_p_cink);
2187 clear_string_option(&buf->b_p_cino);
2188#endif
2189#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
2190 clear_string_option(&buf->b_p_cinw);
2191#endif
2192#ifdef FEAT_INS_EXPAND
2193 clear_string_option(&buf->b_p_cpt);
2194#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002195#ifdef FEAT_COMPL_FUNC
2196 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002197 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002198#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199#ifdef FEAT_QUICKFIX
2200 clear_string_option(&buf->b_p_gp);
2201 clear_string_option(&buf->b_p_mp);
2202 clear_string_option(&buf->b_p_efm);
2203#endif
2204 clear_string_option(&buf->b_p_ep);
2205 clear_string_option(&buf->b_p_path);
2206 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002207 clear_string_option(&buf->b_p_tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208#ifdef FEAT_INS_EXPAND
2209 clear_string_option(&buf->b_p_dict);
2210 clear_string_option(&buf->b_p_tsr);
2211#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002212#ifdef FEAT_TEXTOBJ
2213 clear_string_option(&buf->b_p_qe);
2214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002216 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002217#ifdef FEAT_LISP
2218 clear_string_option(&buf->b_p_lw);
2219#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002220 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002221#ifdef FEAT_MBYTE
2222 clear_string_option(&buf->b_p_menc);
2223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224}
2225
2226/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002227 * Get alternate file "n".
2228 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2229 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 * if (options & GETF_SETMARK) call setpcmark()
2231 * if (options & GETF_ALT) we are jumping to an alternate file.
2232 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2233 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002234 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 */
2236 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002237buflist_getfile(
2238 int n,
2239 linenr_T lnum,
2240 int options,
2241 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242{
2243 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 pos_T *fpos;
2246 colnr_T col;
2247
2248 buf = buflist_findnr(n);
2249 if (buf == NULL)
2250 {
2251 if ((options & GETF_ALT) && n == 0)
2252 EMSG(_(e_noalt));
2253 else
2254 EMSGN(_("E92: Buffer %ld not found"), n);
2255 return FAIL;
2256 }
2257
2258 /* if alternate file is the current buffer, nothing to do */
2259 if (buf == curbuf)
2260 return OK;
2261
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002262 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002263 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002264 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002266 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002267 if (curbuf_locked())
2268 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269
2270 /* altfpos may be changed by getfile(), get it now */
2271 if (lnum == 0)
2272 {
2273 fpos = buflist_findfpos(buf);
2274 lnum = fpos->lnum;
2275 col = fpos->col;
2276 }
2277 else
2278 col = 0;
2279
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 if (options & GETF_SWITCH)
2281 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002282 /* If 'switchbuf' contains "useopen": jump to first window containing
2283 * "buf" if one exists */
2284 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002286
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002287 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00002288 * page containing "buf" if one exists */
2289 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002290 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002291
2292 /* If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2293 * current buffer isn't empty: open new tab or window */
2294 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002295 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002297 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002298 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002299 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2300 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002302 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 }
2304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305
2306 ++RedrawingDisabled;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002307 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2308 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 {
2310 --RedrawingDisabled;
2311
2312 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
2313 if (!p_sol && col != 0)
2314 {
2315 curwin->w_cursor.col = col;
2316 check_cursor_col();
2317#ifdef FEAT_VIRTUALEDIT
2318 curwin->w_cursor.coladd = 0;
2319#endif
2320 curwin->w_set_curswant = TRUE;
2321 }
2322 return OK;
2323 }
2324 --RedrawingDisabled;
2325 return FAIL;
2326}
2327
2328/*
2329 * go to the last know line number for the current buffer
2330 */
2331 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002332buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333{
2334 pos_T *fpos;
2335
2336 fpos = buflist_findfpos(curbuf);
2337
2338 curwin->w_cursor.lnum = fpos->lnum;
2339 check_cursor_lnum();
2340
2341 if (p_sol)
2342 curwin->w_cursor.col = 0;
2343 else
2344 {
2345 curwin->w_cursor.col = fpos->col;
2346 check_cursor_col();
2347#ifdef FEAT_VIRTUALEDIT
2348 curwin->w_cursor.coladd = 0;
2349#endif
2350 curwin->w_set_curswant = TRUE;
2351 }
2352}
2353
Bram Moolenaar81695252004-12-29 20:58:21 +00002354#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2355/*
2356 * Find file in buffer list by name (it has to be for the current window).
2357 * Returns NULL if not found.
2358 */
2359 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002360buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002361{
2362 char_u *ffname;
2363 buf_T *buf = NULL;
2364
2365 /* First make the name into a full path name */
2366 ffname = FullName_save(fname,
2367#ifdef UNIX
2368 TRUE /* force expansion, get rid of symbolic links */
2369#else
2370 FALSE
2371#endif
2372 );
2373 if (ffname != NULL)
2374 {
2375 buf = buflist_findname(ffname);
2376 vim_free(ffname);
2377 }
2378 return buf;
2379}
2380#endif
2381
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382/*
2383 * Find file in buffer list by name (it has to be for the current window).
2384 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002385 * Skips dummy buffers.
2386 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 */
2388 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002389buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390{
2391#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002392 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393
2394 if (mch_stat((char *)ffname, &st) < 0)
2395 st.st_dev = (dev_T)-1;
2396 return buflist_findname_stat(ffname, &st);
2397}
2398
2399/*
2400 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2401 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002402 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 */
2404 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002405buflist_findname_stat(
2406 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002407 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408{
2409#endif
2410 buf_T *buf;
2411
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002412 /* Start at the last buffer, expect to find a match sooner. */
2413 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar81695252004-12-29 20:58:21 +00002414 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415#ifdef UNIX
2416 , stp
2417#endif
2418 ))
2419 return buf;
2420 return NULL;
2421}
2422
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423/*
2424 * Find file in buffer list by a regexp pattern.
2425 * Return fnum of the found buffer.
2426 * Return < 0 for error.
2427 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002429buflist_findpat(
2430 char_u *pattern,
2431 char_u *pattern_end, /* pointer to first char after pattern */
2432 int unlisted, /* find unlisted buffers */
2433 int diffmode UNUSED, /* find diff-mode buffers only */
2434 int curtab_only) /* find buffers in current tab only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435{
2436 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 int match = -1;
2438 int find_listed;
2439 char_u *pat;
2440 char_u *patend;
2441 int attempt;
2442 char_u *p;
2443 int toggledollar;
2444
2445 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2446 {
2447 if (*pattern == '%')
2448 match = curbuf->b_fnum;
2449 else
2450 match = curwin->w_alt_fnum;
2451#ifdef FEAT_DIFF
2452 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2453 match = -1;
2454#endif
2455 }
2456
2457 /*
2458 * Try four ways of matching a listed buffer:
2459 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002460 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 * attempt == 2: with '$' at end (only match at end)
2462 * attempt == 3: with '^' at start and '$' at end (only full match)
2463 * Repeat this for finding an unlisted buffer if there was no matching
2464 * listed buffer.
2465 */
2466 else
2467 {
2468 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2469 if (pat == NULL)
2470 return -1;
2471 patend = pat + STRLEN(pat) - 1;
2472 toggledollar = (patend > pat && *patend == '$');
2473
2474 /* First try finding a listed buffer. If not found and "unlisted"
2475 * is TRUE, try finding an unlisted buffer. */
2476 find_listed = TRUE;
2477 for (;;)
2478 {
2479 for (attempt = 0; attempt <= 3; ++attempt)
2480 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002481 regmatch_T regmatch;
2482
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 /* may add '^' and '$' */
2484 if (toggledollar)
2485 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2486 p = pat;
2487 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2488 ++p;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002489 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2490 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 {
2492 vim_free(pat);
2493 return -1;
2494 }
2495
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002496 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 if (buf->b_p_bl == find_listed
2498#ifdef FEAT_DIFF
2499 && (!diffmode || diff_mode_buf(buf))
2500#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002501 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002503 if (curtab_only)
2504 {
2505 /* Ignore the match if the buffer is not open in
2506 * the current tab. */
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002507 win_T *wp;
2508
Bram Moolenaar29323592016-07-24 22:04:11 +02002509 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002510 if (wp->w_buffer == buf)
2511 break;
2512 if (wp == NULL)
2513 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 if (match >= 0) /* already found a match */
2516 {
2517 match = -2;
2518 break;
2519 }
2520 match = buf->b_fnum; /* remember first match */
2521 }
2522
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002523 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 if (match >= 0) /* found one match */
2525 break;
2526 }
2527
2528 /* Only search for unlisted buffers if there was no match with
2529 * a listed buffer. */
2530 if (!unlisted || !find_listed || match != -1)
2531 break;
2532 find_listed = FALSE;
2533 }
2534
2535 vim_free(pat);
2536 }
2537
2538 if (match == -2)
2539 EMSG2(_("E93: More than one match for %s"), pattern);
2540 else if (match < 0)
2541 EMSG2(_("E94: No matching buffer for %s"), pattern);
2542 return match;
2543}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544
2545#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2546
2547/*
2548 * Find all buffer names that match.
2549 * For command line expansion of ":buf" and ":sbuf".
2550 * Return OK if matches found, FAIL otherwise.
2551 */
2552 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002553ExpandBufnames(
2554 char_u *pat,
2555 int *num_file,
2556 char_u ***file,
2557 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558{
2559 int count = 0;
2560 buf_T *buf;
2561 int round;
2562 char_u *p;
2563 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002564 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565
2566 *num_file = 0; /* return values in case of FAIL */
2567 *file = NULL;
2568
Bram Moolenaar05159a02005-02-26 23:04:13 +00002569 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2570 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002572 patc = alloc((unsigned)STRLEN(pat) + 11);
2573 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002575 STRCPY(patc, "\\(^\\|[\\/]\\)");
2576 STRCPY(patc + 11, pat + 1);
2577 }
2578 else
2579 patc = pat;
2580
2581 /*
2582 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002583 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002584 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002585 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002586 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002587 regmatch_T regmatch;
2588
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002589 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002590 break; /* there was no anchor, no need to try again */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002591 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2592 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002593 {
2594 if (patc != pat)
2595 vim_free(patc);
2596 return FAIL;
2597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598
2599 /*
2600 * round == 1: Count the matches.
2601 * round == 2: Build the array to keep the matches.
2602 */
2603 for (round = 1; round <= 2; ++round)
2604 {
2605 count = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002606 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 {
2608 if (!buf->b_p_bl) /* skip unlisted buffers */
2609 continue;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002610 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 if (p != NULL)
2612 {
2613 if (round == 1)
2614 ++count;
2615 else
2616 {
2617 if (options & WILD_HOME_REPLACE)
2618 p = home_replace_save(buf, p);
2619 else
2620 p = vim_strsave(p);
2621 (*file)[count++] = p;
2622 }
2623 }
2624 }
2625 if (count == 0) /* no match found, break here */
2626 break;
2627 if (round == 1)
2628 {
2629 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2630 if (*file == NULL)
2631 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002632 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002633 if (patc != pat)
2634 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 return FAIL;
2636 }
2637 }
2638 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002639 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 if (count) /* match(es) found, break here */
2641 break;
2642 }
2643
Bram Moolenaar05159a02005-02-26 23:04:13 +00002644 if (patc != pat)
2645 vim_free(patc);
2646
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 *num_file = count;
2648 return (count == 0 ? FAIL : OK);
2649}
2650
2651#endif /* FEAT_CMDL_COMPL */
2652
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653/*
2654 * Check for a match on the file name for buffer "buf" with regprog "prog".
2655 */
2656 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002657buflist_match(
2658 regmatch_T *rmp,
2659 buf_T *buf,
2660 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661{
2662 char_u *match;
2663
2664 /* First try the short file name, then the long file name. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002665 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002667 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668
2669 return match;
2670}
2671
2672/*
2673 * Try matching the regexp in "prog" with file name "name".
2674 * Return "name" when there is a match, NULL when not.
2675 */
2676 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002677fname_match(
2678 regmatch_T *rmp,
2679 char_u *name,
2680 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681{
2682 char_u *match = NULL;
2683 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684
2685 if (name != NULL)
2686 {
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002687 /* Ignore case when 'fileignorecase' or the argument is set. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002688 rmp->rm_ic = p_fic || ignore_case;
2689 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 match = name;
2691 else
2692 {
2693 /* Replace $(HOME) with '~' and try matching again. */
2694 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002695 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 match = name;
2697 vim_free(p);
2698 }
2699 }
2700
2701 return match;
2702}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703
2704/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02002705 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 */
2707 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002708buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709{
Bram Moolenaar480778b2016-07-14 22:09:39 +02002710 char_u key[VIM_SIZEOF_INT * 2 + 1];
2711 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712
2713 if (nr == 0)
2714 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02002715 sprintf((char *)key, "%x", nr);
2716 hi = hash_find(&buf_hashtab, key);
2717
2718 if (!HASHITEM_EMPTY(hi))
2719 return (buf_T *)(hi->hi_key
2720 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 return NULL;
2722}
2723
2724/*
2725 * Get name of file 'n' in the buffer list.
2726 * When the file has no name an empty string is returned.
2727 * home_replace() is used to shorten the file name (used for marks).
2728 * Returns a pointer to allocated memory, of NULL when failed.
2729 */
2730 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002731buflist_nr2name(
2732 int n,
2733 int fullname,
2734 int helptail) /* for help buffers return tail only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735{
2736 buf_T *buf;
2737
2738 buf = buflist_findnr(n);
2739 if (buf == NULL)
2740 return NULL;
2741 return home_replace_save(helptail ? buf : NULL,
2742 fullname ? buf->b_ffname : buf->b_fname);
2743}
2744
2745/*
2746 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2747 * When "copy_options" is TRUE save the local window option values.
2748 * When "lnum" is 0 only do the options.
2749 */
2750 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002751buflist_setfpos(
2752 buf_T *buf,
2753 win_T *win,
2754 linenr_T lnum,
2755 colnr_T col,
2756 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757{
2758 wininfo_T *wip;
2759
2760 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2761 if (wip->wi_win == win)
2762 break;
2763 if (wip == NULL)
2764 {
2765 /* allocate a new entry */
2766 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2767 if (wip == NULL)
2768 return;
2769 wip->wi_win = win;
2770 if (lnum == 0) /* set lnum even when it's 0 */
2771 lnum = 1;
2772 }
2773 else
2774 {
2775 /* remove the entry from the list */
2776 if (wip->wi_prev)
2777 wip->wi_prev->wi_next = wip->wi_next;
2778 else
2779 buf->b_wininfo = wip->wi_next;
2780 if (wip->wi_next)
2781 wip->wi_next->wi_prev = wip->wi_prev;
2782 if (copy_options && wip->wi_optset)
2783 {
2784 clear_winopt(&wip->wi_opt);
2785#ifdef FEAT_FOLDING
2786 deleteFoldRecurse(&wip->wi_folds);
2787#endif
2788 }
2789 }
2790 if (lnum != 0)
2791 {
2792 wip->wi_fpos.lnum = lnum;
2793 wip->wi_fpos.col = col;
2794 }
2795 if (copy_options)
2796 {
2797 /* Save the window-specific option values. */
2798 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2799#ifdef FEAT_FOLDING
2800 wip->wi_fold_manual = win->w_fold_manual;
2801 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2802#endif
2803 wip->wi_optset = TRUE;
2804 }
2805
2806 /* insert the entry in front of the list */
2807 wip->wi_next = buf->b_wininfo;
2808 buf->b_wininfo = wip;
2809 wip->wi_prev = NULL;
2810 if (wip->wi_next)
2811 wip->wi_next->wi_prev = wip;
2812
2813 return;
2814}
2815
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002816#ifdef FEAT_DIFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01002817static int wininfo_other_tab_diff(wininfo_T *wip);
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002818
2819/*
2820 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2821 * page. That's because a diff is local to a tab page.
2822 */
2823 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002824wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002825{
2826 win_T *wp;
2827
2828 if (wip->wi_opt.wo_diff)
2829 {
Bram Moolenaar29323592016-07-24 22:04:11 +02002830 FOR_ALL_WINDOWS(wp)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002831 /* return FALSE when it's a window in the current tab page, thus
2832 * the buffer was in diff mode here */
2833 if (wip->wi_win == wp)
2834 return FALSE;
2835 return TRUE;
2836 }
2837 return FALSE;
2838}
2839#endif
2840
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841/*
2842 * Find info for the current window in buffer "buf".
2843 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002844 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2845 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 * Returns NULL when there isn't any info.
2847 */
2848 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002849find_wininfo(
2850 buf_T *buf,
2851 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852{
2853 wininfo_T *wip;
2854
2855 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002856 if (wip->wi_win == curwin
2857#ifdef FEAT_DIFF
2858 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2859#endif
2860 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002862
2863 /* If no wininfo for curwin, use the first in the list (that doesn't have
2864 * 'diff' set and is in another tab page). */
2865 if (wip == NULL)
2866 {
2867#ifdef FEAT_DIFF
2868 if (skip_diff_buffer)
2869 {
2870 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2871 if (!wininfo_other_tab_diff(wip))
2872 break;
2873 }
2874 else
2875#endif
2876 wip = buf->b_wininfo;
2877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 return wip;
2879}
2880
2881/*
2882 * Reset the local window options to the values last used in this window.
2883 * If the buffer wasn't used in this window before, use the values from
2884 * the most recently used window. If the values were never set, use the
2885 * global values for the window.
2886 */
2887 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002888get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889{
2890 wininfo_T *wip;
2891
2892 clear_winopt(&curwin->w_onebuf_opt);
2893#ifdef FEAT_FOLDING
2894 clearFolding(curwin);
2895#endif
2896
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002897 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 if (wip != NULL && wip->wi_optset)
2899 {
2900 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2901#ifdef FEAT_FOLDING
2902 curwin->w_fold_manual = wip->wi_fold_manual;
2903 curwin->w_foldinvalid = TRUE;
2904 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2905#endif
2906 }
2907 else
2908 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2909
2910#ifdef FEAT_FOLDING
2911 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2912 if (p_fdls >= 0)
2913 curwin->w_p_fdl = p_fdls;
2914#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002915#ifdef FEAT_SYN_HL
2916 check_colorcolumn(curwin);
2917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918}
2919
2920/*
2921 * Find the position (lnum and col) for the buffer 'buf' for the current
2922 * window.
2923 * Returns a pointer to no_position if no position is found.
2924 */
2925 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002926buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927{
2928 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002929 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002931 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 if (wip != NULL)
2933 return &(wip->wi_fpos);
2934 else
2935 return &no_position;
2936}
2937
2938/*
2939 * Find the lnum for the buffer 'buf' for the current window.
2940 */
2941 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01002942buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943{
2944 return buflist_findfpos(buf)->lnum;
2945}
2946
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002948 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002951buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952{
2953 buf_T *buf;
2954 int len;
2955 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02002956 int ro_char;
2957 int changed_char;
Bram Moolenaar0751f512018-03-29 16:37:16 +02002958#ifdef FEAT_TERMINAL
2959 int job_running;
2960 int job_none_open;
2961#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962
2963 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2964 {
Bram Moolenaar0751f512018-03-29 16:37:16 +02002965#ifdef FEAT_TERMINAL
2966 job_running = term_job_running(buf->b_term);
2967 job_none_open = job_running && term_none_open(buf->b_term);
2968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 /* skip unlisted buffers, unless ! was used */
Bram Moolenaard51cb702015-07-21 15:03:06 +02002970 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
2971 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
2972 || (vim_strchr(eap->arg, '+')
2973 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
2974 || (vim_strchr(eap->arg, 'a')
Bram Moolenaar0751f512018-03-29 16:37:16 +02002975 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
Bram Moolenaard51cb702015-07-21 15:03:06 +02002976 || (vim_strchr(eap->arg, 'h')
Bram Moolenaar0751f512018-03-29 16:37:16 +02002977 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
2978#ifdef FEAT_TERMINAL
2979 || (vim_strchr(eap->arg, 'R')
2980 && (!job_running || (job_running && job_none_open)))
2981 || (vim_strchr(eap->arg, '?')
2982 && (!job_running || (job_running && !job_none_open)))
2983 || (vim_strchr(eap->arg, 'F')
2984 && (job_running || buf->b_term == NULL))
2985#endif
Bram Moolenaard51cb702015-07-21 15:03:06 +02002986 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
2987 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
2988 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
2989 || (vim_strchr(eap->arg, '%') && buf != curbuf)
2990 || (vim_strchr(eap->arg, '#')
2991 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02002994 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 else
2996 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02002997 if (message_filtered(NameBuff))
2998 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003000 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3001 : (bufIsChanged(buf) ? '+' : ' ');
3002#ifdef FEAT_TERMINAL
3003 if (term_job_running(buf->b_term))
3004 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02003005 if (term_none_open(buf->b_term))
3006 ro_char = '?';
3007 else
3008 ro_char = 'R';
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003009 changed_char = ' '; /* bufIsChanged() returns TRUE to avoid
3010 * closing, but it's not actually changed. */
3011 }
3012 else if (buf->b_term != NULL)
3013 ro_char = 'F';
3014 else
3015#endif
3016 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3017
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003018 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00003019 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 buf->b_fnum,
3021 buf->b_p_bl ? ' ' : 'u',
3022 buf == curbuf ? '%' :
3023 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3024 buf->b_ml.ml_mfp == NULL ? ' ' :
3025 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003026 ro_char,
3027 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003028 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003029 if (len > IOSIZE - 20)
3030 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031
3032 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 i = 40 - vim_strsize(IObuff);
3034 do
3035 {
3036 IObuff[len++] = ' ';
3037 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003038 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3039 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003040 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 msg_outtrans(IObuff);
3042 out_flush(); /* output one line at a time */
3043 ui_breakcheck();
3044 }
3045}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046
3047/*
3048 * Get file name and line number for file 'fnum'.
3049 * Used by DoOneCmd() for translating '%' and '#'.
3050 * Used by insert_reg() and cmdline_paste() for '#' register.
3051 * Return FAIL if not found, OK for success.
3052 */
3053 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003054buflist_name_nr(
3055 int fnum,
3056 char_u **fname,
3057 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058{
3059 buf_T *buf;
3060
3061 buf = buflist_findnr(fnum);
3062 if (buf == NULL || buf->b_fname == NULL)
3063 return FAIL;
3064
3065 *fname = buf->b_fname;
3066 *lnum = buflist_findlnum(buf);
3067
3068 return OK;
3069}
3070
3071/*
3072 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
3073 * The file name with the full path is also remembered, for when :cd is used.
3074 * Returns FAIL for failure (file name already in use by other buffer)
3075 * OK otherwise.
3076 */
3077 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003078setfname(
3079 buf_T *buf,
3080 char_u *ffname,
3081 char_u *sfname,
3082 int message) /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083{
Bram Moolenaar81695252004-12-29 20:58:21 +00003084 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003086 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087#endif
3088
3089 if (ffname == NULL || *ffname == NUL)
3090 {
3091 /* Removing the name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01003092 VIM_CLEAR(buf->b_ffname);
3093 VIM_CLEAR(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094#ifdef UNIX
3095 st.st_dev = (dev_T)-1;
3096#endif
3097 }
3098 else
3099 {
3100 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
3101 if (ffname == NULL) /* out of memory */
3102 return FAIL;
3103
3104 /*
3105 * if the file name is already used in another buffer:
3106 * - if the buffer is loaded, fail
3107 * - if the buffer is not loaded, delete it from the list
3108 */
3109#ifdef UNIX
3110 if (mch_stat((char *)ffname, &st) < 0)
3111 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003112#endif
3113 if (!(buf->b_flags & BF_DUMMY))
3114#ifdef UNIX
3115 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003117 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118#endif
3119 if (obuf != NULL && obuf != buf)
3120 {
3121 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
3122 {
3123 if (message)
3124 EMSG(_("E95: Buffer with this name already exists"));
3125 vim_free(ffname);
3126 return FAIL;
3127 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01003128 /* delete from the list */
3129 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 }
3131 sfname = vim_strsave(sfname);
3132 if (ffname == NULL || sfname == NULL)
3133 {
3134 vim_free(sfname);
3135 vim_free(ffname);
3136 return FAIL;
3137 }
3138#ifdef USE_FNAME_CASE
3139# ifdef USE_LONG_FNAME
3140 if (USE_LONG_FNAME)
3141# endif
3142 fname_case(sfname, 0); /* set correct case for short file name */
3143#endif
3144 vim_free(buf->b_ffname);
3145 vim_free(buf->b_sfname);
3146 buf->b_ffname = ffname;
3147 buf->b_sfname = sfname;
3148 }
3149 buf->b_fname = buf->b_sfname;
3150#ifdef UNIX
3151 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003152 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 else
3154 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003155 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 buf->b_dev = st.st_dev;
3157 buf->b_ino = st.st_ino;
3158 }
3159#endif
3160
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162
3163 buf_name_changed(buf);
3164 return OK;
3165}
3166
3167/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003168 * Crude way of changing the name of a buffer. Use with care!
3169 * The name should be relative to the current directory.
3170 */
3171 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003172buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003173{
3174 buf_T *buf;
3175
3176 buf = buflist_findnr(fnum);
3177 if (buf != NULL)
3178 {
3179 vim_free(buf->b_sfname);
3180 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003181 buf->b_ffname = vim_strsave(name);
3182 buf->b_sfname = NULL;
3183 /* Allocate ffname and expand into full path. Also resolves .lnk
3184 * files on Win32. */
3185 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003186 buf->b_fname = buf->b_sfname;
3187 }
3188}
3189
3190/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 * Take care of what needs to be done when the name of buffer "buf" has
3192 * changed.
3193 */
3194 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003195buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196{
3197 /*
3198 * If the file name changed, also change the name of the swapfile
3199 */
3200 if (buf->b_ml.ml_mfp != NULL)
3201 ml_setname(buf);
3202
3203 if (curwin->w_buffer == buf)
3204 check_arg_idx(curwin); /* check file name for arg list */
3205#ifdef FEAT_TITLE
3206 maketitle(); /* set window title */
3207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 status_redraw_all(); /* status lines need to be redrawn */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 fmarks_check_names(buf); /* check named file marks */
3210 ml_timestamp(buf); /* reset timestamp */
3211}
3212
3213/*
3214 * set alternate file name for current window
3215 *
3216 * Used by do_one_cmd(), do_write() and do_ecmd().
3217 * Return the buffer.
3218 */
3219 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003220setaltfname(
3221 char_u *ffname,
3222 char_u *sfname,
3223 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224{
3225 buf_T *buf;
3226
3227 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
3228 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003229 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 curwin->w_alt_fnum = buf->b_fnum;
3231 return buf;
3232}
3233
3234/*
3235 * Get alternate file name for current window.
3236 * Return NULL if there isn't any, and give error message if requested.
3237 */
3238 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003239getaltfname(
3240 int errmsg) /* give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241{
3242 char_u *fname;
3243 linenr_T dummy;
3244
3245 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3246 {
3247 if (errmsg)
3248 EMSG(_(e_noalt));
3249 return NULL;
3250 }
3251 return fname;
3252}
3253
3254/*
3255 * Add a file name to the buflist and return its number.
3256 * Uses same flags as buflist_new(), except BLN_DUMMY.
3257 *
3258 * used by qf_init(), main() and doarglist()
3259 */
3260 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003261buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262{
3263 buf_T *buf;
3264
3265 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3266 if (buf != NULL)
3267 return buf->b_fnum;
3268 return 0;
3269}
3270
3271#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3272/*
3273 * Adjust slashes in file names. Called after 'shellslash' was set.
3274 */
3275 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003276buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277{
3278 buf_T *bp;
3279
Bram Moolenaar29323592016-07-24 22:04:11 +02003280 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 {
3282 if (bp->b_ffname != NULL)
3283 slash_adjust(bp->b_ffname);
3284 if (bp->b_sfname != NULL)
3285 slash_adjust(bp->b_sfname);
3286 }
3287}
3288#endif
3289
3290/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003291 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 * Also save the local window option values.
3293 */
3294 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003295buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003297 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298}
3299
3300/*
3301 * Return TRUE if 'ffname' is not the same file as current file.
3302 * Fname must have a full path (expanded by mch_FullName()).
3303 */
3304 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003305otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306{
3307 return otherfile_buf(curbuf, ffname
3308#ifdef UNIX
3309 , NULL
3310#endif
3311 );
3312}
3313
3314 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003315otherfile_buf(
3316 buf_T *buf,
3317 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003319 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003321 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322{
3323 /* no name is different */
3324 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3325 return TRUE;
3326 if (fnamecmp(ffname, buf->b_ffname) == 0)
3327 return FALSE;
3328#ifdef UNIX
3329 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003330 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331
Bram Moolenaar8767f522016-07-01 17:17:39 +02003332 /* If no stat_T given, get it now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 if (stp == NULL)
3334 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003335 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 st.st_dev = (dev_T)-1;
3337 stp = &st;
3338 }
3339 /* Use dev/ino to check if the files are the same, even when the names
3340 * are different (possible with links). Still need to compare the
3341 * name above, for when the file doesn't exist yet.
3342 * Problem: The dev/ino changes when a file is deleted (and created
3343 * again) and remains the same when renamed/moved. We don't want to
3344 * mch_stat() each buffer each time, that would be too slow. Get the
3345 * dev/ino again when they appear to match, but not when they appear
3346 * to be different: Could skip a buffer when it's actually the same
3347 * file. */
3348 if (buf_same_ino(buf, stp))
3349 {
3350 buf_setino(buf);
3351 if (buf_same_ino(buf, stp))
3352 return FALSE;
3353 }
3354 }
3355#endif
3356 return TRUE;
3357}
3358
3359#if defined(UNIX) || defined(PROTO)
3360/*
3361 * Set inode and device number for a buffer.
3362 * Must always be called when b_fname is changed!.
3363 */
3364 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003365buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003367 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368
3369 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3370 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003371 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 buf->b_dev = st.st_dev;
3373 buf->b_ino = st.st_ino;
3374 }
3375 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003376 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377}
3378
3379/*
3380 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3381 */
3382 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003383buf_same_ino(
3384 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003385 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003387 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 && stp->st_dev == buf->b_dev
3389 && stp->st_ino == buf->b_ino);
3390}
3391#endif
3392
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003393/*
3394 * Print info about the current buffer.
3395 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003397fileinfo(
3398 int fullname, /* when non-zero print full path */
3399 int shorthelp,
3400 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401{
3402 char_u *name;
3403 int n;
3404 char_u *p;
3405 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003406 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407
3408 buffer = alloc(IOSIZE);
3409 if (buffer == NULL)
3410 return;
3411
3412 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3413 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003414 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 p = buffer + STRLEN(buffer);
3416 }
3417 else
3418 p = buffer;
3419
3420 *p++ = '"';
3421 if (buf_spname(curbuf) != NULL)
Bram Moolenaarec3cfeb2012-10-03 17:12:47 +02003422 vim_strncpy(p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 else
3424 {
3425 if (!fullname && curbuf->b_fname != NULL)
3426 name = curbuf->b_fname;
3427 else
3428 name = curbuf->b_ffname;
3429 home_replace(shorthelp ? curbuf : NULL, name, p,
3430 (int)(IOSIZE - (p - buffer)), TRUE);
3431 }
3432
Bram Moolenaara800b422010-06-27 01:15:55 +02003433 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 curbufIsChanged() ? (shortmess(SHM_MOD)
3435 ? " [+]" : _(" [Modified]")) : " ",
3436 (curbuf->b_flags & BF_NOTEDITED)
3437#ifdef FEAT_QUICKFIX
3438 && !bt_dontwrite(curbuf)
3439#endif
3440 ? _("[Not edited]") : "",
3441 (curbuf->b_flags & BF_NEW)
3442#ifdef FEAT_QUICKFIX
3443 && !bt_dontwrite(curbuf)
3444#endif
3445 ? _("[New file]") : "",
3446 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003447 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 : _("[readonly]")) : "",
3449 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3450 || curbuf->b_p_ro) ?
3451 " " : "");
3452 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3453 * causes an overflow, thus for large numbers divide instead. */
3454 if (curwin->w_cursor.lnum > 1000000L)
3455 n = (int)(((long)curwin->w_cursor.lnum) /
3456 ((long)curbuf->b_ml.ml_line_count / 100L));
3457 else
3458 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3459 (long)curbuf->b_ml.ml_line_count);
3460 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3461 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003462 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 }
3464#ifdef FEAT_CMDL_INFO
3465 else if (p_ru)
3466 {
3467 /* Current line and column are already on the screen -- webb */
3468 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003469 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003471 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 (long)curbuf->b_ml.ml_line_count, n);
3473 }
3474#endif
3475 else
3476 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003477 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 _("line %ld of %ld --%d%%-- col "),
3479 (long)curwin->w_cursor.lnum,
3480 (long)curbuf->b_ml.ml_line_count,
3481 n);
3482 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003483 len = STRLEN(buffer);
3484 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3486 }
3487
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003488 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489
3490 if (dont_truncate)
3491 {
3492 /* Temporarily set msg_scroll to avoid the message being truncated.
3493 * First call msg_start() to get the message in the right place. */
3494 msg_start();
3495 n = msg_scroll;
3496 msg_scroll = TRUE;
3497 msg(buffer);
3498 msg_scroll = n;
3499 }
3500 else
3501 {
3502 p = msg_trunc_attr(buffer, FALSE, 0);
3503 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 /* Need to repeat the message after redrawing when:
3505 * - When restart_edit is set (otherwise there will be a delay
3506 * before redrawing).
3507 * - When the screen was scrolled but there is no wait-return
3508 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003509 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 }
3511
3512 vim_free(buffer);
3513}
3514
3515 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003516col_print(
3517 char_u *buf,
3518 size_t buflen,
3519 int col,
3520 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521{
3522 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003523 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003525 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526}
3527
3528#if defined(FEAT_TITLE) || defined(PROTO)
3529/*
3530 * put file name in title bar of window and in icon title
3531 */
3532
3533static char_u *lasttitle = NULL;
3534static char_u *lasticon = NULL;
3535
3536 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003537maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538{
3539 char_u *p;
3540 char_u *t_str = NULL;
3541 char_u *i_name;
3542 char_u *i_str = NULL;
3543 int maxlen = 0;
3544 int len;
3545 int mustset;
3546 char_u buf[IOSIZE];
3547 int off;
3548
3549 if (!redrawing())
3550 {
3551 /* Postpone updating the title when 'lazyredraw' is set. */
3552 need_maketitle = TRUE;
3553 return;
3554 }
3555
3556 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003557 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 return;
3559
3560 if (p_title)
3561 {
3562 if (p_titlelen > 0)
3563 {
3564 maxlen = p_titlelen * Columns / 100;
3565 if (maxlen < 10)
3566 maxlen = 10;
3567 }
3568
3569 t_str = buf;
3570 if (*p_titlestring != NUL)
3571 {
3572#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003573 if (stl_syntax & STL_IN_TITLE)
3574 {
3575 int use_sandbox = FALSE;
3576 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003577
3578# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003579 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003580# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003581 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003583 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003584 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003585 if (called_emsg)
3586 set_string_option_direct((char_u *)"titlestring", -1,
3587 (char_u *)"", OPT_FREE, SID_ERROR);
3588 called_emsg |= save_called_emsg;
3589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 else
3591#endif
3592 t_str = p_titlestring;
3593 }
3594 else
3595 {
3596 /* format: "fname + (path) (1 of 2) - VIM" */
3597
Bram Moolenaar2c666692012-09-05 13:30:40 +02003598#define SPACE_FOR_FNAME (IOSIZE - 100)
3599#define SPACE_FOR_DIR (IOSIZE - 20)
3600#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003602 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar21554412017-07-24 21:44:43 +02003603#ifdef FEAT_TERMINAL
3604 else if (curbuf->b_term != NULL)
3605 {
3606 vim_strncpy(buf, term_get_status_text(curbuf->b_term),
3607 SPACE_FOR_FNAME);
3608 }
3609#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 else
3611 {
3612 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003613 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 }
3616
Bram Moolenaar21554412017-07-24 21:44:43 +02003617#ifdef FEAT_TERMINAL
3618 if (curbuf->b_term == NULL)
3619#endif
3620 switch (bufIsChanged(curbuf)
3621 + (curbuf->b_p_ro * 2)
3622 + (!curbuf->b_p_ma * 4))
3623 {
3624 case 1: STRCAT(buf, " +"); break;
3625 case 2: STRCAT(buf, " ="); break;
3626 case 3: STRCAT(buf, " =+"); break;
3627 case 4:
3628 case 6: STRCAT(buf, " -"); break;
3629 case 5:
3630 case 7: STRCAT(buf, " -+"); break;
3631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632
Bram Moolenaar21554412017-07-24 21:44:43 +02003633 if (curbuf->b_fname != NULL
3634#ifdef FEAT_TERMINAL
3635 && curbuf->b_term == NULL
3636#endif
3637 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 {
3639 /* Get path of file, replace home dir with ~ */
3640 off = (int)STRLEN(buf);
3641 buf[off++] = ' ';
3642 buf[off++] = '(';
3643 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003644 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645#ifdef BACKSLASH_IN_FILENAME
3646 /* avoid "c:/name" to be reduced to "c" */
3647 if (isalpha(buf[off]) && buf[off + 1] == ':')
3648 off += 2;
3649#endif
3650 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003651 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 if (p == buf + off)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003653 {
Bram Moolenaar21554412017-07-24 21:44:43 +02003654 /* must be a help buffer */
3655 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003656 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003660
Bram Moolenaar2c666692012-09-05 13:30:40 +02003661 /* Translate unprintable chars and concatenate. Keep some
3662 * room for the server name. When there is no room (very long
3663 * file name) use (...). */
3664 if (off < SPACE_FOR_DIR)
3665 {
3666 p = transstr(buf + off);
3667 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3668 vim_free(p);
3669 }
3670 else
3671 {
3672 vim_strncpy(buf + off, (char_u *)"...",
3673 (size_t)(SPACE_FOR_ARGNR - off));
3674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 STRCAT(buf, ")");
3676 }
3677
Bram Moolenaar2c666692012-09-05 13:30:40 +02003678 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679
3680#if defined(FEAT_CLIENTSERVER)
3681 if (serverName != NULL)
3682 {
3683 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003684 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 }
3686 else
3687#endif
3688 STRCAT(buf, " - VIM");
3689
3690 if (maxlen > 0)
3691 {
3692 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003693 if (vim_strsize(buf) > maxlen)
3694 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 }
3696 }
3697 }
3698 mustset = ti_change(t_str, &lasttitle);
3699
3700 if (p_icon)
3701 {
3702 i_str = buf;
3703 if (*p_iconstring != NUL)
3704 {
3705#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003706 if (stl_syntax & STL_IN_ICON)
3707 {
3708 int use_sandbox = FALSE;
3709 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003710
3711# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003712 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003713# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003714 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003716 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003717 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003718 if (called_emsg)
3719 set_string_option_direct((char_u *)"iconstring", -1,
3720 (char_u *)"", OPT_FREE, SID_ERROR);
3721 called_emsg |= save_called_emsg;
3722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 else
3724#endif
3725 i_str = p_iconstring;
3726 }
3727 else
3728 {
3729 if (buf_spname(curbuf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003730 i_name = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 else /* use file name only in icon */
3732 i_name = gettail(curbuf->b_ffname);
3733 *i_str = NUL;
3734 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003735 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 if (len > 100)
3737 {
3738 len -= 100;
3739#ifdef FEAT_MBYTE
3740 if (has_mbyte)
3741 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3742#endif
3743 i_name += len;
3744 }
3745 STRCPY(i_str, i_name);
3746 trans_characters(i_str, IOSIZE);
3747 }
3748 }
3749
3750 mustset |= ti_change(i_str, &lasticon);
3751
3752 if (mustset)
3753 resettitle();
3754}
3755
3756/*
3757 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3758 * from "str" if it does.
3759 * Return TRUE when "*last" changed.
3760 */
3761 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003762ti_change(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763{
3764 if ((str == NULL) != (*last == NULL)
3765 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3766 {
3767 vim_free(*last);
3768 if (str == NULL)
3769 *last = NULL;
3770 else
3771 *last = vim_strsave(str);
3772 return TRUE;
3773 }
3774 return FALSE;
3775}
3776
3777/*
3778 * Put current window title back (used after calling a shell)
3779 */
3780 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003781resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782{
3783 mch_settitle(lasttitle, lasticon);
3784}
Bram Moolenaarea408852005-06-25 22:49:46 +00003785
3786# if defined(EXITFREE) || defined(PROTO)
3787 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003788free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00003789{
3790 vim_free(lasttitle);
3791 vim_free(lasticon);
3792}
3793# endif
3794
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795#endif /* FEAT_TITLE */
3796
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003797#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003799 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 * Return length of string in screen cells.
3801 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003802 * Normally works for window "wp", except when working for 'tabline' then it
3803 * is "curwin".
3804 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 * Items are drawn interspersed with the text that surrounds it
3806 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3807 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3808 *
3809 * If maxwidth is not zero, the string will be filled at any middle marker
3810 * or truncated if too long, fillchar is used for all whitespace.
3811 */
3812 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003813build_stl_str_hl(
3814 win_T *wp,
3815 char_u *out, /* buffer to write into != NameBuff */
3816 size_t outlen, /* length of out[] */
3817 char_u *fmt,
3818 int use_sandbox UNUSED, /* "fmt" was set insecurely, use sandbox */
3819 int fillchar,
3820 int maxwidth,
3821 struct stl_hlrec *hltab, /* return: HL attributes (can be NULL) */
3822 struct stl_hlrec *tabtab) /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823{
3824 char_u *p;
3825 char_u *s;
3826 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003827 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828#ifdef FEAT_EVAL
Bram Moolenaarba2929b2017-09-08 13:59:21 +02003829 win_T *save_curwin;
3830 buf_T *save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831#endif
3832 int empty_line;
3833 colnr_T virtcol;
3834 long l;
3835 long n;
3836 int prevchar_isflag;
3837 int prevchar_isitem;
3838 int itemisflag;
3839 int fillable;
3840 char_u *str;
3841 long num;
3842 int width;
3843 int itemcnt;
3844 int curitem;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02003845 int group_end_userhl;
3846 int group_start_userhl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 int groupitem[STL_MAX_ITEM];
3848 int groupdepth;
3849 struct stl_item
3850 {
3851 char_u *start;
3852 int minwid;
3853 int maxwid;
3854 enum
3855 {
3856 Normal,
3857 Empty,
3858 Group,
3859 Middle,
3860 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003861 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 Trunc
3863 } type;
3864 } item[STL_MAX_ITEM];
3865 int minwid;
3866 int maxwid;
3867 int zeropad;
3868 char_u base;
3869 char_u opt;
3870#define TMPLEN 70
3871 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003872 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003873 struct stl_hlrec *sp;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02003874 int save_must_redraw = must_redraw;
3875 int save_redr_type = curwin->w_redr_type;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003876
3877#ifdef FEAT_EVAL
3878 /*
3879 * When the format starts with "%!" then evaluate it as an expression and
3880 * use the result as the actual format string.
3881 */
3882 if (fmt[0] == '%' && fmt[1] == '!')
3883 {
3884 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3885 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003886 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003887 }
3888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889
3890 if (fillchar == 0)
3891 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003892#ifdef FEAT_MBYTE
3893 /* Can't handle a multi-byte fill character yet. */
3894 else if (mb_char2len(fillchar) > 1)
3895 fillchar = '-';
3896#endif
3897
Bram Moolenaar567199b2013-04-24 16:52:36 +02003898 /* Get line & check if empty (cursorpos will show "0-1"). Note that
3899 * p will become invalid when getting another buffer line. */
3900 p = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3901 empty_line = (*p == NUL);
3902
3903 /* Get the byte value now, in case we need it below. This is more
3904 * efficient than making a copy of the line. */
3905 if (wp->w_cursor.col > (colnr_T)STRLEN(p))
3906 byteval = 0;
3907 else
3908#ifdef FEAT_MBYTE
3909 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
3910#else
3911 byteval = p[wp->w_cursor.col];
3912#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913
3914 groupdepth = 0;
3915 p = out;
3916 curitem = 0;
3917 prevchar_isflag = TRUE;
3918 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003919 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003921 if (curitem == STL_MAX_ITEM)
3922 {
3923 /* There are too many items. Add the error code to the statusline
3924 * to give the user a hint about what went wrong. */
3925 if (p + 6 < out + outlen)
3926 {
3927 mch_memmove(p, " E541", (size_t)5);
3928 p += 5;
3929 }
3930 break;
3931 }
3932
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 if (*s != NUL && *s != '%')
3934 prevchar_isflag = prevchar_isitem = FALSE;
3935
3936 /*
3937 * Handle up to the next '%' or the end.
3938 */
3939 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3940 *p++ = *s++;
3941 if (*s == NUL || p + 1 >= out + outlen)
3942 break;
3943
3944 /*
3945 * Handle one '%' item.
3946 */
3947 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003948 if (*s == NUL) /* ignore trailing % */
3949 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 if (*s == '%')
3951 {
3952 if (p + 1 >= out + outlen)
3953 break;
3954 *p++ = *s++;
3955 prevchar_isflag = prevchar_isitem = FALSE;
3956 continue;
3957 }
3958 if (*s == STL_MIDDLEMARK)
3959 {
3960 s++;
3961 if (groupdepth > 0)
3962 continue;
3963 item[curitem].type = Middle;
3964 item[curitem++].start = p;
3965 continue;
3966 }
3967 if (*s == STL_TRUNCMARK)
3968 {
3969 s++;
3970 item[curitem].type = Trunc;
3971 item[curitem++].start = p;
3972 continue;
3973 }
3974 if (*s == ')')
3975 {
3976 s++;
3977 if (groupdepth < 1)
3978 continue;
3979 groupdepth--;
3980
3981 t = item[groupitem[groupdepth]].start;
3982 *p = NUL;
3983 l = vim_strsize(t);
3984 if (curitem > groupitem[groupdepth] + 1
3985 && item[groupitem[groupdepth]].minwid == 0)
3986 {
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02003987 /* remove group if all items are empty and highlight group
3988 * doesn't change */
3989 group_start_userhl = group_end_userhl = 0;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02003990 for (n = groupitem[groupdepth] - 1; n >= 0; n--)
3991 {
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02003992 if (item[n].type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02003993 {
3994 group_start_userhl = group_end_userhl = item[n].minwid;
3995 break;
3996 }
3997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02003999 {
4000 if (item[n].type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 break;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004002 if (item[n].type == Highlight)
4003 group_end_userhl = item[n].minwid;
4004 }
4005 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 {
4007 p = t;
4008 l = 0;
4009 }
4010 }
4011 if (l > item[groupitem[groupdepth]].maxwid)
4012 {
4013 /* truncate, remove n bytes of text at the start */
4014#ifdef FEAT_MBYTE
4015 if (has_mbyte)
4016 {
4017 /* Find the first character that should be included. */
4018 n = 0;
4019 while (l >= item[groupitem[groupdepth]].maxwid)
4020 {
4021 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004022 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 }
4024 }
4025 else
4026#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004027 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028
4029 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004030 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 p = p - n + 1;
4032#ifdef FEAT_MBYTE
4033 /* Fill up space left over by half a double-wide char. */
4034 while (++l < item[groupitem[groupdepth]].minwid)
4035 *p++ = fillchar;
4036#endif
4037
4038 /* correct the start of the items for the truncation */
4039 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
4040 {
4041 item[l].start -= n;
4042 if (item[l].start < t)
4043 item[l].start = t;
4044 }
4045 }
4046 else if (abs(item[groupitem[groupdepth]].minwid) > l)
4047 {
4048 /* fill */
4049 n = item[groupitem[groupdepth]].minwid;
4050 if (n < 0)
4051 {
4052 /* fill by appending characters */
4053 n = 0 - n;
4054 while (l++ < n && p + 1 < out + outlen)
4055 *p++ = fillchar;
4056 }
4057 else
4058 {
4059 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004060 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 l = n - l;
4062 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004063 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 p += l;
4065 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
4066 item[n].start += l;
4067 for ( ; l > 0; l--)
4068 *t++ = fillchar;
4069 }
4070 }
4071 continue;
4072 }
4073 minwid = 0;
4074 maxwid = 9999;
4075 zeropad = FALSE;
4076 l = 1;
4077 if (*s == '0')
4078 {
4079 s++;
4080 zeropad = TRUE;
4081 }
4082 if (*s == '-')
4083 {
4084 s++;
4085 l = -1;
4086 }
4087 if (VIM_ISDIGIT(*s))
4088 {
4089 minwid = (int)getdigits(&s);
4090 if (minwid < 0) /* overflow */
4091 minwid = 0;
4092 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004093 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 {
4095 item[curitem].type = Highlight;
4096 item[curitem].start = p;
4097 item[curitem].minwid = minwid > 9 ? 1 : minwid;
4098 s++;
4099 curitem++;
4100 continue;
4101 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004102 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4103 {
4104 if (*s == STL_TABCLOSENR)
4105 {
4106 if (minwid == 0)
4107 {
4108 /* %X ends the close label, go back to the previously
4109 * define tab label nr. */
4110 for (n = curitem - 1; n >= 0; --n)
4111 if (item[n].type == TabPage && item[n].minwid >= 0)
4112 {
4113 minwid = item[n].minwid;
4114 break;
4115 }
4116 }
4117 else
4118 /* close nrs are stored as negative values */
4119 minwid = - minwid;
4120 }
4121 item[curitem].type = TabPage;
4122 item[curitem].start = p;
4123 item[curitem].minwid = minwid;
4124 s++;
4125 curitem++;
4126 continue;
4127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 if (*s == '.')
4129 {
4130 s++;
4131 if (VIM_ISDIGIT(*s))
4132 {
4133 maxwid = (int)getdigits(&s);
4134 if (maxwid <= 0) /* overflow */
4135 maxwid = 50;
4136 }
4137 }
4138 minwid = (minwid > 50 ? 50 : minwid) * l;
4139 if (*s == '(')
4140 {
4141 groupitem[groupdepth++] = curitem;
4142 item[curitem].type = Group;
4143 item[curitem].start = p;
4144 item[curitem].minwid = minwid;
4145 item[curitem].maxwid = maxwid;
4146 s++;
4147 curitem++;
4148 continue;
4149 }
4150 if (vim_strchr(STL_ALL, *s) == NULL)
4151 {
4152 s++;
4153 continue;
4154 }
4155 opt = *s++;
4156
4157 /* OK - now for the real work */
4158 base = 'D';
4159 itemisflag = FALSE;
4160 fillable = TRUE;
4161 num = -1;
4162 str = NULL;
4163 switch (opt)
4164 {
4165 case STL_FILEPATH:
4166 case STL_FULLPATH:
4167 case STL_FILENAME:
4168 fillable = FALSE; /* don't change ' ' to fillchar */
4169 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004170 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 else
4172 {
4173 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004174 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4176 }
4177 trans_characters(NameBuff, MAXPATHL);
4178 if (opt != STL_FILENAME)
4179 str = NameBuff;
4180 else
4181 str = gettail(NameBuff);
4182 break;
4183
4184 case STL_VIM_EXPR: /* '{' */
4185 itemisflag = TRUE;
4186 t = p;
4187 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
4188 *p++ = *s++;
4189 if (*s != '}') /* missing '}' or out of space */
4190 break;
4191 s++;
4192 *p = 0;
4193 p = t;
4194
4195#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004196 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 set_internal_string_var((char_u *)"actual_curbuf", tmp);
4198
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004199 save_curbuf = curbuf;
4200 save_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 curwin = wp;
4202 curbuf = wp->w_buffer;
4203
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004204 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004206 curwin = save_curwin;
4207 curbuf = save_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00004208 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209
4210 if (str != NULL && *str != 0)
4211 {
4212 if (*skipdigits(str) == NUL)
4213 {
4214 num = atoi((char *)str);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004215 VIM_CLEAR(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 itemisflag = FALSE;
4217 }
4218 }
4219#endif
4220 break;
4221
4222 case STL_LINE:
4223 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4224 ? 0L : (long)(wp->w_cursor.lnum);
4225 break;
4226
4227 case STL_NUMLINES:
4228 num = wp->w_buffer->b_ml.ml_line_count;
4229 break;
4230
4231 case STL_COLUMN:
4232 num = !(State & INSERT) && empty_line
4233 ? 0 : (int)wp->w_cursor.col + 1;
4234 break;
4235
4236 case STL_VIRTCOL:
4237 case STL_VIRTCOL_ALT:
4238 /* In list mode virtcol needs to be recomputed */
4239 virtcol = wp->w_virtcol;
4240 if (wp->w_p_list && lcs_tab1 == NUL)
4241 {
4242 wp->w_p_list = FALSE;
4243 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
4244 wp->w_p_list = TRUE;
4245 }
4246 ++virtcol;
4247 /* Don't display %V if it's the same as %c. */
4248 if (opt == STL_VIRTCOL_ALT
4249 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
4250 ? 0 : (int)wp->w_cursor.col + 1)))
4251 break;
4252 num = (long)virtcol;
4253 break;
4254
4255 case STL_PERCENTAGE:
4256 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4257 (long)wp->w_buffer->b_ml.ml_line_count);
4258 break;
4259
4260 case STL_ALTPERCENT:
4261 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004262 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 break;
4264
4265 case STL_ARGLISTSTAT:
4266 fillable = FALSE;
4267 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004268 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 str = tmp;
4270 break;
4271
4272 case STL_KEYMAP:
4273 fillable = FALSE;
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02004274 if (get_keymap_str(wp, (char_u *)"<%s>", tmp, TMPLEN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 str = tmp;
4276 break;
4277 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004278#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004279 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280#else
4281 num = 0;
4282#endif
4283 break;
4284
4285 case STL_BUFNO:
4286 num = wp->w_buffer->b_fnum;
4287 break;
4288
4289 case STL_OFFSET_X:
4290 base = 'X';
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004291 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 case STL_OFFSET:
4293#ifdef FEAT_BYTEOFF
4294 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4295 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4296 0L : l + 1 + (!(State & INSERT) && empty_line ?
4297 0 : (int)wp->w_cursor.col);
4298#endif
4299 break;
4300
4301 case STL_BYTEVAL_X:
4302 base = 'X';
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004303 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004305 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 if (num == NL)
4307 num = 0;
4308 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4309 num = NL;
4310 break;
4311
4312 case STL_ROFLAG:
4313 case STL_ROFLAG_ALT:
4314 itemisflag = TRUE;
4315 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004316 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 break;
4318
4319 case STL_HELPFLAG:
4320 case STL_HELPFLAG_ALT:
4321 itemisflag = TRUE;
4322 if (wp->w_buffer->b_help)
4323 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004324 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 break;
4326
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 case STL_FILETYPE:
4328 if (*wp->w_buffer->b_p_ft != NUL
4329 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4330 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004331 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
4332 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 str = tmp;
4334 }
4335 break;
4336
4337 case STL_FILETYPE_ALT:
4338 itemisflag = TRUE;
4339 if (*wp->w_buffer->b_p_ft != NUL
4340 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4341 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004342 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
4343 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 for (t = tmp; *t != 0; t++)
4345 *t = TOUPPER_LOC(*t);
4346 str = tmp;
4347 }
4348 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349
Bram Moolenaar4033c552017-09-16 20:54:51 +02004350#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 case STL_PREVIEWFLAG:
4352 case STL_PREVIEWFLAG_ALT:
4353 itemisflag = TRUE;
4354 if (wp->w_p_pvw)
4355 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4356 : _("[Preview]"));
4357 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004358
4359 case STL_QUICKFIX:
4360 if (bt_quickfix(wp->w_buffer))
4361 str = (char_u *)(wp->w_llist_ref
4362 ? _(msg_loclist)
4363 : _(msg_qflist));
4364 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365#endif
4366
4367 case STL_MODIFIED:
4368 case STL_MODIFIED_ALT:
4369 itemisflag = TRUE;
4370 switch ((opt == STL_MODIFIED_ALT)
4371 + bufIsChanged(wp->w_buffer) * 2
4372 + (!wp->w_buffer->b_p_ma) * 4)
4373 {
4374 case 2: str = (char_u *)"[+]"; break;
4375 case 3: str = (char_u *)",+"; break;
4376 case 4: str = (char_u *)"[-]"; break;
4377 case 5: str = (char_u *)",-"; break;
4378 case 6: str = (char_u *)"[+-]"; break;
4379 case 7: str = (char_u *)",+-"; break;
4380 }
4381 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004382
4383 case STL_HIGHLIGHT:
4384 t = s;
4385 while (*s != '#' && *s != NUL)
4386 ++s;
4387 if (*s == '#')
4388 {
4389 item[curitem].type = Highlight;
4390 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004391 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004392 curitem++;
4393 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004394 if (*s != NUL)
4395 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004396 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 }
4398
4399 item[curitem].start = p;
4400 item[curitem].type = Normal;
4401 if (str != NULL && *str)
4402 {
4403 t = str;
4404 if (itemisflag)
4405 {
4406 if ((t[0] && t[1])
4407 && ((!prevchar_isitem && *t == ',')
4408 || (prevchar_isflag && *t == ' ')))
4409 t++;
4410 prevchar_isflag = TRUE;
4411 }
4412 l = vim_strsize(t);
4413 if (l > 0)
4414 prevchar_isitem = TRUE;
4415 if (l > maxwid)
4416 {
4417 while (l >= maxwid)
4418#ifdef FEAT_MBYTE
4419 if (has_mbyte)
4420 {
4421 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004422 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 }
4424 else
4425#endif
4426 l -= byte2cells(*t++);
4427 if (p + 1 >= out + outlen)
4428 break;
4429 *p++ = '<';
4430 }
4431 if (minwid > 0)
4432 {
4433 for (; l < minwid && p + 1 < out + outlen; l++)
4434 {
4435 /* Don't put a "-" in front of a digit. */
4436 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4437 *p++ = ' ';
4438 else
4439 *p++ = fillchar;
4440 }
4441 minwid = 0;
4442 }
4443 else
4444 minwid *= -1;
4445 while (*t && p + 1 < out + outlen)
4446 {
4447 *p++ = *t++;
4448 /* Change a space by fillchar, unless fillchar is '-' and a
4449 * digit follows. */
4450 if (fillable && p[-1] == ' '
4451 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4452 p[-1] = fillchar;
4453 }
4454 for (; l < minwid && p + 1 < out + outlen; l++)
4455 *p++ = fillchar;
4456 }
4457 else if (num >= 0)
4458 {
4459 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4460 char_u nstr[20];
4461
4462 if (p + 20 >= out + outlen)
4463 break; /* not sufficient space */
4464 prevchar_isitem = TRUE;
4465 t = nstr;
4466 if (opt == STL_VIRTCOL_ALT)
4467 {
4468 *t++ = '-';
4469 minwid--;
4470 }
4471 *t++ = '%';
4472 if (zeropad)
4473 *t++ = '0';
4474 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004475 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 *t = 0;
4477
4478 for (n = num, l = 1; n >= nbase; n /= nbase)
4479 l++;
4480 if (opt == STL_VIRTCOL_ALT)
4481 l++;
4482 if (l > maxwid)
4483 {
4484 l += 2;
4485 n = l - maxwid;
4486 while (l-- > maxwid)
4487 num /= nbase;
4488 *t++ = '>';
4489 *t++ = '%';
4490 *t = t[-3];
4491 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004492 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4493 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 }
4495 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004496 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4497 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 p += STRLEN(p);
4499 }
4500 else
4501 item[curitem].type = Empty;
4502
4503 if (opt == STL_VIM_EXPR)
4504 vim_free(str);
4505
4506 if (num >= 0 || (!itemisflag && str && *str))
4507 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4508 curitem++;
4509 }
4510 *p = NUL;
4511 itemcnt = curitem;
4512
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004513#ifdef FEAT_EVAL
4514 if (usefmt != fmt)
4515 vim_free(usefmt);
4516#endif
4517
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 width = vim_strsize(out);
4519 if (maxwidth > 0 && width > maxwidth)
4520 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004521 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 l = 0;
4523 if (itemcnt == 0)
4524 s = out;
4525 else
4526 {
4527 for ( ; l < itemcnt; l++)
4528 if (item[l].type == Trunc)
4529 {
4530 /* Truncate at %< item. */
4531 s = item[l].start;
4532 break;
4533 }
4534 if (l == itemcnt)
4535 {
4536 /* No %< item, truncate first item. */
4537 s = item[0].start;
4538 l = 0;
4539 }
4540 }
4541
4542 if (width - vim_strsize(s) >= maxwidth)
4543 {
4544 /* Truncation mark is beyond max length */
4545#ifdef FEAT_MBYTE
4546 if (has_mbyte)
4547 {
4548 s = out;
4549 width = 0;
4550 for (;;)
4551 {
4552 width += ptr2cells(s);
4553 if (width >= maxwidth)
4554 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004555 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 }
4557 /* Fill up for half a double-wide character. */
4558 while (++width < maxwidth)
4559 *s++ = fillchar;
4560 }
4561 else
4562#endif
4563 s = out + maxwidth - 1;
4564 for (l = 0; l < itemcnt; l++)
4565 if (item[l].start > s)
4566 break;
4567 itemcnt = l;
4568 *s++ = '>';
4569 *s = 0;
4570 }
4571 else
4572 {
4573#ifdef FEAT_MBYTE
4574 if (has_mbyte)
4575 {
4576 n = 0;
4577 while (width >= maxwidth)
4578 {
4579 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004580 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 }
4582 }
4583 else
4584#endif
4585 n = width - maxwidth + 1;
4586 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004587 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 *s = '<';
4589
4590 /* Fill up for half a double-wide character. */
4591 while (++width < maxwidth)
4592 {
4593 s = s + STRLEN(s);
4594 *s++ = fillchar;
4595 *s = NUL;
4596 }
4597
4598 --n; /* count the '<' */
4599 for (; l < itemcnt; l++)
4600 {
4601 if (item[l].start - n >= s)
4602 item[l].start -= n;
4603 else
4604 item[l].start = s;
4605 }
4606 }
4607 width = maxwidth;
4608 }
4609 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4610 {
4611 /* Apply STL_MIDDLE if any */
4612 for (l = 0; l < itemcnt; l++)
4613 if (item[l].type == Middle)
4614 break;
4615 if (l < itemcnt)
4616 {
4617 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004618 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 for (s = item[l].start; s < p; s++)
4620 *s = fillchar;
4621 for (l++; l < itemcnt; l++)
4622 item[l].start += maxwidth - width;
4623 width = maxwidth;
4624 }
4625 }
4626
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004627 /* Store the info about highlighting. */
4628 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004630 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 for (l = 0; l < itemcnt; l++)
4632 {
4633 if (item[l].type == Highlight)
4634 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004635 sp->start = item[l].start;
4636 sp->userhl = item[l].minwid;
4637 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 }
4639 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004640 sp->start = NULL;
4641 sp->userhl = 0;
4642 }
4643
4644 /* Store the info about tab pages labels. */
4645 if (tabtab != NULL)
4646 {
4647 sp = tabtab;
4648 for (l = 0; l < itemcnt; l++)
4649 {
4650 if (item[l].type == TabPage)
4651 {
4652 sp->start = item[l].start;
4653 sp->userhl = item[l].minwid;
4654 sp++;
4655 }
4656 }
4657 sp->start = NULL;
4658 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 }
4660
Bram Moolenaar65ed1362017-09-30 16:00:14 +02004661 /* When inside update_screen we do not want redrawing a stausline, ruler,
4662 * title, etc. to trigger another redraw, it may cause an endless loop. */
4663 if (updating_screen)
4664 {
4665 must_redraw = save_must_redraw;
4666 curwin->w_redr_type = save_redr_type;
4667 }
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004668
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 return width;
4670}
4671#endif /* FEAT_STL_OPT */
4672
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004673#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4674 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004676 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4677 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 */
4679 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004680get_rel_pos(
4681 win_T *wp,
4682 char_u *buf,
4683 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684{
4685 long above; /* number of lines above window */
4686 long below; /* number of lines below window */
4687
Bram Moolenaar0027c212015-01-07 13:31:52 +01004688 if (buflen < 3) /* need at least 3 chars for writing */
4689 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 above = wp->w_topline - 1;
4691#ifdef FEAT_DIFF
4692 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02004693 if (wp->w_topline == 1 && wp->w_topfill >= 1)
4694 above = 0; /* All buffer lines are displayed and there is an
4695 * indication of filler lines, that can be considered
4696 * seeing all lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697#endif
4698 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4699 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004700 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4701 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004703 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004705 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 ? (int)(above / ((above + below) / 100L))
4707 : (int)(above * 100L / (above + below)));
4708}
4709#endif
4710
4711/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004712 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 * Return TRUE if it was appended.
4714 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004715 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004716append_arg_number(
4717 win_T *wp,
4718 char_u *buf,
4719 int buflen,
4720 int add_file) /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721{
4722 char_u *p;
4723
4724 if (ARGCOUNT <= 1) /* nothing to do */
4725 return FALSE;
4726
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004727 p = buf + STRLEN(buf); /* go to the end of the buffer */
4728 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 return FALSE;
4730 *p++ = ' ';
4731 *p++ = '(';
4732 if (add_file)
4733 {
4734 STRCPY(p, "file ");
4735 p += 5;
4736 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004737 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4738 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4740 return TRUE;
4741}
4742
4743/*
4744 * If fname is not a full path, make it a full path.
4745 * Returns pointer to allocated memory (NULL for failure).
4746 */
4747 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004748fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749{
4750 /*
4751 * Force expanding the path always for Unix, because symbolic links may
4752 * mess up the full path name, even though it starts with a '/'.
4753 * Also expand when there is ".." in the file name, try to remove it,
4754 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004755 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 * For MS-Windows also expand names like "longna~1" to "longname".
4757 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004758#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 return FullName_save(fname, TRUE);
4760#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004761 if (!vim_isAbsName(fname)
4762 || strstr((char *)fname, "..") != NULL
4763 || strstr((char *)fname, "//") != NULL
4764# ifdef BACKSLASH_IN_FILENAME
4765 || strstr((char *)fname, "\\\\") != NULL
4766# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004767# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004769# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 )
4771 return FullName_save(fname, FALSE);
4772
4773 fname = vim_strsave(fname);
4774
Bram Moolenaar9b942202007-10-03 12:31:33 +00004775# ifdef USE_FNAME_CASE
4776# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004778# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 {
4780 if (fname != NULL)
4781 fname_case(fname, 0); /* set correct case for file name */
4782 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004783# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784
4785 return fname;
4786#endif
4787}
4788
4789/*
4790 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4791 * "ffname" becomes a pointer to allocated memory (or NULL).
4792 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004794fname_expand(
4795 buf_T *buf UNUSED,
4796 char_u **ffname,
4797 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798{
4799 if (*ffname == NULL) /* if no file name given, nothing to do */
4800 return;
4801 if (*sfname == NULL) /* if no short file name given, use ffname */
4802 *sfname = *ffname;
4803 *ffname = fix_fname(*ffname); /* expand to full path */
4804
4805#ifdef FEAT_SHORTCUT
4806 if (!buf->b_p_bin)
4807 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004808 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809
4810 /* If the file name is a shortcut file, use the file it links to. */
4811 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004812 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 {
4814 vim_free(*ffname);
4815 *ffname = rfname;
4816 *sfname = rfname;
4817 }
4818 }
4819#endif
4820}
4821
4822/*
4823 * Get the file name for an argument list entry.
4824 */
4825 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004826alist_name(aentry_T *aep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827{
4828 buf_T *bp;
4829
4830 /* Use the name from the associated buffer if it exists. */
4831 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004832 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 return aep->ae_fname;
4834 return bp->b_fname;
4835}
4836
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837/*
4838 * do_arg_all(): Open up to 'count' windows, one for each argument.
4839 */
4840 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004841do_arg_all(
4842 int count,
4843 int forceit, /* hide buffers in current windows */
4844 int keep_tabs) /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845{
4846 int i;
4847 win_T *wp, *wpnext;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004848 char_u *opened; /* Array of weight for which args are open:
4849 * 0: not opened
4850 * 1: opened in other tab
4851 * 2: opened in curtab
4852 * 3: opened in curtab and curwin
4853 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004854 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 int use_firstwin = FALSE; /* use first window for arglist */
4856 int split_ret = OK;
4857 int p_ea_save;
4858 alist_T *alist; /* argument list to be used */
4859 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004860 tabpage_T *tpnext;
4861 int had_tab = cmdmod.tab;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004862 win_T *old_curwin, *last_curwin;
4863 tabpage_T *old_curtab, *last_curtab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004864 win_T *new_curwin = NULL;
4865 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866
4867 if (ARGCOUNT <= 0)
4868 {
4869 /* Don't give an error message. We don't want it when the ":all"
4870 * command is in the .vimrc. */
4871 return;
4872 }
4873 setpcmark();
4874
4875 opened_len = ARGCOUNT;
4876 opened = alloc_clear((unsigned)opened_len);
4877 if (opened == NULL)
4878 return;
4879
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004880 /* Autocommands may do anything to the argument list. Make sure it's not
4881 * freed while we are working here by "locking" it. We still have to
4882 * watch out for its size to be changed. */
4883 alist = curwin->w_alist;
4884 ++alist->al_refcount;
4885
4886 old_curwin = curwin;
4887 old_curtab = curtab;
4888
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004889# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004891# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892
4893 /*
4894 * Try closing all windows that are not in the argument list.
4895 * Also close windows that are not full width;
4896 * When 'hidden' or "forceit" set the buffer becomes hidden.
4897 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004898 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004900 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004901 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004902 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004904 tpnext = curtab->tp_next;
4905 for (wp = firstwin; wp != NULL; wp = wpnext)
4906 {
4907 wpnext = wp->w_next;
4908 buf = wp->w_buffer;
4909 if (buf->b_ffname == NULL
Bram Moolenaar5a030a52016-12-01 17:48:29 +01004910 || (!keep_tabs && (buf->b_nwindows > 1
4911 || wp->w_width != Columns)))
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004912 i = opened_len;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004913 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004915 /* check if the buffer in this window is in the arglist */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004916 for (i = 0; i < opened_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004918 if (i < alist->al_ga.ga_len
4919 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
4920 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
4921 buf->b_ffname, TRUE) & FPC_SAME))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004923 int weight = 1;
4924
4925 if (old_curtab == curtab)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004926 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004927 ++weight;
4928 if (old_curwin == wp)
4929 ++weight;
4930 }
4931
4932 if (weight > (int)opened[i])
4933 {
4934 opened[i] = (char_u)weight;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004935 if (i == 0)
4936 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004937 if (new_curwin != NULL)
4938 new_curwin->w_arg_idx = opened_len;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004939 new_curwin = wp;
4940 new_curtab = curtab;
4941 }
4942 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004943 else if (keep_tabs)
4944 i = opened_len;
4945
4946 if (wp->w_alist != alist)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004947 {
4948 /* Use the current argument list for all windows
4949 * containing a file from it. */
4950 alist_unlink(wp->w_alist);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004951 wp->w_alist = alist;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004952 ++wp->w_alist->al_refcount;
4953 }
4954 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 }
4957 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004958 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004960 if (i == opened_len && !keep_tabs)/* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02004962 if (buf_hide(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004963 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004965 /* If the buffer was changed, and we would like to hide it,
4966 * try autowriting. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02004967 if (!buf_hide(buf) && buf->b_nwindows <= 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004968 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02004970 bufref_T bufref;
4971
4972 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004973
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004974 (void)autowrite(buf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004975
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004976 /* check if autocommands removed the window */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02004977 if (!win_valid(wp) || !bufref_valid(&bufref))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004978 {
4979 wpnext = firstwin; /* start all over... */
4980 continue;
4981 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004982 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004983 /* don't close last window */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01004984 if (ONE_WINDOW
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004985 && (first_tabpage->tp_next == NULL || !had_tab))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004986 use_firstwin = TRUE;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004987 else
4988 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02004989 win_close(wp, !buf_hide(buf) && !bufIsChanged(buf));
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004990
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004991 /* check if autocommands removed the next window */
4992 if (!win_valid(wpnext))
4993 wpnext = firstwin; /* start all over... */
Bram Moolenaar4033c552017-09-16 20:54:51 +02004994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 }
4997 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004998
4999 /* Without the ":tab" modifier only do the current tab page. */
5000 if (had_tab == 0 || tpnext == NULL)
5001 break;
5002
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005003 /* check if autocommands removed the next tab page */
5004 if (!valid_tabpage(tpnext))
5005 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005006
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005007 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 }
5009
5010 /*
5011 * Open a window for files in the argument list that don't have one.
5012 * ARGCOUNT may change while doing this, because of autocommands.
5013 */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005014 if (count > opened_len || count <= 0)
5015 count = opened_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 /* Don't execute Win/Buf Enter/Leave autocommands here. */
5018 ++autocmd_no_enter;
5019 ++autocmd_no_leave;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005020 last_curwin = curwin;
5021 last_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005023 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
5024 * leaving an empty tab page when executed locally. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005025 if (keep_tabs && BUFEMPTY() && curbuf->b_nwindows == 1
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005026 && curbuf->b_ffname == NULL && !curbuf->b_changed)
5027 use_firstwin = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005028
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005029 for (i = 0; i < count && i < opened_len && !got_int; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 {
5031 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
5032 arg_had_last = TRUE;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005033 if (opened[i] > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 {
5035 /* Move the already present window to below the current window */
5036 if (curwin->w_arg_idx != i)
5037 {
5038 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
5039 {
5040 if (wpnext->w_arg_idx == i)
5041 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005042 if (keep_tabs)
5043 {
5044 new_curwin = wpnext;
5045 new_curtab = curtab;
5046 }
5047 else
5048 win_move_after(wpnext, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 break;
5050 }
5051 }
5052 }
5053 }
5054 else if (split_ret == OK)
5055 {
5056 if (!use_firstwin) /* split current window */
5057 {
5058 p_ea_save = p_ea;
5059 p_ea = TRUE; /* use space from all windows */
5060 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5061 p_ea = p_ea_save;
5062 if (split_ret == FAIL)
5063 continue;
5064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 else /* first window: do autocmd for leaving this buffer */
5066 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067
5068 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005069 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 */
5071 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005072 if (i == 0)
5073 {
5074 new_curwin = curwin;
5075 new_curtab = curtab;
5076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
5078 ECMD_ONE,
Bram Moolenaareb44a682017-08-03 22:44:55 +02005079 ((buf_hide(curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00005081 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 if (use_firstwin)
5083 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 use_firstwin = FALSE;
5085 }
5086 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005087
5088 /* When ":tab" was used open a new tab for a new window repeatedly. */
5089 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5090 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 }
5092
5093 /* Remove the "lock" on the argument list. */
5094 alist_unlink(alist);
5095
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 --autocmd_no_enter;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005097
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005098 /* restore last referenced tabpage's curwin */
5099 if (last_curtab != new_curtab)
5100 {
5101 if (valid_tabpage(last_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005102 goto_tabpage_tp(last_curtab, TRUE, TRUE);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005103 if (win_valid(last_curwin))
5104 win_enter(last_curwin, FALSE);
5105 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005106 /* to window with first arg */
5107 if (valid_tabpage(new_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005108 goto_tabpage_tp(new_curtab, TRUE, TRUE);
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005109 if (win_valid(new_curwin))
5110 win_enter(new_curwin, FALSE);
5111
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 vim_free(opened);
5114}
5115
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116/*
5117 * Open a window for a number of buffers.
5118 */
5119 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005120ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121{
5122 buf_T *buf;
5123 win_T *wp, *wpnext;
5124 int split_ret = OK;
5125 int p_ea_save;
5126 int open_wins = 0;
5127 int r;
5128 int count; /* Maximum number of windows to open. */
5129 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005130 int had_tab = cmdmod.tab;
5131 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132
5133 if (eap->addr_count == 0) /* make as many windows as possible */
5134 count = 9999;
5135 else
5136 count = eap->line2; /* make as many windows as specified */
5137 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5138 all = FALSE;
5139 else
5140 all = TRUE;
5141
5142 setpcmark();
5143
5144#ifdef FEAT_GUI
5145 need_mouse_correct = TRUE;
5146#endif
5147
5148 /*
5149 * Close superfluous windows (two windows for the same buffer).
5150 * Also close windows that are not full-width.
5151 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005152 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005153 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005154 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005156 tpnext = curtab->tp_next;
5157 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005159 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005160 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005161 || ((cmdmod.split & WSP_VERT)
5162 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005163 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005164 : wp->w_width != Columns)
Bram Moolenaar4033c552017-09-16 20:54:51 +02005165 || (had_tab > 0 && wp != firstwin)) && !ONE_WINDOW
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005166 && !(wp->w_closing || wp->w_buffer->b_locked > 0))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005167 {
5168 win_close(wp, FALSE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005169 wpnext = firstwin; /* just in case an autocommand does
5170 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005171 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005172 open_wins = 0;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005173 }
5174 else
5175 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005177
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005178 /* Without the ":tab" modifier only do the current tab page. */
5179 if (had_tab == 0 || tpnext == NULL)
5180 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005181 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 }
5183
5184 /*
5185 * Go through the buffer list. When a buffer doesn't have a window yet,
5186 * open one. Otherwise move the window to the right position.
5187 * Watch out for autocommands that delete buffers or windows!
5188 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 /* Don't execute Win/Buf Enter/Leave autocommands here. */
5190 ++autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 win_enter(lastwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5194 {
5195 /* Check if this buffer needs a window */
5196 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5197 continue;
5198
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005199 if (had_tab != 0)
5200 {
5201 /* With the ":tab" modifier don't move the window. */
5202 if (buf->b_nwindows > 0)
5203 wp = lastwin; /* buffer has a window, skip it */
5204 else
5205 wp = NULL;
5206 }
5207 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005208 {
5209 /* Check if this buffer already has a window */
Bram Moolenaar29323592016-07-24 22:04:11 +02005210 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005211 if (wp->w_buffer == buf)
5212 break;
5213 /* If the buffer already has a window, move it */
5214 if (wp != NULL)
5215 win_move_after(wp, curwin);
5216 }
5217
5218 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005220 bufref_T bufref;
5221
5222 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005223
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 /* Split the window and put the buffer in it */
5225 p_ea_save = p_ea;
5226 p_ea = TRUE; /* use space from all windows */
5227 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5228 ++open_wins;
5229 p_ea = p_ea_save;
5230 if (split_ret == FAIL)
5231 continue;
5232
5233 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005234#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 swap_exists_action = SEA_DIALOG;
5236#endif
5237 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005238 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005240 /* autocommands deleted the buffer!!! */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005241#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 swap_exists_action = SEA_NONE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 break;
5245 }
Bram Moolenaare64ac772005-12-07 20:54:59 +00005246#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 if (swap_exists_action == SEA_QUIT)
5248 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005249# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005250 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005251
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005252 /* Reset the error/interrupt/exception state here so that
5253 * aborting() returns FALSE when closing a window. */
5254 enter_cleanup(&cs);
5255# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005256
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005257 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 win_close(curwin, TRUE);
5259 --open_wins;
5260 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005261 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005262
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005263# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005264 /* Restore the error/interrupt/exception state if not
5265 * discarded by a new aborting error, interrupt, or uncaught
5266 * exception. */
5267 leave_cleanup(&cs);
5268# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 }
5270 else
5271 handle_swap_exists(NULL);
5272#endif
5273 }
5274
5275 ui_breakcheck();
5276 if (got_int)
5277 {
5278 (void)vgetc(); /* only break the file loading, not the rest */
5279 break;
5280 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005281#ifdef FEAT_EVAL
5282 /* Autocommands deleted the buffer or aborted script processing!!! */
5283 if (aborting())
5284 break;
5285#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005286 /* When ":tab" was used open a new tab for a new window repeatedly. */
5287 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5288 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 --autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 win_enter(firstwin, FALSE); /* back to first window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293
5294 /*
5295 * Close superfluous windows.
5296 */
5297 for (wp = lastwin; open_wins > count; )
5298 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005299 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 || autowrite(wp->w_buffer, FALSE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 if (!win_valid(wp))
5302 {
5303 /* BufWrite Autocommands made the window invalid, start over */
5304 wp = lastwin;
5305 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005306 else if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005308 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 --open_wins;
5310 wp = lastwin;
5311 }
5312 else
5313 {
5314 wp = wp->w_prev;
5315 if (wp == NULL)
5316 break;
5317 }
5318 }
5319}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005322static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005323
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324/*
5325 * do_modelines() - process mode lines for the current file
5326 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005327 * "flags" can be:
5328 * OPT_WINONLY only set options local to window
5329 * OPT_NOWIN don't set options local to window
5330 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 * Returns immediately if the "ml" option isn't set.
5332 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005334do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005336 linenr_T lnum;
5337 int nmlines;
5338 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339
5340 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5341 return;
5342
5343 /* Disallow recursive entry here. Can happen when executing a modeline
5344 * triggers an autocommand, which reloads modelines with a ":do". */
5345 if (entered)
5346 return;
5347
5348 ++entered;
5349 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5350 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005351 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 nmlines = 0;
5353
5354 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5355 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005356 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 nmlines = 0;
5358 --entered;
5359}
5360
5361#include "version.h" /* for version number */
5362
5363/*
5364 * chk_modeline() - check a single line for a mode string
5365 * Return FAIL if an error encountered.
5366 */
5367 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005368chk_modeline(
5369 linenr_T lnum,
5370 int flags) /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371{
5372 char_u *s;
5373 char_u *e;
5374 char_u *linecopy; /* local copy of any modeline found */
5375 int prev;
5376 int vers;
5377 int end;
5378 int retval = OK;
5379 char_u *save_sourcing_name;
5380 linenr_T save_sourcing_lnum;
5381#ifdef FEAT_EVAL
5382 scid_T save_SID;
5383#endif
5384
5385 prev = -1;
5386 for (s = ml_get(lnum); *s != NUL; ++s)
5387 {
5388 if (prev == -1 || vim_isspace(prev))
5389 {
5390 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5391 || STRNCMP(s, "vi:", (size_t)3) == 0)
5392 break;
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005393 /* Accept both "vim" and "Vim". */
5394 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 {
5396 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5397 e = s + 4;
5398 else
5399 e = s + 3;
5400 vers = getdigits(&e);
5401 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005402 && (s[0] != 'V'
5403 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 && (s[3] == ':'
5405 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5406 || (VIM_VERSION_100 < vers && s[3] == '<')
5407 || (VIM_VERSION_100 > vers && s[3] == '>')
5408 || (VIM_VERSION_100 == vers && s[3] == '=')))
5409 break;
5410 }
5411 }
5412 prev = *s;
5413 }
5414
5415 if (*s)
5416 {
5417 do /* skip over "ex:", "vi:" or "vim:" */
5418 ++s;
5419 while (s[-1] != ':');
5420
5421 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5422 if (linecopy == NULL)
5423 return FAIL;
5424
5425 save_sourcing_lnum = sourcing_lnum;
5426 save_sourcing_name = sourcing_name;
5427 sourcing_lnum = lnum; /* prepare for emsg() */
5428 sourcing_name = (char_u *)"modelines";
5429
5430 end = FALSE;
5431 while (end == FALSE)
5432 {
5433 s = skipwhite(s);
5434 if (*s == NUL)
5435 break;
5436
5437 /*
5438 * Find end of set command: ':' or end of line.
5439 * Skip over "\:", replacing it with ":".
5440 */
5441 for (e = s; *e != ':' && *e != NUL; ++e)
5442 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005443 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 if (*e == NUL)
5445 end = TRUE;
5446
5447 /*
5448 * If there is a "set" command, require a terminating ':' and
5449 * ignore the stuff after the ':'.
5450 * "vi:set opt opt opt: foo" -- foo not interpreted
5451 * "vi:opt opt opt: foo" -- foo interpreted
5452 * Accept "se" for compatibility with Elvis.
5453 */
5454 if (STRNCMP(s, "set ", (size_t)4) == 0
5455 || STRNCMP(s, "se ", (size_t)3) == 0)
5456 {
5457 if (*e != ':') /* no terminating ':'? */
5458 break;
5459 end = TRUE;
5460 s = vim_strchr(s, ' ') + 1;
5461 }
5462 *e = NUL; /* truncate the set command */
5463
5464 if (*s != NUL) /* skip over an empty "::" */
5465 {
5466#ifdef FEAT_EVAL
5467 save_SID = current_SID;
5468 current_SID = SID_MODELINE;
5469#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005470 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471#ifdef FEAT_EVAL
5472 current_SID = save_SID;
5473#endif
5474 if (retval == FAIL) /* stop if error found */
5475 break;
5476 }
5477 s = e + 1; /* advance to next part */
5478 }
5479
5480 sourcing_lnum = save_sourcing_lnum;
5481 sourcing_name = save_sourcing_name;
5482
5483 vim_free(linecopy);
5484 }
5485 return retval;
5486}
5487
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005488#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005490read_viminfo_bufferlist(
5491 vir_T *virp,
5492 int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493{
5494 char_u *tab;
5495 linenr_T lnum;
5496 colnr_T col;
5497 buf_T *buf;
5498 char_u *sfname;
5499 char_u *xline;
5500
5501 /* Handle long line and escaped characters. */
5502 xline = viminfo_readstring(virp, 1, FALSE);
5503
5504 /* don't read in if there are files on the command-line or if writing: */
5505 if (xline != NULL && !writing && ARGCOUNT == 0
5506 && find_viminfo_parameter('%') != NULL)
5507 {
5508 /* Format is: <fname> Tab <lnum> Tab <col>.
5509 * Watch out for a Tab in the file name, work from the end. */
5510 lnum = 0;
5511 col = 0;
5512 tab = vim_strrchr(xline, '\t');
5513 if (tab != NULL)
5514 {
5515 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005516 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 tab = vim_strrchr(xline, '\t');
5518 if (tab != NULL)
5519 {
5520 *tab++ = '\0';
5521 lnum = atol((char *)tab);
5522 }
5523 }
5524
5525 /* Expand "~/" in the file name at "line + 1" to a full path.
5526 * Then try shortening it by comparing with the current directory */
5527 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005528 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529
5530 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5531 if (buf != NULL) /* just in case... */
5532 {
5533 buf->b_last_cursor.lnum = lnum;
5534 buf->b_last_cursor.col = col;
5535 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5536 }
5537 }
5538 vim_free(xline);
5539
5540 return viminfo_readline(virp);
5541}
5542
5543 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005544write_viminfo_bufferlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545{
5546 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005548 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005550 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551
5552 if (find_viminfo_parameter('%') == NULL)
5553 return;
5554
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005555 /* Without a number -1 is returned: do all buffers. */
5556 max_buffers = get_viminfo_parameter('%');
5557
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005559#define LINE_BUF_LEN (MAXPATHL + 40)
5560 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 if (line == NULL)
5562 return;
5563
Bram Moolenaarf740b292006-02-16 22:11:02 +00005564 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 set_last_cursor(win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005567 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar29323592016-07-24 22:04:11 +02005568 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 {
5570 if (buf->b_fname == NULL
5571 || !buf->b_p_bl
5572#ifdef FEAT_QUICKFIX
5573 || bt_quickfix(buf)
5574#endif
Bram Moolenaare6278052017-08-13 18:11:17 +02005575#ifdef FEAT_TERMINAL
5576 || bt_terminal(buf)
5577#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 || removable(buf->b_ffname))
5579 continue;
5580
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005581 if (max_buffers-- == 0)
5582 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 putc('%', fp);
5584 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005585 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 (long)buf->b_last_cursor.lnum,
5587 buf->b_last_cursor.col);
5588 viminfo_writestring(fp, line);
5589 }
5590 vim_free(line);
5591}
5592#endif
5593
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005594/*
5595 * Return TRUE if "buf" is the quickfix buffer.
5596 */
5597 int
5598bt_quickfix(buf_T *buf)
5599{
5600 return buf != NULL && buf->b_p_bt[0] == 'q';
5601}
5602
5603/*
5604 * Return TRUE if "buf" is a terminal buffer.
5605 */
5606 int
5607bt_terminal(buf_T *buf)
5608{
5609 return buf != NULL && buf->b_p_bt[0] == 't';
5610}
5611
5612/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005613 * Return TRUE if "buf" is a help buffer.
5614 */
5615 int
5616bt_help(buf_T *buf)
5617{
5618 return buf != NULL && buf->b_help;
5619}
5620
5621/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005622 * Return TRUE if "buf" is a "nofile", "acwrite" or "terminal" buffer.
5623 * This means the buffer name is not a file name.
5624 */
5625 int
5626bt_nofile(buf_T *buf)
5627{
5628 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5629 || buf->b_p_bt[0] == 'a'
5630 || buf->b_p_bt[0] == 't');
5631}
5632
5633/*
5634 * Return TRUE if "buf" is a "nowrite", "nofile" or "terminal" buffer.
5635 */
5636 int
5637bt_dontwrite(buf_T *buf)
5638{
5639 return buf != NULL && (buf->b_p_bt[0] == 'n' || buf->b_p_bt[0] == 't');
5640}
5641
5642 int
5643bt_dontwrite_msg(buf_T *buf)
5644{
5645 if (bt_dontwrite(buf))
5646 {
5647 EMSG(_("E382: Cannot write, 'buftype' option is set"));
5648 return TRUE;
5649 }
5650 return FALSE;
5651}
5652
5653/*
5654 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
5655 * and 'bufhidden'.
5656 */
5657 int
5658buf_hide(buf_T *buf)
5659{
5660 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
5661 switch (buf->b_p_bh[0])
5662 {
5663 case 'u': /* "unload" */
5664 case 'w': /* "wipe" */
5665 case 'd': return FALSE; /* "delete" */
5666 case 'h': return TRUE; /* "hide" */
5667 }
5668 return (p_hid || cmdmod.hide);
5669}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670
5671/*
5672 * Return special buffer name.
5673 * Returns NULL when the buffer has a normal file name.
5674 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005675 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005676buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677{
Bram Moolenaar4033c552017-09-16 20:54:51 +02005678#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005680 {
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005681 win_T *win;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005682 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005683
5684 /*
5685 * For location list window, w_llist_ref points to the location list.
5686 * For quickfix window, w_llist_ref is NULL.
5687 */
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005688 if (find_win_for_buf(buf, &win, &tp) == OK && win->w_llist_ref != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005689 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005690 else
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005691 return (char_u *)_(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02005694
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 /* There is no _file_ when 'buftype' is "nofile", b_sfname
Bram Moolenaar21554412017-07-24 21:44:43 +02005696 * contains the name as specified by the user. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 if (bt_nofile(buf))
5698 {
Bram Moolenaar21554412017-07-24 21:44:43 +02005699#ifdef FEAT_TERMINAL
5700 if (buf->b_term != NULL)
5701 return term_get_status_text(buf->b_term);
5702#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02005703 if (buf->b_fname != NULL)
5704 return buf->b_fname;
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005705 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 }
Bram Moolenaar21554412017-07-24 21:44:43 +02005707
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005709 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 return NULL;
5711}
5712
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02005713#if defined(FEAT_JOB_CHANNEL) \
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005714 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5715 || defined(PROTO)
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02005716# define SWITCH_TO_WIN
5717
5718/*
5719 * Find a window that contains "buf" and switch to it.
5720 * If there is no such window, use the current window and change "curbuf".
5721 * Caller must initialize save_curbuf to NULL.
5722 * restore_win_for_buf() MUST be called later!
5723 */
5724 void
5725switch_to_win_for_buf(
5726 buf_T *buf,
5727 win_T **save_curwinp,
5728 tabpage_T **save_curtabp,
5729 bufref_T *save_curbuf)
5730{
5731 win_T *wp;
5732 tabpage_T *tp;
5733
5734 if (find_win_for_buf(buf, &wp, &tp) == FAIL)
5735 switch_buffer(save_curbuf, buf);
5736 else if (switch_win(save_curwinp, save_curtabp, wp, tp, TRUE) == FAIL)
5737 {
5738 restore_win(*save_curwinp, *save_curtabp, TRUE);
5739 switch_buffer(save_curbuf, buf);
5740 }
5741}
5742
5743 void
5744restore_win_for_buf(
5745 win_T *save_curwin,
5746 tabpage_T *save_curtab,
5747 bufref_T *save_curbuf)
5748{
5749 if (save_curbuf->br_buf == NULL)
5750 restore_win(save_curwin, save_curtab, TRUE);
5751 else
5752 restore_buffer(save_curbuf);
5753}
5754#endif
5755
Bram Moolenaar4033c552017-09-16 20:54:51 +02005756#if defined(FEAT_QUICKFIX) || defined(SWITCH_TO_WIN) || defined(PROTO)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005757/*
5758 * Find a window for buffer "buf".
5759 * If found OK is returned and "wp" and "tp" are set to the window and tabpage.
5760 * If not found FAIL is returned.
5761 */
5762 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005763find_win_for_buf(
5764 buf_T *buf,
5765 win_T **wp,
5766 tabpage_T **tp)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005767{
5768 FOR_ALL_TAB_WINDOWS(*tp, *wp)
5769 if ((*wp)->w_buffer == buf)
5770 goto win_found;
5771 return FAIL;
5772win_found:
5773 return OK;
5774}
5775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776
5777#if defined(FEAT_SIGNS) || defined(PROTO)
5778/*
5779 * Insert the sign into the signlist.
5780 */
5781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005782insert_sign(
5783 buf_T *buf, /* buffer to store sign in */
5784 signlist_T *prev, /* previous sign entry */
5785 signlist_T *next, /* next sign entry */
5786 int id, /* sign ID */
5787 linenr_T lnum, /* line number which gets the mark */
5788 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789{
5790 signlist_T *newsign;
5791
5792 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5793 if (newsign != NULL)
5794 {
5795 newsign->id = id;
5796 newsign->lnum = lnum;
5797 newsign->typenr = typenr;
5798 newsign->next = next;
5799#ifdef FEAT_NETBEANS_INTG
5800 newsign->prev = prev;
5801 if (next != NULL)
5802 next->prev = newsign;
5803#endif
5804
5805 if (prev == NULL)
5806 {
5807 /* When adding first sign need to redraw the windows to create the
5808 * column for signs. */
5809 if (buf->b_signlist == NULL)
5810 {
5811 redraw_buf_later(buf, NOT_VALID);
5812 changed_cline_bef_curs();
5813 }
5814
5815 /* first sign in signlist */
5816 buf->b_signlist = newsign;
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01005817#ifdef FEAT_NETBEANS_INTG
5818 if (netbeans_active())
5819 buf->b_has_sign_column = TRUE;
5820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 }
5822 else
5823 prev->next = newsign;
5824 }
5825}
5826
5827/*
5828 * Add the sign into the signlist. Find the right spot to do it though.
5829 */
5830 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005831buf_addsign(
5832 buf_T *buf, /* buffer to store sign in */
5833 int id, /* sign ID */
5834 linenr_T lnum, /* line number which gets the mark */
5835 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836{
5837 signlist_T *sign; /* a sign in the signlist */
5838 signlist_T *prev; /* the previous sign */
5839
5840 prev = NULL;
5841 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5842 {
5843 if (lnum == sign->lnum && id == sign->id)
5844 {
5845 sign->typenr = typenr;
5846 return;
5847 }
5848 else if (
5849#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5850 id < 0 &&
5851#endif
5852 lnum < sign->lnum)
5853 {
5854#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5855 /* XXX - GRP: Is this because of sign slide problem? Or is it
5856 * really needed? Or is it because we allow multiple signs per
5857 * line? If so, should I add that feature to FEAT_SIGNS?
5858 */
5859 while (prev != NULL && prev->lnum == lnum)
5860 prev = prev->prev;
5861 if (prev == NULL)
5862 sign = buf->b_signlist;
5863 else
5864 sign = prev->next;
5865#endif
5866 insert_sign(buf, prev, sign, id, lnum, typenr);
5867 return;
5868 }
5869 prev = sign;
5870 }
5871#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5872 /* XXX - GRP: See previous comment */
5873 while (prev != NULL && prev->lnum == lnum)
5874 prev = prev->prev;
5875 if (prev == NULL)
5876 sign = buf->b_signlist;
5877 else
5878 sign = prev->next;
5879#endif
5880 insert_sign(buf, prev, sign, id, lnum, typenr);
5881
5882 return;
5883}
5884
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005885/*
5886 * For an existing, placed sign "markId" change the type to "typenr".
5887 * Returns the line number of the sign, or zero if the sign is not found.
5888 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005889 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005890buf_change_sign_type(
5891 buf_T *buf, /* buffer to store sign in */
5892 int markId, /* sign ID */
5893 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894{
5895 signlist_T *sign; /* a sign in the signlist */
5896
5897 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5898 {
5899 if (sign->id == markId)
5900 {
5901 sign->typenr = typenr;
5902 return sign->lnum;
5903 }
5904 }
5905
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005906 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907}
5908
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005909 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005910buf_getsigntype(
5911 buf_T *buf,
5912 linenr_T lnum,
5913 int type) /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914{
5915 signlist_T *sign; /* a sign in a b_signlist */
5916
5917 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5918 if (sign->lnum == lnum
5919 && (type == SIGN_ANY
5920# ifdef FEAT_SIGN_ICONS
5921 || (type == SIGN_ICON
5922 && sign_get_image(sign->typenr) != NULL)
5923# endif
5924 || (type == SIGN_TEXT
5925 && sign_get_text(sign->typenr) != NULL)
5926 || (type == SIGN_LINEHL
5927 && sign_get_attr(sign->typenr, TRUE) != 0)))
5928 return sign->typenr;
5929 return 0;
5930}
5931
5932
5933 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005934buf_delsign(
5935 buf_T *buf, /* buffer sign is stored in */
5936 int id) /* sign id */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937{
5938 signlist_T **lastp; /* pointer to pointer to current sign */
5939 signlist_T *sign; /* a sign in a b_signlist */
5940 signlist_T *next; /* the next sign in a b_signlist */
5941 linenr_T lnum; /* line number whose sign was deleted */
5942
5943 lastp = &buf->b_signlist;
5944 lnum = 0;
5945 for (sign = buf->b_signlist; sign != NULL; sign = next)
5946 {
5947 next = sign->next;
5948 if (sign->id == id)
5949 {
5950 *lastp = next;
5951#ifdef FEAT_NETBEANS_INTG
5952 if (next != NULL)
5953 next->prev = sign->prev;
5954#endif
5955 lnum = sign->lnum;
5956 vim_free(sign);
5957 break;
5958 }
5959 else
5960 lastp = &sign->next;
5961 }
5962
5963 /* When deleted the last sign need to redraw the windows to remove the
5964 * sign column. */
5965 if (buf->b_signlist == NULL)
5966 {
5967 redraw_buf_later(buf, NOT_VALID);
5968 changed_cline_bef_curs();
5969 }
5970
5971 return lnum;
5972}
5973
5974
5975/*
5976 * Find the line number of the sign with the requested id. If the sign does
5977 * not exist, return 0 as the line number. This will still let the correct file
5978 * get loaded.
5979 */
5980 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005981buf_findsign(
5982 buf_T *buf, /* buffer to store sign in */
5983 int id) /* sign ID */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984{
5985 signlist_T *sign; /* a sign in the signlist */
5986
5987 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5988 if (sign->id == id)
5989 return sign->lnum;
5990
5991 return 0;
5992}
5993
5994 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005995buf_findsign_id(
5996 buf_T *buf, /* buffer whose sign we are searching for */
5997 linenr_T lnum) /* line number of sign */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998{
5999 signlist_T *sign; /* a sign in the signlist */
6000
6001 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
6002 if (sign->lnum == lnum)
6003 return sign->id;
6004
6005 return 0;
6006}
6007
6008
6009# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
6010/* see if a given type of sign exists on a specific line */
6011 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006012buf_findsigntype_id(
6013 buf_T *buf, /* buffer whose sign we are searching for */
6014 linenr_T lnum, /* line number of sign */
6015 int typenr) /* sign type number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016{
6017 signlist_T *sign; /* a sign in the signlist */
6018
6019 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
6020 if (sign->lnum == lnum && sign->typenr == typenr)
6021 return sign->id;
6022
6023 return 0;
6024}
6025
6026
6027# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
6028/* return the number of icons on the given line */
6029 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006030buf_signcount(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031{
6032 signlist_T *sign; /* a sign in the signlist */
6033 int count = 0;
6034
6035 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
6036 if (sign->lnum == lnum)
6037 if (sign_get_image(sign->typenr) != NULL)
6038 count++;
6039
6040 return count;
6041}
6042# endif /* FEAT_SIGN_ICONS */
6043# endif /* FEAT_NETBEANS_INTG */
6044
6045
6046/*
6047 * Delete signs in buffer "buf".
6048 */
Bram Moolenaarf65e5662012-07-10 15:18:22 +02006049 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006050buf_delete_signs(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051{
6052 signlist_T *next;
6053
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02006054 /* When deleting the last sign need to redraw the windows to remove the
Bram Moolenaar4e036c92014-07-16 16:30:28 +02006055 * sign column. Not when curwin is NULL (this means we're exiting). */
6056 if (buf->b_signlist != NULL && curwin != NULL)
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02006057 {
6058 redraw_buf_later(buf, NOT_VALID);
6059 changed_cline_bef_curs();
6060 }
6061
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 while (buf->b_signlist != NULL)
6063 {
6064 next = buf->b_signlist->next;
6065 vim_free(buf->b_signlist);
6066 buf->b_signlist = next;
6067 }
6068}
6069
6070/*
6071 * Delete all signs in all buffers.
6072 */
6073 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006074buf_delete_all_signs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075{
6076 buf_T *buf; /* buffer we are checking for signs */
6077
Bram Moolenaar29323592016-07-24 22:04:11 +02006078 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 if (buf->b_signlist != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 buf_delete_signs(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081}
6082
6083/*
6084 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
6085 */
6086 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006087sign_list_placed(buf_T *rbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088{
6089 buf_T *buf;
6090 signlist_T *p;
6091 char lbuf[BUFSIZ];
6092
6093 MSG_PUTS_TITLE(_("\n--- Signs ---"));
6094 msg_putchar('\n');
6095 if (rbuf == NULL)
6096 buf = firstbuf;
6097 else
6098 buf = rbuf;
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01006099 while (buf != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 {
6101 if (buf->b_signlist != NULL)
6102 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00006103 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006104 MSG_PUTS_ATTR(lbuf, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 msg_putchar('\n');
6106 }
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01006107 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00006109 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
6111 MSG_PUTS(lbuf);
6112 msg_putchar('\n');
6113 }
6114 if (rbuf != NULL)
6115 break;
6116 buf = buf->b_next;
6117 }
6118}
6119
6120/*
6121 * Adjust a placed sign for inserted/deleted lines.
6122 */
6123 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006124sign_mark_adjust(
6125 linenr_T line1,
6126 linenr_T line2,
6127 long amount,
6128 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129{
6130 signlist_T *sign; /* a sign in a b_signlist */
6131
6132 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
6133 {
6134 if (sign->lnum >= line1 && sign->lnum <= line2)
6135 {
6136 if (amount == MAXLNUM)
6137 sign->lnum = line1;
6138 else
6139 sign->lnum += amount;
6140 }
6141 else if (sign->lnum > line2)
6142 sign->lnum += amount_after;
6143 }
6144}
6145#endif /* FEAT_SIGNS */
6146
6147/*
6148 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
6149 */
6150 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006151set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152{
6153 if (on != curbuf->b_p_bl)
6154 {
6155 curbuf->b_p_bl = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 if (on)
6157 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
6158 else
6159 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 }
6161}
6162
6163/*
6164 * Read the file for "buf" again and check if the contents changed.
6165 * Return TRUE if it changed or this could not be checked.
6166 */
6167 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006168buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169{
6170 buf_T *newbuf;
6171 int differ = TRUE;
6172 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 exarg_T ea;
6175
6176 /* Allocate a buffer without putting it in the buffer list. */
6177 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6178 if (newbuf == NULL)
6179 return TRUE;
6180
6181 /* Force the 'fileencoding' and 'fileformat' to be equal. */
6182 if (prep_exarg(&ea, buf) == FAIL)
6183 {
6184 wipe_buffer(newbuf, FALSE);
6185 return TRUE;
6186 }
6187
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 /* set curwin/curbuf to buf and save a few things */
6189 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190
Bram Moolenaar4770d092006-01-12 23:22:24 +00006191 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 && readfile(buf->b_ffname, buf->b_fname,
6193 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
6194 &ea, READ_NEW | READ_DUMMY) == OK)
6195 {
6196 /* compare the two files line by line */
6197 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
6198 {
6199 differ = FALSE;
6200 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6201 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
6202 {
6203 differ = TRUE;
6204 break;
6205 }
6206 }
6207 }
6208 vim_free(ea.cmd);
6209
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 /* restore curwin/curbuf and a few other things */
6211 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212
6213 if (curbuf != newbuf) /* safety check */
6214 wipe_buffer(newbuf, FALSE);
6215
6216 return differ;
6217}
6218
6219/*
6220 * Wipe out a buffer and decrement the last buffer number if it was used for
6221 * this buffer. Call this to wipe out a temp buffer that does not contain any
6222 * marks.
6223 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006225wipe_buffer(
6226 buf_T *buf,
6227 int aucmd UNUSED) /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228{
6229 if (buf->b_fnum == top_file_num - 1)
6230 --top_file_num;
6231
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006233 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006234
Bram Moolenaar42ec6562012-02-22 14:58:37 +01006235 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006236
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006238 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239}