blob: 2ca131d23e11ee5fd19e4d3a40cc816128fca827 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * buffer.c: functions for dealing with the buffer structure
12 */
13
14/*
15 * The buffer list is a double linked list of all buffers.
16 * Each buffer can be in one of these states:
17 * never loaded: BF_NEVERLOADED is set, only the file name is valid
18 * not loaded: b_ml.ml_mfp == NULL, no memfile allocated
19 * hidden: b_nwindows == 0, loaded but not displayed in a window
20 * normal: loaded and displayed in a window
21 *
22 * Instead of storing file names all over the place, each file name is
23 * stored in the buffer list. It can be referenced by a number.
24 *
25 * The current implementation remembers all file names ever used.
26 */
27
Bram Moolenaar071d4272004-06-13 20:20:40 +000028#include "vim.h"
29
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010030static char_u *buflist_match(regmatch_T *rmp, buf_T *buf, int ignore_case);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010031static char_u *fname_match(regmatch_T *rmp, char_u *name, int ignore_case);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010032static void buflist_setfpos(buf_T *buf, win_T *win, linenr_T lnum, colnr_T col, int copy_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +000033#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +020034static buf_T *buflist_findname_stat(char_u *ffname, stat_T *st);
35static int otherfile_buf(buf_T *buf, char_u *ffname, stat_T *stp);
36static int buf_same_ino(buf_T *buf, stat_T *stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037#else
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010038static int otherfile_buf(buf_T *buf, char_u *ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000039#endif
40#ifdef FEAT_TITLE
Bram Moolenaar84a93082018-06-16 22:58:15 +020041static int value_changed(char_u *str, char_u **last);
Bram Moolenaar071d4272004-06-13 20:20:40 +000042#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010043static int append_arg_number(win_T *wp, char_u *buf, int buflen, int add_file);
44static void free_buffer(buf_T *);
45static void free_buffer_stuff(buf_T *buf, int free_options);
46static void clear_wininfo(buf_T *buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000047
48#ifdef UNIX
49# define dev_T dev_t
50#else
51# define dev_T unsigned
52#endif
53
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);
Bram Moolenaard569bb02018-08-11 13:57:20 +0200207 if (perm >= 0 && (S_ISFIFO(perm)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200208 || S_ISSOCK(perm)
Bram Moolenaarf04507d2016-08-20 15:05:39 +0200209# ifdef OPEN_CHR_FILES
210 || (S_ISCHR(perm) && is_dev_fd_file(curbuf->b_ffname))
211# endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200212 ))
213 read_fifo = TRUE;
214 if (read_fifo)
215 curbuf->b_p_bin = TRUE;
216#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100217 if (shortmess(SHM_FILEINFO))
218 msg_silent = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200220 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200221 flags | READ_NEW | (read_fifo ? READ_FIFO : 0));
222#ifdef UNIX
223 if (read_fifo)
224 {
225 curbuf->b_p_bin = save_bin;
226 if (retval == OK)
227 retval = read_buffer(FALSE, eap, flags);
228 }
229#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100230 msg_silent = old_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231#ifdef FEAT_NETBEANS_INTG
232 netbeansFireChanges = oldFire;
233#endif
234 /* Help buffer is filtered. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +0200235 if (bt_help(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 fix_help_buffer();
237 }
238 else if (read_stdin)
239 {
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200240 int save_bin = curbuf->b_p_bin;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241
242 /*
243 * First read the text in binary mode into the buffer.
244 * Then read from that same buffer and append at the end. This makes
245 * it possible to retry when 'fileformat' or 'fileencoding' was
246 * guessed wrong.
247 */
248 curbuf->b_p_bin = TRUE;
249 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200250 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
251 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 curbuf->b_p_bin = save_bin;
253 if (retval == OK)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200254 retval = read_buffer(TRUE, eap, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255 }
256
257 /* if first time loading this buffer, init b_chartab[] */
258 if (curbuf->b_flags & BF_NEVERLOADED)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100259 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 (void)buf_init_chartab(curbuf, FALSE);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100261#ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100262 parse_cino(curbuf);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100263#endif
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265
266 /*
267 * Set/reset the Changed flag first, autocmds may change the buffer.
268 * Apply the automatic commands, before processing the modelines.
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200269 * So the modelines have priority over autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270 */
271 /* When reading stdin, the buffer contents always needs writing, so set
272 * the changed flag. Unless in readonly mode: "ls | gview -".
273 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000274 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 || modified_was_set /* ":set modified" used in autocmd */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100276#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000279 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100281 else if (retval == OK && !read_stdin && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282 unchanged(curbuf, FALSE);
283 save_file_ff(curbuf); /* keep this fileformat */
284
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100285 /* Set last_changedtick to avoid triggering a TextChanged autocommand right
286 * after it was added. */
287 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
288#ifdef FEAT_INS_EXPAND
289 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
290#endif
291
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292 /* require "!" to overwrite the file, because it wasn't read completely */
293#ifdef FEAT_EVAL
294 if (aborting())
295#else
296 if (got_int)
297#endif
298 curbuf->b_flags |= BF_READERR;
299
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000300#ifdef FEAT_FOLDING
301 /* Need to update automatic folding. Do this before the autocommands,
302 * they may use the fold info. */
303 foldUpdateAll(curwin);
304#endif
305
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 /* need to set w_topline, unless some autocommand already did that. */
307 if (!(curwin->w_valid & VALID_TOPLINE))
308 {
309 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100310#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100314#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100316#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318#endif
319
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100320 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322 /*
323 * The autocommands may have changed the current buffer. Apply the
324 * modelines to the correct buffer, if it still exists and is loaded.
325 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200326 if (bufref_valid(&old_curbuf) && old_curbuf.br_buf->b_ml.ml_mfp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 {
328 aco_save_T aco;
329
330 /* Go to the buffer that was opened. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200331 aucmd_prepbuf(&aco, old_curbuf.br_buf);
Bram Moolenaara3227e22006-03-08 21:32:40 +0000332 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
334
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100335#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100337 &retval);
338#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100340#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341
342 /* restore curwin/curbuf and a few other things */
343 aucmd_restbuf(&aco);
344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 }
346
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 return retval;
348}
349
350/*
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200351 * Store "buf" in "bufref" and set the free count.
352 */
353 void
354set_bufref(bufref_T *bufref, buf_T *buf)
355{
356 bufref->br_buf = buf;
Bram Moolenaarfadacf02017-06-19 20:35:32 +0200357 bufref->br_fnum = buf == NULL ? 0 : buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200358 bufref->br_buf_free_count = buf_free_count;
359}
360
361/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200362 * Return TRUE if "bufref->br_buf" points to the same buffer as when
363 * set_bufref() was called and it is a valid buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200364 * Only goes through the buffer list if buf_free_count changed.
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200365 * Also checks if b_fnum is still the same, a :bwipe followed by :new might get
366 * the same allocated memory, but it's a different buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200367 */
368 int
369bufref_valid(bufref_T *bufref)
370{
371 return bufref->br_buf_free_count == buf_free_count
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200372 ? TRUE : buf_valid(bufref->br_buf)
373 && bufref->br_fnum == bufref->br_buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200374}
375
376/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200378 * This can be slow if there are many buffers, prefer using bufref_valid().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 */
380 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100381buf_valid(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382{
383 buf_T *bp;
384
Bram Moolenaar82404332016-07-10 17:00:38 +0200385 /* Assume that we more often have a recent buffer, start with the last
386 * one. */
387 for (bp = lastbuf; bp != NULL; bp = bp->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 if (bp == buf)
389 return TRUE;
390 return FALSE;
391}
392
393/*
Bram Moolenaar480778b2016-07-14 22:09:39 +0200394 * A hash table used to quickly lookup a buffer by its number.
395 */
396static hashtab_T buf_hashtab;
397
398 static void
399buf_hashtab_add(buf_T *buf)
400{
401 sprintf((char *)buf->b_key, "%x", buf->b_fnum);
402 if (hash_add(&buf_hashtab, buf->b_key) == FAIL)
403 EMSG(_("E931: Buffer cannot be registered"));
404}
405
406 static void
407buf_hashtab_remove(buf_T *buf)
408{
409 hashitem_T *hi = hash_find(&buf_hashtab, buf->b_key);
410
411 if (!HASHITEM_EMPTY(hi))
412 hash_remove(&buf_hashtab, hi);
413}
414
Bram Moolenaara997b452018-04-17 23:24:06 +0200415static char *e_buflocked = N_("E937: Attempt to delete a buffer that is in use");
416
Bram Moolenaar480778b2016-07-14 22:09:39 +0200417/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418 * Close the link to a buffer.
419 * "action" is used when there is no longer a window for the buffer.
420 * It can be:
421 * 0 buffer becomes hidden
422 * DOBUF_UNLOAD buffer is unloaded
423 * DOBUF_DELETE buffer is unloaded and removed from buffer list
424 * DOBUF_WIPE buffer is unloaded and really deleted
425 * When doing all but the first one on the current buffer, the caller should
426 * get a new buffer very soon!
427 *
428 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100429 *
430 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
431 * cause there to be only one window with this buffer. e.g. when ":quit" is
432 * supposed to close the window but autocommands close all other windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 */
434 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100435close_buffer(
436 win_T *win, /* if not NULL, set b_last_cursor */
437 buf_T *buf,
438 int action,
439 int abort_if_last UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100442 int nwindows;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200443 bufref_T bufref;
Bram Moolenaar3a117e12016-10-30 21:57:52 +0100444 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200445 win_T *the_curwin = curwin;
446 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447 int unload_buf = (action != 0);
448 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
449 int wipe_buf = (action == DOBUF_WIPE);
450
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200451 /*
452 * Force unloading or deleting when 'bufhidden' says so.
453 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
454 * "hide" (otherwise we could never free or delete a buffer).
455 */
456 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
457 {
458 del_buf = TRUE;
459 unload_buf = TRUE;
460 }
461 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
462 {
463 del_buf = TRUE;
464 unload_buf = TRUE;
465 wipe_buf = TRUE;
466 }
467 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
468 unload_buf = TRUE;
469
Bram Moolenaar94053a52017-08-01 21:44:33 +0200470#ifdef FEAT_TERMINAL
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200471 if (bt_terminal(buf) && (buf->b_nwindows == 1 || del_buf))
Bram Moolenaar94053a52017-08-01 21:44:33 +0200472 {
473 if (term_job_running(buf->b_term))
474 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200475 if (wipe_buf || unload_buf)
Bram Moolenaara997b452018-04-17 23:24:06 +0200476 {
477 if (buf->b_locked)
478 {
479 EMSG(_(e_buflocked));
480 return;
481 }
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200482 /* Wiping out or unloading a terminal buffer kills the job. */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200483 free_terminal(buf);
Bram Moolenaara997b452018-04-17 23:24:06 +0200484 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200485 else
486 {
487 /* The job keeps running, hide the buffer. */
488 del_buf = FALSE;
489 unload_buf = FALSE;
490 }
491 }
492 else
493 {
494 /* A terminal buffer is wiped out if the job has finished. */
495 del_buf = TRUE;
496 unload_buf = TRUE;
497 wipe_buf = TRUE;
498 }
499 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200502 /* Disallow deleting the buffer when it is locked (already being closed or
503 * halfway a command that relies on it). Unloading is allowed. */
504 if (buf->b_locked > 0 && (del_buf || wipe_buf))
505 {
Bram Moolenaara997b452018-04-17 23:24:06 +0200506 EMSG(_(e_buflocked));
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200507 return;
508 }
509
Bram Moolenaar4033c552017-09-16 20:54:51 +0200510 /* check no autocommands closed the window */
511 if (win != NULL && win_valid_any_tab(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 {
513 /* Set b_last_cursor when closing the last window for the buffer.
514 * Remember the last cursor position and window options of the buffer.
515 * This used to be only for the current window, but then options like
516 * 'foldmethod' may be lost with a ":only" command. */
517 if (buf->b_nwindows == 1)
518 set_last_cursor(win);
519 buflist_setfpos(buf, win,
520 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
521 win->w_cursor.col, TRUE);
522 }
523
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200524 set_bufref(&bufref, buf);
525
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 /* When the buffer is no longer in a window, trigger BufWinLeave */
527 if (buf->b_nwindows == 1)
528 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200529 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200530 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
531 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200532 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100533 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200534 /* Autocommands deleted the buffer. */
535aucmd_abort:
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100536 EMSG(_(e_auabort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 return;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100538 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200539 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200540 if (abort_if_last && one_window())
541 /* Autocommands made this the only window. */
542 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543
544 /* When the buffer becomes hidden, but is not unloaded, trigger
545 * BufHidden */
546 if (!unload_buf)
547 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200548 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200549 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
550 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200551 && !bufref_valid(&bufref))
Bram Moolenaar362ce482012-06-06 19:02:45 +0200552 /* Autocommands deleted the buffer. */
553 goto aucmd_abort;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200554 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200555 if (abort_if_last && one_window())
556 /* Autocommands made this the only window. */
557 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100559#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 if (aborting()) /* autocmds may abort script processing */
561 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100562#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200564
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200565 /* If the buffer was in curwin and the window has changed, go back to that
566 * window, if it still exists. This avoids that ":edit x" triggering a
567 * "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
568 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
569 {
570 block_autocmds();
571 goto_tabpage_win(the_curtab, the_curwin);
572 unblock_autocmds();
573 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200574
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 nwindows = buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576
577 /* decrease the link count from windows (unless not in any window) */
578 if (buf->b_nwindows > 0)
579 --buf->b_nwindows;
580
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100581#ifdef FEAT_DIFF
582 if (diffopt_hiddenoff() && !unload_buf && buf->b_nwindows == 0)
Bram Moolenaarb7407d32018-02-03 17:36:27 +0100583 diff_buf_delete(buf); /* Clear 'diff' for hidden buffer. */
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100584#endif
585
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 /* Return when a window is displaying the buffer or when it's not
587 * unloaded. */
588 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590
591 /* Always remove the buffer when there is no file name. */
592 if (buf->b_ffname == NULL)
593 del_buf = TRUE;
594
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200595 /* When closing the current buffer stop Visual mode before freeing
596 * anything. */
Bram Moolenaar4930a762016-09-11 14:39:53 +0200597 if (buf == curbuf && VIsual_active
Bram Moolenaar9a27c7f2016-09-09 12:57:09 +0200598#if defined(EXITFREE)
599 && !entered_free_all_mem
600#endif
601 )
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200602 end_visual_mode();
603
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 /*
605 * Free all things allocated for this buffer.
606 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
607 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 /* Remember if we are closing the current buffer. Restore the number of
609 * windows, so that autocommands in buf_freeall() don't get confused. */
610 is_curbuf = (buf == curbuf);
611 buf->b_nwindows = nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200613 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 /* Autocommands may have deleted the buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200616 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100618#ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000619 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100621#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 /*
624 * It's possible that autocommands change curbuf to the one being deleted.
625 * This might cause the previous curbuf to be deleted unexpectedly. But
626 * in some cases it's OK to delete the curbuf, because a new one is
627 * obtained anyway. Therefore only return if curbuf changed to the
628 * deleted buffer.
629 */
630 if (buf == curbuf && !is_curbuf)
631 return;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200632
Bram Moolenaar4033c552017-09-16 20:54:51 +0200633 if (win_valid_any_tab(win) && win->w_buffer == buf)
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200634 win->w_buffer = NULL; /* make sure we don't use the buffer now */
635
636 /* Autocommands may have opened or closed windows for this buffer.
637 * Decrement the count for the close we do here. */
638 if (buf->b_nwindows > 0)
639 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 /*
642 * Remove the buffer from the list.
643 */
644 if (wipe_buf)
645 {
646#ifdef FEAT_SUN_WORKSHOP
647 if (usingSunWorkShop)
648 workshop_file_closed_lineno((char *)buf->b_ffname,
649 (int)buf->b_last_cursor.lnum);
650#endif
651 vim_free(buf->b_ffname);
652 vim_free(buf->b_sfname);
653 if (buf->b_prev == NULL)
654 firstbuf = buf->b_next;
655 else
656 buf->b_prev->b_next = buf->b_next;
657 if (buf->b_next == NULL)
658 lastbuf = buf->b_prev;
659 else
660 buf->b_next->b_prev = buf->b_prev;
661 free_buffer(buf);
662 }
663 else
664 {
665 if (del_buf)
666 {
667 /* Free all internal variables and reset option values, to make
668 * ":bdel" compatible with Vim 5.7. */
669 free_buffer_stuff(buf, TRUE);
670
671 /* Make it look like a new buffer. */
672 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
673
674 /* Init the options when loaded again. */
675 buf->b_p_initialized = FALSE;
676 }
677 buf_clear_file(buf);
678 if (del_buf)
679 buf->b_p_bl = FALSE;
680 }
681}
682
683/*
684 * Make buffer not contain a file.
685 */
686 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100687buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688{
689 buf->b_ml.ml_line_count = 1;
690 unchanged(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 buf->b_p_eol = TRUE;
693 buf->b_start_eol = TRUE;
694#ifdef FEAT_MBYTE
695 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000696 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697#endif
698 buf->b_ml.ml_mfp = NULL;
699 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
700#ifdef FEAT_NETBEANS_INTG
701 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702#endif
703}
704
705/*
706 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200707 * the file. Careful: get here with "curwin" NULL when exiting.
708 * flags:
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200709 * BFA_DEL buffer is going to be deleted
710 * BFA_WIPE buffer is going to be wiped out
711 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100714buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200717 bufref_T bufref;
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200718 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaar5a497892016-09-03 16:29:04 +0200719 win_T *the_curwin = curwin;
720 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721
Bram Moolenaar5a497892016-09-03 16:29:04 +0200722 /* Make sure the buffer isn't closed by autocommands. */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200723 ++buf->b_locked;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200724 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200725 if (buf->b_ml.ml_mfp != NULL)
726 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200727 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
728 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200729 && !bufref_valid(&bufref))
730 /* autocommands deleted the buffer */
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200731 return;
732 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200733 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200735 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
736 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200737 && !bufref_valid(&bufref))
738 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 return;
740 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200741 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200743 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
744 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200745 && !bufref_valid(&bufref))
746 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 return;
748 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200749 --buf->b_locked;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200750
Bram Moolenaar5a497892016-09-03 16:29:04 +0200751 /* If the buffer was in curwin and the window has changed, go back to that
752 * window, if it still exists. This avoids that ":edit x" triggering a
753 * "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
754 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
755 {
756 block_autocmds();
757 goto_tabpage_win(the_curtab, the_curwin);
758 unblock_autocmds();
759 }
Bram Moolenaar5a497892016-09-03 16:29:04 +0200760
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100761#ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000762 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765
766 /*
767 * It's possible that autocommands change curbuf to the one being deleted.
768 * This might cause curbuf to be deleted unexpectedly. But in some cases
769 * it's OK to delete the curbuf, because a new one is obtained anyway.
770 * Therefore only return if curbuf changed to the deleted buffer.
771 */
772 if (buf == curbuf && !is_curbuf)
773 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774#ifdef FEAT_DIFF
775 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
776#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200777#ifdef FEAT_SYN_HL
Bram Moolenaar89c71222011-11-30 15:40:56 +0100778 /* Remove any ownsyntax, unless exiting. */
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200779 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaar89c71222011-11-30 15:40:56 +0100780 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200781#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000782
783#ifdef FEAT_FOLDING
784 /* No folds in an empty buffer. */
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000785 {
786 win_T *win;
787 tabpage_T *tp;
788
789 FOR_ALL_TAB_WINDOWS(tp, win)
790 if (win->w_buffer == buf)
791 clearFolding(win);
792 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000793#endif
794
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795#ifdef FEAT_TCL
796 tcl_buffer_free(buf);
797#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 ml_close(buf, TRUE); /* close and delete the memline/memfile */
799 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200800 if ((flags & BFA_KEEP_UNDO) == 0)
801 {
802 u_blockfree(buf); /* free the memory allocated for undo */
803 u_clearall(buf); /* reset all undo information */
804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200806 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000808 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809}
810
811/*
812 * Free a buffer structure and the things it contains related to the buffer
813 * itself (not the file, that must have been done already).
814 */
815 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100816free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200818 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200820#ifdef FEAT_EVAL
Bram Moolenaarb2c87502017-10-14 21:15:58 +0200821 /* b:changedtick uses an item in buf_T, remove it now */
822 dictitem_remove(buf->b_vars, (dictitem_T *)&buf->b_ct_di);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200823 unref_var_dict(buf->b_vars);
824#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200825#ifdef FEAT_LUA
826 lua_buffer_free(buf);
827#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000828#ifdef FEAT_MZSCHEME
829 mzscheme_buffer_free(buf);
830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831#ifdef FEAT_PERL
832 perl_buf_free(buf);
833#endif
834#ifdef FEAT_PYTHON
835 python_buffer_free(buf);
836#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200837#ifdef FEAT_PYTHON3
838 python3_buffer_free(buf);
839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840#ifdef FEAT_RUBY
841 ruby_buffer_free(buf);
842#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200843#ifdef FEAT_JOB_CHANNEL
844 channel_buffer_free(buf);
845#endif
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200846#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +0200847 free_terminal(buf);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200848#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +0200849#ifdef FEAT_JOB_CHANNEL
850 vim_free(buf->b_prompt_text);
851 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
852#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200853
854 buf_hashtab_remove(buf);
855
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200856 aubuflocal_remove(buf);
857
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200858 if (autocmd_busy)
859 {
860 /* Do not free the buffer structure while autocommands are executing,
861 * it's still needed. Free it when autocmd_busy is reset. */
862 buf->b_next = au_pending_free_buf;
863 au_pending_free_buf = buf;
864 }
865 else
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200866 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867}
868
869/*
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100870 * Initializes b:changedtick.
Bram Moolenaar79518e22017-02-17 16:31:35 +0100871 */
872 static void
873init_changedtick(buf_T *buf)
874{
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100875 dictitem_T *di = (dictitem_T *)&buf->b_ct_di;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100876
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100877 di->di_flags = DI_FLAGS_FIX | DI_FLAGS_RO;
878 di->di_tv.v_type = VAR_NUMBER;
879 di->di_tv.v_lock = VAR_FIXED;
880 di->di_tv.vval.v_number = 0;
881
Bram Moolenaar92769c32017-02-25 15:41:37 +0100882#ifdef FEAT_EVAL
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100883 STRCPY(buf->b_ct_di.di_key, "changedtick");
884 (void)dict_add(buf->b_vars, di);
Bram Moolenaar92769c32017-02-25 15:41:37 +0100885#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +0100886}
887
888/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
890 */
891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100892free_buffer_stuff(
893 buf_T *buf,
894 int free_options) /* free options as well */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895{
896 if (free_options)
897 {
898 clear_wininfo(buf); /* including window-local options */
899 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200900#ifdef FEAT_SPELL
901 ga_clear(&buf->b_s.b_langp);
902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 }
904#ifdef FEAT_EVAL
Bram Moolenaar79518e22017-02-17 16:31:35 +0100905 {
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100906 varnumber_T tick = CHANGEDTICK(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +0100907
908 vars_clear(&buf->b_vars->dv_hashtab); /* free all buffer variables */
909 hash_init(&buf->b_vars->dv_hashtab);
910 init_changedtick(buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100911 CHANGEDTICK(buf) = tick;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913#endif
914#ifdef FEAT_USR_CMDS
915 uc_clear(&buf->b_ucmds); /* clear local user commands */
916#endif
917#ifdef FEAT_SIGNS
918 buf_delete_signs(buf); /* delete any signs */
919#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000920#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200921 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000922#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923#ifdef FEAT_LOCALMAP
924 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
925 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
926#endif
927#ifdef FEAT_MBYTE
Bram Moolenaard23a8232018-02-10 18:45:26 +0100928 VIM_CLEAR(buf->b_start_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929#endif
930}
931
932/*
933 * Free the b_wininfo list for buffer "buf".
934 */
935 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100936clear_wininfo(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937{
938 wininfo_T *wip;
939
940 while (buf->b_wininfo != NULL)
941 {
942 wip = buf->b_wininfo;
943 buf->b_wininfo = wip->wi_next;
944 if (wip->wi_optset)
945 {
946 clear_winopt(&wip->wi_opt);
947#ifdef FEAT_FOLDING
948 deleteFoldRecurse(&wip->wi_folds);
949#endif
950 }
951 vim_free(wip);
952 }
953}
954
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955/*
956 * Go to another buffer. Handles the result of the ATTENTION dialog.
957 */
958 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100959goto_buffer(
960 exarg_T *eap,
961 int start,
962 int dir,
963 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964{
Bram Moolenaar0c72fe42018-03-29 16:04:08 +0200965#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200966 bufref_T old_curbuf;
967
968 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969
970 swap_exists_action = SEA_DIALOG;
Bram Moolenaar0c72fe42018-03-29 16:04:08 +0200971#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
973 start, dir, count, eap->forceit);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +0200974#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
976 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +0200977# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000978 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000979
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000980 /* Reset the error/interrupt/exception state here so that
981 * aborting() returns FALSE when closing a window. */
982 enter_cleanup(&cs);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +0200983# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000984
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000985 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 win_close(curwin, TRUE);
987 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000988 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000989
Bram Moolenaar0c72fe42018-03-29 16:04:08 +0200990# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000991 /* Restore the error/interrupt/exception state if not discarded by a
992 * new aborting error, interrupt, or uncaught exception. */
993 leave_cleanup(&cs);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +0200994# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 }
996 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200997 handle_swap_exists(&old_curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998#endif
Bram Moolenaar0c72fe42018-03-29 16:04:08 +0200999}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000
Bram Moolenaare64ac772005-12-07 20:54:59 +00001001#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002/*
1003 * Handle the situation of swap_exists_action being set.
1004 * It is allowed for "old_curbuf" to be NULL or invalid.
1005 */
1006 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001007handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008{
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001009# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001010 cleanup_T cs;
1011# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001012# ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001013 long old_tw = curbuf->b_p_tw;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001014# endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001015 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001016
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 if (swap_exists_action == SEA_QUIT)
1018 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001019# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001020 /* Reset the error/interrupt/exception state here so that
1021 * aborting() returns FALSE when closing a buffer. */
1022 enter_cleanup(&cs);
1023# endif
1024
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 /* User selected Quit at ATTENTION prompt. Go back to previous
1026 * buffer. If that buffer is gone or the same as the current one,
1027 * open a new, empty buffer. */
1028 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001029 swap_exists_did_quit = TRUE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001030 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001031 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1032 || old_curbuf->br_buf == curbuf)
1033 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
1034 else
1035 buf = old_curbuf->br_buf;
1036 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001037 {
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001038 int old_msg_silent = msg_silent;
1039
1040 if (shortmess(SHM_FILEINFO))
1041 msg_silent = 1; // prevent fileinfo message
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001042 enter_buffer(buf);
Bram Moolenaar2f0f8712018-08-21 18:50:18 +02001043 // restore msg_silent, so that the command line will be shown
1044 msg_silent = old_msg_silent;
1045
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001046# ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001047 if (old_tw != curbuf->b_p_tw)
1048 check_colorcolumn(curwin);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001049# endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001052
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001053# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001054 /* Restore the error/interrupt/exception state if not discarded by a
1055 * new aborting error, interrupt, or uncaught exception. */
1056 leave_cleanup(&cs);
1057# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 }
1059 else if (swap_exists_action == SEA_RECOVER)
1060 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001061# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001062 /* Reset the error/interrupt/exception state here so that
1063 * aborting() returns FALSE when closing a buffer. */
1064 enter_cleanup(&cs);
1065# endif
1066
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 /* User selected Recover at ATTENTION prompt. */
1068 msg_scroll = TRUE;
1069 ml_recover();
1070 MSG_PUTS("\n"); /* don't overwrite the last message */
1071 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001072 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001073
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001074# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001075 /* Restore the error/interrupt/exception state if not discarded by a
1076 * new aborting error, interrupt, or uncaught exception. */
1077 leave_cleanup(&cs);
1078# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 }
1080 swap_exists_action = SEA_NONE;
1081}
1082#endif
1083
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084/*
1085 * do_bufdel() - delete or unload buffer(s)
1086 *
1087 * addr_count == 0: ":bdel" - delete current buffer
1088 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1089 * buffer "end_bnr", then any other arguments.
1090 * addr_count == 2: ":N,N bdel" - delete buffers in range
1091 *
1092 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1093 * DOBUF_DEL (":bdel")
1094 *
1095 * Returns error message or NULL
1096 */
1097 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001098do_bufdel(
1099 int command,
1100 char_u *arg, /* pointer to extra arguments */
1101 int addr_count,
1102 int start_bnr, /* first buffer number in a range */
1103 int end_bnr, /* buffer nr or last buffer nr in a range */
1104 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105{
1106 int do_current = 0; /* delete current buffer? */
1107 int deleted = 0; /* number of buffers deleted */
1108 char_u *errormsg = NULL; /* return value */
1109 int bnr; /* buffer number */
1110 char_u *p;
1111
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 if (addr_count == 0)
1113 {
1114 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1115 }
1116 else
1117 {
1118 if (addr_count == 2)
1119 {
1120 if (*arg) /* both range and argument is not allowed */
1121 return (char_u *)_(e_trailing);
1122 bnr = start_bnr;
1123 }
1124 else /* addr_count == 1 */
1125 bnr = end_bnr;
1126
1127 for ( ;!got_int; ui_breakcheck())
1128 {
1129 /*
1130 * delete the current buffer last, otherwise when the
1131 * current buffer is deleted, the next buffer becomes
1132 * the current one and will be loaded, which may then
1133 * also be deleted, etc.
1134 */
1135 if (bnr == curbuf->b_fnum)
1136 do_current = bnr;
1137 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
1138 forceit) == OK)
1139 ++deleted;
1140
1141 /*
1142 * find next buffer number to delete/unload
1143 */
1144 if (addr_count == 2)
1145 {
1146 if (++bnr > end_bnr)
1147 break;
1148 }
1149 else /* addr_count == 1 */
1150 {
1151 arg = skipwhite(arg);
1152 if (*arg == NUL)
1153 break;
1154 if (!VIM_ISDIGIT(*arg))
1155 {
1156 p = skiptowhite_esc(arg);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01001157 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE,
1158 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 if (bnr < 0) /* failed */
1160 break;
1161 arg = p;
1162 }
1163 else
1164 bnr = getdigits(&arg);
1165 }
1166 }
1167 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1168 FORWARD, do_current, forceit) == OK)
1169 ++deleted;
1170
1171 if (deleted == 0)
1172 {
1173 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001174 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001176 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00001178 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 errormsg = IObuff;
1180 }
1181 else if (deleted >= p_report)
1182 {
1183 if (command == DOBUF_UNLOAD)
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001184 smsg((char_u *)NGETTEXT("%d buffer unloaded",
1185 "%d buffers unloaded", deleted), deleted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 else if (command == DOBUF_DEL)
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001187 smsg((char_u *)NGETTEXT("%d buffer deleted",
1188 "%d buffers deleted", deleted), deleted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 else
Bram Moolenaarda6e8912018-08-21 15:12:14 +02001190 smsg((char_u *)NGETTEXT("%d buffer wiped out",
1191 "%d buffers wiped out", deleted), deleted);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 }
1193 }
1194
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195
1196 return errormsg;
1197}
1198
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001199static int empty_curbuf(int close_others, int forceit, int action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001200
1201/*
1202 * Make the current buffer empty.
1203 * Used when it is wiped out and it's the last buffer.
1204 */
1205 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001206empty_curbuf(
1207 int close_others,
1208 int forceit,
1209 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001210{
1211 int retval;
1212 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001213 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001214
1215 if (action == DOBUF_UNLOAD)
1216 {
1217 EMSG(_("E90: Cannot unload last buffer"));
1218 return FAIL;
1219 }
1220
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001221 set_bufref(&bufref, buf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001222 if (close_others)
1223 /* Close any other windows on this buffer, then make it empty. */
Bram Moolenaara02471e2014-01-10 16:43:14 +01001224 close_windows(buf, TRUE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001225
1226 setpcmark();
1227 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1228 forceit ? ECMD_FORCEIT : 0, curwin);
1229
1230 /*
1231 * do_ecmd() may create a new buffer, then we have to delete
1232 * the old one. But do_ecmd() may have done that already, check
1233 * if the buffer still exists.
1234 */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001235 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001236 close_buffer(NULL, buf, action, FALSE);
1237 if (!close_others)
1238 need_fileinfo = FALSE;
1239 return retval;
1240}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241/*
1242 * Implementation of the commands for the buffer list.
1243 *
1244 * action == DOBUF_GOTO go to specified buffer
1245 * action == DOBUF_SPLIT split window and go to specified buffer
1246 * action == DOBUF_UNLOAD unload specified buffer(s)
1247 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1248 * action == DOBUF_WIPE delete specified buffer(s) really
1249 *
1250 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1251 * start == DOBUF_FIRST go to "count" buffer from first buffer
1252 * start == DOBUF_LAST go to "count" buffer from last buffer
1253 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1254 *
1255 * Return FAIL or OK.
1256 */
1257 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001258do_buffer(
1259 int action,
1260 int start,
1261 int dir, /* FORWARD or BACKWARD */
1262 int count, /* buffer number or number of buffers */
1263 int forceit) /* TRUE for :...! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264{
1265 buf_T *buf;
1266 buf_T *bp;
1267 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1268 || action == DOBUF_WIPE);
1269
1270 switch (start)
1271 {
1272 case DOBUF_FIRST: buf = firstbuf; break;
1273 case DOBUF_LAST: buf = lastbuf; break;
1274 default: buf = curbuf; break;
1275 }
1276 if (start == DOBUF_MOD) /* find next modified buffer */
1277 {
1278 while (count-- > 0)
1279 {
1280 do
1281 {
1282 buf = buf->b_next;
1283 if (buf == NULL)
1284 buf = firstbuf;
1285 }
1286 while (buf != curbuf && !bufIsChanged(buf));
1287 }
1288 if (!bufIsChanged(buf))
1289 {
1290 EMSG(_("E84: No modified buffer found"));
1291 return FAIL;
1292 }
1293 }
1294 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
1295 {
1296 while (buf != NULL && buf->b_fnum != count)
1297 buf = buf->b_next;
1298 }
1299 else
1300 {
1301 bp = NULL;
1302 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1303 {
1304 /* remember the buffer where we start, we come back there when all
1305 * buffers are unlisted. */
1306 if (bp == NULL)
1307 bp = buf;
1308 if (dir == FORWARD)
1309 {
1310 buf = buf->b_next;
1311 if (buf == NULL)
1312 buf = firstbuf;
1313 }
1314 else
1315 {
1316 buf = buf->b_prev;
1317 if (buf == NULL)
1318 buf = lastbuf;
1319 }
1320 /* don't count unlisted buffers */
1321 if (unload || buf->b_p_bl)
1322 {
1323 --count;
1324 bp = NULL; /* use this buffer as new starting point */
1325 }
1326 if (bp == buf)
1327 {
1328 /* back where we started, didn't find anything. */
1329 EMSG(_("E85: There is no listed buffer"));
1330 return FAIL;
1331 }
1332 }
1333 }
1334
1335 if (buf == NULL) /* could not find it */
1336 {
1337 if (start == DOBUF_FIRST)
1338 {
1339 /* don't warn when deleting */
1340 if (!unload)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01001341 EMSGN(_(e_nobufnr), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 }
1343 else if (dir == FORWARD)
1344 EMSG(_("E87: Cannot go beyond last buffer"));
1345 else
1346 EMSG(_("E88: Cannot go before first buffer"));
1347 return FAIL;
1348 }
1349
1350#ifdef FEAT_GUI
1351 need_mouse_correct = TRUE;
1352#endif
1353
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 /*
1355 * delete buffer buf from memory and/or the list
1356 */
1357 if (unload)
1358 {
1359 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001360 bufref_T bufref;
1361
Bram Moolenaara997b452018-04-17 23:24:06 +02001362 if (buf->b_locked)
1363 {
1364 EMSG(_(e_buflocked));
1365 return FAIL;
1366 }
1367
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001368 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369
1370 /* When unloading or deleting a buffer that's already unloaded and
1371 * unlisted: fail silently. */
1372 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1373 return FAIL;
1374
1375 if (!forceit && bufIsChanged(buf))
1376 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001377#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 if ((p_confirm || cmdmod.confirm) && p_write)
1379 {
1380 dialog_changed(buf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001381 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 /* Autocommand deleted buffer, oops! It's not changed
1383 * now. */
1384 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001385 /* If it's still changed fail silently, the dialog already
1386 * mentioned why it fails. */
1387 if (bufIsChanged(buf))
1388 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001390 else
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001391#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 {
1393 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1394 buf->b_fnum);
1395 return FAIL;
1396 }
1397 }
1398
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001399 /* When closing the current buffer stop Visual mode. */
Bram Moolenaar4930a762016-09-11 14:39:53 +02001400 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001401 end_visual_mode();
1402
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 /*
1404 * If deleting the last (listed) buffer, make it empty.
1405 * The last (listed) buffer cannot be unloaded.
1406 */
Bram Moolenaar29323592016-07-24 22:04:11 +02001407 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 if (bp->b_p_bl && bp != buf)
1409 break;
1410 if (bp == NULL && buf == curbuf)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001411 return empty_curbuf(TRUE, forceit, action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 /*
1414 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001415 * (unless it's the only window). Repeat this so long as we end up in
1416 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001418 while (buf == curbuf
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001419 && !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
Bram Moolenaar459ca562016-11-10 18:16:33 +01001420 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001421 {
1422 if (win_close(curwin, FALSE) == FAIL)
1423 break;
1424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425
1426 /*
1427 * If the buffer to be deleted is not the current one, delete it here.
1428 */
1429 if (buf != curbuf)
1430 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001431 close_windows(buf, FALSE);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001432 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows <= 0)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001433 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 return OK;
1435 }
1436
1437 /*
1438 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001439 * There should be another, otherwise it would have been handled
1440 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001441 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 * Then prefer the buffer we most recently visited.
1443 * Else try to find one that is loaded, after the current buffer,
1444 * then before the current buffer.
1445 * Finally use any buffer.
1446 */
1447 buf = NULL; /* selected buffer */
1448 bp = NULL; /* used when no loaded buffer found */
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001449 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1450 buf = au_new_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451#ifdef FEAT_JUMPLIST
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001452 else if (curwin->w_jumplistlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 {
1454 int jumpidx;
1455
1456 jumpidx = curwin->w_jumplistidx - 1;
1457 if (jumpidx < 0)
1458 jumpidx = curwin->w_jumplistlen - 1;
1459
1460 forward = jumpidx;
1461 while (jumpidx != curwin->w_jumplistidx)
1462 {
1463 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1464 if (buf != NULL)
1465 {
1466 if (buf == curbuf || !buf->b_p_bl)
1467 buf = NULL; /* skip current and unlisted bufs */
1468 else if (buf->b_ml.ml_mfp == NULL)
1469 {
1470 /* skip unloaded buf, but may keep it for later */
1471 if (bp == NULL)
1472 bp = buf;
1473 buf = NULL;
1474 }
1475 }
1476 if (buf != NULL) /* found a valid buffer: stop searching */
1477 break;
1478 /* advance to older entry in jump list */
1479 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1480 break;
1481 if (--jumpidx < 0)
1482 jumpidx = curwin->w_jumplistlen - 1;
1483 if (jumpidx == forward) /* List exhausted for sure */
1484 break;
1485 }
1486 }
1487#endif
1488
1489 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1490 {
1491 forward = TRUE;
1492 buf = curbuf->b_next;
1493 for (;;)
1494 {
1495 if (buf == NULL)
1496 {
1497 if (!forward) /* tried both directions */
1498 break;
1499 buf = curbuf->b_prev;
1500 forward = FALSE;
1501 continue;
1502 }
1503 /* in non-help buffer, try to skip help buffers, and vv */
1504 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1505 {
1506 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1507 break;
1508 if (bp == NULL) /* remember unloaded buf for later */
1509 bp = buf;
1510 }
1511 if (forward)
1512 buf = buf->b_next;
1513 else
1514 buf = buf->b_prev;
1515 }
1516 }
1517 if (buf == NULL) /* No loaded buffer, use unloaded one */
1518 buf = bp;
1519 if (buf == NULL) /* No loaded buffer, find listed one */
1520 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001521 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 if (buf->b_p_bl && buf != curbuf)
1523 break;
1524 }
1525 if (buf == NULL) /* Still no buffer, just take one */
1526 {
1527 if (curbuf->b_next != NULL)
1528 buf = curbuf->b_next;
1529 else
1530 buf = curbuf->b_prev;
1531 }
1532 }
1533
Bram Moolenaara02471e2014-01-10 16:43:14 +01001534 if (buf == NULL)
1535 {
1536 /* Autocommands must have wiped out all other buffers. Only option
1537 * now is to make the current buffer empty. */
1538 return empty_curbuf(FALSE, forceit, action);
1539 }
1540
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 /*
1542 * make buf current buffer
1543 */
1544 if (action == DOBUF_SPLIT) /* split window first */
1545 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001546 /* If 'switchbuf' contains "useopen": jump to first window containing
1547 * "buf" if one exists */
1548 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001549 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001550 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001551 * page containing "buf" if one exists */
1552 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 return OK;
1554 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 return FAIL;
1556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557
1558 /* go to current buffer - nothing to do */
1559 if (buf == curbuf)
1560 return OK;
1561
1562 /*
1563 * Check if the current buffer may be abandoned.
1564 */
1565 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1566 {
1567#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1568 if ((p_confirm || cmdmod.confirm) && p_write)
1569 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001570 bufref_T bufref;
1571
1572 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 dialog_changed(curbuf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001574 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 /* Autocommand deleted buffer, oops! */
1576 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 }
1578 if (bufIsChanged(curbuf))
1579#endif
1580 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001581 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 return FAIL;
1583 }
1584 }
1585
1586 /* Go to the other buffer. */
1587 set_curbuf(buf, action);
1588
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001590 {
1591 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
1592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001594#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 if (aborting()) /* autocmds may abort script processing */
1596 return FAIL;
1597#endif
1598
1599 return OK;
1600}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601
1602/*
1603 * Set current buffer to "buf". Executes autocommands and closes current
1604 * buffer. "action" tells how to close the current buffer:
1605 * DOBUF_GOTO free or hide it
1606 * DOBUF_SPLIT nothing
1607 * DOBUF_UNLOAD unload it
1608 * DOBUF_DEL delete it
1609 * DOBUF_WIPE wipe it out
1610 */
1611 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001612set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613{
1614 buf_T *prevbuf;
1615 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1616 || action == DOBUF_WIPE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001617#ifdef FEAT_SYN_HL
1618 long old_tw = curbuf->b_p_tw;
1619#endif
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001620 bufref_T newbufref;
1621 bufref_T prevbufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622
1623 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001624 if (!cmdmod.keepalt)
1625 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001626 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 /* Don't restart Select mode after switching to another buffer. */
1629 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001631 /* close_windows() or apply_autocmds() may change curbuf and wipe out "buf"
1632 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 prevbuf = curbuf;
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001634 set_bufref(&prevbufref, prevbuf);
1635 set_bufref(&newbufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001637 /* Autocommands may delete the curren buffer and/or the buffer we wan to go
1638 * to. In those cases don't close the buffer. */
Bram Moolenaar82404332016-07-10 17:00:38 +02001639 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001640 || (bufref_valid(&prevbufref)
1641 && bufref_valid(&newbufref)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001642#ifdef FEAT_EVAL
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001643 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001645 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001647#ifdef FEAT_SYN_HL
1648 if (prevbuf == curwin->w_buffer)
1649 reset_synblock(curwin);
1650#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001652 close_windows(prevbuf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001653#if defined(FEAT_EVAL)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001654 if (bufref_valid(&prevbufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655#else
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001656 if (bufref_valid(&prevbufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001658 {
Bram Moolenaare25865a2012-07-06 16:22:02 +02001659 win_T *previouswin = curwin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001660 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001661 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1663 unload ? action : (action == DOBUF_GOTO
Bram Moolenaareb44a682017-08-03 22:44:55 +02001664 && !buf_hide(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001665 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaare25865a2012-07-06 16:22:02 +02001666 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaar9e931222012-06-20 11:55:01 +02001667 /* autocommands changed curwin, Grr! */
Bram Moolenaare25865a2012-07-06 16:22:02 +02001668 curwin = previouswin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 }
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001671 /* An autocommand may have deleted "buf", already entered it (e.g., when
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001672 * it did ":bunload") or aborted the script processing.
Bram Moolenaar9e931222012-06-20 11:55:01 +02001673 * If curwin->w_buffer is null, enter_buffer() will make it valid again */
1674 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001675#ifdef FEAT_EVAL
Bram Moolenaar4033c552017-09-16 20:54:51 +02001676 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001678 ) || curwin->w_buffer == NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001679 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001681#ifdef FEAT_SYN_HL
1682 if (old_tw != curbuf->b_p_tw)
1683 check_colorcolumn(curwin);
1684#endif
1685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686}
1687
1688/*
1689 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001690 * Old curbuf must have been abandoned already! This also means "curbuf" may
1691 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 */
1693 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001694enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695{
1696 /* Copy buffer and window local option values. Not for a help buffer. */
1697 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1698 if (!buf->b_help)
1699 get_winopts(buf);
1700#ifdef FEAT_FOLDING
1701 else
1702 /* Remove all folds in the window. */
1703 clearFolding(curwin);
1704 foldUpdateAll(curwin); /* update folds (later). */
1705#endif
1706
1707 /* Get the buffer in the current window. */
1708 curwin->w_buffer = buf;
1709 curbuf = buf;
1710 ++curbuf->b_nwindows;
1711
1712#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001713 if (curwin->w_p_diff)
1714 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715#endif
1716
Bram Moolenaara971b822011-09-14 14:43:25 +02001717#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001718 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001719#endif
1720
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 /* Cursor on first line by default. */
1722 curwin->w_cursor.lnum = 1;
1723 curwin->w_cursor.col = 0;
1724#ifdef FEAT_VIRTUALEDIT
1725 curwin->w_cursor.coladd = 0;
1726#endif
1727 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001728 curwin->w_topline_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001730 /* mark cursor position as being invalid */
1731 curwin->w_valid = 0;
1732
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 /* Make sure the buffer is loaded. */
1734 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001735 {
1736 /* If there is no filetype, allow for detecting one. Esp. useful for
1737 * ":ball" used in a autocommand. If there already is a filetype we
1738 * might prefer to keep it. */
1739 if (*curbuf->b_p_ft == NUL)
1740 did_filetype = FALSE;
1741
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001742 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 else
1745 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001746 if (!msg_silent)
1747 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001750#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1754 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 }
1756
1757 /* If autocommands did not change the cursor position, restore cursor lnum
1758 * and possibly cursor col. */
1759 if (curwin->w_cursor.lnum == 1 && inindent(0))
1760 buflist_getfpos();
1761
1762 check_arg_idx(curwin); /* check for valid arg_idx */
1763#ifdef FEAT_TITLE
1764 maketitle();
1765#endif
Bram Moolenaard4153d42008-11-15 15:06:17 +00001766 /* when autocmds didn't change it */
1767 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1769
Bram Moolenaar009b2592004-10-24 19:18:58 +00001770#ifdef FEAT_NETBEANS_INTG
1771 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001772 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001773#endif
1774
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001775 /* Change directories when the 'acd' option is set. */
Bram Moolenaar6f470022018-04-10 18:47:20 +02001776 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777
1778#ifdef FEAT_KEYMAP
1779 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001780 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001782#ifdef FEAT_SPELL
1783 /* May need to set the spell language. Can only do this after the buffer
1784 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001785 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1786 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001787#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001788#ifdef FEAT_VIMINFO
1789 curbuf->b_last_used = vim_time();
1790#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001791
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 redraw_later(NOT_VALID);
1793}
1794
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001795#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1796/*
1797 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001798 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001799 */
1800 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001801do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001802{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001803 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001804 && curbuf->b_ffname != NULL
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001805 && vim_chdirfile(curbuf->b_ffname, "auto") == OK)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001806 shorten_fnames(TRUE);
1807}
1808#endif
1809
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001810 void
1811no_write_message(void)
1812{
1813#ifdef FEAT_TERMINAL
1814 if (term_job_running(curbuf->b_term))
1815 EMSG(_("E948: Job still running (add ! to end the job)"));
1816 else
1817#endif
1818 EMSG(_("E37: No write since last change (add ! to override)"));
1819}
1820
1821 void
Bram Moolenaar7a760922018-02-19 23:10:02 +01001822no_write_message_nobang(buf_T *buf UNUSED)
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001823{
1824#ifdef FEAT_TERMINAL
Bram Moolenaar7a760922018-02-19 23:10:02 +01001825 if (term_job_running(buf->b_term))
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001826 EMSG(_("E948: Job still running"));
1827 else
1828#endif
1829 EMSG(_("E37: No write since last change"));
1830}
1831
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832/*
1833 * functions for dealing with the buffer list
1834 */
1835
Bram Moolenaar480778b2016-07-14 22:09:39 +02001836static int top_file_num = 1; /* highest file number */
1837
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838/*
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001839 * Return TRUE if the current buffer is empty, unnamed, unmodified and used in
1840 * only one window. That means it can be re-used.
1841 */
1842 int
1843curbuf_reusable(void)
1844{
1845 return (curbuf != NULL
1846 && curbuf->b_ffname == NULL
1847 && curbuf->b_nwindows <= 1
1848 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY())
1849 && !curbufIsChanged());
1850}
1851
1852/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 * Add a file name to the buffer list. Return a pointer to the buffer.
1854 * If the same file name already exists return a pointer to that buffer.
1855 * If it does not exist, or if fname == NULL, a new entry is created.
1856 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1857 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1858 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001859 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001860 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
1861 * if the buffer already exists.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 * This is the ONLY way to create a new buffer.
1863 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001865buflist_new(
1866 char_u *ffname, /* full path of fname or relative */
1867 char_u *sfname, /* short fname or NULL */
1868 linenr_T lnum, /* preferred cursor line */
1869 int flags) /* BLN_ defines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870{
1871 buf_T *buf;
1872#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001873 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874#endif
1875
Bram Moolenaar480778b2016-07-14 22:09:39 +02001876 if (top_file_num == 1)
1877 hash_init(&buf_hashtab);
1878
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1880
1881 /*
1882 * If file name already exists in the list, update the entry.
1883 */
1884#ifdef UNIX
1885 /* On Unix we can use inode numbers when the file exists. Works better
1886 * for hard links. */
1887 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1888 st.st_dev = (dev_T)-1;
1889#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001890 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891#ifdef UNIX
1892 buflist_findname_stat(ffname, &st)
1893#else
1894 buflist_findname(ffname)
1895#endif
1896 ) != NULL)
1897 {
1898 vim_free(ffname);
1899 if (lnum != 0)
1900 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02001901
1902 if ((flags & BLN_NOOPT) == 0)
1903 /* copy the options now, if 'cpo' doesn't have 's' and not done
1904 * already */
1905 buf_copy_options(buf, 0);
1906
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1908 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001909 bufref_T bufref;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001910
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 buf->b_p_bl = TRUE;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001912 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001914 {
Bram Moolenaar82404332016-07-10 17:00:38 +02001915 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001916 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001917 return NULL;
1918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 }
1920 return buf;
1921 }
1922
1923 /*
1924 * If the current buffer has no name and no contents, use the current
1925 * buffer. Otherwise: Need to allocate a new buffer structure.
1926 *
1927 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001928 * (A spell file buffer is allocated in spell.c, but that's not a normal
1929 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 */
1931 buf = NULL;
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001932 if ((flags & BLN_CURBUF) && curbuf_reusable())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 {
1934 buf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 /* It's like this buffer is deleted. Watch out for autocommands that
1936 * change curbuf! If that happens, allocate a new buffer anyway. */
1937 if (curbuf->b_p_bl)
1938 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1939 if (buf == curbuf)
1940 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001941#ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001942 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 if (buf == curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 {
1947 /* Make sure 'bufhidden' and 'buftype' are empty */
1948 clear_string_option(&buf->b_p_bh);
1949 clear_string_option(&buf->b_p_bt);
1950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 }
1952 if (buf != curbuf || curbuf == NULL)
1953 {
1954 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1955 if (buf == NULL)
1956 {
1957 vim_free(ffname);
1958 return NULL;
1959 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02001960#ifdef FEAT_EVAL
1961 /* init b: variables */
1962 buf->b_vars = dict_alloc();
1963 if (buf->b_vars == NULL)
1964 {
1965 vim_free(ffname);
1966 vim_free(buf);
1967 return NULL;
1968 }
1969 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
1970#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01001971 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 }
1973
1974 if (ffname != NULL)
1975 {
1976 buf->b_ffname = ffname;
1977 buf->b_sfname = vim_strsave(sfname);
1978 }
1979
1980 clear_wininfo(buf);
1981 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1982
1983 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1984 || buf->b_wininfo == NULL)
1985 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001986 VIM_CLEAR(buf->b_ffname);
1987 VIM_CLEAR(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 if (buf != curbuf)
1989 free_buffer(buf);
1990 return NULL;
1991 }
1992
1993 if (buf == curbuf)
1994 {
1995 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001996 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 if (buf != curbuf) /* autocommands deleted the buffer! */
1998 return NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001999#if defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002000 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 return NULL;
2002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002004
2005 /* Init the options. */
2006 buf->b_p_initialized = FALSE;
2007 buf_copy_options(buf, BCO_ENTER);
2008
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009#ifdef FEAT_KEYMAP
2010 /* need to reload lmaps and set b:keymap_name */
2011 curbuf->b_kmap_state |= KEYMAP_INIT;
2012#endif
2013 }
2014 else
2015 {
2016 /*
2017 * put new buffer at the end of the buffer list
2018 */
2019 buf->b_next = NULL;
2020 if (firstbuf == NULL) /* buffer list is empty */
2021 {
2022 buf->b_prev = NULL;
2023 firstbuf = buf;
2024 }
2025 else /* append new buffer at end of list */
2026 {
2027 lastbuf->b_next = buf;
2028 buf->b_prev = lastbuf;
2029 }
2030 lastbuf = buf;
2031
2032 buf->b_fnum = top_file_num++;
2033 if (top_file_num < 0) /* wrap around (may cause duplicates) */
2034 {
2035 EMSG(_("W14: Warning: List of file names overflow"));
2036 if (emsg_silent == 0)
2037 {
2038 out_flush();
2039 ui_delay(3000L, TRUE); /* make sure it is noticed */
2040 }
2041 top_file_num = 1;
2042 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002043 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044
2045 /*
2046 * Always copy the options from the current buffer.
2047 */
2048 buf_copy_options(buf, BCO_ALWAYS);
2049 }
2050
2051 buf->b_wininfo->wi_fpos.lnum = lnum;
2052 buf->b_wininfo->wi_win = curwin;
2053
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002054#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002055 hash_init(&buf->b_s.b_keywtab);
2056 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057#endif
2058
2059 buf->b_fname = buf->b_sfname;
2060#ifdef UNIX
2061 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002062 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 else
2064 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002065 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 buf->b_dev = st.st_dev;
2067 buf->b_ino = st.st_ino;
2068 }
2069#endif
2070 buf->b_u_synced = TRUE;
2071 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002072 if (flags & BLN_DUMMY)
2073 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 buf_clear_file(buf);
2075 clrallmarks(buf); /* clear marks */
2076 fmarks_check_names(buf); /* check file marks for this file */
2077 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 if (!(flags & BLN_DUMMY))
2079 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002080 bufref_T bufref;
2081
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01002082 /* Tricky: these autocommands may change the buffer list. They could
2083 * also split the window with re-using the one empty buffer. This may
2084 * result in unexpectedly losing the empty buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002085 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002086 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002087 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002088 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002090 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002091 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002092 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002093 return NULL;
2094 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002095#ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002096 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100
2101 return buf;
2102}
2103
2104/*
2105 * Free the memory for the options of a buffer.
2106 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2107 * 'fileencoding'.
2108 */
2109 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002110free_buf_options(
2111 buf_T *buf,
2112 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113{
2114 if (free_p_ff)
2115 {
2116#ifdef FEAT_MBYTE
2117 clear_string_option(&buf->b_p_fenc);
2118#endif
2119 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 clear_string_option(&buf->b_p_bh);
2121 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 }
2123#ifdef FEAT_FIND_ID
2124 clear_string_option(&buf->b_p_def);
2125 clear_string_option(&buf->b_p_inc);
2126# ifdef FEAT_EVAL
2127 clear_string_option(&buf->b_p_inex);
2128# endif
2129#endif
2130#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
2131 clear_string_option(&buf->b_p_inde);
2132 clear_string_option(&buf->b_p_indk);
2133#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002134#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2135 clear_string_option(&buf->b_p_bexpr);
2136#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002137#if defined(FEAT_CRYPT)
2138 clear_string_option(&buf->b_p_cm);
2139#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002140 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002141#if defined(FEAT_EVAL)
2142 clear_string_option(&buf->b_p_fex);
2143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144#ifdef FEAT_CRYPT
2145 clear_string_option(&buf->b_p_key);
2146#endif
2147 clear_string_option(&buf->b_p_kp);
2148 clear_string_option(&buf->b_p_mps);
2149 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002150 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 clear_string_option(&buf->b_p_isk);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002152#ifdef FEAT_VARTABS
2153 clear_string_option(&buf->b_p_vsts);
2154 if (buf->b_p_vsts_nopaste)
2155 vim_free(buf->b_p_vsts_nopaste);
2156 buf->b_p_vsts_nopaste = NULL;
2157 if (buf->b_p_vsts_array)
2158 vim_free(buf->b_p_vsts_array);
2159 buf->b_p_vsts_array = NULL;
2160 clear_string_option(&buf->b_p_vts);
2161 if (buf->b_p_vts_array)
2162 vim_free(buf->b_p_vts_array);
2163 buf->b_p_vts_array = NULL;
2164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165#ifdef FEAT_KEYMAP
2166 clear_string_option(&buf->b_p_keymap);
Bram Moolenaar50138322018-01-28 17:05:16 +01002167 keymap_clear(&buf->b_kmap_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 ga_clear(&buf->b_kmap_ga);
2169#endif
2170#ifdef FEAT_COMMENTS
2171 clear_string_option(&buf->b_p_com);
2172#endif
2173#ifdef FEAT_FOLDING
2174 clear_string_option(&buf->b_p_cms);
2175#endif
2176 clear_string_option(&buf->b_p_nf);
2177#ifdef FEAT_SYN_HL
2178 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002179 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002180#endif
2181#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002182 clear_string_option(&buf->b_s.b_p_spc);
2183 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002184 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002185 buf->b_s.b_cap_prog = NULL;
2186 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187#endif
2188#ifdef FEAT_SEARCHPATH
2189 clear_string_option(&buf->b_p_sua);
2190#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 clear_string_option(&buf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192#ifdef FEAT_CINDENT
2193 clear_string_option(&buf->b_p_cink);
2194 clear_string_option(&buf->b_p_cino);
2195#endif
2196#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
2197 clear_string_option(&buf->b_p_cinw);
2198#endif
2199#ifdef FEAT_INS_EXPAND
2200 clear_string_option(&buf->b_p_cpt);
2201#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002202#ifdef FEAT_COMPL_FUNC
2203 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002204 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206#ifdef FEAT_QUICKFIX
2207 clear_string_option(&buf->b_p_gp);
2208 clear_string_option(&buf->b_p_mp);
2209 clear_string_option(&buf->b_p_efm);
2210#endif
2211 clear_string_option(&buf->b_p_ep);
2212 clear_string_option(&buf->b_p_path);
2213 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002214 clear_string_option(&buf->b_p_tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215#ifdef FEAT_INS_EXPAND
2216 clear_string_option(&buf->b_p_dict);
2217 clear_string_option(&buf->b_p_tsr);
2218#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002219#ifdef FEAT_TEXTOBJ
2220 clear_string_option(&buf->b_p_qe);
2221#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002223 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002224#ifdef FEAT_LISP
2225 clear_string_option(&buf->b_p_lw);
2226#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002227 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002228#ifdef FEAT_MBYTE
2229 clear_string_option(&buf->b_p_menc);
2230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231}
2232
2233/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002234 * Get alternate file "n".
2235 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2236 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 * if (options & GETF_SETMARK) call setpcmark()
2238 * if (options & GETF_ALT) we are jumping to an alternate file.
2239 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2240 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002241 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 */
2243 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002244buflist_getfile(
2245 int n,
2246 linenr_T lnum,
2247 int options,
2248 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249{
2250 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 pos_T *fpos;
2253 colnr_T col;
2254
2255 buf = buflist_findnr(n);
2256 if (buf == NULL)
2257 {
2258 if ((options & GETF_ALT) && n == 0)
2259 EMSG(_(e_noalt));
2260 else
2261 EMSGN(_("E92: Buffer %ld not found"), n);
2262 return FAIL;
2263 }
2264
2265 /* if alternate file is the current buffer, nothing to do */
2266 if (buf == curbuf)
2267 return OK;
2268
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002269 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002270 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002271 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002273 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002274 if (curbuf_locked())
2275 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276
2277 /* altfpos may be changed by getfile(), get it now */
2278 if (lnum == 0)
2279 {
2280 fpos = buflist_findfpos(buf);
2281 lnum = fpos->lnum;
2282 col = fpos->col;
2283 }
2284 else
2285 col = 0;
2286
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 if (options & GETF_SWITCH)
2288 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002289 /* If 'switchbuf' contains "useopen": jump to first window containing
2290 * "buf" if one exists */
2291 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002293
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002294 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00002295 * page containing "buf" if one exists */
2296 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002297 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002298
2299 /* If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2300 * current buffer isn't empty: open new tab or window */
2301 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002302 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002304 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002305 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002306 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2307 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002309 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 }
2311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312
2313 ++RedrawingDisabled;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002314 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2315 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 {
2317 --RedrawingDisabled;
2318
2319 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
2320 if (!p_sol && col != 0)
2321 {
2322 curwin->w_cursor.col = col;
2323 check_cursor_col();
2324#ifdef FEAT_VIRTUALEDIT
2325 curwin->w_cursor.coladd = 0;
2326#endif
2327 curwin->w_set_curswant = TRUE;
2328 }
2329 return OK;
2330 }
2331 --RedrawingDisabled;
2332 return FAIL;
2333}
2334
2335/*
2336 * go to the last know line number for the current buffer
2337 */
2338 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002339buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340{
2341 pos_T *fpos;
2342
2343 fpos = buflist_findfpos(curbuf);
2344
2345 curwin->w_cursor.lnum = fpos->lnum;
2346 check_cursor_lnum();
2347
2348 if (p_sol)
2349 curwin->w_cursor.col = 0;
2350 else
2351 {
2352 curwin->w_cursor.col = fpos->col;
2353 check_cursor_col();
2354#ifdef FEAT_VIRTUALEDIT
2355 curwin->w_cursor.coladd = 0;
2356#endif
2357 curwin->w_set_curswant = TRUE;
2358 }
2359}
2360
Bram Moolenaar81695252004-12-29 20:58:21 +00002361#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2362/*
2363 * Find file in buffer list by name (it has to be for the current window).
2364 * Returns NULL if not found.
2365 */
2366 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002367buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002368{
2369 char_u *ffname;
2370 buf_T *buf = NULL;
2371
2372 /* First make the name into a full path name */
2373 ffname = FullName_save(fname,
2374#ifdef UNIX
2375 TRUE /* force expansion, get rid of symbolic links */
2376#else
2377 FALSE
2378#endif
2379 );
2380 if (ffname != NULL)
2381 {
2382 buf = buflist_findname(ffname);
2383 vim_free(ffname);
2384 }
2385 return buf;
2386}
2387#endif
2388
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389/*
2390 * Find file in buffer list by name (it has to be for the current window).
2391 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002392 * Skips dummy buffers.
2393 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 */
2395 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002396buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397{
2398#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002399 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400
2401 if (mch_stat((char *)ffname, &st) < 0)
2402 st.st_dev = (dev_T)-1;
2403 return buflist_findname_stat(ffname, &st);
2404}
2405
2406/*
2407 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2408 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002409 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 */
2411 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002412buflist_findname_stat(
2413 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002414 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415{
2416#endif
2417 buf_T *buf;
2418
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002419 /* Start at the last buffer, expect to find a match sooner. */
2420 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar81695252004-12-29 20:58:21 +00002421 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422#ifdef UNIX
2423 , stp
2424#endif
2425 ))
2426 return buf;
2427 return NULL;
2428}
2429
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430/*
2431 * Find file in buffer list by a regexp pattern.
2432 * Return fnum of the found buffer.
2433 * Return < 0 for error.
2434 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002436buflist_findpat(
2437 char_u *pattern,
2438 char_u *pattern_end, /* pointer to first char after pattern */
2439 int unlisted, /* find unlisted buffers */
2440 int diffmode UNUSED, /* find diff-mode buffers only */
2441 int curtab_only) /* find buffers in current tab only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442{
2443 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 int match = -1;
2445 int find_listed;
2446 char_u *pat;
2447 char_u *patend;
2448 int attempt;
2449 char_u *p;
2450 int toggledollar;
2451
2452 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2453 {
2454 if (*pattern == '%')
2455 match = curbuf->b_fnum;
2456 else
2457 match = curwin->w_alt_fnum;
2458#ifdef FEAT_DIFF
2459 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2460 match = -1;
2461#endif
2462 }
2463
2464 /*
2465 * Try four ways of matching a listed buffer:
2466 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002467 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 * attempt == 2: with '$' at end (only match at end)
2469 * attempt == 3: with '^' at start and '$' at end (only full match)
2470 * Repeat this for finding an unlisted buffer if there was no matching
2471 * listed buffer.
2472 */
2473 else
2474 {
2475 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2476 if (pat == NULL)
2477 return -1;
2478 patend = pat + STRLEN(pat) - 1;
2479 toggledollar = (patend > pat && *patend == '$');
2480
2481 /* First try finding a listed buffer. If not found and "unlisted"
2482 * is TRUE, try finding an unlisted buffer. */
2483 find_listed = TRUE;
2484 for (;;)
2485 {
2486 for (attempt = 0; attempt <= 3; ++attempt)
2487 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002488 regmatch_T regmatch;
2489
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 /* may add '^' and '$' */
2491 if (toggledollar)
2492 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2493 p = pat;
2494 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2495 ++p;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002496 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2497 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 {
2499 vim_free(pat);
2500 return -1;
2501 }
2502
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002503 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 if (buf->b_p_bl == find_listed
2505#ifdef FEAT_DIFF
2506 && (!diffmode || diff_mode_buf(buf))
2507#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002508 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002510 if (curtab_only)
2511 {
2512 /* Ignore the match if the buffer is not open in
2513 * the current tab. */
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002514 win_T *wp;
2515
Bram Moolenaar29323592016-07-24 22:04:11 +02002516 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002517 if (wp->w_buffer == buf)
2518 break;
2519 if (wp == NULL)
2520 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 if (match >= 0) /* already found a match */
2523 {
2524 match = -2;
2525 break;
2526 }
2527 match = buf->b_fnum; /* remember first match */
2528 }
2529
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002530 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 if (match >= 0) /* found one match */
2532 break;
2533 }
2534
2535 /* Only search for unlisted buffers if there was no match with
2536 * a listed buffer. */
2537 if (!unlisted || !find_listed || match != -1)
2538 break;
2539 find_listed = FALSE;
2540 }
2541
2542 vim_free(pat);
2543 }
2544
2545 if (match == -2)
2546 EMSG2(_("E93: More than one match for %s"), pattern);
2547 else if (match < 0)
2548 EMSG2(_("E94: No matching buffer for %s"), pattern);
2549 return match;
2550}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551
2552#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2553
2554/*
2555 * Find all buffer names that match.
2556 * For command line expansion of ":buf" and ":sbuf".
2557 * Return OK if matches found, FAIL otherwise.
2558 */
2559 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002560ExpandBufnames(
2561 char_u *pat,
2562 int *num_file,
2563 char_u ***file,
2564 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565{
2566 int count = 0;
2567 buf_T *buf;
2568 int round;
2569 char_u *p;
2570 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002571 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572
2573 *num_file = 0; /* return values in case of FAIL */
2574 *file = NULL;
2575
Bram Moolenaar05159a02005-02-26 23:04:13 +00002576 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2577 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002579 patc = alloc((unsigned)STRLEN(pat) + 11);
2580 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002582 STRCPY(patc, "\\(^\\|[\\/]\\)");
2583 STRCPY(patc + 11, pat + 1);
2584 }
2585 else
2586 patc = pat;
2587
2588 /*
2589 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002590 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002591 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002592 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002593 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002594 regmatch_T regmatch;
2595
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002596 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002597 break; /* there was no anchor, no need to try again */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002598 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2599 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002600 {
2601 if (patc != pat)
2602 vim_free(patc);
2603 return FAIL;
2604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605
2606 /*
2607 * round == 1: Count the matches.
2608 * round == 2: Build the array to keep the matches.
2609 */
2610 for (round = 1; round <= 2; ++round)
2611 {
2612 count = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002613 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 {
2615 if (!buf->b_p_bl) /* skip unlisted buffers */
2616 continue;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002617 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 if (p != NULL)
2619 {
2620 if (round == 1)
2621 ++count;
2622 else
2623 {
2624 if (options & WILD_HOME_REPLACE)
2625 p = home_replace_save(buf, p);
2626 else
2627 p = vim_strsave(p);
2628 (*file)[count++] = p;
2629 }
2630 }
2631 }
2632 if (count == 0) /* no match found, break here */
2633 break;
2634 if (round == 1)
2635 {
2636 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2637 if (*file == NULL)
2638 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002639 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002640 if (patc != pat)
2641 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 return FAIL;
2643 }
2644 }
2645 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002646 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 if (count) /* match(es) found, break here */
2648 break;
2649 }
2650
Bram Moolenaar05159a02005-02-26 23:04:13 +00002651 if (patc != pat)
2652 vim_free(patc);
2653
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 *num_file = count;
2655 return (count == 0 ? FAIL : OK);
2656}
2657
2658#endif /* FEAT_CMDL_COMPL */
2659
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660/*
2661 * Check for a match on the file name for buffer "buf" with regprog "prog".
2662 */
2663 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002664buflist_match(
2665 regmatch_T *rmp,
2666 buf_T *buf,
2667 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668{
2669 char_u *match;
2670
2671 /* First try the short file name, then the long file name. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002672 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002674 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675
2676 return match;
2677}
2678
2679/*
2680 * Try matching the regexp in "prog" with file name "name".
2681 * Return "name" when there is a match, NULL when not.
2682 */
2683 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002684fname_match(
2685 regmatch_T *rmp,
2686 char_u *name,
2687 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688{
2689 char_u *match = NULL;
2690 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691
2692 if (name != NULL)
2693 {
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002694 /* Ignore case when 'fileignorecase' or the argument is set. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002695 rmp->rm_ic = p_fic || ignore_case;
2696 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 match = name;
2698 else
2699 {
2700 /* Replace $(HOME) with '~' and try matching again. */
2701 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002702 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 match = name;
2704 vim_free(p);
2705 }
2706 }
2707
2708 return match;
2709}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710
2711/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02002712 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 */
2714 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002715buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716{
Bram Moolenaar480778b2016-07-14 22:09:39 +02002717 char_u key[VIM_SIZEOF_INT * 2 + 1];
2718 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719
2720 if (nr == 0)
2721 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02002722 sprintf((char *)key, "%x", nr);
2723 hi = hash_find(&buf_hashtab, key);
2724
2725 if (!HASHITEM_EMPTY(hi))
2726 return (buf_T *)(hi->hi_key
2727 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 return NULL;
2729}
2730
2731/*
2732 * Get name of file 'n' in the buffer list.
2733 * When the file has no name an empty string is returned.
2734 * home_replace() is used to shorten the file name (used for marks).
2735 * Returns a pointer to allocated memory, of NULL when failed.
2736 */
2737 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002738buflist_nr2name(
2739 int n,
2740 int fullname,
2741 int helptail) /* for help buffers return tail only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742{
2743 buf_T *buf;
2744
2745 buf = buflist_findnr(n);
2746 if (buf == NULL)
2747 return NULL;
2748 return home_replace_save(helptail ? buf : NULL,
2749 fullname ? buf->b_ffname : buf->b_fname);
2750}
2751
2752/*
2753 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2754 * When "copy_options" is TRUE save the local window option values.
2755 * When "lnum" is 0 only do the options.
2756 */
2757 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002758buflist_setfpos(
2759 buf_T *buf,
2760 win_T *win,
2761 linenr_T lnum,
2762 colnr_T col,
2763 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764{
2765 wininfo_T *wip;
2766
2767 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2768 if (wip->wi_win == win)
2769 break;
2770 if (wip == NULL)
2771 {
2772 /* allocate a new entry */
2773 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2774 if (wip == NULL)
2775 return;
2776 wip->wi_win = win;
2777 if (lnum == 0) /* set lnum even when it's 0 */
2778 lnum = 1;
2779 }
2780 else
2781 {
2782 /* remove the entry from the list */
2783 if (wip->wi_prev)
2784 wip->wi_prev->wi_next = wip->wi_next;
2785 else
2786 buf->b_wininfo = wip->wi_next;
2787 if (wip->wi_next)
2788 wip->wi_next->wi_prev = wip->wi_prev;
2789 if (copy_options && wip->wi_optset)
2790 {
2791 clear_winopt(&wip->wi_opt);
2792#ifdef FEAT_FOLDING
2793 deleteFoldRecurse(&wip->wi_folds);
2794#endif
2795 }
2796 }
2797 if (lnum != 0)
2798 {
2799 wip->wi_fpos.lnum = lnum;
2800 wip->wi_fpos.col = col;
2801 }
2802 if (copy_options)
2803 {
2804 /* Save the window-specific option values. */
2805 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2806#ifdef FEAT_FOLDING
2807 wip->wi_fold_manual = win->w_fold_manual;
2808 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2809#endif
2810 wip->wi_optset = TRUE;
2811 }
2812
2813 /* insert the entry in front of the list */
2814 wip->wi_next = buf->b_wininfo;
2815 buf->b_wininfo = wip;
2816 wip->wi_prev = NULL;
2817 if (wip->wi_next)
2818 wip->wi_next->wi_prev = wip;
2819
2820 return;
2821}
2822
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002823#ifdef FEAT_DIFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01002824static int wininfo_other_tab_diff(wininfo_T *wip);
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002825
2826/*
2827 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2828 * page. That's because a diff is local to a tab page.
2829 */
2830 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002831wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002832{
2833 win_T *wp;
2834
2835 if (wip->wi_opt.wo_diff)
2836 {
Bram Moolenaar29323592016-07-24 22:04:11 +02002837 FOR_ALL_WINDOWS(wp)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002838 /* return FALSE when it's a window in the current tab page, thus
2839 * the buffer was in diff mode here */
2840 if (wip->wi_win == wp)
2841 return FALSE;
2842 return TRUE;
2843 }
2844 return FALSE;
2845}
2846#endif
2847
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848/*
2849 * Find info for the current window in buffer "buf".
2850 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002851 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2852 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 * Returns NULL when there isn't any info.
2854 */
2855 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002856find_wininfo(
2857 buf_T *buf,
2858 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859{
2860 wininfo_T *wip;
2861
2862 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002863 if (wip->wi_win == curwin
2864#ifdef FEAT_DIFF
2865 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2866#endif
2867 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002869
2870 /* If no wininfo for curwin, use the first in the list (that doesn't have
2871 * 'diff' set and is in another tab page). */
2872 if (wip == NULL)
2873 {
2874#ifdef FEAT_DIFF
2875 if (skip_diff_buffer)
2876 {
2877 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2878 if (!wininfo_other_tab_diff(wip))
2879 break;
2880 }
2881 else
2882#endif
2883 wip = buf->b_wininfo;
2884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 return wip;
2886}
2887
2888/*
2889 * Reset the local window options to the values last used in this window.
2890 * If the buffer wasn't used in this window before, use the values from
2891 * the most recently used window. If the values were never set, use the
2892 * global values for the window.
2893 */
2894 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002895get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896{
2897 wininfo_T *wip;
2898
2899 clear_winopt(&curwin->w_onebuf_opt);
2900#ifdef FEAT_FOLDING
2901 clearFolding(curwin);
2902#endif
2903
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002904 wip = find_wininfo(buf, TRUE);
Bram Moolenaar25782a72018-05-13 18:05:33 +02002905 if (wip != NULL && wip->wi_win != NULL
2906 && wip->wi_win != curwin && wip->wi_win->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 {
Bram Moolenaar25782a72018-05-13 18:05:33 +02002908 /* The buffer is currently displayed in the window: use the actual
2909 * option values instead of the saved (possibly outdated) values. */
2910 win_T *wp = wip->wi_win;
2911
2912 copy_winopt(&wp->w_onebuf_opt, &curwin->w_onebuf_opt);
2913#ifdef FEAT_FOLDING
2914 curwin->w_fold_manual = wp->w_fold_manual;
2915 curwin->w_foldinvalid = TRUE;
2916 cloneFoldGrowArray(&wp->w_folds, &curwin->w_folds);
2917#endif
2918 }
2919 else if (wip != NULL && wip->wi_optset)
2920 {
2921 /* the buffer was displayed in the current window earlier */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2923#ifdef FEAT_FOLDING
2924 curwin->w_fold_manual = wip->wi_fold_manual;
2925 curwin->w_foldinvalid = TRUE;
2926 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2927#endif
2928 }
2929 else
2930 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2931
2932#ifdef FEAT_FOLDING
2933 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2934 if (p_fdls >= 0)
2935 curwin->w_p_fdl = p_fdls;
2936#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002937#ifdef FEAT_SYN_HL
2938 check_colorcolumn(curwin);
2939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940}
2941
2942/*
2943 * Find the position (lnum and col) for the buffer 'buf' for the current
2944 * window.
2945 * Returns a pointer to no_position if no position is found.
2946 */
2947 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002948buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949{
2950 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002951 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002953 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 if (wip != NULL)
2955 return &(wip->wi_fpos);
2956 else
2957 return &no_position;
2958}
2959
2960/*
2961 * Find the lnum for the buffer 'buf' for the current window.
2962 */
2963 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01002964buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965{
2966 return buflist_findfpos(buf)->lnum;
2967}
2968
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002970 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002973buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974{
2975 buf_T *buf;
2976 int len;
2977 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02002978 int ro_char;
2979 int changed_char;
Bram Moolenaar0751f512018-03-29 16:37:16 +02002980#ifdef FEAT_TERMINAL
2981 int job_running;
2982 int job_none_open;
2983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984
2985 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2986 {
Bram Moolenaar0751f512018-03-29 16:37:16 +02002987#ifdef FEAT_TERMINAL
2988 job_running = term_job_running(buf->b_term);
2989 job_none_open = job_running && term_none_open(buf->b_term);
2990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 /* skip unlisted buffers, unless ! was used */
Bram Moolenaard51cb702015-07-21 15:03:06 +02002992 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
2993 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
2994 || (vim_strchr(eap->arg, '+')
2995 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
2996 || (vim_strchr(eap->arg, 'a')
Bram Moolenaar0751f512018-03-29 16:37:16 +02002997 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
Bram Moolenaard51cb702015-07-21 15:03:06 +02002998 || (vim_strchr(eap->arg, 'h')
Bram Moolenaar0751f512018-03-29 16:37:16 +02002999 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3000#ifdef FEAT_TERMINAL
3001 || (vim_strchr(eap->arg, 'R')
3002 && (!job_running || (job_running && job_none_open)))
3003 || (vim_strchr(eap->arg, '?')
3004 && (!job_running || (job_running && !job_none_open)))
3005 || (vim_strchr(eap->arg, 'F')
3006 && (job_running || buf->b_term == NULL))
3007#endif
Bram Moolenaard51cb702015-07-21 15:03:06 +02003008 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3009 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3010 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3011 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3012 || (vim_strchr(eap->arg, '#')
3013 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003016 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 else
3018 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003019 if (message_filtered(NameBuff))
3020 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003022 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3023 : (bufIsChanged(buf) ? '+' : ' ');
3024#ifdef FEAT_TERMINAL
3025 if (term_job_running(buf->b_term))
3026 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02003027 if (term_none_open(buf->b_term))
3028 ro_char = '?';
3029 else
3030 ro_char = 'R';
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003031 changed_char = ' '; /* bufIsChanged() returns TRUE to avoid
3032 * closing, but it's not actually changed. */
3033 }
3034 else if (buf->b_term != NULL)
3035 ro_char = 'F';
3036 else
3037#endif
3038 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3039
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003040 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00003041 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 buf->b_fnum,
3043 buf->b_p_bl ? ' ' : 'u',
3044 buf == curbuf ? '%' :
3045 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3046 buf->b_ml.ml_mfp == NULL ? ' ' :
3047 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003048 ro_char,
3049 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003050 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003051 if (len > IOSIZE - 20)
3052 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053
3054 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 i = 40 - vim_strsize(IObuff);
3056 do
3057 {
3058 IObuff[len++] = ' ';
3059 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003060 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3061 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003062 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 msg_outtrans(IObuff);
3064 out_flush(); /* output one line at a time */
3065 ui_breakcheck();
3066 }
3067}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068
3069/*
3070 * Get file name and line number for file 'fnum'.
3071 * Used by DoOneCmd() for translating '%' and '#'.
3072 * Used by insert_reg() and cmdline_paste() for '#' register.
3073 * Return FAIL if not found, OK for success.
3074 */
3075 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003076buflist_name_nr(
3077 int fnum,
3078 char_u **fname,
3079 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080{
3081 buf_T *buf;
3082
3083 buf = buflist_findnr(fnum);
3084 if (buf == NULL || buf->b_fname == NULL)
3085 return FAIL;
3086
3087 *fname = buf->b_fname;
3088 *lnum = buflist_findlnum(buf);
3089
3090 return OK;
3091}
3092
3093/*
3094 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
3095 * The file name with the full path is also remembered, for when :cd is used.
3096 * Returns FAIL for failure (file name already in use by other buffer)
3097 * OK otherwise.
3098 */
3099 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003100setfname(
3101 buf_T *buf,
3102 char_u *ffname,
3103 char_u *sfname,
3104 int message) /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105{
Bram Moolenaar81695252004-12-29 20:58:21 +00003106 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003108 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109#endif
3110
3111 if (ffname == NULL || *ffname == NUL)
3112 {
3113 /* Removing the name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01003114 VIM_CLEAR(buf->b_ffname);
3115 VIM_CLEAR(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116#ifdef UNIX
3117 st.st_dev = (dev_T)-1;
3118#endif
3119 }
3120 else
3121 {
3122 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
3123 if (ffname == NULL) /* out of memory */
3124 return FAIL;
3125
3126 /*
3127 * if the file name is already used in another buffer:
3128 * - if the buffer is loaded, fail
3129 * - if the buffer is not loaded, delete it from the list
3130 */
3131#ifdef UNIX
3132 if (mch_stat((char *)ffname, &st) < 0)
3133 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003134#endif
3135 if (!(buf->b_flags & BF_DUMMY))
3136#ifdef UNIX
3137 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003139 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140#endif
3141 if (obuf != NULL && obuf != buf)
3142 {
3143 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
3144 {
3145 if (message)
3146 EMSG(_("E95: Buffer with this name already exists"));
3147 vim_free(ffname);
3148 return FAIL;
3149 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01003150 /* delete from the list */
3151 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 }
3153 sfname = vim_strsave(sfname);
3154 if (ffname == NULL || sfname == NULL)
3155 {
3156 vim_free(sfname);
3157 vim_free(ffname);
3158 return FAIL;
3159 }
3160#ifdef USE_FNAME_CASE
3161# ifdef USE_LONG_FNAME
3162 if (USE_LONG_FNAME)
3163# endif
3164 fname_case(sfname, 0); /* set correct case for short file name */
3165#endif
3166 vim_free(buf->b_ffname);
3167 vim_free(buf->b_sfname);
3168 buf->b_ffname = ffname;
3169 buf->b_sfname = sfname;
3170 }
3171 buf->b_fname = buf->b_sfname;
3172#ifdef UNIX
3173 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003174 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 else
3176 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003177 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 buf->b_dev = st.st_dev;
3179 buf->b_ino = st.st_ino;
3180 }
3181#endif
3182
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184
3185 buf_name_changed(buf);
3186 return OK;
3187}
3188
3189/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003190 * Crude way of changing the name of a buffer. Use with care!
3191 * The name should be relative to the current directory.
3192 */
3193 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003194buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003195{
3196 buf_T *buf;
3197
3198 buf = buflist_findnr(fnum);
3199 if (buf != NULL)
3200 {
3201 vim_free(buf->b_sfname);
3202 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003203 buf->b_ffname = vim_strsave(name);
3204 buf->b_sfname = NULL;
3205 /* Allocate ffname and expand into full path. Also resolves .lnk
3206 * files on Win32. */
3207 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003208 buf->b_fname = buf->b_sfname;
3209 }
3210}
3211
3212/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 * Take care of what needs to be done when the name of buffer "buf" has
3214 * changed.
3215 */
3216 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003217buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218{
3219 /*
3220 * If the file name changed, also change the name of the swapfile
3221 */
3222 if (buf->b_ml.ml_mfp != NULL)
3223 ml_setname(buf);
3224
3225 if (curwin->w_buffer == buf)
3226 check_arg_idx(curwin); /* check file name for arg list */
3227#ifdef FEAT_TITLE
3228 maketitle(); /* set window title */
3229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 status_redraw_all(); /* status lines need to be redrawn */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 fmarks_check_names(buf); /* check named file marks */
3232 ml_timestamp(buf); /* reset timestamp */
3233}
3234
3235/*
3236 * set alternate file name for current window
3237 *
3238 * Used by do_one_cmd(), do_write() and do_ecmd().
3239 * Return the buffer.
3240 */
3241 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003242setaltfname(
3243 char_u *ffname,
3244 char_u *sfname,
3245 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246{
3247 buf_T *buf;
3248
3249 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
3250 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003251 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 curwin->w_alt_fnum = buf->b_fnum;
3253 return buf;
3254}
3255
3256/*
3257 * Get alternate file name for current window.
3258 * Return NULL if there isn't any, and give error message if requested.
3259 */
3260 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003261getaltfname(
3262 int errmsg) /* give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263{
3264 char_u *fname;
3265 linenr_T dummy;
3266
3267 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3268 {
3269 if (errmsg)
3270 EMSG(_(e_noalt));
3271 return NULL;
3272 }
3273 return fname;
3274}
3275
3276/*
3277 * Add a file name to the buflist and return its number.
3278 * Uses same flags as buflist_new(), except BLN_DUMMY.
3279 *
3280 * used by qf_init(), main() and doarglist()
3281 */
3282 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003283buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284{
3285 buf_T *buf;
3286
3287 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3288 if (buf != NULL)
3289 return buf->b_fnum;
3290 return 0;
3291}
3292
3293#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3294/*
3295 * Adjust slashes in file names. Called after 'shellslash' was set.
3296 */
3297 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003298buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299{
3300 buf_T *bp;
3301
Bram Moolenaar29323592016-07-24 22:04:11 +02003302 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 {
3304 if (bp->b_ffname != NULL)
3305 slash_adjust(bp->b_ffname);
3306 if (bp->b_sfname != NULL)
3307 slash_adjust(bp->b_sfname);
3308 }
3309}
3310#endif
3311
3312/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003313 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 * Also save the local window option values.
3315 */
3316 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003317buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003319 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320}
3321
3322/*
3323 * Return TRUE if 'ffname' is not the same file as current file.
3324 * Fname must have a full path (expanded by mch_FullName()).
3325 */
3326 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003327otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328{
3329 return otherfile_buf(curbuf, ffname
3330#ifdef UNIX
3331 , NULL
3332#endif
3333 );
3334}
3335
3336 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003337otherfile_buf(
3338 buf_T *buf,
3339 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003341 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003343 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344{
3345 /* no name is different */
3346 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3347 return TRUE;
3348 if (fnamecmp(ffname, buf->b_ffname) == 0)
3349 return FALSE;
3350#ifdef UNIX
3351 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003352 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353
Bram Moolenaar8767f522016-07-01 17:17:39 +02003354 /* If no stat_T given, get it now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 if (stp == NULL)
3356 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003357 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 st.st_dev = (dev_T)-1;
3359 stp = &st;
3360 }
3361 /* Use dev/ino to check if the files are the same, even when the names
3362 * are different (possible with links). Still need to compare the
3363 * name above, for when the file doesn't exist yet.
3364 * Problem: The dev/ino changes when a file is deleted (and created
3365 * again) and remains the same when renamed/moved. We don't want to
3366 * mch_stat() each buffer each time, that would be too slow. Get the
3367 * dev/ino again when they appear to match, but not when they appear
3368 * to be different: Could skip a buffer when it's actually the same
3369 * file. */
3370 if (buf_same_ino(buf, stp))
3371 {
3372 buf_setino(buf);
3373 if (buf_same_ino(buf, stp))
3374 return FALSE;
3375 }
3376 }
3377#endif
3378 return TRUE;
3379}
3380
3381#if defined(UNIX) || defined(PROTO)
3382/*
3383 * Set inode and device number for a buffer.
3384 * Must always be called when b_fname is changed!.
3385 */
3386 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003387buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003389 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390
3391 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3392 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003393 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 buf->b_dev = st.st_dev;
3395 buf->b_ino = st.st_ino;
3396 }
3397 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003398 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399}
3400
3401/*
3402 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3403 */
3404 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003405buf_same_ino(
3406 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003407 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003409 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 && stp->st_dev == buf->b_dev
3411 && stp->st_ino == buf->b_ino);
3412}
3413#endif
3414
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003415/*
3416 * Print info about the current buffer.
3417 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003419fileinfo(
3420 int fullname, /* when non-zero print full path */
3421 int shorthelp,
3422 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423{
3424 char_u *name;
3425 int n;
3426 char_u *p;
3427 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003428 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429
3430 buffer = alloc(IOSIZE);
3431 if (buffer == NULL)
3432 return;
3433
3434 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3435 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003436 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 p = buffer + STRLEN(buffer);
3438 }
3439 else
3440 p = buffer;
3441
3442 *p++ = '"';
3443 if (buf_spname(curbuf) != NULL)
Bram Moolenaarec3cfeb2012-10-03 17:12:47 +02003444 vim_strncpy(p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 else
3446 {
3447 if (!fullname && curbuf->b_fname != NULL)
3448 name = curbuf->b_fname;
3449 else
3450 name = curbuf->b_ffname;
3451 home_replace(shorthelp ? curbuf : NULL, name, p,
3452 (int)(IOSIZE - (p - buffer)), TRUE);
3453 }
3454
Bram Moolenaara800b422010-06-27 01:15:55 +02003455 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 curbufIsChanged() ? (shortmess(SHM_MOD)
3457 ? " [+]" : _(" [Modified]")) : " ",
3458 (curbuf->b_flags & BF_NOTEDITED)
3459#ifdef FEAT_QUICKFIX
3460 && !bt_dontwrite(curbuf)
3461#endif
3462 ? _("[Not edited]") : "",
3463 (curbuf->b_flags & BF_NEW)
3464#ifdef FEAT_QUICKFIX
3465 && !bt_dontwrite(curbuf)
3466#endif
3467 ? _("[New file]") : "",
3468 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003469 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 : _("[readonly]")) : "",
3471 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3472 || curbuf->b_p_ro) ?
3473 " " : "");
3474 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3475 * causes an overflow, thus for large numbers divide instead. */
3476 if (curwin->w_cursor.lnum > 1000000L)
3477 n = (int)(((long)curwin->w_cursor.lnum) /
3478 ((long)curbuf->b_ml.ml_line_count / 100L));
3479 else
3480 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3481 (long)curbuf->b_ml.ml_line_count);
3482 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaara800b422010-06-27 01:15:55 +02003483 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484#ifdef FEAT_CMDL_INFO
3485 else if (p_ru)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 /* Current line and column are already on the screen -- webb */
Bram Moolenaarda6e8912018-08-21 15:12:14 +02003487 vim_snprintf_add((char *)buffer, IOSIZE,
3488 NGETTEXT("%ld line --%d%%--", "%ld lines --%d%%--",
3489 curbuf->b_ml.ml_line_count),
3490 (long)curbuf->b_ml.ml_line_count, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491#endif
3492 else
3493 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003494 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 _("line %ld of %ld --%d%%-- col "),
3496 (long)curwin->w_cursor.lnum,
3497 (long)curbuf->b_ml.ml_line_count,
3498 n);
3499 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003500 len = STRLEN(buffer);
3501 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3503 }
3504
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003505 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506
3507 if (dont_truncate)
3508 {
3509 /* Temporarily set msg_scroll to avoid the message being truncated.
3510 * First call msg_start() to get the message in the right place. */
3511 msg_start();
3512 n = msg_scroll;
3513 msg_scroll = TRUE;
3514 msg(buffer);
3515 msg_scroll = n;
3516 }
3517 else
3518 {
3519 p = msg_trunc_attr(buffer, FALSE, 0);
3520 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 /* Need to repeat the message after redrawing when:
3522 * - When restart_edit is set (otherwise there will be a delay
3523 * before redrawing).
3524 * - When the screen was scrolled but there is no wait-return
3525 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003526 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 }
3528
3529 vim_free(buffer);
3530}
3531
3532 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003533col_print(
3534 char_u *buf,
3535 size_t buflen,
3536 int col,
3537 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538{
3539 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003540 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003542 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543}
3544
3545#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546static char_u *lasttitle = NULL;
3547static char_u *lasticon = NULL;
3548
Bram Moolenaar84a93082018-06-16 22:58:15 +02003549/*
3550 * Put the file name in the title bar and icon of the window.
3551 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003553maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554{
3555 char_u *p;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003556 char_u *title_str = NULL;
3557 char_u *icon_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 int maxlen = 0;
3559 int len;
3560 int mustset;
3561 char_u buf[IOSIZE];
3562 int off;
3563
3564 if (!redrawing())
3565 {
3566 /* Postpone updating the title when 'lazyredraw' is set. */
3567 need_maketitle = TRUE;
3568 return;
3569 }
3570
3571 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003572 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003573 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574
3575 if (p_title)
3576 {
3577 if (p_titlelen > 0)
3578 {
3579 maxlen = p_titlelen * Columns / 100;
3580 if (maxlen < 10)
3581 maxlen = 10;
3582 }
3583
Bram Moolenaar84a93082018-06-16 22:58:15 +02003584 title_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 if (*p_titlestring != NUL)
3586 {
3587#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003588 if (stl_syntax & STL_IN_TITLE)
3589 {
3590 int use_sandbox = FALSE;
3591 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003592
3593# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003594 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003595# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003596 called_emsg = FALSE;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003597 build_stl_str_hl(curwin, title_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003598 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003599 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003600 if (called_emsg)
3601 set_string_option_direct((char_u *)"titlestring", -1,
3602 (char_u *)"", OPT_FREE, SID_ERROR);
3603 called_emsg |= save_called_emsg;
3604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 else
3606#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003607 title_str = p_titlestring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 }
3609 else
3610 {
3611 /* format: "fname + (path) (1 of 2) - VIM" */
3612
Bram Moolenaar2c666692012-09-05 13:30:40 +02003613#define SPACE_FOR_FNAME (IOSIZE - 100)
3614#define SPACE_FOR_DIR (IOSIZE - 20)
3615#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003617 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar21554412017-07-24 21:44:43 +02003618#ifdef FEAT_TERMINAL
3619 else if (curbuf->b_term != NULL)
3620 {
3621 vim_strncpy(buf, term_get_status_text(curbuf->b_term),
3622 SPACE_FOR_FNAME);
3623 }
3624#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 else
3626 {
3627 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003628 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 }
3631
Bram Moolenaar21554412017-07-24 21:44:43 +02003632#ifdef FEAT_TERMINAL
3633 if (curbuf->b_term == NULL)
3634#endif
3635 switch (bufIsChanged(curbuf)
3636 + (curbuf->b_p_ro * 2)
3637 + (!curbuf->b_p_ma * 4))
3638 {
3639 case 1: STRCAT(buf, " +"); break;
3640 case 2: STRCAT(buf, " ="); break;
3641 case 3: STRCAT(buf, " =+"); break;
3642 case 4:
3643 case 6: STRCAT(buf, " -"); break;
3644 case 5:
3645 case 7: STRCAT(buf, " -+"); break;
3646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647
Bram Moolenaar21554412017-07-24 21:44:43 +02003648 if (curbuf->b_fname != NULL
3649#ifdef FEAT_TERMINAL
3650 && curbuf->b_term == NULL
3651#endif
3652 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 {
3654 /* Get path of file, replace home dir with ~ */
3655 off = (int)STRLEN(buf);
3656 buf[off++] = ' ';
3657 buf[off++] = '(';
3658 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003659 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660#ifdef BACKSLASH_IN_FILENAME
3661 /* avoid "c:/name" to be reduced to "c" */
3662 if (isalpha(buf[off]) && buf[off + 1] == ':')
3663 off += 2;
3664#endif
3665 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003666 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 if (p == buf + off)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003668 {
Bram Moolenaar21554412017-07-24 21:44:43 +02003669 /* must be a help buffer */
3670 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003671 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003675
Bram Moolenaar2c666692012-09-05 13:30:40 +02003676 /* Translate unprintable chars and concatenate. Keep some
3677 * room for the server name. When there is no room (very long
3678 * file name) use (...). */
3679 if (off < SPACE_FOR_DIR)
3680 {
3681 p = transstr(buf + off);
3682 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3683 vim_free(p);
3684 }
3685 else
3686 {
3687 vim_strncpy(buf + off, (char_u *)"...",
3688 (size_t)(SPACE_FOR_ARGNR - off));
3689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 STRCAT(buf, ")");
3691 }
3692
Bram Moolenaar2c666692012-09-05 13:30:40 +02003693 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694
3695#if defined(FEAT_CLIENTSERVER)
3696 if (serverName != NULL)
3697 {
3698 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003699 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 }
3701 else
3702#endif
3703 STRCAT(buf, " - VIM");
3704
3705 if (maxlen > 0)
3706 {
3707 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003708 if (vim_strsize(buf) > maxlen)
3709 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 }
3711 }
3712 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02003713 mustset = value_changed(title_str, &lasttitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714
3715 if (p_icon)
3716 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02003717 icon_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 if (*p_iconstring != NUL)
3719 {
3720#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003721 if (stl_syntax & STL_IN_ICON)
3722 {
3723 int use_sandbox = FALSE;
3724 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003725
3726# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003727 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003728# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003729 called_emsg = FALSE;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003730 build_stl_str_hl(curwin, icon_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003731 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003732 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003733 if (called_emsg)
3734 set_string_option_direct((char_u *)"iconstring", -1,
3735 (char_u *)"", OPT_FREE, SID_ERROR);
3736 called_emsg |= save_called_emsg;
3737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 else
3739#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003740 icon_str = p_iconstring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 }
3742 else
3743 {
3744 if (buf_spname(curbuf) != NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003745 p = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 else /* use file name only in icon */
Bram Moolenaar84a93082018-06-16 22:58:15 +02003747 p = gettail(curbuf->b_ffname);
3748 *icon_str = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 /* Truncate name at 100 bytes. */
Bram Moolenaar84a93082018-06-16 22:58:15 +02003750 len = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 if (len > 100)
3752 {
3753 len -= 100;
3754#ifdef FEAT_MBYTE
3755 if (has_mbyte)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003756 len += (*mb_tail_off)(p, p + len) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003758 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02003760 STRCPY(icon_str, p);
3761 trans_characters(icon_str, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 }
3763 }
3764
Bram Moolenaar84a93082018-06-16 22:58:15 +02003765 mustset |= value_changed(icon_str, &lasticon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766
3767 if (mustset)
3768 resettitle();
3769}
3770
3771/*
3772 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3773 * from "str" if it does.
Bram Moolenaar84a93082018-06-16 22:58:15 +02003774 * Return TRUE if resettitle() is to be called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 */
3776 static int
Bram Moolenaar84a93082018-06-16 22:58:15 +02003777value_changed(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778{
3779 if ((str == NULL) != (*last == NULL)
3780 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3781 {
3782 vim_free(*last);
3783 if (str == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003784 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 *last = NULL;
Bram Moolenaar40385db2018-08-07 22:31:44 +02003786 mch_restore_title(
3787 last == &lasttitle ? SAVE_RESTORE_TITLE : SAVE_RESTORE_ICON);
Bram Moolenaar84a93082018-06-16 22:58:15 +02003788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 else
Bram Moolenaar84a93082018-06-16 22:58:15 +02003790 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 *last = vim_strsave(str);
Bram Moolenaar84a93082018-06-16 22:58:15 +02003792 return TRUE;
3793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 }
3795 return FALSE;
3796}
3797
3798/*
3799 * Put current window title back (used after calling a shell)
3800 */
3801 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003802resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803{
3804 mch_settitle(lasttitle, lasticon);
3805}
Bram Moolenaarea408852005-06-25 22:49:46 +00003806
3807# if defined(EXITFREE) || defined(PROTO)
3808 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003809free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00003810{
3811 vim_free(lasttitle);
3812 vim_free(lasticon);
3813}
3814# endif
3815
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816#endif /* FEAT_TITLE */
3817
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003818#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003820 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 * Return length of string in screen cells.
3822 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003823 * Normally works for window "wp", except when working for 'tabline' then it
3824 * is "curwin".
3825 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 * Items are drawn interspersed with the text that surrounds it
3827 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3828 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3829 *
3830 * If maxwidth is not zero, the string will be filled at any middle marker
3831 * or truncated if too long, fillchar is used for all whitespace.
3832 */
3833 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003834build_stl_str_hl(
3835 win_T *wp,
3836 char_u *out, /* buffer to write into != NameBuff */
3837 size_t outlen, /* length of out[] */
3838 char_u *fmt,
3839 int use_sandbox UNUSED, /* "fmt" was set insecurely, use sandbox */
3840 int fillchar,
3841 int maxwidth,
3842 struct stl_hlrec *hltab, /* return: HL attributes (can be NULL) */
3843 struct stl_hlrec *tabtab) /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844{
3845 char_u *p;
3846 char_u *s;
3847 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003848 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849#ifdef FEAT_EVAL
Bram Moolenaarba2929b2017-09-08 13:59:21 +02003850 win_T *save_curwin;
3851 buf_T *save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852#endif
3853 int empty_line;
3854 colnr_T virtcol;
3855 long l;
3856 long n;
3857 int prevchar_isflag;
3858 int prevchar_isitem;
3859 int itemisflag;
3860 int fillable;
3861 char_u *str;
3862 long num;
3863 int width;
3864 int itemcnt;
3865 int curitem;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02003866 int group_end_userhl;
3867 int group_start_userhl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 int groupitem[STL_MAX_ITEM];
3869 int groupdepth;
3870 struct stl_item
3871 {
3872 char_u *start;
3873 int minwid;
3874 int maxwid;
3875 enum
3876 {
3877 Normal,
3878 Empty,
3879 Group,
3880 Middle,
3881 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003882 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 Trunc
3884 } type;
3885 } item[STL_MAX_ITEM];
3886 int minwid;
3887 int maxwid;
3888 int zeropad;
3889 char_u base;
3890 char_u opt;
3891#define TMPLEN 70
3892 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003893 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003894 struct stl_hlrec *sp;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02003895 int save_must_redraw = must_redraw;
3896 int save_redr_type = curwin->w_redr_type;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003897
3898#ifdef FEAT_EVAL
3899 /*
3900 * When the format starts with "%!" then evaluate it as an expression and
3901 * use the result as the actual format string.
3902 */
3903 if (fmt[0] == '%' && fmt[1] == '!')
3904 {
3905 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3906 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003907 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003908 }
3909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910
3911 if (fillchar == 0)
3912 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003913#ifdef FEAT_MBYTE
3914 /* Can't handle a multi-byte fill character yet. */
3915 else if (mb_char2len(fillchar) > 1)
3916 fillchar = '-';
3917#endif
3918
Bram Moolenaar567199b2013-04-24 16:52:36 +02003919 /* Get line & check if empty (cursorpos will show "0-1"). Note that
3920 * p will become invalid when getting another buffer line. */
3921 p = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3922 empty_line = (*p == NUL);
3923
3924 /* Get the byte value now, in case we need it below. This is more
3925 * efficient than making a copy of the line. */
3926 if (wp->w_cursor.col > (colnr_T)STRLEN(p))
3927 byteval = 0;
3928 else
3929#ifdef FEAT_MBYTE
3930 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
3931#else
3932 byteval = p[wp->w_cursor.col];
3933#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934
3935 groupdepth = 0;
3936 p = out;
3937 curitem = 0;
3938 prevchar_isflag = TRUE;
3939 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003940 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003942 if (curitem == STL_MAX_ITEM)
3943 {
3944 /* There are too many items. Add the error code to the statusline
3945 * to give the user a hint about what went wrong. */
3946 if (p + 6 < out + outlen)
3947 {
3948 mch_memmove(p, " E541", (size_t)5);
3949 p += 5;
3950 }
3951 break;
3952 }
3953
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 if (*s != NUL && *s != '%')
3955 prevchar_isflag = prevchar_isitem = FALSE;
3956
3957 /*
3958 * Handle up to the next '%' or the end.
3959 */
3960 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3961 *p++ = *s++;
3962 if (*s == NUL || p + 1 >= out + outlen)
3963 break;
3964
3965 /*
3966 * Handle one '%' item.
3967 */
3968 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003969 if (*s == NUL) /* ignore trailing % */
3970 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 if (*s == '%')
3972 {
3973 if (p + 1 >= out + outlen)
3974 break;
3975 *p++ = *s++;
3976 prevchar_isflag = prevchar_isitem = FALSE;
3977 continue;
3978 }
3979 if (*s == STL_MIDDLEMARK)
3980 {
3981 s++;
3982 if (groupdepth > 0)
3983 continue;
3984 item[curitem].type = Middle;
3985 item[curitem++].start = p;
3986 continue;
3987 }
3988 if (*s == STL_TRUNCMARK)
3989 {
3990 s++;
3991 item[curitem].type = Trunc;
3992 item[curitem++].start = p;
3993 continue;
3994 }
3995 if (*s == ')')
3996 {
3997 s++;
3998 if (groupdepth < 1)
3999 continue;
4000 groupdepth--;
4001
4002 t = item[groupitem[groupdepth]].start;
4003 *p = NUL;
4004 l = vim_strsize(t);
4005 if (curitem > groupitem[groupdepth] + 1
4006 && item[groupitem[groupdepth]].minwid == 0)
4007 {
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004008 /* remove group if all items are empty and highlight group
4009 * doesn't change */
4010 group_start_userhl = group_end_userhl = 0;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004011 for (n = groupitem[groupdepth] - 1; n >= 0; n--)
4012 {
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004013 if (item[n].type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004014 {
4015 group_start_userhl = group_end_userhl = item[n].minwid;
4016 break;
4017 }
4018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004020 {
4021 if (item[n].type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 break;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004023 if (item[n].type == Highlight)
4024 group_end_userhl = item[n].minwid;
4025 }
4026 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 {
4028 p = t;
4029 l = 0;
4030 }
4031 }
4032 if (l > item[groupitem[groupdepth]].maxwid)
4033 {
4034 /* truncate, remove n bytes of text at the start */
4035#ifdef FEAT_MBYTE
4036 if (has_mbyte)
4037 {
4038 /* Find the first character that should be included. */
4039 n = 0;
4040 while (l >= item[groupitem[groupdepth]].maxwid)
4041 {
4042 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004043 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 }
4045 }
4046 else
4047#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004048 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049
4050 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004051 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 p = p - n + 1;
4053#ifdef FEAT_MBYTE
4054 /* Fill up space left over by half a double-wide char. */
4055 while (++l < item[groupitem[groupdepth]].minwid)
4056 *p++ = fillchar;
4057#endif
4058
4059 /* correct the start of the items for the truncation */
4060 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
4061 {
4062 item[l].start -= n;
4063 if (item[l].start < t)
4064 item[l].start = t;
4065 }
4066 }
4067 else if (abs(item[groupitem[groupdepth]].minwid) > l)
4068 {
4069 /* fill */
4070 n = item[groupitem[groupdepth]].minwid;
4071 if (n < 0)
4072 {
4073 /* fill by appending characters */
4074 n = 0 - n;
4075 while (l++ < n && p + 1 < out + outlen)
4076 *p++ = fillchar;
4077 }
4078 else
4079 {
4080 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004081 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 l = n - l;
4083 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004084 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 p += l;
4086 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
4087 item[n].start += l;
4088 for ( ; l > 0; l--)
4089 *t++ = fillchar;
4090 }
4091 }
4092 continue;
4093 }
4094 minwid = 0;
4095 maxwid = 9999;
4096 zeropad = FALSE;
4097 l = 1;
4098 if (*s == '0')
4099 {
4100 s++;
4101 zeropad = TRUE;
4102 }
4103 if (*s == '-')
4104 {
4105 s++;
4106 l = -1;
4107 }
4108 if (VIM_ISDIGIT(*s))
4109 {
4110 minwid = (int)getdigits(&s);
4111 if (minwid < 0) /* overflow */
4112 minwid = 0;
4113 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004114 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 {
4116 item[curitem].type = Highlight;
4117 item[curitem].start = p;
4118 item[curitem].minwid = minwid > 9 ? 1 : minwid;
4119 s++;
4120 curitem++;
4121 continue;
4122 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004123 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4124 {
4125 if (*s == STL_TABCLOSENR)
4126 {
4127 if (minwid == 0)
4128 {
4129 /* %X ends the close label, go back to the previously
4130 * define tab label nr. */
4131 for (n = curitem - 1; n >= 0; --n)
4132 if (item[n].type == TabPage && item[n].minwid >= 0)
4133 {
4134 minwid = item[n].minwid;
4135 break;
4136 }
4137 }
4138 else
4139 /* close nrs are stored as negative values */
4140 minwid = - minwid;
4141 }
4142 item[curitem].type = TabPage;
4143 item[curitem].start = p;
4144 item[curitem].minwid = minwid;
4145 s++;
4146 curitem++;
4147 continue;
4148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 if (*s == '.')
4150 {
4151 s++;
4152 if (VIM_ISDIGIT(*s))
4153 {
4154 maxwid = (int)getdigits(&s);
4155 if (maxwid <= 0) /* overflow */
4156 maxwid = 50;
4157 }
4158 }
4159 minwid = (minwid > 50 ? 50 : minwid) * l;
4160 if (*s == '(')
4161 {
4162 groupitem[groupdepth++] = curitem;
4163 item[curitem].type = Group;
4164 item[curitem].start = p;
4165 item[curitem].minwid = minwid;
4166 item[curitem].maxwid = maxwid;
4167 s++;
4168 curitem++;
4169 continue;
4170 }
4171 if (vim_strchr(STL_ALL, *s) == NULL)
4172 {
4173 s++;
4174 continue;
4175 }
4176 opt = *s++;
4177
4178 /* OK - now for the real work */
4179 base = 'D';
4180 itemisflag = FALSE;
4181 fillable = TRUE;
4182 num = -1;
4183 str = NULL;
4184 switch (opt)
4185 {
4186 case STL_FILEPATH:
4187 case STL_FULLPATH:
4188 case STL_FILENAME:
4189 fillable = FALSE; /* don't change ' ' to fillchar */
4190 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004191 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 else
4193 {
4194 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004195 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4197 }
4198 trans_characters(NameBuff, MAXPATHL);
4199 if (opt != STL_FILENAME)
4200 str = NameBuff;
4201 else
4202 str = gettail(NameBuff);
4203 break;
4204
4205 case STL_VIM_EXPR: /* '{' */
4206 itemisflag = TRUE;
4207 t = p;
4208 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
4209 *p++ = *s++;
4210 if (*s != '}') /* missing '}' or out of space */
4211 break;
4212 s++;
4213 *p = 0;
4214 p = t;
4215
4216#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004217 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar3cb44482018-08-05 13:22:26 +02004218 set_internal_string_var((char_u *)"g:actual_curbuf", tmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004220 save_curbuf = curbuf;
4221 save_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 curwin = wp;
4223 curbuf = wp->w_buffer;
4224
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004225 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004227 curwin = save_curwin;
4228 curbuf = save_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00004229 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230
4231 if (str != NULL && *str != 0)
4232 {
4233 if (*skipdigits(str) == NUL)
4234 {
4235 num = atoi((char *)str);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004236 VIM_CLEAR(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 itemisflag = FALSE;
4238 }
4239 }
4240#endif
4241 break;
4242
4243 case STL_LINE:
4244 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4245 ? 0L : (long)(wp->w_cursor.lnum);
4246 break;
4247
4248 case STL_NUMLINES:
4249 num = wp->w_buffer->b_ml.ml_line_count;
4250 break;
4251
4252 case STL_COLUMN:
4253 num = !(State & INSERT) && empty_line
4254 ? 0 : (int)wp->w_cursor.col + 1;
4255 break;
4256
4257 case STL_VIRTCOL:
4258 case STL_VIRTCOL_ALT:
4259 /* In list mode virtcol needs to be recomputed */
4260 virtcol = wp->w_virtcol;
4261 if (wp->w_p_list && lcs_tab1 == NUL)
4262 {
4263 wp->w_p_list = FALSE;
4264 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
4265 wp->w_p_list = TRUE;
4266 }
4267 ++virtcol;
4268 /* Don't display %V if it's the same as %c. */
4269 if (opt == STL_VIRTCOL_ALT
4270 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
4271 ? 0 : (int)wp->w_cursor.col + 1)))
4272 break;
4273 num = (long)virtcol;
4274 break;
4275
4276 case STL_PERCENTAGE:
4277 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4278 (long)wp->w_buffer->b_ml.ml_line_count);
4279 break;
4280
4281 case STL_ALTPERCENT:
4282 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004283 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 break;
4285
4286 case STL_ARGLISTSTAT:
4287 fillable = FALSE;
4288 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004289 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 str = tmp;
4291 break;
4292
4293 case STL_KEYMAP:
4294 fillable = FALSE;
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02004295 if (get_keymap_str(wp, (char_u *)"<%s>", tmp, TMPLEN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 str = tmp;
4297 break;
4298 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004299#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004300 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301#else
4302 num = 0;
4303#endif
4304 break;
4305
4306 case STL_BUFNO:
4307 num = wp->w_buffer->b_fnum;
4308 break;
4309
4310 case STL_OFFSET_X:
4311 base = 'X';
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004312 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 case STL_OFFSET:
4314#ifdef FEAT_BYTEOFF
4315 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4316 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4317 0L : l + 1 + (!(State & INSERT) && empty_line ?
4318 0 : (int)wp->w_cursor.col);
4319#endif
4320 break;
4321
4322 case STL_BYTEVAL_X:
4323 base = 'X';
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004324 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004326 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 if (num == NL)
4328 num = 0;
4329 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4330 num = NL;
4331 break;
4332
4333 case STL_ROFLAG:
4334 case STL_ROFLAG_ALT:
4335 itemisflag = TRUE;
4336 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004337 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 break;
4339
4340 case STL_HELPFLAG:
4341 case STL_HELPFLAG_ALT:
4342 itemisflag = TRUE;
4343 if (wp->w_buffer->b_help)
4344 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004345 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 break;
4347
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 case STL_FILETYPE:
4349 if (*wp->w_buffer->b_p_ft != NUL
4350 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4351 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004352 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
4353 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 str = tmp;
4355 }
4356 break;
4357
4358 case STL_FILETYPE_ALT:
4359 itemisflag = TRUE;
4360 if (*wp->w_buffer->b_p_ft != NUL
4361 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4362 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004363 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
4364 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 for (t = tmp; *t != 0; t++)
4366 *t = TOUPPER_LOC(*t);
4367 str = tmp;
4368 }
4369 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370
Bram Moolenaar4033c552017-09-16 20:54:51 +02004371#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 case STL_PREVIEWFLAG:
4373 case STL_PREVIEWFLAG_ALT:
4374 itemisflag = TRUE;
4375 if (wp->w_p_pvw)
4376 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4377 : _("[Preview]"));
4378 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004379
4380 case STL_QUICKFIX:
4381 if (bt_quickfix(wp->w_buffer))
4382 str = (char_u *)(wp->w_llist_ref
4383 ? _(msg_loclist)
4384 : _(msg_qflist));
4385 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386#endif
4387
4388 case STL_MODIFIED:
4389 case STL_MODIFIED_ALT:
4390 itemisflag = TRUE;
4391 switch ((opt == STL_MODIFIED_ALT)
4392 + bufIsChanged(wp->w_buffer) * 2
4393 + (!wp->w_buffer->b_p_ma) * 4)
4394 {
4395 case 2: str = (char_u *)"[+]"; break;
4396 case 3: str = (char_u *)",+"; break;
4397 case 4: str = (char_u *)"[-]"; break;
4398 case 5: str = (char_u *)",-"; break;
4399 case 6: str = (char_u *)"[+-]"; break;
4400 case 7: str = (char_u *)",+-"; break;
4401 }
4402 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004403
4404 case STL_HIGHLIGHT:
4405 t = s;
4406 while (*s != '#' && *s != NUL)
4407 ++s;
4408 if (*s == '#')
4409 {
4410 item[curitem].type = Highlight;
4411 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004412 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004413 curitem++;
4414 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004415 if (*s != NUL)
4416 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004417 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 }
4419
4420 item[curitem].start = p;
4421 item[curitem].type = Normal;
4422 if (str != NULL && *str)
4423 {
4424 t = str;
4425 if (itemisflag)
4426 {
4427 if ((t[0] && t[1])
4428 && ((!prevchar_isitem && *t == ',')
4429 || (prevchar_isflag && *t == ' ')))
4430 t++;
4431 prevchar_isflag = TRUE;
4432 }
4433 l = vim_strsize(t);
4434 if (l > 0)
4435 prevchar_isitem = TRUE;
4436 if (l > maxwid)
4437 {
4438 while (l >= maxwid)
4439#ifdef FEAT_MBYTE
4440 if (has_mbyte)
4441 {
4442 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004443 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 }
4445 else
4446#endif
4447 l -= byte2cells(*t++);
4448 if (p + 1 >= out + outlen)
4449 break;
4450 *p++ = '<';
4451 }
4452 if (minwid > 0)
4453 {
4454 for (; l < minwid && p + 1 < out + outlen; l++)
4455 {
4456 /* Don't put a "-" in front of a digit. */
4457 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4458 *p++ = ' ';
4459 else
4460 *p++ = fillchar;
4461 }
4462 minwid = 0;
4463 }
4464 else
4465 minwid *= -1;
4466 while (*t && p + 1 < out + outlen)
4467 {
4468 *p++ = *t++;
4469 /* Change a space by fillchar, unless fillchar is '-' and a
4470 * digit follows. */
4471 if (fillable && p[-1] == ' '
4472 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4473 p[-1] = fillchar;
4474 }
4475 for (; l < minwid && p + 1 < out + outlen; l++)
4476 *p++ = fillchar;
4477 }
4478 else if (num >= 0)
4479 {
4480 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4481 char_u nstr[20];
4482
4483 if (p + 20 >= out + outlen)
4484 break; /* not sufficient space */
4485 prevchar_isitem = TRUE;
4486 t = nstr;
4487 if (opt == STL_VIRTCOL_ALT)
4488 {
4489 *t++ = '-';
4490 minwid--;
4491 }
4492 *t++ = '%';
4493 if (zeropad)
4494 *t++ = '0';
4495 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004496 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 *t = 0;
4498
4499 for (n = num, l = 1; n >= nbase; n /= nbase)
4500 l++;
4501 if (opt == STL_VIRTCOL_ALT)
4502 l++;
4503 if (l > maxwid)
4504 {
4505 l += 2;
4506 n = l - maxwid;
4507 while (l-- > maxwid)
4508 num /= nbase;
4509 *t++ = '>';
4510 *t++ = '%';
4511 *t = t[-3];
4512 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004513 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4514 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 }
4516 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004517 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4518 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 p += STRLEN(p);
4520 }
4521 else
4522 item[curitem].type = Empty;
4523
4524 if (opt == STL_VIM_EXPR)
4525 vim_free(str);
4526
4527 if (num >= 0 || (!itemisflag && str && *str))
4528 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4529 curitem++;
4530 }
4531 *p = NUL;
4532 itemcnt = curitem;
4533
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004534#ifdef FEAT_EVAL
4535 if (usefmt != fmt)
4536 vim_free(usefmt);
4537#endif
4538
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 width = vim_strsize(out);
4540 if (maxwidth > 0 && width > maxwidth)
4541 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004542 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 l = 0;
4544 if (itemcnt == 0)
4545 s = out;
4546 else
4547 {
4548 for ( ; l < itemcnt; l++)
4549 if (item[l].type == Trunc)
4550 {
4551 /* Truncate at %< item. */
4552 s = item[l].start;
4553 break;
4554 }
4555 if (l == itemcnt)
4556 {
4557 /* No %< item, truncate first item. */
4558 s = item[0].start;
4559 l = 0;
4560 }
4561 }
4562
4563 if (width - vim_strsize(s) >= maxwidth)
4564 {
4565 /* Truncation mark is beyond max length */
4566#ifdef FEAT_MBYTE
4567 if (has_mbyte)
4568 {
4569 s = out;
4570 width = 0;
4571 for (;;)
4572 {
4573 width += ptr2cells(s);
4574 if (width >= maxwidth)
4575 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004576 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 }
4578 /* Fill up for half a double-wide character. */
4579 while (++width < maxwidth)
4580 *s++ = fillchar;
4581 }
4582 else
4583#endif
4584 s = out + maxwidth - 1;
4585 for (l = 0; l < itemcnt; l++)
4586 if (item[l].start > s)
4587 break;
4588 itemcnt = l;
4589 *s++ = '>';
4590 *s = 0;
4591 }
4592 else
4593 {
4594#ifdef FEAT_MBYTE
4595 if (has_mbyte)
4596 {
4597 n = 0;
4598 while (width >= maxwidth)
4599 {
4600 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004601 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 }
4603 }
4604 else
4605#endif
4606 n = width - maxwidth + 1;
4607 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004608 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 *s = '<';
4610
4611 /* Fill up for half a double-wide character. */
4612 while (++width < maxwidth)
4613 {
4614 s = s + STRLEN(s);
4615 *s++ = fillchar;
4616 *s = NUL;
4617 }
4618
4619 --n; /* count the '<' */
4620 for (; l < itemcnt; l++)
4621 {
4622 if (item[l].start - n >= s)
4623 item[l].start -= n;
4624 else
4625 item[l].start = s;
4626 }
4627 }
4628 width = maxwidth;
4629 }
4630 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4631 {
4632 /* Apply STL_MIDDLE if any */
4633 for (l = 0; l < itemcnt; l++)
4634 if (item[l].type == Middle)
4635 break;
4636 if (l < itemcnt)
4637 {
4638 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004639 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 for (s = item[l].start; s < p; s++)
4641 *s = fillchar;
4642 for (l++; l < itemcnt; l++)
4643 item[l].start += maxwidth - width;
4644 width = maxwidth;
4645 }
4646 }
4647
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004648 /* Store the info about highlighting. */
4649 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004651 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 for (l = 0; l < itemcnt; l++)
4653 {
4654 if (item[l].type == Highlight)
4655 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004656 sp->start = item[l].start;
4657 sp->userhl = item[l].minwid;
4658 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 }
4660 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004661 sp->start = NULL;
4662 sp->userhl = 0;
4663 }
4664
4665 /* Store the info about tab pages labels. */
4666 if (tabtab != NULL)
4667 {
4668 sp = tabtab;
4669 for (l = 0; l < itemcnt; l++)
4670 {
4671 if (item[l].type == TabPage)
4672 {
4673 sp->start = item[l].start;
4674 sp->userhl = item[l].minwid;
4675 sp++;
4676 }
4677 }
4678 sp->start = NULL;
4679 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 }
4681
Bram Moolenaar65ed1362017-09-30 16:00:14 +02004682 /* When inside update_screen we do not want redrawing a stausline, ruler,
4683 * title, etc. to trigger another redraw, it may cause an endless loop. */
4684 if (updating_screen)
4685 {
4686 must_redraw = save_must_redraw;
4687 curwin->w_redr_type = save_redr_type;
4688 }
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004689
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 return width;
4691}
4692#endif /* FEAT_STL_OPT */
4693
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004694#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4695 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004697 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4698 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 */
4700 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004701get_rel_pos(
4702 win_T *wp,
4703 char_u *buf,
4704 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705{
4706 long above; /* number of lines above window */
4707 long below; /* number of lines below window */
4708
Bram Moolenaar0027c212015-01-07 13:31:52 +01004709 if (buflen < 3) /* need at least 3 chars for writing */
4710 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 above = wp->w_topline - 1;
4712#ifdef FEAT_DIFF
4713 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02004714 if (wp->w_topline == 1 && wp->w_topfill >= 1)
4715 above = 0; /* All buffer lines are displayed and there is an
4716 * indication of filler lines, that can be considered
4717 * seeing all lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718#endif
4719 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4720 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004721 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4722 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004724 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004726 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 ? (int)(above / ((above + below) / 100L))
4728 : (int)(above * 100L / (above + below)));
4729}
4730#endif
4731
4732/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004733 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 * Return TRUE if it was appended.
4735 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004736 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004737append_arg_number(
4738 win_T *wp,
4739 char_u *buf,
4740 int buflen,
4741 int add_file) /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742{
4743 char_u *p;
4744
4745 if (ARGCOUNT <= 1) /* nothing to do */
4746 return FALSE;
4747
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004748 p = buf + STRLEN(buf); /* go to the end of the buffer */
4749 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 return FALSE;
4751 *p++ = ' ';
4752 *p++ = '(';
4753 if (add_file)
4754 {
4755 STRCPY(p, "file ");
4756 p += 5;
4757 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004758 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4759 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4761 return TRUE;
4762}
4763
4764/*
4765 * If fname is not a full path, make it a full path.
4766 * Returns pointer to allocated memory (NULL for failure).
4767 */
4768 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004769fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770{
4771 /*
4772 * Force expanding the path always for Unix, because symbolic links may
4773 * mess up the full path name, even though it starts with a '/'.
4774 * Also expand when there is ".." in the file name, try to remove it,
4775 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004776 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 * For MS-Windows also expand names like "longna~1" to "longname".
4778 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004779#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 return FullName_save(fname, TRUE);
4781#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004782 if (!vim_isAbsName(fname)
4783 || strstr((char *)fname, "..") != NULL
4784 || strstr((char *)fname, "//") != NULL
4785# ifdef BACKSLASH_IN_FILENAME
4786 || strstr((char *)fname, "\\\\") != NULL
4787# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004788# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004790# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 )
4792 return FullName_save(fname, FALSE);
4793
4794 fname = vim_strsave(fname);
4795
Bram Moolenaar9b942202007-10-03 12:31:33 +00004796# ifdef USE_FNAME_CASE
4797# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004799# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 {
4801 if (fname != NULL)
4802 fname_case(fname, 0); /* set correct case for file name */
4803 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004804# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805
4806 return fname;
4807#endif
4808}
4809
4810/*
4811 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4812 * "ffname" becomes a pointer to allocated memory (or NULL).
4813 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004815fname_expand(
4816 buf_T *buf UNUSED,
4817 char_u **ffname,
4818 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819{
4820 if (*ffname == NULL) /* if no file name given, nothing to do */
4821 return;
4822 if (*sfname == NULL) /* if no short file name given, use ffname */
4823 *sfname = *ffname;
4824 *ffname = fix_fname(*ffname); /* expand to full path */
4825
4826#ifdef FEAT_SHORTCUT
4827 if (!buf->b_p_bin)
4828 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004829 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830
4831 /* If the file name is a shortcut file, use the file it links to. */
4832 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004833 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 {
4835 vim_free(*ffname);
4836 *ffname = rfname;
4837 *sfname = rfname;
4838 }
4839 }
4840#endif
4841}
4842
4843/*
4844 * Get the file name for an argument list entry.
4845 */
4846 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004847alist_name(aentry_T *aep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848{
4849 buf_T *bp;
4850
4851 /* Use the name from the associated buffer if it exists. */
4852 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004853 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 return aep->ae_fname;
4855 return bp->b_fname;
4856}
4857
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858/*
4859 * do_arg_all(): Open up to 'count' windows, one for each argument.
4860 */
4861 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004862do_arg_all(
4863 int count,
4864 int forceit, /* hide buffers in current windows */
4865 int keep_tabs) /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866{
4867 int i;
4868 win_T *wp, *wpnext;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004869 char_u *opened; /* Array of weight for which args are open:
4870 * 0: not opened
4871 * 1: opened in other tab
4872 * 2: opened in curtab
4873 * 3: opened in curtab and curwin
4874 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004875 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 int use_firstwin = FALSE; /* use first window for arglist */
4877 int split_ret = OK;
4878 int p_ea_save;
4879 alist_T *alist; /* argument list to be used */
4880 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004881 tabpage_T *tpnext;
4882 int had_tab = cmdmod.tab;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004883 win_T *old_curwin, *last_curwin;
4884 tabpage_T *old_curtab, *last_curtab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004885 win_T *new_curwin = NULL;
4886 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887
4888 if (ARGCOUNT <= 0)
4889 {
4890 /* Don't give an error message. We don't want it when the ":all"
4891 * command is in the .vimrc. */
4892 return;
4893 }
4894 setpcmark();
4895
4896 opened_len = ARGCOUNT;
4897 opened = alloc_clear((unsigned)opened_len);
4898 if (opened == NULL)
4899 return;
4900
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004901 /* Autocommands may do anything to the argument list. Make sure it's not
4902 * freed while we are working here by "locking" it. We still have to
4903 * watch out for its size to be changed. */
4904 alist = curwin->w_alist;
4905 ++alist->al_refcount;
4906
4907 old_curwin = curwin;
4908 old_curtab = curtab;
4909
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004910# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004912# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913
4914 /*
4915 * Try closing all windows that are not in the argument list.
4916 * Also close windows that are not full width;
4917 * When 'hidden' or "forceit" set the buffer becomes hidden.
4918 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004919 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004921 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004922 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004923 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004925 tpnext = curtab->tp_next;
4926 for (wp = firstwin; wp != NULL; wp = wpnext)
4927 {
4928 wpnext = wp->w_next;
4929 buf = wp->w_buffer;
4930 if (buf->b_ffname == NULL
Bram Moolenaar5a030a52016-12-01 17:48:29 +01004931 || (!keep_tabs && (buf->b_nwindows > 1
4932 || wp->w_width != Columns)))
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004933 i = opened_len;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004934 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004936 /* check if the buffer in this window is in the arglist */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004937 for (i = 0; i < opened_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004939 if (i < alist->al_ga.ga_len
4940 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
4941 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
4942 buf->b_ffname, TRUE) & FPC_SAME))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004944 int weight = 1;
4945
4946 if (old_curtab == curtab)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004947 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004948 ++weight;
4949 if (old_curwin == wp)
4950 ++weight;
4951 }
4952
4953 if (weight > (int)opened[i])
4954 {
4955 opened[i] = (char_u)weight;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004956 if (i == 0)
4957 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004958 if (new_curwin != NULL)
4959 new_curwin->w_arg_idx = opened_len;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004960 new_curwin = wp;
4961 new_curtab = curtab;
4962 }
4963 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004964 else if (keep_tabs)
4965 i = opened_len;
4966
4967 if (wp->w_alist != alist)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004968 {
4969 /* Use the current argument list for all windows
4970 * containing a file from it. */
4971 alist_unlink(wp->w_alist);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004972 wp->w_alist = alist;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004973 ++wp->w_alist->al_refcount;
4974 }
4975 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 }
4978 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004979 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004981 if (i == opened_len && !keep_tabs)/* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02004983 if (buf_hide(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004984 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004986 /* If the buffer was changed, and we would like to hide it,
4987 * try autowriting. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02004988 if (!buf_hide(buf) && buf->b_nwindows <= 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004989 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02004991 bufref_T bufref;
4992
4993 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004994
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004995 (void)autowrite(buf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004996
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004997 /* check if autocommands removed the window */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02004998 if (!win_valid(wp) || !bufref_valid(&bufref))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004999 {
5000 wpnext = firstwin; /* start all over... */
5001 continue;
5002 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005003 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005004 /* don't close last window */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005005 if (ONE_WINDOW
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005006 && (first_tabpage->tp_next == NULL || !had_tab))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005007 use_firstwin = TRUE;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005008 else
5009 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005010 win_close(wp, !buf_hide(buf) && !bufIsChanged(buf));
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005011
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005012 /* check if autocommands removed the next window */
5013 if (!win_valid(wpnext))
5014 wpnext = firstwin; /* start all over... */
Bram Moolenaar4033c552017-09-16 20:54:51 +02005015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 }
5018 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005019
5020 /* Without the ":tab" modifier only do the current tab page. */
5021 if (had_tab == 0 || tpnext == NULL)
5022 break;
5023
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005024 /* check if autocommands removed the next tab page */
5025 if (!valid_tabpage(tpnext))
5026 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005027
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005028 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 }
5030
5031 /*
5032 * Open a window for files in the argument list that don't have one.
5033 * ARGCOUNT may change while doing this, because of autocommands.
5034 */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005035 if (count > opened_len || count <= 0)
5036 count = opened_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 /* Don't execute Win/Buf Enter/Leave autocommands here. */
5039 ++autocmd_no_enter;
5040 ++autocmd_no_leave;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005041 last_curwin = curwin;
5042 last_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005044 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
5045 * leaving an empty tab page when executed locally. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005046 if (keep_tabs && BUFEMPTY() && curbuf->b_nwindows == 1
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005047 && curbuf->b_ffname == NULL && !curbuf->b_changed)
5048 use_firstwin = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005049
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005050 for (i = 0; i < count && i < opened_len && !got_int; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 {
5052 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
5053 arg_had_last = TRUE;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005054 if (opened[i] > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 {
5056 /* Move the already present window to below the current window */
5057 if (curwin->w_arg_idx != i)
5058 {
5059 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
5060 {
5061 if (wpnext->w_arg_idx == i)
5062 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005063 if (keep_tabs)
5064 {
5065 new_curwin = wpnext;
5066 new_curtab = curtab;
5067 }
5068 else
5069 win_move_after(wpnext, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 break;
5071 }
5072 }
5073 }
5074 }
5075 else if (split_ret == OK)
5076 {
5077 if (!use_firstwin) /* split current window */
5078 {
5079 p_ea_save = p_ea;
5080 p_ea = TRUE; /* use space from all windows */
5081 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5082 p_ea = p_ea_save;
5083 if (split_ret == FAIL)
5084 continue;
5085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 else /* first window: do autocmd for leaving this buffer */
5087 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088
5089 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005090 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 */
5092 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005093 if (i == 0)
5094 {
5095 new_curwin = curwin;
5096 new_curtab = curtab;
5097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
5099 ECMD_ONE,
Bram Moolenaareb44a682017-08-03 22:44:55 +02005100 ((buf_hide(curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00005102 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 if (use_firstwin)
5104 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 use_firstwin = FALSE;
5106 }
5107 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005108
5109 /* When ":tab" was used open a new tab for a new window repeatedly. */
5110 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5111 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 }
5113
5114 /* Remove the "lock" on the argument list. */
5115 alist_unlink(alist);
5116
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 --autocmd_no_enter;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005118
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005119 /* restore last referenced tabpage's curwin */
5120 if (last_curtab != new_curtab)
5121 {
5122 if (valid_tabpage(last_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005123 goto_tabpage_tp(last_curtab, TRUE, TRUE);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005124 if (win_valid(last_curwin))
5125 win_enter(last_curwin, FALSE);
5126 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005127 /* to window with first arg */
5128 if (valid_tabpage(new_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005129 goto_tabpage_tp(new_curtab, TRUE, TRUE);
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005130 if (win_valid(new_curwin))
5131 win_enter(new_curwin, FALSE);
5132
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 vim_free(opened);
5135}
5136
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137/*
5138 * Open a window for a number of buffers.
5139 */
5140 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005141ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142{
5143 buf_T *buf;
5144 win_T *wp, *wpnext;
5145 int split_ret = OK;
5146 int p_ea_save;
5147 int open_wins = 0;
5148 int r;
5149 int count; /* Maximum number of windows to open. */
5150 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005151 int had_tab = cmdmod.tab;
5152 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153
5154 if (eap->addr_count == 0) /* make as many windows as possible */
5155 count = 9999;
5156 else
5157 count = eap->line2; /* make as many windows as specified */
5158 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5159 all = FALSE;
5160 else
5161 all = TRUE;
5162
5163 setpcmark();
5164
5165#ifdef FEAT_GUI
5166 need_mouse_correct = TRUE;
5167#endif
5168
5169 /*
5170 * Close superfluous windows (two windows for the same buffer).
5171 * Also close windows that are not full-width.
5172 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005173 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005174 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005175 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005177 tpnext = curtab->tp_next;
5178 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005180 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005181 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005182 || ((cmdmod.split & WSP_VERT)
5183 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005184 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005185 : wp->w_width != Columns)
Bram Moolenaar4033c552017-09-16 20:54:51 +02005186 || (had_tab > 0 && wp != firstwin)) && !ONE_WINDOW
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005187 && !(wp->w_closing || wp->w_buffer->b_locked > 0))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005188 {
5189 win_close(wp, FALSE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005190 wpnext = firstwin; /* just in case an autocommand does
5191 something strange with windows */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005192 tpnext = first_tabpage; /* start all over... */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005193 open_wins = 0;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005194 }
5195 else
5196 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005198
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005199 /* Without the ":tab" modifier only do the current tab page. */
5200 if (had_tab == 0 || tpnext == NULL)
5201 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005202 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 }
5204
5205 /*
5206 * Go through the buffer list. When a buffer doesn't have a window yet,
5207 * open one. Otherwise move the window to the right position.
5208 * Watch out for autocommands that delete buffers or windows!
5209 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 /* Don't execute Win/Buf Enter/Leave autocommands here. */
5211 ++autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 win_enter(lastwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5215 {
5216 /* Check if this buffer needs a window */
5217 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5218 continue;
5219
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005220 if (had_tab != 0)
5221 {
5222 /* With the ":tab" modifier don't move the window. */
5223 if (buf->b_nwindows > 0)
5224 wp = lastwin; /* buffer has a window, skip it */
5225 else
5226 wp = NULL;
5227 }
5228 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005229 {
5230 /* Check if this buffer already has a window */
Bram Moolenaar29323592016-07-24 22:04:11 +02005231 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005232 if (wp->w_buffer == buf)
5233 break;
5234 /* If the buffer already has a window, move it */
5235 if (wp != NULL)
5236 win_move_after(wp, curwin);
5237 }
5238
5239 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005241 bufref_T bufref;
5242
5243 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005244
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 /* Split the window and put the buffer in it */
5246 p_ea_save = p_ea;
5247 p_ea = TRUE; /* use space from all windows */
5248 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5249 ++open_wins;
5250 p_ea = p_ea_save;
5251 if (split_ret == FAIL)
5252 continue;
5253
5254 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005255#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 swap_exists_action = SEA_DIALOG;
5257#endif
5258 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005259 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005261 /* autocommands deleted the buffer!!! */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005262#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 swap_exists_action = SEA_NONE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 break;
5266 }
Bram Moolenaare64ac772005-12-07 20:54:59 +00005267#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 if (swap_exists_action == SEA_QUIT)
5269 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005270# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005271 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005272
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005273 /* Reset the error/interrupt/exception state here so that
5274 * aborting() returns FALSE when closing a window. */
5275 enter_cleanup(&cs);
5276# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005277
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005278 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 win_close(curwin, TRUE);
5280 --open_wins;
5281 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005282 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005283
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005284# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005285 /* Restore the error/interrupt/exception state if not
5286 * discarded by a new aborting error, interrupt, or uncaught
5287 * exception. */
5288 leave_cleanup(&cs);
5289# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 }
5291 else
5292 handle_swap_exists(NULL);
5293#endif
5294 }
5295
5296 ui_breakcheck();
5297 if (got_int)
5298 {
5299 (void)vgetc(); /* only break the file loading, not the rest */
5300 break;
5301 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005302#ifdef FEAT_EVAL
5303 /* Autocommands deleted the buffer or aborted script processing!!! */
5304 if (aborting())
5305 break;
5306#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005307 /* When ":tab" was used open a new tab for a new window repeatedly. */
5308 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5309 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 --autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 win_enter(firstwin, FALSE); /* back to first window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314
5315 /*
5316 * Close superfluous windows.
5317 */
5318 for (wp = lastwin; open_wins > count; )
5319 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005320 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 || autowrite(wp->w_buffer, FALSE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 if (!win_valid(wp))
5323 {
5324 /* BufWrite Autocommands made the window invalid, start over */
5325 wp = lastwin;
5326 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005327 else if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005329 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 --open_wins;
5331 wp = lastwin;
5332 }
5333 else
5334 {
5335 wp = wp->w_prev;
5336 if (wp == NULL)
5337 break;
5338 }
5339 }
5340}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005343static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005344
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345/*
5346 * do_modelines() - process mode lines for the current file
5347 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005348 * "flags" can be:
5349 * OPT_WINONLY only set options local to window
5350 * OPT_NOWIN don't set options local to window
5351 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 * Returns immediately if the "ml" option isn't set.
5353 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005355do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005357 linenr_T lnum;
5358 int nmlines;
5359 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360
5361 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5362 return;
5363
5364 /* Disallow recursive entry here. Can happen when executing a modeline
5365 * triggers an autocommand, which reloads modelines with a ":do". */
5366 if (entered)
5367 return;
5368
5369 ++entered;
5370 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5371 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005372 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 nmlines = 0;
5374
5375 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5376 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005377 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 nmlines = 0;
5379 --entered;
5380}
5381
5382#include "version.h" /* for version number */
5383
5384/*
5385 * chk_modeline() - check a single line for a mode string
5386 * Return FAIL if an error encountered.
5387 */
5388 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005389chk_modeline(
5390 linenr_T lnum,
5391 int flags) /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392{
5393 char_u *s;
5394 char_u *e;
5395 char_u *linecopy; /* local copy of any modeline found */
5396 int prev;
5397 int vers;
5398 int end;
5399 int retval = OK;
5400 char_u *save_sourcing_name;
5401 linenr_T save_sourcing_lnum;
5402#ifdef FEAT_EVAL
5403 scid_T save_SID;
5404#endif
5405
5406 prev = -1;
5407 for (s = ml_get(lnum); *s != NUL; ++s)
5408 {
5409 if (prev == -1 || vim_isspace(prev))
5410 {
5411 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5412 || STRNCMP(s, "vi:", (size_t)3) == 0)
5413 break;
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005414 /* Accept both "vim" and "Vim". */
5415 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 {
5417 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5418 e = s + 4;
5419 else
5420 e = s + 3;
5421 vers = getdigits(&e);
5422 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005423 && (s[0] != 'V'
5424 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 && (s[3] == ':'
5426 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5427 || (VIM_VERSION_100 < vers && s[3] == '<')
5428 || (VIM_VERSION_100 > vers && s[3] == '>')
5429 || (VIM_VERSION_100 == vers && s[3] == '=')))
5430 break;
5431 }
5432 }
5433 prev = *s;
5434 }
5435
5436 if (*s)
5437 {
5438 do /* skip over "ex:", "vi:" or "vim:" */
5439 ++s;
5440 while (s[-1] != ':');
5441
5442 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5443 if (linecopy == NULL)
5444 return FAIL;
5445
5446 save_sourcing_lnum = sourcing_lnum;
5447 save_sourcing_name = sourcing_name;
5448 sourcing_lnum = lnum; /* prepare for emsg() */
5449 sourcing_name = (char_u *)"modelines";
5450
5451 end = FALSE;
5452 while (end == FALSE)
5453 {
5454 s = skipwhite(s);
5455 if (*s == NUL)
5456 break;
5457
5458 /*
5459 * Find end of set command: ':' or end of line.
5460 * Skip over "\:", replacing it with ":".
5461 */
5462 for (e = s; *e != ':' && *e != NUL; ++e)
5463 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005464 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 if (*e == NUL)
5466 end = TRUE;
5467
5468 /*
5469 * If there is a "set" command, require a terminating ':' and
5470 * ignore the stuff after the ':'.
5471 * "vi:set opt opt opt: foo" -- foo not interpreted
5472 * "vi:opt opt opt: foo" -- foo interpreted
5473 * Accept "se" for compatibility with Elvis.
5474 */
5475 if (STRNCMP(s, "set ", (size_t)4) == 0
5476 || STRNCMP(s, "se ", (size_t)3) == 0)
5477 {
5478 if (*e != ':') /* no terminating ':'? */
5479 break;
5480 end = TRUE;
5481 s = vim_strchr(s, ' ') + 1;
5482 }
5483 *e = NUL; /* truncate the set command */
5484
5485 if (*s != NUL) /* skip over an empty "::" */
5486 {
5487#ifdef FEAT_EVAL
5488 save_SID = current_SID;
5489 current_SID = SID_MODELINE;
5490#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005491 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492#ifdef FEAT_EVAL
5493 current_SID = save_SID;
5494#endif
5495 if (retval == FAIL) /* stop if error found */
5496 break;
5497 }
5498 s = e + 1; /* advance to next part */
5499 }
5500
5501 sourcing_lnum = save_sourcing_lnum;
5502 sourcing_name = save_sourcing_name;
5503
5504 vim_free(linecopy);
5505 }
5506 return retval;
5507}
5508
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005509#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005511read_viminfo_bufferlist(
5512 vir_T *virp,
5513 int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514{
5515 char_u *tab;
5516 linenr_T lnum;
5517 colnr_T col;
5518 buf_T *buf;
5519 char_u *sfname;
5520 char_u *xline;
5521
5522 /* Handle long line and escaped characters. */
5523 xline = viminfo_readstring(virp, 1, FALSE);
5524
5525 /* don't read in if there are files on the command-line or if writing: */
5526 if (xline != NULL && !writing && ARGCOUNT == 0
5527 && find_viminfo_parameter('%') != NULL)
5528 {
5529 /* Format is: <fname> Tab <lnum> Tab <col>.
5530 * Watch out for a Tab in the file name, work from the end. */
5531 lnum = 0;
5532 col = 0;
5533 tab = vim_strrchr(xline, '\t');
5534 if (tab != NULL)
5535 {
5536 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005537 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 tab = vim_strrchr(xline, '\t');
5539 if (tab != NULL)
5540 {
5541 *tab++ = '\0';
5542 lnum = atol((char *)tab);
5543 }
5544 }
5545
5546 /* Expand "~/" in the file name at "line + 1" to a full path.
5547 * Then try shortening it by comparing with the current directory */
5548 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005549 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550
5551 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5552 if (buf != NULL) /* just in case... */
5553 {
5554 buf->b_last_cursor.lnum = lnum;
5555 buf->b_last_cursor.col = col;
5556 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5557 }
5558 }
5559 vim_free(xline);
5560
5561 return viminfo_readline(virp);
5562}
5563
5564 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005565write_viminfo_bufferlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566{
5567 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005569 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005571 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572
5573 if (find_viminfo_parameter('%') == NULL)
5574 return;
5575
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005576 /* Without a number -1 is returned: do all buffers. */
5577 max_buffers = get_viminfo_parameter('%');
5578
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005580#define LINE_BUF_LEN (MAXPATHL + 40)
5581 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 if (line == NULL)
5583 return;
5584
Bram Moolenaarf740b292006-02-16 22:11:02 +00005585 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 set_last_cursor(win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005588 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar29323592016-07-24 22:04:11 +02005589 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 {
5591 if (buf->b_fname == NULL
5592 || !buf->b_p_bl
5593#ifdef FEAT_QUICKFIX
5594 || bt_quickfix(buf)
5595#endif
Bram Moolenaare6278052017-08-13 18:11:17 +02005596#ifdef FEAT_TERMINAL
5597 || bt_terminal(buf)
5598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 || removable(buf->b_ffname))
5600 continue;
5601
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005602 if (max_buffers-- == 0)
5603 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 putc('%', fp);
5605 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005606 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 (long)buf->b_last_cursor.lnum,
5608 buf->b_last_cursor.col);
5609 viminfo_writestring(fp, line);
5610 }
5611 vim_free(line);
5612}
5613#endif
5614
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005615/*
Bram Moolenaar91335e52018-08-01 17:53:12 +02005616 * Return TRUE if "buf" is a normal buffer, 'buftype' is empty.
5617 */
5618 int
5619bt_normal(buf_T *buf)
5620{
5621 return buf != NULL && buf->b_p_bt[0] == NUL;
5622}
5623
5624/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005625 * Return TRUE if "buf" is the quickfix buffer.
5626 */
5627 int
5628bt_quickfix(buf_T *buf)
5629{
5630 return buf != NULL && buf->b_p_bt[0] == 'q';
5631}
5632
5633/*
5634 * Return TRUE if "buf" is a terminal buffer.
5635 */
5636 int
5637bt_terminal(buf_T *buf)
5638{
5639 return buf != NULL && buf->b_p_bt[0] == 't';
5640}
5641
5642/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005643 * Return TRUE if "buf" is a help buffer.
5644 */
5645 int
5646bt_help(buf_T *buf)
5647{
5648 return buf != NULL && buf->b_help;
5649}
5650
5651/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02005652 * Return TRUE if "buf" is a prompt buffer.
5653 */
5654 int
5655bt_prompt(buf_T *buf)
5656{
5657 return buf != NULL && buf->b_p_bt[0] == 'p';
5658}
5659
5660/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005661 * Return TRUE if "buf" is a "nofile", "acwrite", "terminal" or "prompt"
5662 * buffer. This means the buffer name is not a file name.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005663 */
5664 int
5665bt_nofile(buf_T *buf)
5666{
5667 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5668 || buf->b_p_bt[0] == 'a'
Bram Moolenaarf2732452018-06-03 14:47:35 +02005669 || buf->b_p_bt[0] == 't'
5670 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005671}
5672
5673/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005674 * Return TRUE if "buf" is a "nowrite", "nofile", "terminal" or "prompt"
5675 * buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005676 */
5677 int
5678bt_dontwrite(buf_T *buf)
5679{
Bram Moolenaarf2732452018-06-03 14:47:35 +02005680 return buf != NULL && (buf->b_p_bt[0] == 'n'
5681 || buf->b_p_bt[0] == 't'
5682 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005683}
5684
5685 int
5686bt_dontwrite_msg(buf_T *buf)
5687{
5688 if (bt_dontwrite(buf))
5689 {
5690 EMSG(_("E382: Cannot write, 'buftype' option is set"));
5691 return TRUE;
5692 }
5693 return FALSE;
5694}
5695
5696/*
5697 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
5698 * and 'bufhidden'.
5699 */
5700 int
5701buf_hide(buf_T *buf)
5702{
5703 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
5704 switch (buf->b_p_bh[0])
5705 {
5706 case 'u': /* "unload" */
5707 case 'w': /* "wipe" */
5708 case 'd': return FALSE; /* "delete" */
5709 case 'h': return TRUE; /* "hide" */
5710 }
5711 return (p_hid || cmdmod.hide);
5712}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713
5714/*
5715 * Return special buffer name.
5716 * Returns NULL when the buffer has a normal file name.
5717 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005718 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005719buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720{
Bram Moolenaar4033c552017-09-16 20:54:51 +02005721#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005723 {
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005724 win_T *win;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005725 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005726
5727 /*
5728 * For location list window, w_llist_ref points to the location list.
5729 * For quickfix window, w_llist_ref is NULL.
5730 */
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005731 if (find_win_for_buf(buf, &win, &tp) == OK && win->w_llist_ref != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005732 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005733 else
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005734 return (char_u *)_(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02005737
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 /* There is no _file_ when 'buftype' is "nofile", b_sfname
Bram Moolenaar21554412017-07-24 21:44:43 +02005739 * contains the name as specified by the user. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 if (bt_nofile(buf))
5741 {
Bram Moolenaar21554412017-07-24 21:44:43 +02005742#ifdef FEAT_TERMINAL
5743 if (buf->b_term != NULL)
5744 return term_get_status_text(buf->b_term);
5745#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02005746 if (buf->b_fname != NULL)
5747 return buf->b_fname;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02005748#ifdef FEAT_JOB_CHANNEL
5749 if (bt_prompt(buf))
5750 return (char_u *)_("[Prompt]");
5751#endif
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005752 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 }
Bram Moolenaar21554412017-07-24 21:44:43 +02005754
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005756 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 return NULL;
5758}
5759
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02005760#if defined(FEAT_JOB_CHANNEL) \
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005761 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5762 || defined(PROTO)
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02005763# define SWITCH_TO_WIN
5764
5765/*
5766 * Find a window that contains "buf" and switch to it.
5767 * If there is no such window, use the current window and change "curbuf".
5768 * Caller must initialize save_curbuf to NULL.
5769 * restore_win_for_buf() MUST be called later!
5770 */
5771 void
5772switch_to_win_for_buf(
5773 buf_T *buf,
5774 win_T **save_curwinp,
5775 tabpage_T **save_curtabp,
5776 bufref_T *save_curbuf)
5777{
5778 win_T *wp;
5779 tabpage_T *tp;
5780
5781 if (find_win_for_buf(buf, &wp, &tp) == FAIL)
5782 switch_buffer(save_curbuf, buf);
5783 else if (switch_win(save_curwinp, save_curtabp, wp, tp, TRUE) == FAIL)
5784 {
5785 restore_win(*save_curwinp, *save_curtabp, TRUE);
5786 switch_buffer(save_curbuf, buf);
5787 }
5788}
5789
5790 void
5791restore_win_for_buf(
5792 win_T *save_curwin,
5793 tabpage_T *save_curtab,
5794 bufref_T *save_curbuf)
5795{
5796 if (save_curbuf->br_buf == NULL)
5797 restore_win(save_curwin, save_curtab, TRUE);
5798 else
5799 restore_buffer(save_curbuf);
5800}
5801#endif
5802
Bram Moolenaar4033c552017-09-16 20:54:51 +02005803#if defined(FEAT_QUICKFIX) || defined(SWITCH_TO_WIN) || defined(PROTO)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005804/*
5805 * Find a window for buffer "buf".
5806 * If found OK is returned and "wp" and "tp" are set to the window and tabpage.
5807 * If not found FAIL is returned.
5808 */
5809 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005810find_win_for_buf(
5811 buf_T *buf,
5812 win_T **wp,
5813 tabpage_T **tp)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005814{
5815 FOR_ALL_TAB_WINDOWS(*tp, *wp)
5816 if ((*wp)->w_buffer == buf)
5817 goto win_found;
5818 return FAIL;
5819win_found:
5820 return OK;
5821}
5822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823
5824#if defined(FEAT_SIGNS) || defined(PROTO)
5825/*
5826 * Insert the sign into the signlist.
5827 */
5828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005829insert_sign(
5830 buf_T *buf, /* buffer to store sign in */
5831 signlist_T *prev, /* previous sign entry */
5832 signlist_T *next, /* next sign entry */
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 *newsign;
5838
5839 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5840 if (newsign != NULL)
5841 {
5842 newsign->id = id;
5843 newsign->lnum = lnum;
5844 newsign->typenr = typenr;
5845 newsign->next = next;
5846#ifdef FEAT_NETBEANS_INTG
5847 newsign->prev = prev;
5848 if (next != NULL)
5849 next->prev = newsign;
5850#endif
5851
5852 if (prev == NULL)
5853 {
5854 /* When adding first sign need to redraw the windows to create the
5855 * column for signs. */
5856 if (buf->b_signlist == NULL)
5857 {
5858 redraw_buf_later(buf, NOT_VALID);
5859 changed_cline_bef_curs();
5860 }
5861
5862 /* first sign in signlist */
5863 buf->b_signlist = newsign;
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01005864#ifdef FEAT_NETBEANS_INTG
5865 if (netbeans_active())
5866 buf->b_has_sign_column = TRUE;
5867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 }
5869 else
5870 prev->next = newsign;
5871 }
5872}
5873
5874/*
5875 * Add the sign into the signlist. Find the right spot to do it though.
5876 */
5877 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005878buf_addsign(
5879 buf_T *buf, /* buffer to store sign in */
5880 int id, /* sign ID */
5881 linenr_T lnum, /* line number which gets the mark */
5882 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883{
5884 signlist_T *sign; /* a sign in the signlist */
5885 signlist_T *prev; /* the previous sign */
5886
5887 prev = NULL;
5888 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5889 {
5890 if (lnum == sign->lnum && id == sign->id)
5891 {
5892 sign->typenr = typenr;
5893 return;
5894 }
5895 else if (
5896#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5897 id < 0 &&
5898#endif
5899 lnum < sign->lnum)
5900 {
5901#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5902 /* XXX - GRP: Is this because of sign slide problem? Or is it
5903 * really needed? Or is it because we allow multiple signs per
5904 * line? If so, should I add that feature to FEAT_SIGNS?
5905 */
5906 while (prev != NULL && prev->lnum == lnum)
5907 prev = prev->prev;
5908 if (prev == NULL)
5909 sign = buf->b_signlist;
5910 else
5911 sign = prev->next;
5912#endif
5913 insert_sign(buf, prev, sign, id, lnum, typenr);
5914 return;
5915 }
5916 prev = sign;
5917 }
5918#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5919 /* XXX - GRP: See previous comment */
5920 while (prev != NULL && prev->lnum == lnum)
5921 prev = prev->prev;
5922 if (prev == NULL)
5923 sign = buf->b_signlist;
5924 else
5925 sign = prev->next;
5926#endif
5927 insert_sign(buf, prev, sign, id, lnum, typenr);
5928
5929 return;
5930}
5931
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005932/*
5933 * For an existing, placed sign "markId" change the type to "typenr".
5934 * Returns the line number of the sign, or zero if the sign is not found.
5935 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005936 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005937buf_change_sign_type(
5938 buf_T *buf, /* buffer to store sign in */
5939 int markId, /* sign ID */
5940 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941{
5942 signlist_T *sign; /* a sign in the signlist */
5943
5944 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5945 {
5946 if (sign->id == markId)
5947 {
5948 sign->typenr = typenr;
5949 return sign->lnum;
5950 }
5951 }
5952
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005953 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954}
5955
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005956 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005957buf_getsigntype(
5958 buf_T *buf,
5959 linenr_T lnum,
5960 int type) /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961{
5962 signlist_T *sign; /* a sign in a b_signlist */
5963
5964 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5965 if (sign->lnum == lnum
5966 && (type == SIGN_ANY
5967# ifdef FEAT_SIGN_ICONS
5968 || (type == SIGN_ICON
5969 && sign_get_image(sign->typenr) != NULL)
5970# endif
5971 || (type == SIGN_TEXT
5972 && sign_get_text(sign->typenr) != NULL)
5973 || (type == SIGN_LINEHL
5974 && sign_get_attr(sign->typenr, TRUE) != 0)))
5975 return sign->typenr;
5976 return 0;
5977}
5978
5979
5980 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005981buf_delsign(
5982 buf_T *buf, /* buffer sign is stored in */
5983 int id) /* sign id */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984{
5985 signlist_T **lastp; /* pointer to pointer to current sign */
5986 signlist_T *sign; /* a sign in a b_signlist */
5987 signlist_T *next; /* the next sign in a b_signlist */
5988 linenr_T lnum; /* line number whose sign was deleted */
5989
5990 lastp = &buf->b_signlist;
5991 lnum = 0;
5992 for (sign = buf->b_signlist; sign != NULL; sign = next)
5993 {
5994 next = sign->next;
5995 if (sign->id == id)
5996 {
5997 *lastp = next;
5998#ifdef FEAT_NETBEANS_INTG
5999 if (next != NULL)
6000 next->prev = sign->prev;
6001#endif
6002 lnum = sign->lnum;
6003 vim_free(sign);
6004 break;
6005 }
6006 else
6007 lastp = &sign->next;
6008 }
6009
6010 /* When deleted the last sign need to redraw the windows to remove the
6011 * sign column. */
6012 if (buf->b_signlist == NULL)
6013 {
6014 redraw_buf_later(buf, NOT_VALID);
6015 changed_cline_bef_curs();
6016 }
6017
6018 return lnum;
6019}
6020
6021
6022/*
6023 * Find the line number of the sign with the requested id. If the sign does
6024 * not exist, return 0 as the line number. This will still let the correct file
6025 * get loaded.
6026 */
6027 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006028buf_findsign(
6029 buf_T *buf, /* buffer to store sign in */
6030 int id) /* sign ID */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031{
6032 signlist_T *sign; /* a sign in the signlist */
6033
6034 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
6035 if (sign->id == id)
6036 return sign->lnum;
6037
6038 return 0;
6039}
6040
6041 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006042buf_findsign_id(
6043 buf_T *buf, /* buffer whose sign we are searching for */
6044 linenr_T lnum) /* line number of sign */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045{
6046 signlist_T *sign; /* a sign in the signlist */
6047
6048 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
6049 if (sign->lnum == lnum)
6050 return sign->id;
6051
6052 return 0;
6053}
6054
6055
6056# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
6057/* see if a given type of sign exists on a specific line */
6058 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006059buf_findsigntype_id(
6060 buf_T *buf, /* buffer whose sign we are searching for */
6061 linenr_T lnum, /* line number of sign */
6062 int typenr) /* sign type number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063{
6064 signlist_T *sign; /* a sign in the signlist */
6065
6066 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
6067 if (sign->lnum == lnum && sign->typenr == typenr)
6068 return sign->id;
6069
6070 return 0;
6071}
6072
6073
6074# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
6075/* return the number of icons on the given line */
6076 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006077buf_signcount(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078{
6079 signlist_T *sign; /* a sign in the signlist */
6080 int count = 0;
6081
6082 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
6083 if (sign->lnum == lnum)
6084 if (sign_get_image(sign->typenr) != NULL)
6085 count++;
6086
6087 return count;
6088}
6089# endif /* FEAT_SIGN_ICONS */
6090# endif /* FEAT_NETBEANS_INTG */
6091
6092
6093/*
6094 * Delete signs in buffer "buf".
6095 */
Bram Moolenaarf65e5662012-07-10 15:18:22 +02006096 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006097buf_delete_signs(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098{
6099 signlist_T *next;
6100
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02006101 /* When deleting the last sign need to redraw the windows to remove the
Bram Moolenaar4e036c92014-07-16 16:30:28 +02006102 * sign column. Not when curwin is NULL (this means we're exiting). */
6103 if (buf->b_signlist != NULL && curwin != NULL)
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02006104 {
6105 redraw_buf_later(buf, NOT_VALID);
6106 changed_cline_bef_curs();
6107 }
6108
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 while (buf->b_signlist != NULL)
6110 {
6111 next = buf->b_signlist->next;
6112 vim_free(buf->b_signlist);
6113 buf->b_signlist = next;
6114 }
6115}
6116
6117/*
6118 * Delete all signs in all buffers.
6119 */
6120 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006121buf_delete_all_signs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122{
6123 buf_T *buf; /* buffer we are checking for signs */
6124
Bram Moolenaar29323592016-07-24 22:04:11 +02006125 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 if (buf->b_signlist != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 buf_delete_signs(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128}
6129
6130/*
6131 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
6132 */
6133 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006134sign_list_placed(buf_T *rbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135{
6136 buf_T *buf;
6137 signlist_T *p;
6138 char lbuf[BUFSIZ];
6139
6140 MSG_PUTS_TITLE(_("\n--- Signs ---"));
6141 msg_putchar('\n');
6142 if (rbuf == NULL)
6143 buf = firstbuf;
6144 else
6145 buf = rbuf;
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01006146 while (buf != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 {
6148 if (buf->b_signlist != NULL)
6149 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00006150 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006151 MSG_PUTS_ATTR(lbuf, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 msg_putchar('\n');
6153 }
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01006154 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00006156 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
6158 MSG_PUTS(lbuf);
6159 msg_putchar('\n');
6160 }
6161 if (rbuf != NULL)
6162 break;
6163 buf = buf->b_next;
6164 }
6165}
6166
6167/*
6168 * Adjust a placed sign for inserted/deleted lines.
6169 */
6170 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006171sign_mark_adjust(
6172 linenr_T line1,
6173 linenr_T line2,
6174 long amount,
6175 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176{
6177 signlist_T *sign; /* a sign in a b_signlist */
6178
6179 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
6180 {
6181 if (sign->lnum >= line1 && sign->lnum <= line2)
6182 {
6183 if (amount == MAXLNUM)
6184 sign->lnum = line1;
6185 else
6186 sign->lnum += amount;
6187 }
6188 else if (sign->lnum > line2)
6189 sign->lnum += amount_after;
6190 }
6191}
6192#endif /* FEAT_SIGNS */
6193
6194/*
6195 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
6196 */
6197 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006198set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199{
6200 if (on != curbuf->b_p_bl)
6201 {
6202 curbuf->b_p_bl = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 if (on)
6204 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
6205 else
6206 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 }
6208}
6209
6210/*
6211 * Read the file for "buf" again and check if the contents changed.
6212 * Return TRUE if it changed or this could not be checked.
6213 */
6214 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006215buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216{
6217 buf_T *newbuf;
6218 int differ = TRUE;
6219 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 exarg_T ea;
6222
6223 /* Allocate a buffer without putting it in the buffer list. */
6224 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6225 if (newbuf == NULL)
6226 return TRUE;
6227
6228 /* Force the 'fileencoding' and 'fileformat' to be equal. */
6229 if (prep_exarg(&ea, buf) == FAIL)
6230 {
6231 wipe_buffer(newbuf, FALSE);
6232 return TRUE;
6233 }
6234
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 /* set curwin/curbuf to buf and save a few things */
6236 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237
Bram Moolenaar4770d092006-01-12 23:22:24 +00006238 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 && readfile(buf->b_ffname, buf->b_fname,
6240 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
6241 &ea, READ_NEW | READ_DUMMY) == OK)
6242 {
6243 /* compare the two files line by line */
6244 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
6245 {
6246 differ = FALSE;
6247 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6248 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
6249 {
6250 differ = TRUE;
6251 break;
6252 }
6253 }
6254 }
6255 vim_free(ea.cmd);
6256
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 /* restore curwin/curbuf and a few other things */
6258 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259
6260 if (curbuf != newbuf) /* safety check */
6261 wipe_buffer(newbuf, FALSE);
6262
6263 return differ;
6264}
6265
6266/*
6267 * Wipe out a buffer and decrement the last buffer number if it was used for
6268 * this buffer. Call this to wipe out a temp buffer that does not contain any
6269 * marks.
6270 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006272wipe_buffer(
6273 buf_T *buf,
6274 int aucmd UNUSED) /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275{
6276 if (buf->b_fnum == top_file_num - 1)
6277 --top_file_num;
6278
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006280 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006281
Bram Moolenaar42ec6562012-02-22 14:58:37 +01006282 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006283
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006285 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286}